/* eslint-env jest */ import { fetchCopilotHistory, fetchCopilotThreads, messageToThreadRecord, threadRecordToMessage, } from '../copilot.service'; describe('reading the conversations before the token is there', () => { const fetchMock = jest.fn(); beforeEach(() => { fetchMock.mockReset(); (global as unknown as { fetch: jest.Mock }).fetch = fetchMock; }); it('asks the agent nothing it can only turn down', async () => { expect(await fetchCopilotThreads('', 'client-1')).toEqual([]); expect(await fetchCopilotHistory('', 'client-1')).toEqual([]); expect(fetchMock).not.toHaveBeenCalled(); }); it('asks with the token once there is one', async () => { fetchMock.mockResolvedValue({ ok: true, json: () => Promise.resolve({ threads: [] }), }); await fetchCopilotThreads('jwt', 'client-1'); expect(fetchMock).toHaveBeenCalledTimes(1); expect(fetchMock.mock.calls[0][1].headers.Authorization).toBe('Bearer jwt'); }); }); describe('a saved conversation', () => { it('keeps a turn the page sent marked, both ways', () => { const record = messageToThreadRecord({ role: 'user', content: 'Fix this SEO checkup item.', hidden: true, }); expect(record.hidden).toBe(true); expect(threadRecordToMessage(record).hidden).toBe(true); const typed = messageToThreadRecord({ role: 'user', content: 'Hi' }); expect(typed).not.toHaveProperty('hidden'); expect(threadRecordToMessage(typed)).not.toHaveProperty('hidden'); }); });