import { beforeEach, describe, expect, it, vi } from 'vitest'; const { parseJsonBodyMock, readDocumentsMock, resetDocumentMock, saveDocumentMock, sendJsonMock, } = vi.hoisted(() => ({ parseJsonBodyMock: vi.fn(), readDocumentsMock: vi.fn(), resetDocumentMock: vi.fn(), saveDocumentMock: vi.fn(), sendJsonMock: vi.fn(), })); vi.mock('@electron/utils/agent-system-documents', () => ({ isAgentSystemDocumentKind: (kind: string) => ['soul', 'identity', 'user', 'agent', 'tool', 'heartbeat', 'boot'].includes(kind), readAgentSystemDocuments: readDocumentsMock, resetAgentSystemDocument: resetDocumentMock, saveAgentSystemDocument: saveDocumentMock, })); vi.mock('@electron/api/route-utils', () => ({ parseJsonBody: parseJsonBodyMock, sendJson: sendJsonMock, })); function createContext(state: 'running' | 'stopped' = 'running') { return { gatewayManager: { getStatus: vi.fn(() => ({ state })), debouncedReload: vi.fn(), }, }; } describe('agent system document routes', () => { beforeEach(() => { vi.clearAllMocks(); readDocumentsMock.mockResolvedValue({ success: true, selectedAgentId: 'main', documents: [] }); saveDocumentMock.mockResolvedValue({ success: true, selectedAgentId: 'main', documents: [] }); resetDocumentMock.mockResolvedValue({ success: true, selectedAgentId: 'main', documents: [] }); }); it('lists documents for the requested agent', async () => { const { handleAgentSystemDocumentRoutes } = await import('@electron/api/routes/agent-system-documents'); const ctx = createContext(); const handled = await handleAgentSystemDocumentRoutes( { method: 'GET' } as never, {} as never, new URL('http://127.0.0.1/api/agent-system-documents?agentId=coder'), ctx as never, ); expect(handled).toBe(true); expect(readDocumentsMock).toHaveBeenCalledWith('coder'); expect(sendJsonMock).toHaveBeenCalledWith(expect.anything(), 200, { success: true, selectedAgentId: 'main', documents: [], }); expect(ctx.gatewayManager.debouncedReload).not.toHaveBeenCalled(); }); it('saves a document and schedules a gateway reload when running', async () => { parseJsonBodyMock.mockResolvedValue({ agentId: 'main', content: '# Heartbeat\n' }); const { handleAgentSystemDocumentRoutes } = await import('@electron/api/routes/agent-system-documents'); const ctx = createContext('running'); const handled = await handleAgentSystemDocumentRoutes( { method: 'PUT' } as never, {} as never, new URL('http://127.0.0.1/api/agent-system-documents/heartbeat'), ctx as never, ); expect(handled).toBe(true); expect(saveDocumentMock).toHaveBeenCalledWith('main', 'heartbeat', '# Heartbeat\n'); expect(ctx.gatewayManager.debouncedReload).toHaveBeenCalledTimes(1); }); it('restores a document from template without reloading a stopped gateway', async () => { parseJsonBodyMock.mockResolvedValue({ agentId: 'main' }); const { handleAgentSystemDocumentRoutes } = await import('@electron/api/routes/agent-system-documents'); const ctx = createContext('stopped'); const handled = await handleAgentSystemDocumentRoutes( { method: 'POST' } as never, {} as never, new URL('http://127.0.0.1/api/agent-system-documents/tool/reset'), ctx as never, ); expect(handled).toBe(true); expect(resetDocumentMock).toHaveBeenCalledWith('main', 'tool'); expect(ctx.gatewayManager.debouncedReload).not.toHaveBeenCalled(); }); it('rejects unsupported document kinds', async () => { const { handleAgentSystemDocumentRoutes } = await import('@electron/api/routes/agent-system-documents'); const handled = await handleAgentSystemDocumentRoutes( { method: 'PUT' } as never, {} as never, new URL('http://127.0.0.1/api/agent-system-documents/memory'), createContext() as never, ); expect(handled).toBe(true); expect(sendJsonMock).toHaveBeenCalledWith(expect.anything(), 404, { success: false, error: 'Unsupported system document kind "memory"', }); }); });