39 lines
1.7 KiB
TypeScript
39 lines
1.7 KiB
TypeScript
/** Isolated persistence providers sharing one test-owned storage directory. @module */
|
|
import { Context } from '@deepseek-ai/cordis'
|
|
import { mkdtemp, rm } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { onTestFinished } from 'vitest'
|
|
import * as SqlitePersistence from '../../src/index.ts'
|
|
import * as JsonlPersistence from '@deepseek-ai/dsh-session-persistence-jsonl'
|
|
import type { JsonlCompression } from '@deepseek-ai/dsh-session-persistence-jsonl'
|
|
import SessionStore from '@deepseek-ai/dsh-session'
|
|
|
|
/**
|
|
* Allocate private storage and close every mounted provider before removing it.
|
|
* @returns storage root and factories for independently owned providers.
|
|
*/
|
|
export async function persistenceFixture() {
|
|
const root = await mkdtemp(join(tmpdir(), 'dsh-sqlite-persistence-'))
|
|
const contexts: Context[] = []
|
|
onTestFinished(async () => {
|
|
let failures: unknown[]
|
|
try {
|
|
const results = await Promise.allSettled(contexts.map(context => context.fiber.dispose()))
|
|
failures = results.filter(result => result.status === 'rejected').map((result): unknown => result.reason)
|
|
} finally {
|
|
await rm(root, { recursive: true, force: true })
|
|
}
|
|
if (failures.length > 0) throw new AggregateError(failures, 'Persistence fixture disposal failed')
|
|
})
|
|
async function mount(storage: 'sqlite' | 'jsonl', compression: JsonlCompression = 'none') {
|
|
const context = new Context()
|
|
contexts.push(context)
|
|
await context.plugin(SessionStore)
|
|
const plugin = storage === 'sqlite' ? SqlitePersistence : JsonlPersistence
|
|
const provider = await context.plugin(plugin, { root, compression })
|
|
return { context, persistence: context.sessionPersistence, provider }
|
|
}
|
|
return { root, mount }
|
|
}
|