Files
deepseek-harness/packages/session-query/session-log-export/tests/route.host.spec.ts
T
Turtle bec6805d6a refactor(session-persistence)!: handle-based seam with a lifecycle-owned write path
The persistence seam is now create/open/stat/list returning per-session
SessionHandles (read/append/flush/close); every log read and write flows
through the owning handle. The seam package exports only the service and
handle contracts, consumer-visible errors, and pure durable-data
validation helpers; each backend owns its complete storage runtime, and
the shared contract suites pin equivalent observable behavior. The
backend routes published sessions' live events by id into the active
write handle; agent-loop only acquires, seeds, and closes the handle.
Resume appends interruptedTurnClosers through its write handle;
session-query owns the revision-keyed cold cache. Legacy-only surfaces
are removed in the same swap: locate/readRaw/supportsRawArtifacts, the
legacy event-shape read migration, zstd torn-frame salvage,
DSH_SESSION_JSONL, and hook transcript_path population; a torn final
zstd frame is discarded whole; the session-list cold blank probe returns
on stat metadata (eventCount derived from the last physical row,
sizeBytes). The WebUI ZIP export serializes the logical log from a read
handle, so both backends export identically.

Refs #3245
2026-09-01 23:19:02 +08:00

106 lines
3.8 KiB
TypeScript

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 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_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: 0,
id: sid(id),
createdAt: 1,
isSeeded: false,
cwd: '/workspace',
delegationDepth: 0,
}
return {
id: header.id,
header,
access: 'read',
read: async () => [],
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.jsonl'] 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()
}
})
})