Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { memoryLogStore, memoryPersistence } from '../src/memory'
3import { withGenerationPersistence, withPersistence } from '../src/middleware'
4import { reconstructChat } from '../src/reconstruct'
5import { defineAIPersistence } from '../src/types'
6import type { ChatTranscriptPersistence } from '../src/types'
7import {
8 createGenerationRunStore,
9 createInterruptStore,
10 createMessageStore,
11 createRunStore,
12} from './persistence-fixtures'
13
14/** Cast for intentional runtime-misconfiguration tests. */
15function asChatTranscript(
16 persistence: ReturnType<typeof defineAIPersistence>,
17): ChatTranscriptPersistence {
18 return persistence as ChatTranscriptPersistence
19}
20
21describe('persistence store dependency validation', () => {
22 it('rejects chat persistence without messages', () => {
23 const persistence = defineAIPersistence({
24 stores: { runs: createRunStore() },
25 })
26
27 expect(() => withPersistence(asChatTranscript(persistence))).toThrow(
28 /requires stores\.messages/i,
29 )
30 })
31
32 it('rejects a dynamic chat persistence with interrupts but no runs', () => {
33 const persistence = defineAIPersistence({
34 stores: {
35 messages: createMessageStore(),
36 interrupts: createInterruptStore(),
37 },
38 })
39
40 expect(() => withPersistence(asChatTranscript(persistence))).toThrow(
41 /interrupts.*stores\.runs/i,
42 )
43 })
44
45 it('allows message-only chat persistence', () => {
46 const messages = defineAIPersistence({
47 stores: { messages: createMessageStore() },
48 })
49
50 expect(() => withPersistence(messages)).not.toThrow()
51 })
52
53 it('allows a dynamic chat persistence with paired run and interrupt stores', () => {
54 const persistence = defineAIPersistence({
55 stores: {
56 messages: createMessageStore(),
57 runs: createRunStore(),
58 interrupts: createInterruptStore(),
59 },
60 })
61
62 expect(() => withPersistence(persistence)).not.toThrow()
63 })
64
65 it('rejects generation persistence without generationRuns', () => {
66 const persistence = defineAIPersistence({
67 stores: { messages: createMessageStore() },
68 })
69
70 expect(() =>
71 withGenerationPersistence(
72 persistence as Parameters<typeof withGenerationPersistence>[0],
73 { threadId: 'scope' },
74 ),
75 ).toThrow(/requires stores\.generationRuns/i)
76 })
77
78 it('allows generation persistence with generationRuns', () => {
79 const persistence = defineAIPersistence({
80 stores: { generationRuns: createGenerationRunStore() },
81 })
82
83 expect(() =>
84 withGenerationPersistence(persistence, { threadId: 'thread-test' }),
85 ).not.toThrow()
86 })
87
88 it('rejects reconstructChat without messages', async () => {
89 const persistence = defineAIPersistence({
90 stores: { runs: createRunStore() },
91 })
92
93 await expect(
94 reconstructChat(
95 asChatTranscript(persistence),
96 new Request('http://example.test/api/chat?threadId=t1'),
97 ),
98 ).rejects.toThrow(/requires stores\.messages/i)
99 })
100
101 it('accepts every session store key', () => {
102 const { sessions } = memoryPersistence().stores
103 const log = memoryLogStore()
104
105 expect(() =>
106 defineAIPersistence({
107 stores: { log, sessions },
108 }),
109 ).not.toThrow()
110 })
111})