import assert from 'node:assert/strict'; import test from 'node:test'; import { createInMemoryHealthObservationStore } from './health-observation-store.mjs'; import { createInMemoryHealthObservationDraftStore } from './health-observation-draft-store.mjs'; import { createHealthObservationService } from './health-observation-service.mjs'; import { createHealthObservationDraftService } from './health-observation-draft-service.mjs'; import { normalizeHealthDocumentOcr, parseHealthDocumentOcrJson } from './health-document-ocr.mjs'; import { buildHealthAgentContext, wrapHealthAgentUserMessage } from './health-agent-context.mjs'; import { hashHealthImageBuffer } from './health-image-hash.mjs'; test('hashHealthImageBuffer is stable for same bytes', () => { const buffer = Buffer.from('health-image-bytes'); assert.equal(hashHealthImageBuffer(buffer), hashHealthImageBuffer(buffer)); }); test('draft service dedups by sourceRef and commits after confirm', async () => { const observationStore = createInMemoryHealthObservationStore(); const draftStore = createInMemoryHealthObservationDraftStore(); const observationService = createHealthObservationService({ store: observationStore }); const draftService = createHealthObservationDraftService({ draftStore, observationService }); const userId = 'u1'; const sourceRef = 'sha256-deadbeef'; const extraction = { ok: true, metricSet: 'heart_rate', values: { hr: 72 }, lowConfidence: false, }; const draft1 = await draftService.saveExtractionDraft(userId, { extraction, sourceRef, channel: 'h5', }); const draft2 = await draftService.saveExtractionDraft(userId, { extraction, sourceRef, channel: 'h5', }); assert.equal(draft1.draftKey, draft2.draftKey); const result = await draftService.commitDraft(userId, draft1.draftKey, { source: 'photo' }); assert.equal(result.observations.length, 1); assert.equal(result.observations[0].metricType, 'hr'); assert.equal(result.observations[0].sourceRef, sourceRef); }); test('normalizeHealthDocumentOcr parses lab metrics', () => { const parsed = parseHealthDocumentOcrJson( '{"doc_type":"lab","report_date":"2026-08-01","institution":"市医院","title":"血常规","metrics":[{"name":"WBC","value":"6.2","unit":"10^9/L","flag":""}],"summary":"血常规报告","confidence":0.9}', ); const normalized = normalizeHealthDocumentOcr(parsed); assert.equal(normalized.ok, true); assert.equal(normalized.extractedMetrics.length, 1); assert.equal(normalized.extractionStatus, 'partial'); }); test('buildHealthAgentContext wraps assess summary', () => { const now = Date.parse('2026-09-02T08:00:00+08:00'); const observations = []; for (let i = 0; i < 8; i += 1) { const day = new Date(now - i * 86400000); observations.push({ confirmed: true, observedAt: day.getTime(), metricType: 'bp_systolic', valueNum: 120 + i, unit: 'mmHg', context: 'morning', source: 'manual', qualityFlag: 'ok', }); } const context = buildHealthAgentContext(observations, { now }); assert.match(context, /健康档案摘要/); const wrapped = wrapHealthAgentUserMessage(context, '最近怎么样?'); assert.match(wrapped, /最近怎么样/); });