sessionProtocol.test.ts 1.2 KB

123456789101112131415161718192021222324
  1. import { afterEach, describe, expect, it, vi } from 'vitest'
  2. import { getMessages } from '@/api/sessions'
  3. afterEach(() => vi.unstubAllGlobals())
  4. describe('ordinary session history protocol', () => {
  5. it('always uses Desktop-compatible latest compacted pagination', async () => {
  6. const fetchMock = vi.fn(async (_input: RequestInfo | URL, _init?: RequestInit) => new Response(JSON.stringify({
  7. messages: [{ id: 'm1', role: 'assistant', content: 'ok', timestamp: 1 }],
  8. pagination: { total: 20, returned: 1, has_more: true, limit: 500 },
  9. }), { status: 200, headers: { 'content-type': 'application/json' } }))
  10. vi.stubGlobal('fetch', fetchMock)
  11. const page = await getMessages('session/a', 12, 900, 'yao er')
  12. const [input, init] = fetchMock.mock.calls[0]
  13. const url = new URL(String(input), 'http://localhost')
  14. expect(url.pathname).toBe('/api/app/sessions/session%2Fa/messages')
  15. expect(Object.fromEntries(url.searchParams)).toMatchObject({
  16. offset: '12', limit: '500', order: 'latest', include_compacted: 'true', profile: 'yao er',
  17. })
  18. expect(init).toMatchObject({ credentials: 'include', cache: 'no-store' })
  19. expect(page).toMatchObject({ total: 20, returned: 1, hasMore: true })
  20. })
  21. })