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

changes-api.worker.test.ts

126 lines3.2 KB

1import { beforeAll, describe, expect, it } from 'vitest'
2import { env } from 'cloudflare:test'
3​
4import { getDb } from '../db/index.ts'
5import type { Db } from '../db/index.ts'
6import { changes, providers } from '../db/schema.ts'
7import { decodeCursor, encodeCursor, listChanges } from './changes-api.ts'
8​
9const NOW = 1_781_150_000
10let db: Db
11​
12beforeAll(async () => {
13 db = getDb(env)
14 await db.insert(providers).values({
15 id: 'chg-prov',
16 displayName: 'Changes Prov',
17 specSourceUrl: 'https://example.com/spec.json',
18 })
19 // Five changes: two share a createdAt to exercise the (createdAt, id)
20 // keyset tie-break.
21 await db.insert(changes).values([
22 {
23 id: 'chg-1',
24 type: 'model.added',
25 providerId: 'chg-prov',
26 subjectId: 'm1',
27 summary: 'one',
28 createdAt: NOW + 1,
29 },
30 {
31 id: 'chg-2',
32 type: 'model.added',
33 providerId: 'chg-prov',
34 subjectId: 'm2',
35 summary: 'two',
36 createdAt: NOW + 2,
37 },
38 {
39 id: 'chg-3a',
40 type: 'schema.updated',
41 providerId: 'chg-prov',
42 subjectId: 'e1',
43 summary: 'three-a',
44 createdAt: NOW + 3,
45 },
46 {
47 id: 'chg-3b',
48 type: 'schema.updated',
49 providerId: 'chg-prov',
50 subjectId: 'e2',
51 summary: 'three-b',
52 createdAt: NOW + 3,
53 },
54 {
55 id: 'chg-4',
56 type: 'model.removed',
57 providerId: 'chg-prov',
58 subjectId: 'm1',
59 summary: 'four',
60 createdAt: NOW + 4,
61 },
62 ])
63})
64​
65describe('listChanges pagination', () => {
66 it('pages through all rows without overlap, newest first', async () => {
67 const seen: Array<string> = []
68 let cursor: string | undefined
69 let pages = 0
70 do {
71 const outcome = await listChanges(db, {
72 provider: 'chg-prov',
73 limit: 2,
74 cursor,
75 })
76 expect(outcome.ok).toBe(true)
77 if (!outcome.ok) break
78 seen.push(...outcome.result.changes.map((c) => c.id))
79 if (outcome.result.nextCursor) {
80 expect(outcome.result._links.next?.href).toContain('cursor=')
81 }
82 cursor = outcome.result.nextCursor ?? undefined
83 pages++
84 } while (cursor && pages < 10)
85​
86 expect(pages).toBe(3) // 2 + 2 + 1
87 expect(seen).toEqual(['chg-4', 'chg-3b', 'chg-3a', 'chg-2', 'chg-1'])
88 expect(new Set(seen).size).toBe(seen.length)
89 })
90​
91 it('filters by since and type', async () => {
92 const since = await listChanges(db, {
93 provider: 'chg-prov',
94 since: NOW + 3,
95 })
96 expect(since.ok && since.result.changes.map((c) => c.id)).toEqual([
97 'chg-4',
98 'chg-3b',
99 'chg-3a',
100 ])
101​
102 const typed = await listChanges(db, {
103 provider: 'chg-prov',
104 type: 'model.added',
105 })
106 expect(typed.ok && typed.result.changes.map((c) => c.id)).toEqual([
107 'chg-2',
108 'chg-1',
109 ])
110 })
111​
112 it('rejects garbage cursors with a 400', async () => {
113 const outcome = await listChanges(db, { cursor: 'not-base64!!' })
114 expect(outcome).toMatchObject({
115 ok: false,
116 status: 400,
117 code: 'invalid_cursor',
118 })
119 })
120​
121 it('round-trips cursors, including ids containing colons', () => {
122 const cursor = { createdAt: NOW, id: 'a:b:c' }
123 expect(decodeCursor(encodeCursor(cursor))).toEqual(cursor)
124 expect(decodeCursor('AAAA')).toBeNull()
125 })
126})