MCPcopy Create free account
hub / github.com/msgbyte/tailchat / createTestServiceBroker

Function createTestServiceBroker

server/test/utils.ts:18–80  ·  view source on GitHub ↗
(
  serviceCls: typeof TcService,
  options?: TestServiceBrokerOptions
)

Source from the content-addressed store, hash-verified

16 * @param serviceCls 微服务类
17 */
18export function createTestServiceBroker<T extends TcService = TcService>(
19 serviceCls: typeof TcService,
20 options?: TestServiceBrokerOptions
21): {
22 broker: TcBroker;
23 contextCallMock: jest.Mock;
24 service: MockedService<T>;
25 insertTestData: <E, R extends E = E>(
26 entity: E
27 ) => Promise<DocumentType<R & { _id: string }>>;
28} {
29 const broker = new TcBroker({ logger: false });
30 const service = broker.createService(serviceCls) as MockedService<T>;
31 const testDataStack = [];
32 const contextCallMock = jest.fn(options?.contextCallMockFn);
33
34 broker.ContextFactory = class extends broker.ContextFactory {
35 call = contextCallMock as any;
36 } as any;
37
38 // Mock
39 service.roomcastNotify = jest.fn();
40 service.cleanActionCache = jest.fn();
41
42 beforeAll(async () => {
43 await broker.start();
44 });
45 afterAll(async () => {
46 await Promise.all(
47 testDataStack.map((item) => {
48 if (typeof service.adapter !== 'object') {
49 throw new Error('无法调用 insertTestData');
50 }
51
52 return service.adapter.removeById(item._id);
53 })
54 )
55 .then(() => {
56 console.log(`已清理 ${testDataStack.length} 条测试数据`);
57 })
58 .catch((err) => {
59 console.error('测试数据清理失败:', err);
60 });
61
62 await broker.stop();
63 });
64
65 const insertTestData = async (entity: any) => {
66 if (typeof service.adapter !== 'object') {
67 throw new Error('无法调用 insertTestData');
68 }
69 const doc = await service.adapter.insert(entity);
70 testDataStack.push(doc);
71 return doc;
72 };
73
74 return {
75 broker,

Callers 8

user.spec.tsFile · 0.90
dmlist.spec.tsFile · 0.90
message.spec.tsFile · 0.90
ack.spec.tsFile · 0.90
group.spec.tsFile · 0.90
app.spec.tsFile · 0.90
tasks.spec.tsFile · 0.90
linkmeta.spec.tsFile · 0.90

Calls 6

startMethod · 0.80
removeByIdMethod · 0.80
errorMethod · 0.80
stopMethod · 0.80
logMethod · 0.65
allMethod · 0.45

Tested by

no test coverage detected