更新
This commit is contained in:
@@ -55,6 +55,11 @@ namespace app\adminapi\logic\tcm {
|
||||
self::$events[] = ['read', $params];
|
||||
return self::$records;
|
||||
}
|
||||
public static function saveLocalAudioTranscript(array $params): array
|
||||
{
|
||||
self::$events[] = ['recover', $params];
|
||||
return ['call_record_id' => 1102, 'transcription_status' => 'completed', 'saved' => true];
|
||||
}
|
||||
public static function getError(): string { return self::$error; }
|
||||
}
|
||||
}
|
||||
@@ -79,6 +84,7 @@ namespace {
|
||||
public object $controllerObject;
|
||||
public string $actionName = 'getCallRecords';
|
||||
public string $controllerName = 'tcm.Diagnosis';
|
||||
public bool $postRequest = false;
|
||||
public function __construct(public array $query = [], bool $root = false)
|
||||
{
|
||||
$this->adminInfo = ['admin_id' => 91, 'root' => $root ? 1 : 0,
|
||||
@@ -92,6 +98,8 @@ namespace {
|
||||
return $name === null ? $this->query : ($this->query[$name] ?? $default);
|
||||
}
|
||||
public function param(string $name, $default = null) { return $default; }
|
||||
public function post($name = '', $default = null, $filter = '') { return $this->query; }
|
||||
public function isPost(): bool { return $this->postRequest; }
|
||||
public function controller(): string { return $this->controllerName; }
|
||||
public function action(): string { return $this->actionName; }
|
||||
public function ip(): string { return '192.0.2.91'; }
|
||||
@@ -207,5 +215,45 @@ namespace {
|
||||
$empty = transcriptAuthorizationInvoke(['diagnosis_id' => 701], ['tcm.diagnosis/getCallRecords']);
|
||||
transcriptAuthorizationExpect($empty['code'] === 1 && $empty['data'] === [], 'authorized empty history retains the original array response');
|
||||
|
||||
$recoveryGrants = ['tcm.diagnosis/startCallTranscription', 'tcm.diagnosis/upsertCallTranscriptSegments',
|
||||
'tcm.diagnosis/finishCallTranscription'];
|
||||
foreach ([[], ['tcm.diagnosis/huifang'], ['tcm.diagnosis/edit'], ['tcm.diagnosis/video-call'],
|
||||
['tcm.diagnosis/saveLocalAudioTranscript'], [$recoveryGrants[0]], array_slice($recoveryGrants, 0, 2),
|
||||
[$recoveryGrants[0], $recoveryGrants[1], $recoveryGrants[2] . 'Other']] as $permissions) {
|
||||
$request = new ConsultationTranscriptRequest();
|
||||
$request->actionName = 'SaVeLoCaLaUdIoTrAnScRiPt';
|
||||
$GLOBALS['consultation_transcript_request'] = $request;
|
||||
AdminAuthCache::$permissions = $permissions;
|
||||
AdminAuthCache::$allUriReads = 0;
|
||||
$continued = false;
|
||||
$denied = (new AuthMiddleware())->handle($request, static function () use (&$continued): array {
|
||||
$continued = true; return ['code' => 1];
|
||||
});
|
||||
transcriptAuthorizationExpect($denied['code'] === 0 && !$continued, 'recovery requires all exact realtime grants');
|
||||
transcriptAuthorizationExpect(AdminAuthCache::$allUriReads === 0, 'recovery never uses unregistered route bypass');
|
||||
}
|
||||
foreach ([false, true] as $root) {
|
||||
$payload = ['diagnosis_id' => 701, 'call_record_id' => 1102, 'admin_id' => 9999,
|
||||
'recovery_id' => 'audio-' . str_repeat('a', 32), 'expected_session_id' => 'session',
|
||||
'expected_status' => 'partial', 'audio_url' => 'https://audio.invalid/test.webm',
|
||||
'transcript_text' => "原文 <test> &\n结尾"];
|
||||
$request = new ConsultationTranscriptRequest($payload, $root);
|
||||
$request->actionName = 'saveLocalAudioTranscript';
|
||||
$request->postRequest = true;
|
||||
$GLOBALS['consultation_transcript_request'] = $request;
|
||||
$controller = new ConsultationTranscriptController($request);
|
||||
$request->controllerObject = $controller;
|
||||
AdminAuthCache::$permissions = $root ? [] : array_map('strtoupper', $recoveryGrants);
|
||||
DiagnosisLogic::$events = [];
|
||||
$saved = (new AuthMiddleware())->handle($request, static fn () => $controller->saveLocalAudioTranscript());
|
||||
transcriptAuthorizationExpect($saved['code'] === 1 && $saved['data']['saved'] === true, 'granted actor can invoke recovery');
|
||||
$payload['admin_id'] = 91;
|
||||
transcriptAuthorizationExpect(DiagnosisLogic::$events === [['recover', $payload]], 'controller uses login actor and preserves transcript text');
|
||||
$request->postRequest = false;
|
||||
DiagnosisLogic::$events = [];
|
||||
transcriptAuthorizationExpect($controller->saveLocalAudioTranscript()['code'] === 0 && DiagnosisLogic::$events === [],
|
||||
'recovery rejects GET before persistence');
|
||||
}
|
||||
|
||||
echo "Consultation transcript authorization: {$assertions} assertions passed (no DB or network)\n";
|
||||
}
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
// Execute the production recovery and realtime finish logic against in-memory IO.
|
||||
namespace recovery_test {
|
||||
final class Record extends \ArrayObject
|
||||
{
|
||||
public function save(array $values): void
|
||||
{
|
||||
if (!\think\facade\Db::$transaction) { throw new \RuntimeException('Write outside transaction'); }
|
||||
if (\think\facade\Db::$failSave) { throw new \RuntimeException('Synthetic database failure'); }
|
||||
foreach ($values as $key => $value) { $this[$key] = $value; }
|
||||
}
|
||||
}
|
||||
|
||||
final class RecordQuery
|
||||
{
|
||||
private array $filters = [];
|
||||
private bool $locked = false;
|
||||
public function where(string $key, $value): self { $this->filters[$key] = $value; return $this; }
|
||||
public function lock(bool $locked): self { $this->locked = $locked; return $this; }
|
||||
public function find(): ?Record
|
||||
{
|
||||
if (!$this->locked || !\think\facade\Db::$transaction) { throw new \RuntimeException('Missing transaction row lock'); }
|
||||
foreach (\app\common\model\tcm\CallRecord::$rows as $row) {
|
||||
foreach ($this->filters as $key => $value) { if (($row[$key] ?? null) !== $value) { continue 2; } }
|
||||
return $row;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
final class SegmentQuery
|
||||
{
|
||||
private array $filters = [];
|
||||
public function where(string $key, $value): self { $this->filters[$key] = $value; return $this; }
|
||||
public function order(string $order): self { return $this; }
|
||||
public function select(): self { return $this; }
|
||||
public function toArray(): array
|
||||
{
|
||||
return array_values(array_filter(\think\facade\Db::$segments, function (array $row): bool {
|
||||
foreach ($this->filters as $key => $value) { if ($row[$key] !== $value) { return false; } }
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
public function insertAll(array $rows): void
|
||||
{
|
||||
if (!\think\facade\Db::$transaction) { throw new \RuntimeException('Segment write outside transaction'); }
|
||||
foreach ($rows as $row) {
|
||||
foreach (\think\facade\Db::$segments as $existing) {
|
||||
if ($row['call_record_id'] === $existing['call_record_id']
|
||||
&& $row['transcription_session_id'] === $existing['transcription_session_id']
|
||||
&& $row['segment_id'] === $existing['segment_id']) { throw new \RuntimeException('Duplicate segment'); }
|
||||
}
|
||||
\think\facade\Db::$segments[] = $row;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace app\common\model\tcm {
|
||||
final class CallRecord
|
||||
{
|
||||
public static array $rows = [];
|
||||
public static function where(string $key, $value): \recovery_test\RecordQuery
|
||||
{
|
||||
return (new \recovery_test\RecordQuery())->where($key, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace think\facade {
|
||||
final class Db
|
||||
{
|
||||
public static bool $transaction = false;
|
||||
public static bool $failSave = false;
|
||||
public static array $segments = [];
|
||||
public static function transaction(callable $callback)
|
||||
{
|
||||
$rows = array_map(static fn ($row) => clone $row, \app\common\model\tcm\CallRecord::$rows);
|
||||
$segments = self::$segments;
|
||||
self::$transaction = true;
|
||||
try { return $callback(); }
|
||||
catch (\Throwable $e) {
|
||||
\app\common\model\tcm\CallRecord::$rows = $rows;
|
||||
self::$segments = $segments;
|
||||
throw $e;
|
||||
} finally { self::$transaction = false; }
|
||||
}
|
||||
public static function name(string $name): \recovery_test\SegmentQuery
|
||||
{
|
||||
if ($name !== 'tcm_call_transcript_segment') { throw new \RuntimeException('Unexpected table'); }
|
||||
return new \recovery_test\SegmentQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use app\adminapi\logic\tcm\DiagnosisLogic as Logic;
|
||||
use app\common\model\tcm\CallRecord;
|
||||
use think\facade\Db;
|
||||
|
||||
$assertions = 0;
|
||||
function check(bool $condition, string $message): void
|
||||
{
|
||||
global $assertions;
|
||||
if (!$condition) { throw new RuntimeException($message); }
|
||||
$assertions++;
|
||||
}
|
||||
function fixture(array $changes = []): array
|
||||
{
|
||||
CallRecord::$rows = [new recovery_test\Record(array_replace([
|
||||
'id' => 17, 'diagnosis_id' => 23, 'caller_id' => 91, 'caller_type' => 'doctor',
|
||||
'status' => 2, 'local_audio_status' => 2, 'local_audio_urls' => '["https://audio.invalid/synthetic.webm"]',
|
||||
'transcription_session_id' => 'original-session', 'transcription_status' => 'partial',
|
||||
'transcription_segment_count' => 1, 'transcript_text' => '未知说话人:片段',
|
||||
], $changes))];
|
||||
Db::$segments = [['call_record_id' => 17, 'transcription_session_id' => 'original-session',
|
||||
'segment_id' => 'old-1', 'text' => '片段', 'speaker_role' => 'unknown', 'timestamp_ms' => 10]];
|
||||
Db::$failSave = false;
|
||||
return ['diagnosis_id' => 23, 'call_record_id' => 17, 'admin_id' => 91,
|
||||
'recovery_id' => 'audio-' . str_repeat('a', 32), 'expected_session_id' => 'original-session',
|
||||
'expected_status' => 'partial', 'audio_url' => 'https://audio.invalid/synthetic.webm',
|
||||
'transcript_text' => "完整的合成录音。\n保留结尾及否定:没有服药。"];
|
||||
}
|
||||
function rejects(array $params, string $label): void
|
||||
{
|
||||
$before = serialize([CallRecord::$rows, Db::$segments]);
|
||||
check(Logic::saveLocalAudioTranscript($params) === false, $label . ' rejects');
|
||||
check(serialize([CallRecord::$rows, Db::$segments]) === $before, $label . ' does not mutate');
|
||||
}
|
||||
|
||||
$params = fixture();
|
||||
$result = Logic::saveLocalAudioTranscript($params);
|
||||
check($result === ['call_record_id' => 17, 'transcription_status' => 'completed', 'saved' => true], 'recovery saves');
|
||||
check(CallRecord::$rows[0]['transcription_session_id'] === $params['recovery_id'], 'new session is active');
|
||||
check(CallRecord::$rows[0]['transcript_text'] === '未知说话人:' . $params['transcript_text'], 'complete text retained without claiming a speaker');
|
||||
check(count(Db::$segments) === 2 && Db::$segments[0]['text'] === '片段', 'old session retained');
|
||||
check(Db::$segments[1]['speaker_role'] === 'unknown' && Db::$segments[1]['timestamp_ms'] === 0, 'no invented attribution or timing');
|
||||
$before = serialize([CallRecord::$rows, Db::$segments]);
|
||||
check(Logic::saveLocalAudioTranscript($params) === $result, 'lost response retry is idempotent despite stale CAS');
|
||||
check(serialize([CallRecord::$rows, Db::$segments]) === $before, 'retry writes no duplicate segments');
|
||||
$late = Logic::upsertCallTranscriptSegments(['diagnosis_id' => 23, 'call_record_id' => 17, 'admin_id' => 91,
|
||||
'transcription_session_id' => 'original-session', 'segments' => [['segment_id' => 'late', 'text' => '迟到分段']]]);
|
||||
check($late === false, 'old realtime writer cannot overwrite replacement session');
|
||||
$lateFinish = Logic::finishCallTranscription(['diagnosis_id' => 23, 'call_record_id' => 17, 'admin_id' => 91,
|
||||
'transcription_session_id' => 'original-session', 'status' => 'completed', 'expected_segment_count' => 1]);
|
||||
check($lateFinish === false && serialize([CallRecord::$rows, Db::$segments]) === $before, 'late finish cannot overwrite replacement');
|
||||
|
||||
$params = fixture(['transcription_status' => 'completed']);
|
||||
$before = serialize([CallRecord::$rows, Db::$segments]);
|
||||
check(Logic::saveLocalAudioTranscript($params) === ['call_record_id' => 17, 'transcription_status' => 'completed',
|
||||
'saved' => false, 'already_complete' => true], 'another complete session wins even with stale CAS');
|
||||
check(serialize([CallRecord::$rows, Db::$segments]) === $before, 'completed transcript never overwritten');
|
||||
foreach (['', 'pending', 'running', 'partial', 'failed', 'completed'] as $status) {
|
||||
$params = fixture(['transcription_status' => $status, 'transcript_text' => '']);
|
||||
$params['expected_status'] = $status;
|
||||
check(Logic::saveLocalAudioTranscript($params)['saved'] === true, 'recover empty/nonterminal session ' . $status);
|
||||
}
|
||||
$params = fixture(['transcription_session_id' => '', 'transcription_status' => '', 'transcript_text' => '']);
|
||||
$params['expected_session_id'] = $params['expected_status'] = '';
|
||||
check(Logic::saveLocalAudioTranscript($params)['saved'] === true, 'record without prior session can recover');
|
||||
|
||||
foreach (['expected_session_id' => 'changed-session', 'expected_status' => 'running',
|
||||
'audio_url' => 'https://audio.invalid/different.webm', 'admin_id' => 92, 'diagnosis_id' => 24,
|
||||
'call_record_id' => 18, 'recovery_id' => 'invalid', 'transcript_text' => ' '] as $key => $value) {
|
||||
$params = fixture(); $params[$key] = $value; rejects($params, 'invalid ' . $key);
|
||||
}
|
||||
foreach ([['status' => 1], ['status' => 3], ['caller_type' => 'patient'], ['local_audio_status' => 0],
|
||||
['local_audio_urls' => 'invalid'], ['local_audio_urls' => '[]']] as $changes) {
|
||||
rejects(fixture($changes), 'invalid record ' . json_encode($changes));
|
||||
}
|
||||
foreach ([null, [], false, 23] as $value) {
|
||||
$params = fixture(); $params['transcript_text'] = $value; rejects($params, 'non-string text');
|
||||
}
|
||||
$params = fixture(); $params['transcript_text'] = str_repeat('字', 100001); rejects($params, 'oversize text');
|
||||
$params = fixture(); $params['transcript_text'] = str_repeat('字', 100000);
|
||||
check(Logic::saveLocalAudioTranscript($params)['saved'] === true, 'Unicode limit counts characters');
|
||||
$newSegments = array_slice(Db::$segments, 1);
|
||||
check(count($newSegments) === 25 && implode('', array_column($newSegments, 'text')) === $params['transcript_text'],
|
||||
'bounded segments preserve all 100000 characters in order');
|
||||
check(max(array_map(static fn ($row) => mb_strlen($row['text']), $newSegments)) <= 4000, 'segment schema limit respected');
|
||||
|
||||
$params = fixture(); Db::$failSave = true;
|
||||
rejects($params, 'transaction rollback after segment insertion');
|
||||
check(!str_contains(Logic::getError(), 'Synthetic'), 'internal failure detail is not exposed');
|
||||
|
||||
fixture(['transcription_status' => 'running', 'transcript_text' => '', 'transcription_segment_count' => 0]);
|
||||
Db::$segments = [];
|
||||
$finish = ['diagnosis_id' => 23, 'call_record_id' => 17, 'admin_id' => 91,
|
||||
'transcription_session_id' => 'original-session', 'status' => 'completed', 'expected_segment_count' => 0];
|
||||
check(Logic::finishCallTranscription($finish)['status'] === 'failed', 'zero-segment completion is failed');
|
||||
fixture(['transcription_status' => 'running']);
|
||||
$finish['expected_segment_count'] = 2;
|
||||
check(Logic::finishCallTranscription($finish)['status'] === 'partial', 'missing requested segments stays partial');
|
||||
fixture(['transcription_status' => 'running']);
|
||||
$finish['expected_segment_count'] = 1;
|
||||
check(Logic::finishCallTranscription($finish)['status'] === 'completed', 'nonempty complete realtime session still completes');
|
||||
|
||||
echo "Local audio transcript recovery: {$assertions} assertions passed (no DB or network)\n";
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/** Disposable in-memory synthetic database only. No .env, services or production connection. */
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
require dirname(__DIR__) . '/vendor/topthink/framework/src/helper.php';
|
||||
|
||||
use app\adminapi\logic\tcm\MobileRecordingLogic as Logic;
|
||||
use think\Container;
|
||||
use think\facade\Db;
|
||||
|
||||
if (!in_array('sqlite', PDO::getAvailableDrivers(), true)) {
|
||||
fwrite(STDERR, "SQLite PDO is required; no production fallback is allowed.\n");
|
||||
exit(1);
|
||||
}
|
||||
new think\App(); // Do not initialize application services or load configuration.
|
||||
$manager = new think\DbManager();
|
||||
$manager->setConfig(['default' => 'sqlite', 'auto_timestamp' => false, 'datetime_format' => false,
|
||||
'connections' => ['sqlite' => ['type' => 'sqlite', 'database' => ':memory:', 'prefix' => 'zyt_']]]);
|
||||
Container::getInstance()->instance('think\DbManager', $manager);
|
||||
$config = new think\Config();
|
||||
$config->set(['data_scope' => ['enabled' => false]], 'project');
|
||||
Container::getInstance()->instance('config', $config);
|
||||
|
||||
foreach ([
|
||||
'CREATE TABLE zyt_admin (id INTEGER PRIMARY KEY, root INT, name TEXT, disable INT, delete_time INT)',
|
||||
'CREATE TABLE zyt_admin_role (admin_id INT, role_id INT)',
|
||||
'CREATE TABLE zyt_system_role_menu (role_id INT, menu_id INT)',
|
||||
'CREATE TABLE zyt_system_menu (id INTEGER PRIMARY KEY, perms TEXT, is_disable INT)',
|
||||
'CREATE TABLE zyt_tcm_diagnosis (id INTEGER PRIMARY KEY, patient_id INT, patient_name TEXT, phone TEXT, assistant_id INT, status INT, delete_time INT)',
|
||||
'CREATE TABLE zyt_doctor_appointment (id INTEGER PRIMARY KEY, patient_id INT, doctor_id INT, status INT)',
|
||||
'CREATE TABLE zyt_mobile_recording_binding (id INTEGER PRIMARY KEY AUTOINCREMENT, admin_id INT NOT NULL,
|
||||
installation_id TEXT NOT NULL, recording_key TEXT NOT NULL, diagnosis_id INT NOT NULL, source_platform TEXT NOT NULL,
|
||||
recorded_at INT NOT NULL, duration_ms INT NOT NULL, created_at INT NOT NULL, updated_at INT NOT NULL,
|
||||
UNIQUE(admin_id, installation_id, recording_key))',
|
||||
] as $ddl) {
|
||||
Db::execute($ddl);
|
||||
}
|
||||
Db::name('admin')->insertAll([
|
||||
['id' => 1, 'root' => 1, 'name' => '合成管理员', 'disable' => 0],
|
||||
['id' => 11, 'root' => 0, 'name' => '合成助理甲', 'disable' => 0],
|
||||
['id' => 12, 'root' => 0, 'name' => '合成助理乙', 'disable' => 0],
|
||||
['id' => 13, 'root' => 0, 'name' => '合成医生', 'disable' => 0],
|
||||
['id' => 14, 'root' => 0, 'name' => '合成停用账号', 'disable' => 1],
|
||||
['id' => 15, 'root' => 0, 'name' => '合成无权限账号', 'disable' => 0],
|
||||
]);
|
||||
Db::name('admin_role')->insertAll([
|
||||
['admin_id' => 11, 'role_id' => 2], ['admin_id' => 12, 'role_id' => 2],
|
||||
['admin_id' => 13, 'role_id' => 1], ['admin_id' => 14, 'role_id' => 2],
|
||||
]);
|
||||
Db::name('system_menu')->insert(['id' => 1, 'perms' => Logic::PAGE_PERMISSION, 'is_disable' => 0]);
|
||||
Db::name('system_role_menu')->insertAll([['role_id' => 1, 'menu_id' => 1], ['role_id' => 2, 'menu_id' => 1]]);
|
||||
Db::name('tcm_diagnosis')->insertAll([
|
||||
['id' => 101, 'patient_id' => 501, 'patient_name' => '合成患者甲', 'phone' => '13800000001', 'assistant_id' => 11, 'status' => 1, 'delete_time' => null],
|
||||
['id' => 102, 'patient_id' => 502, 'patient_name' => '合成患者乙', 'phone' => '13800000002', 'assistant_id' => 11, 'status' => 1, 'delete_time' => null],
|
||||
['id' => 103, 'patient_id' => 503, 'patient_name' => '合成患者丙', 'phone' => '13800000003', 'assistant_id' => 12, 'status' => 1, 'delete_time' => null],
|
||||
['id' => 104, 'patient_id' => 504, 'patient_name' => '合成停用患者', 'phone' => '', 'assistant_id' => 11, 'status' => 0, 'delete_time' => null],
|
||||
['id' => 105, 'patient_id' => 505, 'patient_name' => '合成已删患者', 'phone' => '', 'assistant_id' => 11, 'status' => 1, 'delete_time' => 1],
|
||||
['id' => 106, 'patient_id' => 506, 'patient_name' => '合成接诊患者', 'phone' => '', 'assistant_id' => 12, 'status' => 1, 'delete_time' => null],
|
||||
]);
|
||||
Db::name('doctor_appointment')->insert(['id' => 1, 'patient_id' => 106, 'doctor_id' => 13, 'status' => 3]);
|
||||
|
||||
$checks = 0;
|
||||
$expect = static function (bool $ok, string $why) use (&$checks): void {
|
||||
if (!$ok) { throw new RuntimeException($why); }
|
||||
$checks++;
|
||||
};
|
||||
$denied = static function (callable $call, string $why) use ($expect): void {
|
||||
try { $call(); } catch (DomainException $e) { $expect(true, $why); return; }
|
||||
throw new RuntimeException('Expected denial: ' . $why);
|
||||
};
|
||||
$input = [
|
||||
'installation_id' => '12345678-1234-4123-8123-123456789abc',
|
||||
'recording_key' => str_repeat('a', 64), 'diagnosis_id' => 101,
|
||||
'source_platform' => 'android_bcr', 'recorded_at' => 1700000000000, 'duration_ms' => 65000,
|
||||
];
|
||||
$identity = array_intersect_key($input, array_flip(['installation_id', 'recording_key']));
|
||||
foreach ([
|
||||
['admin_id' => 1], ['phone' => '13800000000'], ['file_name' => 'secret.wav'],
|
||||
['audio_path' => '/private/audio'], ['transcript_text' => 'content'],
|
||||
] as $extra) {
|
||||
$denied(fn () => Logic::bindingInput($input + $extra), 'unexpected private or actor field rejected');
|
||||
}
|
||||
foreach ([
|
||||
['installation_id' => 'not-a-uuid'], ['recording_key' => '../path'], ['diagnosis_id' => true],
|
||||
['duration_ms' => 1.5], ['duration_ms' => -1], ['duration_ms' => 604800001],
|
||||
['recorded_at' => (time() + 3600) * 1000], ['recorded_at' => '1e10'], ['source_platform' => 'other'],
|
||||
] as $invalid) {
|
||||
$denied(fn () => Logic::bindingInput(array_replace($input, $invalid)), 'invalid typed field rejected');
|
||||
}
|
||||
$denied(fn () => Logic::bind($input, 15), 'missing page permission');
|
||||
$denied(fn () => Logic::bind($input, 14), 'disabled actor');
|
||||
$denied(fn () => Logic::bind($input, 0), 'missing actor');
|
||||
$denied(fn () => Logic::bind(array_replace($input, ['diagnosis_id' => 103]), 11), 'other assistant patient');
|
||||
$denied(fn () => Logic::bind(array_replace($input, ['diagnosis_id' => 104]), 11), 'disabled patient');
|
||||
$denied(fn () => Logic::bind(array_replace($input, ['diagnosis_id' => 105]), 11), 'deleted patient');
|
||||
$created = Logic::bind($input, 11)['binding'];
|
||||
$again = Logic::bind($input, 11)['binding'];
|
||||
$expect($created['id'] === $again['id'] && Db::name('mobile_recording_binding')->count() === 1, 'repeat bind is idempotent');
|
||||
$expect($created['patient']['diagnosis_id'] === 101 && $created['patient']['source_patient_id'] === 501, 'separate diagnosis/source patient identities');
|
||||
$expect($created['patient']['phone_masked'] === '138****0001', 'masked phone only');
|
||||
$expect(!str_contains(json_encode($created), '13800000001') && !isset($created['admin_id']), 'private data and actor ID omitted');
|
||||
|
||||
$other = Logic::bind(array_replace($input, ['diagnosis_id' => 103]), 12)['binding'];
|
||||
$expect($other['id'] !== $created['id'], 'unique key includes current actor');
|
||||
Logic::unbind($identity, 12);
|
||||
Logic::unbind($identity, 12);
|
||||
$expect(Db::name('mobile_recording_binding')->where('id', $created['id'])->count() === 1, 'another actor unbind cannot touch original row');
|
||||
$denied(fn () => Logic::bind(array_replace($input, ['diagnosis_id' => 103]), 11), 'replacement target is checked');
|
||||
$replaced = Logic::bind(array_replace($input, ['diagnosis_id' => 102]), 11)['binding'];
|
||||
$expect($replaced['id'] === $created['id'] && $replaced['diagnosis_id'] === 102, 'replacement updates existing identity');
|
||||
|
||||
$second = array_replace($input, ['recording_key' => str_repeat('b', 64)]);
|
||||
Logic::bind($second, 11);
|
||||
$device = ['installation_id' => $input['installation_id']];
|
||||
$page = Logic::deviceLists($device + ['page_no' => 1, 'page_size' => 1], 11);
|
||||
$expect($page['count'] === 2 && count($page['lists']) === 1 && $page['page_size'] === 1, 'bounded device pagination');
|
||||
$expect(Logic::deviceLists(['installation_id' => '22345678-1234-4123-8123-123456789abc'], 11)['count'] === 0, 'device isolation');
|
||||
$expect(Logic::deviceLists($device, 12)['count'] === 0, 'account isolation');
|
||||
$denied(fn () => Logic::patientLists(['diagnosis_id' => 102], 12), 'patient listing scope');
|
||||
$records = Logic::patientLists(['diagnosis_id' => 102], 11);
|
||||
$record = $records['lists'][0];
|
||||
$expect($record['bound_by'] === '合成助理甲' && $record['playback_location'] === 'original_device', 'archive playback and actor metadata');
|
||||
$expect(!isset($record['installation_id'], $record['recording_key']) && !isset($record['patient_name']), 'archive omits device keys and unnecessary patient names');
|
||||
$denied(fn () => Logic::deviceLists($device + ['page_size' => 101], 11), 'page limit');
|
||||
|
||||
$doctor = Logic::bind(array_replace($input, ['diagnosis_id' => 106]), 13);
|
||||
$expect($doctor['binding']['diagnosis_id'] === 106, 'doctor uses own effective appointment');
|
||||
$denied(fn () => Logic::bind($input, 13), 'doctor cannot use unrelated patient');
|
||||
Logic::bind(array_replace($input, ['diagnosis_id' => 103]), 1);
|
||||
$expect(Logic::patientLists(['diagnosis_id' => 103], 1)['count'] === 1, 'root uses same metadata contract');
|
||||
|
||||
Db::name('tcm_diagnosis')->where('id', 102)->update(['assistant_id' => 12]);
|
||||
$denied(fn () => Logic::bind($input, 11), 'old patient checked on reassignment after scope loss');
|
||||
$denied(fn () => Logic::unbind($identity, 11), 'old patient checked on unlink after scope loss');
|
||||
$afterTransfer = Logic::deviceLists($device, 11);
|
||||
$expect($afterTransfer['count'] === 1 && !str_contains(json_encode($afterTransfer, JSON_UNESCAPED_UNICODE), '合成患者乙'), 'lost-scope row and patient name hidden');
|
||||
Db::name('tcm_diagnosis')->where('id', 101)->update(['delete_time' => 2]);
|
||||
$expect(Logic::deviceLists($device, 11)['count'] === 0, 'deleted diagnosis hidden on restore');
|
||||
$denied(fn () => Logic::patientLists(['diagnosis_id' => 101], 11), 'deleted patient archive denied');
|
||||
Db::name('tcm_diagnosis')->where('id', 101)->update(['delete_time' => null]);
|
||||
Logic::unbind(array_replace($identity, ['recording_key' => str_repeat('b', 64)]), 11);
|
||||
$expect(Logic::deviceLists($device, 11)['count'] === 0, 'authorized unbind removes association');
|
||||
Db::name('system_menu')->where('id', 1)->update(['is_disable' => 1]);
|
||||
$denied(fn () => Logic::deviceLists($device, 11), 'revoked page permission is re-read');
|
||||
Db::name('admin')->where('id', 1)->update(['root' => 0]);
|
||||
$denied(fn () => Logic::deviceLists($device, 1), 'revoked root is re-read');
|
||||
|
||||
// Guard the integration boundary which runtime service tests cannot exercise without an HTTP app.
|
||||
$controller = file_get_contents(dirname(__DIR__) . '/app/adminapi/controller/tcm/MobileRecordingController.php');
|
||||
$middleware = file_get_contents(dirname(__DIR__) . '/app/adminapi/http/middleware/AuthMiddleware.php');
|
||||
$expect(str_contains($controller, 'MobileRecordingLogic::actor((int) $this->adminId)'), 'controller explicitly checks page grant');
|
||||
$expect(str_contains($controller, '$this->request->file()'), 'controller refuses binary upload');
|
||||
$expect(str_contains($middleware, "'tcm.mobilerecording/'")
|
||||
&& strpos($middleware, "'tcm.mobilerecording/'") < strpos($middleware, '$allUri ='), 'middleware guards before legacy unregistered-route bypass');
|
||||
echo "Mobile recording binding: {$checks} checks passed (synthetic in-memory SQLite).\n";
|
||||
Reference in New Issue
Block a user