Files
memind/health-page-data.test.mjs
T
john 2baf29b3ae feat(health): complete P0 health channel — baseline engine, page-data, MindSpace UI
Deliver encrypted health zone, observation API, baseline maturity pipeline,
page-data bindings, and H5/WeChat channel integration for health P0.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-09 18:04:57 +08:00

70 lines
2.7 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import { createUserDataSpaceService } from './user-data-space-service.mjs';
import { createPageDataHealthObservationStore } from './health-page-data-observation-store.mjs';
import { createHealthObservationService } from './health-observation-service.mjs';
import { resetHealthPageDataBootstrapCache } from './health-page-data-bootstrap.mjs';
import { parseMetricInput } from './health-metric-input.mjs';
import { applyHealthChannelTurn } from './health-wechat-turn.mjs';
import { createIdleHealthChannelState } from './health-channel-state.mjs';
test('parseMetricInput accepts heart rate and weight', () => {
const hr = parseMetricInput('heart_rate', '心率 72');
assert.equal(hr.ok, true);
assert.equal(hr.valueNum, 72);
const weight = parseMetricInput('weight', '65.5');
assert.equal(weight.ok, true);
assert.equal(weight.valueNum, 65.5);
});
test('health channel records heart rate after confirm', () => {
let session = {
channelState: createIdleHealthChannelState({ channel: 'h5' }),
pendingValues: null,
};
const pickRecord = applyHealthChannelTurn({ channel: 'h5', session, text: '1' });
session = pickRecord.session;
const pickHr = applyHealthChannelTurn({ channel: 'h5', session, text: '2' });
session = pickHr.session;
const value = applyHealthChannelTurn({ channel: 'h5', session, text: '72' });
session = value.session;
assert.match(value.reply ?? '', /确认保存/);
const commit = applyHealthChannelTurn({ channel: 'h5', session, text: '1' });
assert.equal(commit.commit?.confirmed, true);
assert.equal(commit.commit?.values?.metricSet, 'heart_rate');
assert.equal(commit.commit?.values?.valueNum, 72);
});
test('page data observation store persists blood pressure via sqlite test backend', async () => {
resetHealthPageDataBootstrapCache();
const workspaceRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'health-pg-'));
const userId = 'health-user-1';
const resolveWorkspaceRoot = async () => workspaceRoot;
const store = createPageDataHealthObservationStore({ resolveWorkspaceRoot });
const service = createHealthObservationService({ store });
const result = await service.commit(userId, {
confirmed: true,
metricSet: 'blood_pressure',
systolic: 128,
diastolic: 76,
context: 'morning',
observedAt: Date.now(),
source: 'manual',
});
assert.equal(result.observations.length, 2);
const listed = await store.list(userId, { limit: 10 });
assert.equal(listed.length, 2);
assert.ok(listed.some((row) => row.metricType === 'bp_systolic' && row.valueNum === 128));
});