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 { disclaimer } from "../resource/agreement.js"; const USER_ID = "64b000000000000000000101"; const PROFILE_ID = "64b000000000000000000102"; const EMPLOYEE_ID = "64b000000000000000000103"; const EMPLOYEE_USER_ID = "64b000000000000000000104"; const RECORD_ID = "64b000000000000000000105"; function futureDate(days) { const date = new Date(); date.setDate(date.getDate() + days); return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String( date.getDate() ).padStart(2, "0")}`; } function buildProfile() { return { _id: PROFILE_ID, userId: USER_ID, profile: { name: "张三", mobile: "13800138000", sex: "male", birth: "1990-01-01" }, }; } function buildEmployee(overrides = {}) { return { _id: EMPLOYEE_ID, userId: EMPLOYEE_USER_ID, role: "attendant", status: "active", profile: { name: "李四", sex: "female", phone: "13700137000" }, attendantInfo: { stage: "certified", grade: "silver" }, ...overrides, }; } function buildBody(attendant) { return { healthProfileId: PROFILE_ID, escort: { serviceId: 1, sexRequirement: "none" }, hospital: { province: "北京市", name: "北京协和医院" }, schedule: { date: futureDate(3), startTime: "08:00" }, payment: { payChannel: "offline" }, // 非管理员下单必须确认免责协议,且版本需与服务端当前版本一致 agreement: { accepted: true, version: disclaimer.version }, ...(attendant ? { attendant } : {}), }; } function createCtx(body, user = { _id: USER_ID }) { return { request: { query: {}, body }, params: {}, state: { user }, headers: {}, status: 200, body: null, set(name, value) { this.headers[name] = value; }, }; } function withModel(modelName, stub, fn) { const previous = DBModel[modelName]; DBModel[modelName] = stub; return Promise.resolve(fn()).finally(() => { DBModel[modelName] = previous; }); } function employeeFindOneStub(employee, overrides = {}) { const query = { lean: () => ({ exec: async () => employee }) }; return { findOne: () => ({ ...query, select: () => query, }), incrementOrderCount: async () => {}, ...overrides, }; } async function runCreate({ attendant, employee = buildEmployee(), user, bodyOverrides } = {}) { const calls = { payload: null, bumped: null }; const recordStub = { findByIdempotencyKey: async () => null, countDocuments: async () => 0, createRecord: async (payload) => { calls.payload = payload; return { _id: RECORD_ID, ...payload }; }, }; const employeeStub = employeeFindOneStub(employee, { incrementOrderCount: async (id) => { calls.bumped = id; }, }); const ctx = createCtx({ ...buildBody(attendant), ...(bodyOverrides || {}) }, user); await withModel("HealthProfile", { findProfileById: async () => buildProfile() }, () => withModel("EscortRecord", recordStub, () => withModel("Employee", employeeStub, () => new HandlerEscortRecord().createRecord(ctx)) ) ); return { ctx, calls }; } test("用户下单指定陪诊师:服务端回填快照并累加接单数", async () => { const { ctx, calls } = await runCreate({ // 客户端伪造的姓名 / 电话必须被服务端档案覆盖 attendant: { employeeId: EMPLOYEE_ID, name: "伪造姓名", mobile: "10000000000" }, }); assert.equal(ctx.body.code, 0); assert.deepEqual(calls.payload.attendant, { id: EMPLOYEE_USER_ID, name: "李四", sex: "female", mobile: "13700137000", sharedWith: [], }); assert.equal(calls.bumped, EMPLOYEE_ID); }); test("管理员用用户端下单指定陪诊师:同样回填快照而不是落库 employeeId", async () => { const admin = { _id: USER_ID, apps: [{ appId: "web-escort-admin", roles: ["admin"], status: "active" }], }; const { ctx, calls } = await runCreate({ user: admin, attendant: { employeeId: EMPLOYEE_ID }, }); assert.equal(ctx.body.code, 0); assert.deepEqual(calls.payload.attendant, { id: EMPLOYEE_USER_ID, name: "李四", sex: "female", mobile: "13700137000", sharedWith: [], }); assert.equal(calls.payload.attendant.employeeId, undefined); assert.equal(calls.bumped, EMPLOYEE_ID); }); test("用户不指定陪诊师:订单进入公共池,不写陪诊师、不累加接单数", async () => { const { ctx, calls } = await runCreate({ attendant: null }); assert.equal(ctx.body.code, 0); assert.equal(calls.payload.attendant, undefined); assert.equal(calls.bumped, null); }); test("指定实习陪诊师或不在职陪诊师返回 400 且不创建订单", async () => { const intern = await runCreate({ attendant: { employeeId: EMPLOYEE_ID }, employee: buildEmployee({ attendantInfo: { stage: "intern" } }), }); assert.equal(intern.ctx.body.code, 400); assert.equal(intern.calls.payload, null); const inactive = await runCreate({ attendant: { employeeId: EMPLOYEE_ID }, employee: null, }); assert.equal(inactive.ctx.body.code, 400); assert.equal(inactive.calls.payload, null); }); test("指定未绑定登录账号的陪诊师返回 400", async () => { const { ctx, calls } = await runCreate({ attendant: { employeeId: EMPLOYEE_ID }, employee: buildEmployee({ userId: null }), }); assert.equal(ctx.body.code, 400); assert.equal(calls.payload, null); }); test("陪诊师 ID 不合法返回 400", async () => { const { ctx, calls } = await runCreate({ attendant: { employeeId: "bad-id" } }); assert.equal(ctx.body.code, 400); assert.equal(calls.payload, null); }); test("用户下单未确认免责协议返回 400 且不创建订单", async () => { const { ctx, calls } = await runCreate({ bodyOverrides: { agreement: undefined }, }); assert.equal(ctx.body.code, 400); assert.match(ctx.body.msg, /免责协议/); assert.equal(calls.payload, null); }); test("用户下单协议版本过期返回 400 且不创建订单", async () => { const { ctx, calls } = await runCreate({ bodyOverrides: { agreement: { accepted: true, version: "2000-01-01" } }, }); assert.equal(ctx.body.code, 400); assert.match(ctx.body.msg, /已更新/); assert.equal(calls.payload, null); });