import {afterEach,describe,expect,it,vi} from 'vitest' import {api} from './client' describe('UI API client',()=>{ afterEach(()=>vi.unstubAllGlobals()) it('submits every supplied task field to the UI creation endpoint',async()=>{ const fetch=vi.fn().mockResolvedValue(new Response(JSON.stringify({task_id:'t'}),{status:200})) vi.stubGlobal('fetch',fetch) await api.create({source:'web',external_id:'x',project:'p',parent:'parent',inherent_priority:2}) expect(fetch).toHaveBeenCalledWith('/v1/ui/tasks',expect.objectContaining({method:'POST'})) expect((fetch.mock.calls[0][1].body as string)).toContain('"parent":"parent"') }) it('uploads a completion report before a task completion action',async()=>{ const fetch=vi.fn().mockResolvedValue(new Response(JSON.stringify({ref:'a'.repeat(64)}),{status:201})) vi.stubGlobal('fetch',fetch) await expect(api.upload('report')).resolves.toBe('a'.repeat(64)) expect(fetch).toHaveBeenCalledWith('/v1/artifacts',expect.objectContaining({method:'POST',body:'report'})) }) it('submits username and password to the browser login endpoint',async()=>{ const fetch=vi.fn().mockResolvedValue(new Response(JSON.stringify({username:'operator'}),{status:200})) vi.stubGlobal('fetch',fetch) await expect(api.login('operator','not stored in the browser')).resolves.toEqual({username:'operator'}) expect(fetch).toHaveBeenCalledWith('/v1/ui/session',expect.objectContaining({method:'POST',body:JSON.stringify({username:'operator',password:'not stored in the browser'})})) }) it('treats a missing browser session as a normal signed-out state',async()=>{ const fetch=vi.fn().mockResolvedValue(new Response('unauthorized',{status:401})) vi.stubGlobal('fetch',fetch) await expect(api.session()).resolves.toBeUndefined() }) it('updates account credentials through the session-gated account endpoint',async()=>{ const fetch=vi.fn().mockResolvedValue(new Response(JSON.stringify({username:'kami'}),{status:200})) vi.stubGlobal('fetch',fetch) await api.updateAccount({current_password:'old password',username:'kami',new_password:'new password'}) expect(fetch).toHaveBeenCalledWith('/v1/ui/account',expect.objectContaining({method:'PUT'})) }) })