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