import type { IncomingMessage, ServerResponse } from 'node:http'; import { Readable } from 'node:stream'; import { beforeEach, expect, it, vi } from 'vitest'; import { handleCloudAgentsRoutes } from '@electron/api/routes/cloud-agents'; const service = vi.hoisted(() => ({ useAvatarPreset: vi.fn(), uploadAvatar: vi.fn() })); vi.mock('@electron/services/cloud-agents', () => ({ CloudAgentsModule: class { useAvatarPreset = service.useAvatarPreset; uploadAvatar = service.uploadAvatar; }, CloudAgentsError: class extends Error { constructor(readonly status: number, readonly code: string) { super(code); } }, })); beforeEach(() => vi.resetAllMocks()); async function call(path: string, body?: string, method = 'POST') { const request = Object.assign(Readable.from(body ? [body] : []), { method, headers: {} }) as IncomingMessage; let output = ''; const response = { setHeader: vi.fn(), statusCode: 0, end: (text: string) => { output = text; } } as unknown as ServerResponse; const handled = await handleCloudAgentsRoutes(request, response, new URL(`http://localhost/api/cloud-agents${path}`)); return { handled, status: response.statusCode, body: JSON.parse(output) }; } it('routes only the preset id to Main and returns the canonical icon projection', async () => { const avatar = { icon: '/api/storage/public/images/account/avatar.png', icon_url: 'https://agents.test/api/storage/public/images/account/avatar.png' }; service.useAvatarPreset.mockResolvedValue(avatar); expect(await call('/avatars/preset', JSON.stringify({ id: 'avatar-03' }))).toEqual({ handled: true, status: 200, body: avatar }); expect(service.useAvatarPreset).toHaveBeenCalledExactlyOnceWith({ id: 'avatar-03' }); expect(service.uploadAvatar).not.toHaveBeenCalled(); }); it('keeps the existing file picker route and rejects malformed preset JSON', async () => { service.uploadAvatar.mockResolvedValue(null); expect(await call('/avatars/pick')).toEqual({ handled: true, status: 200, body: null }); expect(service.uploadAvatar).toHaveBeenCalledOnce(); expect((await call('/avatars/preset', '{')).status).toBe(400); expect(service.useAvatarPreset).not.toHaveBeenCalled(); expect((await call('/avatars/preset', undefined, 'GET')).status).toBe(404); });