Open History to see the last commit
1import { beforeAll, describe, expect, it } from 'vitest'
2import { env } from 'cloudflare:test'
3import { eq } from 'drizzle-orm'
4
5import { getDb } from '../db/index.ts'
6import type { Db } from '../db/index.ts'
7import { changes, providers } from '../db/schema.ts'
8import { MODEL_ROW_SHAPE, recordRowShape } from './row-shape.ts'
9
10const NOW = 1_781_150_000
11let db: Db
12
13beforeAll(async () => {
14 db = getDb(env)
15 await db.insert(providers).values(
16 Array.from({ length: 12 }, (_, i) => ({
17 id: `shape-prov-${String(i)}`,
18 displayName: `Shape ${String(i)}`,
19 specSourceUrl: 'https://example.com/spec.json',
20 })),
21 )
22})
23
24async function shapeRows() {
25 return db
26 .select()
27 .from(changes)
28 .where(eq(changes.type, 'catalog.shape_changed'))
29}
30
31describe('recordRowShape', () => {
32 it('announces a shape once per provider, then only when the hash moves', async () => {
33 const providerCount = (await db.select().from(providers)).length
34 expect(await recordRowShape(db, NOW)).toBe(providerCount)
35 expect(await recordRowShape(db, NOW + 900)).toBe(0)
36
37 const first = await shapeRows()
38 expect(first).toHaveLength(providerCount)
39 expect(first[0]).toMatchObject({
40 subjectId: 'model-row',
41 summary: `Model row shape v${String(MODEL_ROW_SHAPE.version)}`,
42 payload: {
43 version: MODEL_ROW_SHAPE.version,
44 contentHash: MODEL_ROW_SHAPE.hash,
45 previousHash: null,
46 },
47 })
48
49 // A provider added later gets the current shape on its first poll.
50 await db.insert(providers).values({
51 id: 'shape-prov-late',
52 displayName: 'Shape late',
53 specSourceUrl: 'https://example.com/spec.json',
54 })
55 expect(await recordRowShape(db, NOW + 1200)).toBe(1)
56 expect(await recordRowShape(db, NOW + 1500)).toBe(0)
57
58 const next = { version: 99, hash: 'b'.repeat(64), notes: 'next' }
59 expect(await recordRowShape(db, NOW + 1800, next)).toBe(providerCount + 1)
60 const bumped = (await shapeRows()).filter(
61 (row) => row.createdAt === NOW + 1800,
62 )
63 expect(bumped[0]?.payload).toEqual({
64 version: 99,
65 contentHash: next.hash,
66 previousHash: MODEL_ROW_SHAPE.hash,
67 notes: 'next',
68 })
69 })
70})