Files
deepseek-harness/packages/session/session-persistence-sqlite/tests/persistence.spec.ts
T
liukx0205 d16104bd9a test(session): cover SQLite frame storage and document its settings
Fixtures construct the store with the plugin's frame defaults and read
durable history through frames; existing tests query the frames table
and its triggers. New tests cover the frame codec, frame layout per
write and per setting, sliced reads across frame boundaries, decode
grouping, and the corruption refusals for a missing frame, a frame
shorter than its range, an unknown codec, and damaged bytes.

The SQLite README, the persistence subsystem page and the generated
configuration catalog describe `frameEvents` and the frame encoding
selected by `compression`.
2026-11-17 18:00:51 +08:00

82 lines
3.7 KiB
TypeScript

/** Shared persistence semantics and SQLite plugin activation. @module */
import { describe, expect, it } from 'vitest'
import { DatabaseSync } from 'node:sqlite'
import { readdir } from 'node:fs/promises'
import { join } from 'node:path'
import { meta, oneTurnLog, runPersistenceContract } from '../../session-persistence/tests/contract.ts'
import { persistenceFixture } from './fixtures/persistence.ts'
import { storedEvents } from './fixtures/store.ts'
runPersistenceContract('sqlite', async () => {
const fixture = await persistenceFixture()
const backend = await fixture.mount('sqlite')
return {
persistence: backend.persistence,
dispose: () => backend.context.fiber.dispose(),
reopen: async () => {
const reopened = await fixture.mount('sqlite')
return { persistence: reopened.persistence, dispose: () => reopened.context.fiber.dispose() }
},
}
})
describe('SQLite persistence plugin', () => {
it('registers the service before creating the database and initializes on first create', async () => {
const fixture = await persistenceFixture()
const { persistence } = await fixture.mount('sqlite')
expect(persistence).toBeDefined()
expect(await persistence.list()).toEqual([])
expect(await readdir(fixture.root)).toEqual([])
const writer = await persistence.create(meta('first-create'))
await writer.flush()
await writer.close()
expect(await readdir(join(fixture.root, '.segments'))).toContain('sessions.sqlite')
})
it('stores one complete logical event stream in an independent root segment', async () => {
const fixture = await persistenceFixture()
const { persistence, context } = await fixture.mount('sqlite')
const header = meta('root-segment', '/workspace')
const events = oneTurnLog()
const writer = await persistence.create(header)
await writer.append(events)
await writer.close()
await context.fiber.dispose()
const db = new DatabaseSync(join(fixture.root, '.segments', 'sessions.sqlite'))
try {
const thread = db.prepare('SELECT header, tail_segment_uuid FROM traces WHERE trace_uuid = ?').get(header.id)
expect(JSON.parse(String(thread?.header))).toEqual({ ...header, delegationDepth: 0 })
const segment = db.prepare('SELECT parent_segment_uuid, parent_end_seq, base_seq FROM segments WHERE segment_uuid = ?').get(thread?.tail_segment_uuid as string)
expect(segment).toEqual({ parent_segment_uuid: null, parent_end_seq: null, base_seq: 0 })
expect(storedEvents(db, thread?.tail_segment_uuid as string)).toEqual(events)
} finally {
db.close()
}
})
it('keeps the default JSONL provider independent of SQLite storage', async () => {
const fixture = await persistenceFixture()
const { persistence } = await fixture.mount('jsonl')
const writer = await persistence.create(meta('default-jsonl', '/workspace'))
await writer.append(oneTurnLog())
await writer.close()
expect(await readdir(fixture.root)).not.toContain('.segments')
})
it('drains live Session events before provider disposal', async () => {
const fixture = await persistenceFixture()
const { persistence, context } = await fixture.mount('sqlite')
const session = context.sessions.create(meta('shutdown-drain').id)
await persistence.create(session.header)
const events = [
session.append('turn/start', { turn: 1 }),
session.append('turn/end', { turn: 1, reason: { kind: 'completed' } }),
]
await context.fiber.dispose()
const reopened = await fixture.mount('sqlite')
const reader = await reopened.persistence.open(session.id, 'read')
expect((await reader.read()).events).toEqual(events)
await reader.close()
})
})