新增独立病历整理 Agent 与异步解析任务,支持 PDF 文本、图片 OCR、 语音 ASR 与文字输入,统一整理为基础健康、检查治疗、用药和问诊诉求。 Agent 只产出草稿,订单本人在编辑页核对确认后才写入当前订单; 原件在任务结束、失败或取消时立即清除。 inquiry 增加三段规范字段,medicalHistory 保留为旧版兼容摘要, 并兼容旧客户端只写 medicalHistory 的更新方式。
227 lines
8.0 KiB
JavaScript
227 lines
8.0 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import { DBModel } from "../models/index.js";
|
|
import { MedicalInquiryAgent } from "../agent/bots/bot-medical-inquiry/agent.js";
|
|
import { MedicalInquiryTask, normalizeJobInputs } from "../agent/bots/bot-medical-inquiry/task.js";
|
|
import { extractPdfText } from "../agent/bots/bot-medical-inquiry/pdf.js";
|
|
import { HandlerMedicalInquiry } from "../handler/medical_inquiry.js";
|
|
import { buildInquiryUpdate, normalizeInquiryView } from "../utils/inquiry.js";
|
|
|
|
const RECORD_ID = "64b000000000000000000110";
|
|
const OWNER_ID = "64b000000000000000000111";
|
|
const JOB_ID = "64b000000000000000000112";
|
|
|
|
function withModel(modelName, stub, fn) {
|
|
const previous = DBModel[modelName];
|
|
DBModel[modelName] = stub;
|
|
return Promise.resolve(fn()).finally(() => {
|
|
DBModel[modelName] = previous;
|
|
});
|
|
}
|
|
|
|
function createCtx({ body = {}, userId = OWNER_ID } = {}) {
|
|
return {
|
|
request: { body },
|
|
params: { id: RECORD_ID, jobId: JOB_ID },
|
|
state: { user: { _id: userId } },
|
|
headers: {},
|
|
body: null,
|
|
set(name, value) { this.headers[name] = value; },
|
|
};
|
|
}
|
|
|
|
test("四段摘要生成旧版病历字段,老订单读取时归入检查与治疗", () => {
|
|
const update = buildInquiryUpdate({
|
|
basicHealth: " 高血压 ",
|
|
treatmentHistory: "2026 年复查",
|
|
medications: "每日一次",
|
|
demand: "咨询复查结果",
|
|
});
|
|
assert.equal(update["inquiry.basicHealth"], "高血压");
|
|
assert.equal(update["inquiry.demand"], "咨询复查结果");
|
|
assert.match(update["inquiry.medicalHistory"], /基础健康与基础疾病/);
|
|
assert.match(update["inquiry.medicalHistory"], /用药情况/);
|
|
|
|
const oldView = normalizeInquiryView({ medicalHistory: "既往住院", demand: "陪同复诊" });
|
|
assert.equal(oldView.treatmentHistory, "既往住院");
|
|
assert.equal(oldView.basicHealth, "");
|
|
});
|
|
|
|
test("独立 Agent 只接收材料并返回四段结构化结果", async () => {
|
|
let messages = null;
|
|
const agent = new MedicalInquiryAgent({
|
|
structuredModel: {
|
|
invoke: async (input) => {
|
|
messages = input;
|
|
return {
|
|
basicHealth: " 高血压 ",
|
|
treatmentHistory: "复查",
|
|
medications: "",
|
|
demand: "解读报告",
|
|
uncertainties: ["剂量需核对"],
|
|
};
|
|
},
|
|
},
|
|
});
|
|
const result = await agent.parse({
|
|
materials: [{ type: "text", name: "补充", text: "有高血压,想解读报告" }],
|
|
current: {},
|
|
});
|
|
assert.equal(result.basicHealth, "高血压");
|
|
assert.equal(result.demand, "解读报告");
|
|
assert.match(messages[0].content, /不编造/);
|
|
assert.match(messages[1].content, /有高血压/);
|
|
});
|
|
|
|
test("解析输入校验类型、数量和总大小", () => {
|
|
assert.throws(() => normalizeJobInputs({}), /请添加/);
|
|
assert.throws(
|
|
() => normalizeJobInputs({ text: "x", materials: Array.from({ length: 7 }, () => ({ type: "pdf" })) }),
|
|
/最多上传 6 份/
|
|
);
|
|
assert.throws(
|
|
() => normalizeJobInputs({ materials: [{ type: "file", data: "WA==" }] }),
|
|
/类型不支持/
|
|
);
|
|
const pdf = Buffer.from("%PDF-1.4\ncontent").toString("base64");
|
|
const inputs = normalizeJobInputs({ materials: [{ type: "pdf", name: "报告.pdf", data: pdf }] });
|
|
assert.equal(inputs.materials[0].mime, "application/pdf");
|
|
assert.equal(Buffer.from(inputs.materials[0].data, "base64").toString(), "%PDF-1.4\ncontent");
|
|
});
|
|
|
|
test("非 PDF 文件在进入解析前被拒绝", async () => {
|
|
await assert.rejects(() => extractPdfText(Buffer.from("not a pdf")), /有效的 PDF/);
|
|
});
|
|
|
|
test("同一订单重复创建时续接活动任务", async () => {
|
|
const active = { _id: JOB_ID, status: "processing", active: true };
|
|
const task = new MedicalInquiryTask();
|
|
const jobStub = {
|
|
findOne: () => ({ lean: async () => active }),
|
|
create: async () => { throw new Error("不应重复创建"); },
|
|
};
|
|
await withModel("InquiryParseJob", jobStub, async () => {
|
|
const result = await task.create({
|
|
record: { _id: RECORD_ID, meta: {} },
|
|
userId: OWNER_ID,
|
|
body: { text: "重新提交" },
|
|
});
|
|
assert.equal(result._id, JOB_ID);
|
|
});
|
|
});
|
|
|
|
test("文本任务完成后保存草稿并清除原始输入", async () => {
|
|
const job = {
|
|
_id: JOB_ID,
|
|
inputs: { text: "高血压,想咨询复查", materials: [], current: {} },
|
|
};
|
|
const updates = [];
|
|
const jobStub = {
|
|
findOneAndUpdate: () => ({ select: async () => job }),
|
|
updateOne: async (filter, update) => { updates.push({ filter, update }); return { modifiedCount: 1 }; },
|
|
};
|
|
const task = new MedicalInquiryTask({
|
|
agent: {
|
|
parse: async () => ({
|
|
basicHealth: "高血压",
|
|
treatmentHistory: "",
|
|
medications: "",
|
|
demand: "咨询复查",
|
|
uncertainties: [],
|
|
}),
|
|
},
|
|
});
|
|
|
|
await withModel("InquiryParseJob", jobStub, () => task.process(JOB_ID));
|
|
const completed = updates.find(({ update }) => update.$set?.status === "waiting_confirmation");
|
|
assert.ok(completed);
|
|
assert.equal(completed.update.$set.draft.basicHealth, "高血压");
|
|
assert.ok(completed.update.$unset.inputs);
|
|
assert.ok(updates.at(-1).update.$unset.inputs);
|
|
});
|
|
|
|
test("非订单本人不能创建解析任务", async () => {
|
|
const record = { _id: RECORD_ID, userId: OWNER_ID, status: "confirmed", cancelStatus: "none" };
|
|
await withModel("EscortRecord", { findRecordById: async () => record }, async () => {
|
|
const ctx = createCtx({ userId: "64b000000000000000000999", body: { text: "test" } });
|
|
await new HandlerMedicalInquiry().create(ctx);
|
|
assert.equal(ctx.body.code, 403);
|
|
});
|
|
});
|
|
|
|
test("老订单缺少版本时间时仍可确认草稿", async () => {
|
|
const record = {
|
|
_id: RECORD_ID,
|
|
userId: OWNER_ID,
|
|
status: "confirmed",
|
|
cancelStatus: "none",
|
|
meta: {},
|
|
};
|
|
const job = { _id: JOB_ID, status: "waiting_confirmation", baseUpdatedAt: null };
|
|
let fallbackSaved = null;
|
|
const recordStub = {
|
|
findRecordById: async () => record,
|
|
updateRecordIfUnchanged: async () => {
|
|
throw new Error("缺少版本时间时不应走条件更新");
|
|
},
|
|
updateRecord: async (id, update) => {
|
|
fallbackSaved = update;
|
|
return { ...record, inquiry: update };
|
|
},
|
|
};
|
|
const jobStub = {
|
|
findOne: async () => job,
|
|
updateOne: async () => ({ modifiedCount: 1 }),
|
|
};
|
|
|
|
await withModel("EscortRecord", recordStub, () =>
|
|
withModel("InquiryParseJob", jobStub, async () => {
|
|
const ctx = createCtx({ body: { basicHealth: "高血压", demand: "复诊" } });
|
|
await new HandlerMedicalInquiry().confirm(ctx);
|
|
assert.equal(ctx.body.code, 0);
|
|
assert.equal(fallbackSaved["inquiry.basicHealth"], "高血压");
|
|
})
|
|
);
|
|
});
|
|
|
|
test("确认草稿按订单版本写入四段和兼容摘要", async () => {
|
|
const baseUpdatedAt = new Date("2026-11-16T00:00:00.000Z");
|
|
const record = {
|
|
_id: RECORD_ID,
|
|
userId: OWNER_ID,
|
|
status: "confirmed",
|
|
cancelStatus: "none",
|
|
meta: { updatetime: baseUpdatedAt },
|
|
};
|
|
const job = { _id: JOB_ID, status: "waiting_confirmation", baseUpdatedAt };
|
|
let saved = null;
|
|
let jobUpdate = null;
|
|
const recordStub = {
|
|
findRecordById: async () => record,
|
|
updateRecordIfUnchanged: async (id, expected, update) => {
|
|
assert.equal(String(expected), String(baseUpdatedAt));
|
|
saved = update;
|
|
return { ...record, inquiry: update };
|
|
},
|
|
};
|
|
const jobStub = {
|
|
findOne: async () => job,
|
|
updateOne: async (filter, update) => { jobUpdate = update; return { modifiedCount: 1 }; },
|
|
};
|
|
|
|
await withModel("EscortRecord", recordStub, () =>
|
|
withModel("InquiryParseJob", jobStub, async () => {
|
|
const ctx = createCtx({
|
|
body: { basicHealth: "高血压", treatmentHistory: "复查", medications: "", demand: "解读" },
|
|
});
|
|
await new HandlerMedicalInquiry().confirm(ctx);
|
|
assert.equal(ctx.body.code, 0);
|
|
assert.equal(saved["inquiry.basicHealth"], "高血压");
|
|
assert.match(saved["inquiry.medicalHistory"], /检查与治疗病历/);
|
|
assert.equal(jobUpdate.$set.status, "confirmed");
|
|
assert.ok(jobUpdate.$unset.draft);
|
|
})
|
|
);
|
|
});
|