hospital_info_list/query/set 改为读写 hospital 集合,适配 entityName/nicknames/fudanRank;医院子代理与提示词同步字段口径;health_profile_set 更新启用 schema 校验;escort_record_set 限制线下收款方式,存在未支付流水时禁止改价。
192 lines
6.9 KiB
JavaScript
192 lines
6.9 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import mongoose from "mongoose";
|
|
|
|
import { DBModel } from "../models/index.js";
|
|
import {
|
|
hospitalInfoListTool,
|
|
hospitalInfoQueryTool,
|
|
hospitalInfoSetTool,
|
|
} from "../agent/infra/tools/index.js";
|
|
|
|
const MAIN_ID = "64b000000000000000000101";
|
|
const BRANCH_ID = "64b000000000000000000102";
|
|
const MAIN_NAME = "中国医学科学院北京协和医院";
|
|
const BRANCH_NAME = "中国医学科学院北京协和医院-西单院区";
|
|
|
|
const main = {
|
|
_id: new mongoose.Types.ObjectId(MAIN_ID),
|
|
name: MAIN_NAME,
|
|
entityName: MAIN_NAME,
|
|
nicknames: ["协和", "北京协和"],
|
|
pinyin: "zhongguoyixuekexueyuanbeijingxieheyiyuan",
|
|
pinyinFL: "zgyxkxybjxhyy",
|
|
province: "北京市",
|
|
city: "北京市",
|
|
address: "东城区帅府园1号",
|
|
phone: "010-69151188",
|
|
level: "三级甲等",
|
|
departments: ["心内科", "风湿免疫科"],
|
|
fudanRank: { rank: 1, grade: "A+++++", order: 1 },
|
|
meta: { updatetime: new Date("2026-11-05T00:00:00.000Z") },
|
|
};
|
|
|
|
const branch = {
|
|
...main,
|
|
_id: new mongoose.Types.ObjectId(BRANCH_ID),
|
|
name: BRANCH_NAME,
|
|
address: "西城区大木仓胡同41号",
|
|
departments: ["妇科内分泌与生殖医学中心"],
|
|
};
|
|
|
|
function parseResult(result) {
|
|
return typeof result === "string" ? JSON.parse(result) : result;
|
|
}
|
|
|
|
/**
|
|
* hospital 模型桩:工具通过模型静态方法读写,桩直接记录调用参数
|
|
*/
|
|
function createHospitalStub({ list = [], total = list.length, one = null } = {}) {
|
|
const calls = [];
|
|
return {
|
|
calls,
|
|
findHospitals(options) {
|
|
calls.push({ method: "findHospitals", options });
|
|
return Promise.resolve({
|
|
list,
|
|
total,
|
|
page: options.page ?? 1,
|
|
pageSize: options.pageSize ?? 20,
|
|
});
|
|
},
|
|
findHospitalById(id) {
|
|
calls.push({ method: "findHospitalById", id });
|
|
return Promise.resolve(one);
|
|
},
|
|
updateHospitalById(id, update) {
|
|
calls.push({ method: "updateHospitalById", id, update });
|
|
return Promise.resolve({
|
|
...one,
|
|
...update,
|
|
// 模拟模型静态方法自动补派生字段
|
|
pinyin: "paiban",
|
|
fudanRank: { rank: 1, grade: "A+++++", order: 1 },
|
|
});
|
|
},
|
|
};
|
|
}
|
|
|
|
function withHospital(stub, fn) {
|
|
const previous = DBModel.Hospital;
|
|
DBModel.Hospital = stub;
|
|
return Promise.resolve(fn()).finally(() => {
|
|
DBModel.Hospital = previous;
|
|
});
|
|
}
|
|
|
|
test("医院列表走 hospital 集合,并支持关键词与主体名称过滤", async () => {
|
|
const stub = createHospitalStub({ list: [main, branch], total: 2 });
|
|
await withHospital(stub, async () => {
|
|
const byKeyword = parseResult(await hospitalInfoListTool.invoke({ keyword: "协和" }));
|
|
assert.equal(byKeyword.success, true);
|
|
assert.equal(byKeyword.total, 2);
|
|
assert.equal(byKeyword.data[0].entityName, MAIN_NAME);
|
|
assert.equal(byKeyword.data[0].phone, "010-69151188");
|
|
assert.equal(stub.calls[0].options.keyword, "协和");
|
|
|
|
const byEntity = parseResult(
|
|
await hospitalInfoListTool.invoke({ entityName: MAIN_NAME })
|
|
);
|
|
assert.equal(byEntity.success, true);
|
|
assert.equal(stub.calls.at(-1).options.entityName, MAIN_NAME);
|
|
});
|
|
});
|
|
|
|
test("按 id 查询返回 hospital 基础信息与派生字段", async () => {
|
|
const stub = createHospitalStub({ one: branch });
|
|
await withHospital(stub, async () => {
|
|
const result = parseResult(await hospitalInfoQueryTool.invoke({ hospitalId: BRANCH_ID }));
|
|
assert.equal(result.success, true);
|
|
assert.equal(result.data[0].name, BRANCH_NAME);
|
|
assert.equal(result.data[0].entityName, MAIN_NAME);
|
|
assert.deepEqual(result.data[0].nicknames, ["协和", "北京协和"]);
|
|
assert.equal(result.data[0].fudanRank.grade, "A+++++");
|
|
assert.deepEqual(result.data[0].departments, ["妇科内分泌与生殖医学中心"]);
|
|
});
|
|
});
|
|
|
|
test("按名称查询可按科室过滤,科室为字符串数组", async () => {
|
|
const stub = createHospitalStub({ list: [main, branch], total: 2 });
|
|
await withHospital(stub, async () => {
|
|
const result = parseResult(
|
|
await hospitalInfoQueryTool.invoke({ hospitalName: "协和", department: "风湿" })
|
|
);
|
|
assert.equal(result.success, true);
|
|
assert.equal(result.total, 2);
|
|
assert.deepEqual(result.data[0].departments, ["风湿免疫科"]);
|
|
assert.deepEqual(result.data[1].departments, []);
|
|
});
|
|
});
|
|
|
|
test("查询缺少 id 与名称时明确报错", async () => {
|
|
const stub = createHospitalStub();
|
|
await withHospital(stub, async () => {
|
|
const result = parseResult(await hospitalInfoQueryTool.invoke({}));
|
|
assert.equal(result.success, false);
|
|
assert.match(result.error, /hospitalId 或 hospitalName/);
|
|
});
|
|
});
|
|
|
|
test("写入时名称命中多家返回候选,不落库", async () => {
|
|
const stub = createHospitalStub({ list: [main, branch], total: 2, one: branch });
|
|
await withHospital(stub, async () => {
|
|
const result = parseResult(
|
|
await hospitalInfoSetTool.invoke({ hospitalName: "协和", phone: "010-12345678" })
|
|
);
|
|
assert.equal(result.success, false);
|
|
assert.equal(result.candidates.length, 2);
|
|
assert.ok(result.candidates.every((item) => item.id));
|
|
assert.equal(stub.calls.filter((call) => call.method === "updateHospitalById").length, 0);
|
|
});
|
|
});
|
|
|
|
test("按 id 写入只提交基础字段,派生字段由模型同步", async () => {
|
|
const stub = createHospitalStub({ one: branch });
|
|
await withHospital(stub, async () => {
|
|
const result = parseResult(
|
|
await hospitalInfoSetTool.invoke({
|
|
hospitalId: BRANCH_ID,
|
|
phone: " 010-12345678 ",
|
|
departments: ["心内科", " ", "风湿免疫科"],
|
|
})
|
|
);
|
|
assert.equal(result.success, true);
|
|
|
|
const updateCall = stub.calls.find((call) => call.method === "updateHospitalById");
|
|
assert.equal(String(updateCall.id), BRANCH_ID);
|
|
assert.equal(updateCall.update.phone, "010-12345678");
|
|
assert.deepEqual(updateCall.update.departments, ["心内科", "风湿免疫科"]);
|
|
// 拼音/昵称/复旦榜档位不允许手写
|
|
assert.equal("pinyin" in updateCall.update, false);
|
|
assert.equal("nicknames" in updateCall.update, false);
|
|
assert.equal("fudanRank" in updateCall.update, false);
|
|
assert.equal(result.data.phone, "010-12345678");
|
|
assert.deepEqual(result.data.departments, ["心内科", "风湿免疫科"]);
|
|
});
|
|
});
|
|
|
|
test("写入未提供字段或名称为空时拒绝", async () => {
|
|
const stub = createHospitalStub({ one: branch });
|
|
await withHospital(stub, async () => {
|
|
const empty = parseResult(await hospitalInfoSetTool.invoke({ hospitalId: BRANCH_ID }));
|
|
assert.equal(empty.success, false);
|
|
assert.match(empty.error, /未提供任何要更新的字段/);
|
|
|
|
const emptyName = parseResult(
|
|
await hospitalInfoSetTool.invoke({ hospitalId: BRANCH_ID, name: " " })
|
|
);
|
|
assert.equal(emptyName.success, false);
|
|
assert.match(emptyName.error, /医院名称不能为空/);
|
|
});
|
|
});
|