import { test } from "node:test"; import assert from "node:assert/strict"; import { DBModel } from "../models/index.js"; import { HandlerEscortRecord } from "../handler/escort_record.js"; import { HandlerHealthProfile } from "../handler/health_profile.js"; const RECORD_ID = "64b000000000000000000010"; const OWNER_ID = "64b000000000000000000001"; const ATTENDANT_ID = "64b000000000000000000002"; const ADMIN_ID = "64b000000000000000000003"; const PAYMENT_ID = "64b000000000000000000020"; const ADMIN_USER = { _id: ADMIN_ID, app: { "wxapp_companion": { role: ["admin"] } }, }; function buildRecord(overrides = {}) { return { _id: RECORD_ID, userId: OWNER_ID, status: "confirmed", cancelStatus: "none", escort: { serviceId: 1, serviceName: "全天陪诊" }, hospital: { province: "北京市", name: "北京协和医院" }, schedule: { date: new Date("2026-12-01T00:00:00.000Z"), startTime: "08:00", endTime: "17:00", duration: 480, }, payment: { totalFee: 500, paidFee: 0, status: "unpaid", payChannel: "online_wechat", paymentRecordId: PAYMENT_ID, }, notes: { patientNote: "患者原备注", escortNote: "原服务记录", medicalSummary: "" }, attendant: { id: ATTENDANT_ID, name: "李四" }, ...overrides, }; } function createCtx({ body = {}, user, params = { id: RECORD_ID } } = {}) { return { request: { query: {}, body }, params, state: { user: user ?? { _id: OWNER_ID } }, headers: {}, status: 200, body: null, set(name, value) { this.headers[name] = value; }, }; } function createRecordStub(record) { const calls = { update: null, count: 0 }; return { calls, findById: async () => record, updateRecord: async (id, payload) => { calls.update = payload; calls.count += 1; return { ...record, ...payload }; }, }; } function withModel(modelName, stub, fn) { const previous = DBModel[modelName]; DBModel[modelName] = stub; return Promise.resolve(fn()).finally(() => { DBModel[modelName] = previous; }); } test("陪诊员只能写服务备注,医院与患者备注被过滤", async () => { const stub = createRecordStub(buildRecord()); await withModel("EscortRecord", stub, async () => { const ctx = createCtx({ user: { _id: ATTENDANT_ID }, body: { "hospital.name": "被篡改的医院", "notes.patientNote": "被篡改的患者备注", "notes.escortNote": "已完成陪同", }, }); await new HandlerEscortRecord().updateRecord(ctx); assert.equal(ctx.body.code, 0); assert.deepEqual(stub.calls.update, { "notes.escortNote": "已完成陪同" }); }); }); test("陪诊员在待确认订单上不能写入", async () => { const stub = createRecordStub(buildRecord({ status: "pending" })); await withModel("EscortRecord", stub, async () => { const ctx = createCtx({ user: { _id: ATTENDANT_ID }, body: { "notes.escortNote": "提前写" }, }); await new HandlerEscortRecord().updateRecord(ctx); assert.equal(ctx.body.code, 403); assert.equal(stub.calls.count, 0); }); }); test("订单主人仍可修改医院等预约字段", async () => { const stub = createRecordStub(buildRecord()); await withModel("EscortRecord", stub, async () => { const ctx = createCtx({ body: { "hospital.name": "北京协和医院东院" } }); await new HandlerEscortRecord().updateRecord(ctx); assert.equal(ctx.body.code, 0); assert.equal(stub.calls.update["hospital.name"], "北京协和医院东院"); }); }); test("订单主人写四段病历时同步生成旧版兼容摘要", async () => { const stub = createRecordStub(buildRecord()); await withModel("EscortRecord", stub, async () => { const ctx = createCtx({ body: { "inquiry.basicHealth": "高血压", "inquiry.treatmentHistory": "近期复查", "inquiry.medications": "每日一次", "inquiry.demand": "解读报告", }, }); await new HandlerEscortRecord().updateRecord(ctx); assert.equal(ctx.body.code, 0); assert.match(stub.calls.update["inquiry.medicalHistory"], /基础健康与基础疾病/); assert.match(stub.calls.update["inquiry.medicalHistory"], /检查与治疗病历/); assert.match(stub.calls.update["inquiry.medicalHistory"], /用药情况/); }); }); test("旧客户端只写 medicalHistory 时迁移到检查与治疗病历", async () => { const stub = createRecordStub(buildRecord({ inquiry: { basicHealth: "旧基础", treatmentHistory: "旧检查", medications: "旧用药", demand: "旧诉求" }, })); await withModel("EscortRecord", stub, async () => { const ctx = createCtx({ body: { "inquiry.medicalHistory": "旧版整段病历" } }); await new HandlerEscortRecord().updateRecord(ctx); assert.equal(ctx.body.code, 0); assert.equal(stub.calls.update["inquiry.basicHealth"], ""); assert.equal(stub.calls.update["inquiry.treatmentHistory"], "旧版整段病历"); assert.equal(stub.calls.update["inquiry.medications"], ""); }); }); test("管理员通用更新不能绕过状态机回退状态", async () => { const stub = createRecordStub(buildRecord({ status: "completed" })); await withModel("EscortRecord", stub, async () => { const ctx = createCtx({ user: ADMIN_USER, body: { status: "in_progress" } }); await new HandlerEscortRecord().updateRecord(ctx); assert.equal(ctx.body.code, 400); assert.equal(stub.calls.count, 0); }); }); test("管理员合法状态流转仍可通过", async () => { const stub = createRecordStub(buildRecord({ status: "pending" })); await withModel("EscortRecord", stub, async () => { const ctx = createCtx({ user: ADMIN_USER, body: { status: "confirmed" } }); await new HandlerEscortRecord().updateRecord(ctx); assert.equal(ctx.body.code, 0); assert.equal(stub.calls.update.status, "confirmed"); }); }); test("管理员只提交部分 payment 字段时保留中间支付状态", async () => { const stub = createRecordStub(buildRecord()); const paymentStub = { findOne: async () => null }; await withModel("EscortRecord", stub, () => withModel("PaymentRecord", paymentStub, async () => { const ctx = createCtx({ user: ADMIN_USER, body: { payment: { totalFee: 600 } } }); await new HandlerEscortRecord().updateRecord(ctx); assert.equal(ctx.body.code, 0); assert.equal(stub.calls.update["payment.totalFee"], 600); assert.equal(stub.calls.update["payment.status"], "unpaid"); assert.equal(stub.calls.update["payment.paymentRecordId"], PAYMENT_ID); assert.equal(stub.calls.update["payment.payChannel"], "online_wechat"); // 嵌套对象必须摊平,不能与点号路径同时出现在 $set 里 assert.equal(stub.calls.update.payment, undefined); }) ); }); test("切换支付渠道时嵌套 payment 摊平后不会产生路径冲突", async () => { const stub = createRecordStub(buildRecord()); const paymentStub = { findOne: async () => null, find: async () => [], }; await withModel("EscortRecord", stub, () => withModel("PaymentRecord", paymentStub, async () => { const ctx = createCtx({ user: ADMIN_USER, body: { payment: { payChannel: "offline" } } }); await new HandlerEscortRecord().updateRecord(ctx); assert.equal(ctx.body.code, 0); assert.equal(stub.calls.update.payment, undefined); assert.equal(stub.calls.update["payment.payChannel"], "offline"); assert.equal(stub.calls.update["payment.paymentRecordId"], null); }) ); }); test("在职陪诊员不能确认未分配给自己的本人订单", async () => { const record = buildRecord({ status: "pending" }); let statusUpdates = 0; const recordStub = { ...createRecordStub(record), updateRecordStatus: async () => { statusUpdates += 1; return record; }, }; const employeeStub = { findOne: () => ({ select: () => ({ lean: async () => ({ role: "attendant" }), }), }), }; await withModel("EscortRecord", recordStub, () => withModel("Employee", employeeStub, async () => { const ctx = createCtx({ user: { _id: OWNER_ID, app: { "wxapp_companion": { role: ["user"] } }, }, body: { status: "confirmed" }, }); await new HandlerEscortRecord().updateStatus(ctx); assert.equal(ctx.body.code, 403); assert.equal(statusUpdates, 0); }) ); }); test("健康档案更新把嵌套对象摊平成点号路径", () => { const picked = HandlerHealthProfile.pickFields( { profile: { name: "李四", sex: "male" }, health: { height: 170, weight: 60 }, userId: OWNER_ID, }, { flatten: true } ); assert.deepEqual(picked, { "profile.name": "李四", "profile.sex": "male", "health.height": 170, "health.weight": 60, userId: OWNER_ID, }); assert.equal(picked.profile, undefined); assert.equal(picked.health, undefined); });