feat: add unit tests for RPCPlugin hook execution and error handling

This commit is contained in:
2025-11-27 21:36:41 +08:00
parent bc1445d3a5
commit 809a3759d9

View File

@@ -0,0 +1,43 @@
import { createHookRunner, RPCPlugin } from "@/core/RPCPlugin"
import type { CallIncomingCtx } from "@/core/RPCPlugin"
describe('RPCPlugin.test', () => {
const plugin3 = {
onCallIncoming(ctx: CallIncomingCtx) { throw new Error() }
} as RPCPlugin;
const plugin4 = {
async onCallIncoming(ctx: CallIncomingCtx) { throw new Error() }
} as RPCPlugin;
test('should be resolved', async () => {
const plugin1 = {
onCallIncoming: jest.fn(),
} as RPCPlugin;
const plugin2 = {
async onCallIncoming(ctx: CallIncomingCtx) { }
} as RPCPlugin;
const plugins = [plugin1, plugin2];
const hookRunner = createHookRunner(plugins, 'onCallIncoming');
await hookRunner({} as any);
expect(plugin1.onCallIncoming).toHaveBeenCalled()
})
test('should be resolved2', async () => {
const plugins = [] as RPCPlugin[];
const hookRunner = createHookRunner(plugins, 'onCallIncoming');
await hookRunner({} as any);
expect.assertions(0);
})
test('should be rejected1', async () => {
const plugins = [plugin3];
const hookRunner = createHookRunner(plugins, 'onCallIncoming');
await expect(hookRunner({} as any)).rejects.toThrow(Error)
})
test('should be rejected2', async () => {
const plugins = [plugin4];
const hookRunner = createHookRunner(plugins, 'onCallIncoming');
await expect(hookRunner({} as any)).rejects.toThrow(Error)
})
})