Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

snapshot-atom.test.ts

133 lines4.1 KB

1import { describe, expect, it, vi } from 'vitest'
2import {
3 cloneSnapshotValue,
4 createAtom,
5 freezeSnapshotMessages,
6 patchAtom,
7 subscribeAtom,
8} from '../src/snapshot-atom'
9​
10describe('snapshot-atom', () => {
11 it('patchAtom freezes the patched snapshot and keeps identity without a change', () => {
12 const atom = createAtom({ count: 1 })
13 patchAtom(atom, { count: 2 })
14 const snapshot = atom.get()
15​
16 expect(Object.isFrozen(snapshot)).toBe(true)
17 patchAtom(atom, { count: 2 })
18 expect(atom.get()).toBe(snapshot)
19 })
20​
21 it('patchAtom skips notify when every field is unchanged', () => {
22 const atom = createAtom({ count: 1, label: 'a' })
23 const listener = vi.fn()
24 const stop = subscribeAtom(atom, listener)
25​
26 patchAtom(atom, { count: 1, label: 'a' })
27 expect(listener).not.toHaveBeenCalled()
28 expect(atom.get()).toEqual({ count: 1, label: 'a' })
29​
30 patchAtom(atom, { count: 2 })
31 expect(listener).toHaveBeenCalledTimes(1)
32 expect(atom.get()).toEqual({ count: 2, label: 'a' })
33 stop()
34 })
35​
36 it('subscribeAtom adapts { unsubscribe } to a plain cleanup', () => {
37 const atom = createAtom(0)
38 const listener = vi.fn()
39 const stop = subscribeAtom(atom, listener)
40​
41 atom.set(1)
42 expect(listener).toHaveBeenCalledTimes(1)
43 stop()
44 atom.set(2)
45 expect(listener).toHaveBeenCalledTimes(1)
46 })
47​
48 it('isolates a throwing listener from the next subscriber', () => {
49 const atom = createAtom(0)
50 const second = vi.fn()
51 const stopFirst = subscribeAtom(atom, () => {
52 throw new Error('listener failed')
53 })
54 const stopSecond = subscribeAtom(atom, second)
55 const error = vi.spyOn(console, 'error').mockImplementation(() => {})
56​
57 atom.set(1)
58​
59 expect(second).toHaveBeenCalledTimes(1)
60 expect(error).toHaveBeenCalled()
61 stopFirst()
62 stopSecond()
63 error.mockRestore()
64 })
65​
66 it('subscribeAtom calls the listener again for a change made inside it', () => {
67 const atom = createAtom({ isLoading: true, status: 'generating' })
68 const seen: Array<boolean> = []
69 const stop = subscribeAtom(atom, () => {
70 seen.push(atom.get().isLoading)
71 // A Solid effect or Vue watcher that calls `stop()` from here.
72 if (atom.get().status === 'idle' && atom.get().isLoading) {
73 patchAtom(atom, { isLoading: false })
74 }
75 })
76​
77 patchAtom(atom, { status: 'idle' })
78 expect(seen).toEqual([true, false])
79 stop()
80 })
81​
82 it('stops calling a listener that changes the atom on every call', () => {
83 const atom = createAtom({ count: 0 })
84 let calls = 0
85 const stop = subscribeAtom(atom, () => {
86 calls++
87 patchAtom(atom, { count: atom.get().count + 1 })
88 })
89​
90 patchAtom(atom, { count: 1 })
91​
92 expect(calls).toBe(8)
93 stop()
94 })
95​
96 it('reuses the frozen copy of an unchanged message and part', () => {
97 const cache = new WeakMap<object, object>()
98 const text = { type: 'text', content: 'hi' }
99 const tool = { type: 'tool-call', input: { q: 'a' } }
100 const first = { id: 'm1', parts: [text, tool] }
101​
102 const [frozenFirst] = freezeSnapshotMessages(cache, [first])
103 // The processor replaces the message and the changed part only.
104 const second = { ...first, parts: [{ ...text, content: 'hi!' }, tool] }
105 const [frozenSecond] = freezeSnapshotMessages(cache, [second])
106​
107 expect(frozenSecond).not.toBe(frozenFirst)
108 expect(frozenSecond?.parts[1]).toBe(frozenFirst?.parts[1])
109 expect(frozenSecond?.parts[0]).not.toBe(frozenFirst?.parts[0])
110 expect(freezeSnapshotMessages(cache, [second])[0]).toBe(frozenSecond)
111 })
112​
113 it('cloneSnapshotValue copies plain values and keeps other objects', () => {
114 class Result {
115 read() {
116 return 'ok'
117 }
118 }
119 const blob = new Blob(['x'])
120 const map = new Map([['a', 1]])
121 const instance = new Result()
122​
123 expect(cloneSnapshotValue(blob)).toBe(blob)
124 expect(cloneSnapshotValue(map)).toBe(map)
125 expect(cloneSnapshotValue(instance).read()).toBe('ok')
126​
127 const plain = { url: 'a' }
128 const copy = cloneSnapshotValue(plain)
129 expect(copy).toEqual(plain)
130 expect(copy).not.toBe(plain)
131 expect(Object.isFrozen(copy)).toBe(true)
132 })
133})