process.env.CONFIG_ZYB_YB_APPID = '1234567890123456'; process.env.CONFIG_ZYB_YB_SECRET = 'hlw-patient-test-secret'; const { ObjectId } = require('mongodb'); const patientApi = require('./index'); function matches(document, query) { return Object.entries(query).every(([key, value]) => { if (key === '_id') return document._id.toString() === value.toString(); return document[key] === value; }); } function createDb() { const documents = []; return { documents, collection: jest.fn(() => ({ insertOne: async document => { const insertedId = new ObjectId(); documents.push({ _id: insertedId, ...document }); return { insertedId }; }, countDocuments: async query => documents.filter(document => matches(document, query)).length, find: query => ({ sort: () => ({ toArray: async () => documents.filter(document => matches(document, query)), }), }), findOne: async query => documents.find(document => matches(document, query)) || null, updateOne: async (query, update) => { const document = documents.find(item => matches(item, query)); if (!document) return { matchedCount: 0, modifiedCount: 0 }; Object.assign(document, update.$set); return { matchedCount: 1, modifiedCount: 1 }; }, })), }; } describe('hlw-patient', () => { test('新增、查询、更新并软删除就诊人', async () => { const db = createDb(); const patient = { accountId: 'account-1', name: '张三', certNo: '330101199001011234', mobile: '13800138000', }; const added = await patientApi.main({ type: 'addHlwPatient', ...patient }, db); expect(added.success).toBe(true); expect(db.documents[0]).toMatchObject({ accountId: 'account-1', address: '', disabled: false, }); expect(db.documents[0].anotherName).not.toBe(patient.name); expect(db.documents[0].anotherIdNo).not.toBe(patient.certNo); expect(db.documents[0].anotherMobile).not.toBe(patient.mobile); expect(db.documents[0].createTime).toBeInstanceOf(Date); const queried = await patientApi.main({ type: 'getHlwPatientList', accountId: 'account-1', }, db); expect(queried.data).toEqual([expect.objectContaining(patient)]); const detail = await patientApi.main({ type: 'getHlwPatient', accountId: 'account-1', id: added.id, }, db); expect(detail).toMatchObject({ success: true, data: patient, }); const otherAccountDetail = await patientApi.main({ type: 'getHlwPatient', accountId: 'account-2', id: added.id, }, db); expect(otherAccountDetail.success).toBe(false); const updated = await patientApi.main({ type: 'updateHlwPatient', ...patient, id: added.id, name: '李四', mobile: '13900139000', address: '杭州市', }, db); expect(updated.success).toBe(true); const updatedList = await patientApi.main({ type: 'getHlwPatientList', accountId: 'account-1', }, db); expect(updatedList.data[0]).toMatchObject({ name: '李四', mobile: '13900139000', certNo: patient.certNo, address: '杭州市', }); const deleted = await patientApi.main({ type: 'deleteHlwPatient', accountId: 'account-1', id: added.id, }, db); expect(deleted.success).toBe(true); expect(db.documents[0].disabled).toBe(true); const emptyList = await patientApi.main({ type: 'getHlwPatientList', accountId: 'account-1', }, db); expect(emptyList.data).toEqual([]); }); test('拒绝格式错误的手机号', async () => { const db = createDb(); const result = await patientApi.main({ type: 'addHlwPatient', accountId: 'account-1', name: '张三', certNo: '330101199001011234', mobile: '12345', }, db); expect(result.success).toBe(false); expect(db.documents).toHaveLength(0); }); test('同一账户下不能新增相同证件号的就诊人', async () => { const db = createDb(); const patient = { type: 'addHlwPatient', accountId: 'account-1', name: '张三', certNo: '330101199001011234', mobile: '13800138000', }; const first = await patientApi.main(patient, db); const duplicate = await patientApi.main({ ...patient, name: '李四', mobile: '13900139000', }, db); expect(first.success).toBe(true); expect(duplicate).toEqual({ success: false, message: '当前账户下已存在相同证件号的就诊人', }); expect(db.documents).toHaveLength(1); const otherAccount = await patientApi.main({ ...patient, accountId: 'account-2', }, db); expect(otherAccount.success).toBe(true); expect(db.documents).toHaveLength(2); }); test('自动新增接口对同一账号和证件号保持幂等', async () => { const db = createDb(); const request = { type: 'autoAddHlwPatient', accountId: 'account-1', name: '张三', certNo: '330101199001011234', mobile: '13800138000', }; const first = await patientApi.main(request, db); const second = await patientApi.main(request, db); expect(first).toMatchObject({ success: true, data: true }); expect(second).toMatchObject({ success: true, data: true }); expect(db.documents).toHaveLength(1); }); });