Open History to see the last commit
1/**
2 * Conformance suite for a {@link SandboxInstanceStore} implementation.
3 *
4 * Run this against a fresh BYO store (or the in-memory reference) to prove it
5 * satisfies the get / upsert / delete contract `@tanstack/ai-sandbox`'s ensure
6 * algorithm relies on — including insert-vs-overwrite and optional-field
7 * handling (full replace must clear omitted optionals).
8 *
9 * Vitest is an OPTIONAL peer dependency: this module is imported only from test
10 * files, which already run under Vitest.
11 */
12import { describe, expect, it } from 'vitest'
13import type {
14 SandboxInstanceRecord,
15 SandboxInstanceStore,
16} from '../instance-store'
17
18// The `./testkit` package.json subpath resolves to THIS module's build output
19// only (`dist/esm/testkit/conformance.js`), not a barrel over `src/testkit/`.
20// Re-export the journal, takeover, and reaper conformance suites here so they
21// are reachable from `@tanstack/ai-sandbox/testkit` without changing that
22// mapping.
23export { runJournalConformance } from './journal-conformance'
24export type { JournalConformanceConfig } from './journal-conformance'
25export { runTakeoverConformance } from './takeover-conformance'
26export type { TakeoverConformanceConfig } from './takeover-conformance'
27export { runReaperConformance } from './reaper-conformance'
28export type { ReaperConformanceConfig } from './reaper-conformance'
29export { runDurableRunFieldsConformance } from './durable-run-fields-conformance'
30export type { MakeRunStore } from './durable-run-fields-conformance'
31export { makeFakeShellSpawn } from './shell-spawn'
32export { runSandboxCheckpointStoreConformance } from './checkpoint-conformance'
33export type { SandboxCheckpointStoreOptions } from '../checkpoint-store'
34export { runSandboxCheckpointForkConformance } from './checkpoint-fork-conformance'
35export type {
36 SandboxCheckpointForkConformanceInput,
37 SandboxCheckpointForkConformanceFactory,
38} from './checkpoint-fork-conformance'
39
40function makeRecord(
41 overrides?: Partial<SandboxInstanceRecord>,
42): SandboxInstanceRecord {
43 return {
44 key: 'thread-1',
45 provider: 'fake',
46 providerSandboxId: 'sb-1',
47 threadId: 'thread-1',
48 updatedAt: 1,
49 ...overrides,
50 }
51}
52
53/**
54 * Assert `makeStore()` produces a spec-compliant {@link SandboxInstanceStore}. Each
55 * `it` gets a fresh store, so implementations may share process state across
56 * calls without cross-test bleed only if `makeStore` returns an isolated store.
57 */
58export function runSandboxInstanceStoreConformance(
59 name: string,
60 makeStore: () => SandboxInstanceStore | Promise<SandboxInstanceStore>,
61): void {
62 describe(`SandboxInstanceStore conformance: ${name}`, () => {
63 it('returns null for a missing key', async () => {
64 const store = await makeStore()
65 expect(await store.get('absent')).toBeNull()
66 })
67
68 it('round-trips an upserted record with all fields', async () => {
69 const store = await makeStore()
70 const record = makeRecord({
71 latestSnapshotId: 'snap-1',
72 latestRunId: 'run-1',
73 })
74 await store.upsert(record)
75 expect(await store.get(record.key)).toEqual(record)
76 })
77
78 it('omits absent optional fields on read', async () => {
79 const store = await makeStore()
80 const record = makeRecord()
81 await store.upsert(record)
82 const loaded = await store.get(record.key)
83 expect(loaded).toEqual(record)
84 expect(loaded && 'latestSnapshotId' in loaded).toBe(false)
85 expect(loaded && 'latestRunId' in loaded).toBe(false)
86 })
87
88 it('overwrites an existing record on re-upsert', async () => {
89 const store = await makeStore()
90 await store.upsert(makeRecord({ latestSnapshotId: 'snap-1' }))
91 await store.upsert(
92 makeRecord({ providerSandboxId: 'sb-2', updatedAt: 2 }),
93 )
94 const loaded = await store.get('thread-1')
95 expect(loaded?.providerSandboxId).toBe('sb-2')
96 expect(loaded?.updatedAt).toBe(2)
97 // The overwrite dropped latestSnapshotId — a durable store must clear it,
98 // not retain the prior value.
99 expect(loaded && 'latestSnapshotId' in loaded).toBe(false)
100 })
101
102 it('isolates records by key', async () => {
103 const store = await makeStore()
104 await store.upsert(makeRecord({ key: 'a', threadId: 'a' }))
105 await store.upsert(
106 makeRecord({ key: 'b', threadId: 'b', providerSandboxId: 'sb-b' }),
107 )
108 expect((await store.get('a'))?.providerSandboxId).toBe('sb-1')
109 expect((await store.get('b'))?.providerSandboxId).toBe('sb-b')
110 })
111
112 it('deletes a record', async () => {
113 const store = await makeStore()
114 await store.upsert(makeRecord())
115 await store.delete('thread-1')
116 expect(await store.get('thread-1')).toBeNull()
117 })
118
119 it('delete of a missing key is a no-op', async () => {
120 const store = await makeStore()
121 await expect(store.delete('absent')).resolves.toBeUndefined()
122 })
123 })
124}