Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import {
4 LOOKBACK_SECONDS,
5 commitMessage,
6 mirrorFiles,
7 newChanges,
8 parseExport,
9 parseFeedState,
10 refuseRun,
11} from './mirror-catalog.ts'
12
13const body = {
14 latestChange: { id: 'chg-b', createdAt: 20 },
15 providers: [{ id: 'fal', displayName: 'FAL' }],
16 models: [{ id: 'fal-flux-dev', provider: 'fal', rawId: 'fal-ai/flux/dev' }],
17 schemas: [
18 { provider: 'fal', endpointId: 'fal-ai/flux/dev', activity: 'image' },
19 ],
20}
21
22const change = (id: string, createdAt: number) => ({
23 id,
24 createdAt,
25 type: 'model.updated',
26 summary: `summary ${id}`,
27})
28
29describe('mirrorFiles', () => {
30 it('writes one pretty file per entity, the same bytes each run', () => {
31 const files = mirrorFiles(body)
32 expect([...files.keys()]).toEqual([
33 'providers/fal.json',
34 'models/fal/fal-flux-dev.json',
35 'schemas/fal/fal-ai/flux/dev.json',
36 ])
37 expect(files.get('providers/fal.json')).toBe(
38 '{\n "id": "fal",\n "displayName": "FAL"\n}\n',
39 )
40 expect(mirrorFiles(structuredClone(body))).toEqual(files)
41 })
42
43 it('refuses ids that would leave their folder', () => {
44 for (const endpointId of ['../x', 'a//b', 'a\\b', '']) {
45 expect(() =>
46 mirrorFiles({ ...body, schemas: [{ provider: 'fal', endpointId }] }),
47 ).toThrow(/unsafe id/)
48 }
49 })
50
51 it('refuses two entities on one path', () => {
52 expect(() =>
53 mirrorFiles({ ...body, models: [...body.models, ...body.models] }),
54 ).toThrow(/two entities/)
55 })
56})
57
58describe('parseExport', () => {
59 it('refuses a body without latestChange, providers, or array sections', () => {
60 const { latestChange: _, ...noLatest } = body
61 expect(() => parseExport(noLatest)).toThrow(/no latestChange/)
62 expect(() => parseExport({ ...body, providers: [] })).toThrow(
63 /no providers/,
64 )
65 expect(() => parseExport({ ...body, models: {} })).toThrow(/not an array/)
66 expect(() => parseExport({ ...body, latestChange: { id: 1 } })).toThrow(
67 /latestChange/,
68 )
69 })
70})
71
72describe('refuseRun', () => {
73 const a = { id: 'a', createdAt: 10 }
74 const b = { id: 'b', createdAt: 20 }
75 it('refuses a latestChange that vanished or moved backwards', () => {
76 expect(() => refuseRun(b, null, 10, 10, false)).toThrow(/no latestChange/)
77 expect(() => refuseRun(b, a, 10, 10, false)).toThrow(/moved backwards/)
78 expect(() => refuseRun(a, b, 10, 10, false)).not.toThrow()
79 })
80
81 it('refuses dropping more than half the files unless allowed', () => {
82 expect(() => refuseRun(a, b, 100, 49, false)).toThrow(/MIRROR_ALLOW_SHRINK/)
83 expect(() => refuseRun(a, b, 100, 49, true)).not.toThrow()
84 expect(() => refuseRun(null, b, 0, 3, false)).not.toThrow()
85 })
86})
87
88describe('newChanges', () => {
89 it('lists a change a running poll inserted late with an earlier createdAt and smaller id', () => {
90 // Run 1 saw chg-m at T=100. The poll that started at T=100 then wrote
91 // chg-a (same createdAt, smaller random id): a keyset cursor skips it.
92 const run1 = newChanges(parseFeedState(null), [change('chg-m', 100)], {
93 id: 'chg-m',
94 createdAt: 100,
95 })
96 const run2 = newChanges(
97 run1.state,
98 [change('chg-m', 100), change('chg-a', 100), change('chg-z', 150)],
99 { id: 'chg-z', createdAt: 150 },
100 )
101 expect(run2.listed.map((c) => c.id)).toEqual(['chg-a', 'chg-z'])
102 expect(run2.state.highWater).toBe(150)
103 expect(run2.state.listed).toEqual([
104 { id: 'chg-a', createdAt: 100 },
105 { id: 'chg-m', createdAt: 100 },
106 { id: 'chg-z', createdAt: 150 },
107 ])
108 })
109
110 it('forgets listed ids once they fall behind the window', () => {
111 const later = 100 + LOOKBACK_SECONDS + 1
112 const { state } = newChanges(
113 {
114 latestChange: null,
115 highWater: 100,
116 listed: [{ id: 'old', createdAt: 100 }],
117 },
118 [change('new', later)],
119 null,
120 )
121 expect(state.listed).toEqual([{ id: 'new', createdAt: later }])
122 })
123
124 it('round-trips through feed.json', () => {
125 const { state } = newChanges(parseFeedState(null), [change('x', 5)], null)
126 expect(parseFeedState(JSON.stringify(state))).toEqual(state)
127 expect(() => parseFeedState('{"highWater":1}')).toThrow(/listed/)
128 })
129})
130
131describe('commitMessage', () => {
132 it('lists every new id', () => {
133 expect(
134 commitMessage(false, { id: 'c', createdAt: 3 }, [
135 change('c', 3),
136 change('b', 2),
137 ]),
138 ).toBe(
139 'Catalog: 2 changes\n\n/v1/changes ids recorded since the last mirror commit:\n- c model.updated: summary c\n- b model.updated: summary b\n',
140 )
141 })
142
143 it('names the first snapshot without listing the feed', () => {
144 expect(
145 commitMessage(true, { id: 'c', createdAt: 3 }, [change('c', 3)]),
146 ).toBe('Catalog snapshot up to change c\n')
147 })
148})