Files
2026-07-13 13:20:22 +08:00

90 lines
2.4 KiB
TypeScript

/**
* @vitest-environment node
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';
type HttpCall = {
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
path: string;
body?: unknown;
};
const httpBridgeMocks = vi.hoisted(() => {
const calls: HttpCall[] = [];
const provider =
(method: HttpCall['method']) =>
<Data, Params = undefined>(path: string | ((params: Params) => string), mapBody?: (params: Params) => unknown) => ({
provider: vi.fn(),
invoke: vi.fn(async (params?: Params) => {
const resolvedPath = typeof path === 'function' ? path(params as Params) : path;
calls.push({
method,
path: resolvedPath,
body: mapBody && params !== undefined ? mapBody(params as Params) : undefined,
});
return true as Data;
}),
});
const emitter = () => ({ on: vi.fn(() => vi.fn()), emit: vi.fn() });
return {
calls,
httpGet: provider('GET'),
httpPost: provider('POST'),
httpPut: provider('PUT'),
httpPatch: provider('PATCH'),
httpDelete: provider('DELETE'),
httpRequest: vi.fn(),
stubProvider: vi.fn((name: string, defaultValue: unknown) => ({
provider: vi.fn(),
invoke: vi.fn(async () => defaultValue),
})),
withResponseMap: vi.fn(
(
inner: { provider: unknown; invoke: (params?: unknown) => Promise<unknown> },
map: (raw: unknown) => unknown
) => ({
provider: inner.provider,
invoke: vi.fn(async (params?: unknown) => map(await inner.invoke(params))),
})
),
wsEmitter: vi.fn(emitter),
wsMappedEmitter: vi.fn(emitter),
stubEmitter: vi.fn(emitter),
};
});
vi.mock('@/common/adapter/httpBridge', () => httpBridgeMocks);
vi.mock('@office-ai/platform', () => ({
bridge: {
buildProvider: vi.fn(() => ({
provider: vi.fn(),
invoke: vi.fn(),
})),
buildEmitter: vi.fn(() => ({
on: vi.fn(() => vi.fn()),
emit: vi.fn(),
})),
},
}));
describe('ipcBridge conversation adapter', () => {
beforeEach(() => {
httpBridgeMocks.calls.length = 0;
});
it('deletes conversations through the standard conversation endpoint', async () => {
const { conversation } = await import('@/common/adapter/ipcBridge');
await conversation.remove.invoke({ id: 'conv-1' });
expect(httpBridgeMocks.calls).toContainEqual({
method: 'DELETE',
path: '/api/conversations/conv-1',
body: undefined,
});
});
});