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, shortName: "北京协和医院", parentId: null, type: "hospital", status: "active", address: { city: "北京" }, overview: "总院就医信息", departments: [{ name: "心内科", detail: "总院心内科" }], doctors: [], photos: [], guides: {}, }; const branch = { _id: new mongoose.Types.ObjectId(BRANCH_ID), name: BRANCH_NAME, shortName: "协和西院", parentId: main._id, type: "hospital", status: "active", address: { city: "北京" }, overview: "西单院区就医信息", departments: [{ name: "妇科内分泌与生殖医学中心", detail: "西单院区" }], doctors: [], photos: [], guides: {}, // 历史遗留字段不应再被工具读取 forAgent: { overview: "旧字段", departments: [{ name: "旧科室" }] }, }; function parseResult(result) { return typeof result === "string" ? JSON.parse(result) : result; } // Mongoose Query 链式调用的最小桩 function createQuery(docs, calls, method, filter) { calls.push({ method, filter }); return { sort() { return this; }, limit() { return this; }, select() { return this; }, lean() { return Promise.resolve(docs); }, exec() { return Promise.resolve(docs); }, }; } function createOrganizationStub({ list = [], one = null, count = 0 } = {}) { const calls = []; return { calls, countDocuments(filter) { calls.push({ method: "countDocuments", filter }); return Promise.resolve(count); }, find(filter) { return createQuery(list, calls, "find", filter); }, findOne(filter) { return createQuery(one, calls, "findOne", filter); }, findByIdAndUpdate(id, update) { calls.push({ method: "findByIdAndUpdate", id, update }); return { exec() { return Promise.resolve({ ...one, ...update.$set }); }, }; }, }; } function withOrganization(stub, fn) { const previous = DBModel.Organization; DBModel.Organization = stub; return Promise.resolve(fn()).finally(() => { DBModel.Organization = previous; }); } test("医院列表只查可用医院,并可按 parentId 列院区", async () => { const stub = createOrganizationStub({ list: [main, branch], count: 2 }); await withOrganization(stub, async () => { const byKeyword = parseResult(await hospitalInfoListTool.invoke({ keyword: "协和" })); assert.equal(byKeyword.success, true); assert.equal(byKeyword.data.length, 2); assert.equal(byKeyword.data[1].parentId.toHexString(), MAIN_ID); assert.equal(stub.calls[0].filter.type, "hospital"); assert.equal(stub.calls[0].filter.status, "active"); assert.ok(stub.calls[0].filter.$or.some((item) => "shortName" in item)); const byParent = parseResult(await hospitalInfoListTool.invoke({ parentId: MAIN_ID })); assert.equal(byParent.success, true); assert.equal(stub.calls.at(-2).filter.parentId.toHexString(), MAIN_ID); }); }); test("按分院 id 查询返回平铺字段和总院名称", async () => { const stub = createOrganizationStub({ list: [main], one: branch }); await withOrganization(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].parentName, MAIN_NAME); assert.equal(result.data[0].overview, "西单院区就医信息"); assert.equal(result.data[0].departments[0].name, "妇科内分泌与生殖医学中心"); }); }); test("名称命中多家时返回候选,改用 id 后写入顶层字段", async () => { const stub = createOrganizationStub({ list: [main, branch], one: branch, count: 2 }); await withOrganization(stub, async () => { const ambiguous = parseResult(await hospitalInfoSetTool.invoke({ hospitalName: "协和", section: "overview", content: "x" })); assert.equal(ambiguous.success, false); assert.equal(ambiguous.candidates.length, 2); assert.ok(ambiguous.candidates.every((item) => item.id)); const set = parseResult( await hospitalInfoSetTool.invoke({ hospitalId: BRANCH_ID, section: "overview", content: "新就医信息" }) ); assert.equal(set.success, true); assert.equal(set.data.overview, "新就医信息"); assert.ok(set.data.infoUpdatedAt); const update = stub.calls.at(-1).update.$set; assert.equal(update.overview, "新就医信息"); assert.equal("forAgent.overview" in update, false); }); });