Makelore 2.0 initial clean snapshot
This commit is contained in:
58
tests/unit/host-api-proxy.test.ts
Normal file
58
tests/unit/host-api-proxy.test.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const handleMock = vi.fn();
|
||||
|
||||
vi.mock('electron', () => ({
|
||||
ipcMain: { handle: handleMock },
|
||||
}));
|
||||
|
||||
vi.mock('../../electron/utils/config', () => ({
|
||||
getPort: () => 13210,
|
||||
}));
|
||||
|
||||
vi.mock('../../electron/api/server', () => ({
|
||||
getHostApiToken: () => 'host-token',
|
||||
}));
|
||||
|
||||
describe('Host API IPC proxy', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules();
|
||||
vi.clearAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it('connects to the loopback Host API with direct fetch', async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValue(new Response(
|
||||
JSON.stringify({ success: true }),
|
||||
{ status: 200, headers: { 'content-type': 'application/json' } },
|
||||
));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const { registerHostApiProxyHandlers } = await import('../../electron/main/ipc/host-api-proxy');
|
||||
registerHostApiProxyHandlers();
|
||||
|
||||
const fetchHandler = handleMock.mock.calls.find(([channel]) => channel === 'hostapi:fetch')?.[1];
|
||||
expect(fetchHandler).toBeTypeOf('function');
|
||||
|
||||
const result = await fetchHandler(undefined, {
|
||||
path: '/api/opencode/projects/create',
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ projectName: '测试项目' }),
|
||||
});
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledWith(
|
||||
'http://127.0.0.1:13210/api/opencode/projects/create',
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
headers: expect.objectContaining({
|
||||
Authorization: 'Bearer host-token',
|
||||
'Content-Type': 'application/json',
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(result).toEqual({
|
||||
ok: true,
|
||||
data: { status: 200, ok: true, json: { success: true } },
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user