Files
zyt/server/tests/TcmAncientBooksReferenceTest.php
T
2026-10-08 18:03:11 +08:00

60 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use app\common\service\ClinicalKnowledgeReference;
use app\common\service\NihaixiaClinicalSkill;
use app\common\service\TcmAncientBooksReference;
function ancientExpect(bool $condition, string $message): void
{
if (!$condition) {
fwrite(STDERR, "FAIL: {$message}\n");
exit(1);
}
}
$indexProperty = new ReflectionProperty(TcmAncientBooksReference::class, 'index');
$indexProperty->setValue(null, [
'commit' => TcmAncientBooksReference::COMMIT,
'index_version' => TcmAncientBooksReference::INDEX_VERSION,
'terms' => [
'消渴' => [[
'title' => '测试古籍', 'chapter' => '消渴篇',
'path' => '123-测试古籍.txt', 'line' => 42,
'excerpt' => '原文中谈到消渴的历史记载。', 'score' => 15,
]],
],
'chapter_terms' => [
'痛风' => [[
'title' => '测试古籍二', 'chapter' => '痛风',
'path' => '124-测试古籍二.txt', 'line' => 88,
'excerpt' => '痛风相关的原文。', 'score' => 12,
]],
],
]);
$query = '患者糖尿病、高血糖,请分析病症。';
$sources = TcmAncientBooksReference::sourcesForText($query);
ancientExpect(count($sources) === 1, 'matching patient symptoms select one excerpt');
ancientExpect($sources[0]['title'] === '测试古籍' && $sources[0]['chapter'] === '消渴篇', 'book and chapter retained');
ancientExpect(str_ends_with($sources[0]['url'], '/123-%E6%B5%8B%E8%AF%95%E5%8F%A4%E7%B1%8D.txt#L42'), 'permalink pins file and line');
ancientExpect(TcmAncientBooksReference::sourcesForText('请分析耳鸣。') === [], 'unmatched symptoms have no invented citation');
ancientExpect(TcmAncientBooksReference::sourcesForText('患者出现痛风症状。')[0]['line'] === 88, 'unlisted diseases can match indexed chapter headings');
$augmented = ClinicalKnowledgeReference::augmentQuery($query);
ancientExpect(str_contains($augmented, TcmAncientBooksReference::COMMIT), 'query includes ancient-book version');
ancientExpect(str_contains($augmented, NihaixiaClinicalSkill::VERSION), 'existing clinical skill remains');
ancientExpect(str_ends_with($augmented, $query), 'original patient question remains intact');
ancientExpect(ClinicalKnowledgeReference::augmentQuery($augmented) === $augmented, 'retry does not duplicate references');
$messages = [['role' => 'user', 'content' => $query]];
$wire = ClinicalKnowledgeReference::augmentMessages($messages);
ancientExpect(count($wire) === 2 && str_contains($wire[0]['content'], '测试古籍'), 'chat receives reference in system message');
ancientExpect(ClinicalKnowledgeReference::augmentMessages($wire) === $wire, 'stream retry does not duplicate reference');
$indexProperty->setValue(null, null);
echo "TCM ancient-books reference contract passed.\n";