| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import { Context } from '@deepseek-ai/cordis'
- import { HostConnectionService } from '@deepseek-ai/dsh-client-connection'
- import type { BrowserAuth } from '@deepseek-ai/dsh-client-connection/src/browser-auth.ts'
- import { SESSION_FORMAT_VERSION } from '@deepseek-ai/dsh-session'
- import type { SessionHeader, SessionId } from '@deepseek-ai/dsh-session'
- import type { SessionHandle } from '@deepseek-ai/dsh-session-persistence'
- import { strFromU8, unzipSync } from 'fflate'
- import { describe, expect, it } from 'vitest'
- import {
- Config,
- SESSION_LOG_FILENAME,
- SESSION_LOG_EXPORT_PATH,
- apply,
- inject,
- } from '../src/index.ts'
- const sid = (value: string): SessionId => value as SessionId
- function readHandle(id: string): SessionHandle {
- const header: SessionHeader = {
- version: SESSION_FORMAT_VERSION,
- id: sid(id),
- createdAt: 1,
- isSeeded: false,
- cwd: '/workspace',
- delegationDepth: 0,
- }
- return {
- id: header.id,
- header,
- access: 'read',
- read: async () => ({ eventState: 'detached', events: [] }),
- close: async () => {},
- } as unknown as SessionHandle
- }
- async function mounted(withServices: boolean): Promise<{
- readonly connection: HostConnectionService
- readonly dispose: () => Promise<void>
- }> {
- const ctx = new Context()
- ctx.provide('commands', { register: () => () => {} } as never)
- if (withServices) {
- ctx.provide('sessionQuery', {
- traceSession: async () => ({ descendants: [] }),
- } as never)
- ctx.provide('sessionPersistence', {
- stat: async (id: SessionId) => ({ header: readHandle(String(id)).header }),
- open: async (id: SessionId) => readHandle(String(id)),
- } as never)
- ctx.provide('attachments', {
- readImage: async () => { throw new Error('fixture has no images') },
- } as never)
- }
- const connection = new HostConnectionService(ctx, [], {} as BrowserAuth)
- const fiber = ctx.plugin({ inject: [...inject], apply })
- await fiber
- return { connection, dispose: () => fiber.dispose() }
- }
- describe('Session log export Fetch route', () => {
- it('registers one GET/HEAD route and removes it with the plugin fiber', async () => {
- const { connection, dispose } = await mounted(true)
- const shared = connection.createSharedFetchHandler('/api')
- const response = await shared.fetch(new Request(
- `http://host${SESSION_LOG_EXPORT_PATH}?sessionId=session-1`,
- ))
- expect(response.status).toBe(200)
- expect(response.headers.get('content-type')).toBe('application/zip')
- const files = unzipSync(new Uint8Array(await response.arrayBuffer()))
- expect(strFromU8(files[SESSION_LOG_FILENAME] as Uint8Array)).toContain('"id":"session-1"')
- const head = await shared.fetch(new Request(
- `http://host${SESSION_LOG_EXPORT_PATH}?sessionId=session-1`, { method: 'HEAD' },
- ))
- expect(head.status).toBe(200)
- expect(head.body).toBeNull()
- await dispose()
- expect((await shared.fetch(new Request(
- `http://host${SESSION_LOG_EXPORT_PATH}?sessionId=session-1`,
- ))).status).toBe(404)
- })
- it('validates the query before reporting missing export services', async () => {
- const { connection, dispose } = await mounted(false)
- const shared = connection.createSharedFetchHandler('/api')
- expect((await shared.fetch(new Request(`http://host${SESSION_LOG_EXPORT_PATH}`))).status).toBe(400)
- expect((await shared.fetch(new Request(
- `http://host${SESSION_LOG_EXPORT_PATH}?sessionId=session-1&includeDescendants=1`,
- ))).status).toBe(400)
- expect((await shared.fetch(new Request(
- `http://host${SESSION_LOG_EXPORT_PATH}?sessionId=session-1`,
- ))).status).toBe(500)
- await dispose()
- })
- it('validates the compression level', () => {
- expect(Config({})).toEqual({ compressionLevel: 6 })
- expect(Config({ compressionLevel: 0 })).toEqual({ compressionLevel: 0 })
- expect(Config({ compressionLevel: 9 })).toEqual({ compressionLevel: 9 })
- for (const compressionLevel of [-1, 10, 1.5]) {
- expect(() => Config({ compressionLevel } as never)).toThrow()
- }
- })
- })
|