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

deprecations.worker.test.ts

135 lines4.2 KB

1import { env } from 'cloudflare:test'
2import { eq } from 'drizzle-orm'
3import { describe, expect, it } from 'vitest'
4​
5import { getDb } from '../../db/index.ts'
6import { changes, models, providers } from '../../db/schema.ts'
7import type {
8 ListModelsResult,
9 ModelDeprecation,
10 ModelInfo,
11 ProviderConfig,
12} from '../providers/types.ts'
13import { modelDbId, pollProviderModels } from './poll-models.ts'
14import type { SyncDeps } from './sync.ts'
15​
16function stub(id: string, next: () => ListModelsResult): ProviderConfig {
17 return {
18 id,
19 displayName: 'Stub',
20 defaultDerivation: 'upstream-spec',
21 fetchSpec: () =>
22 Promise.resolve({ specs: [], sources: [], outputStrategy: 'post-200' }),
23 listModels: () => Promise.resolve(next()),
24 classify: () => null,
25 }
27​
28async function deps(providerId: string): Promise<SyncDeps> {
29 const db = getDb(env)
30 await db.insert(providers).values({
31 id: providerId,
32 displayName: 'Stub',
33 specSourceUrl: 'https://example.com/spec.json',
34 })
35 let tick = 1_790_000_000
36 return { db, kv: env.SCHEMA_CACHE, secrets: {}, now: () => tick++ }
38​
39const live: ModelInfo = { rawId: 'live-1', activity: 'chat' }
40const old: ModelInfo = { rawId: 'old-1', activity: 'chat' }
41const entry = (rawId: string): ModelDeprecation => ({
42 rawId,
43 deprecatedAt: 1_780_000_000,
44 retiresAt: 1_786_000_000,
45 replacedBy: ['live-1'],
46 sourceUrl: 'https://example.com/deprecations',
47})
48​
49describe('deprecations page in the models poll (issue #410)', () => {
50 it('deprecates listed and unlisted rows, adds never-seen ids, keeps them when the page fails', async () => {
51 const p = 'dep-stub'
52 const d = await deps(p)
53 let result: ListModelsResult = { models: [live, old] }
54 const provider = stub(p, () => result)
55 await pollProviderModels(d, provider)
56​
57 result = {
58 models: [live, old],
59 deprecations: [entry('old-1'), entry('gone-1')],
60 }
61 await pollProviderModels(d, provider)
62 const row = async (rawId: string) =>
63 (
64 await d.db
65 .select()
66 .from(models)
67 .where(eq(models.id, modelDbId(p, rawId)))
68 )[0]
69 expect((await row('live-1'))?.deprecatedAt).toBeNull()
70 expect(await row('old-1')).toEqual(
71 expect.objectContaining({
72 deprecatedAt: 1_780_000_000,
73 deprecation: entry('old-1'),
74 }),
75 )
76 expect(await row('gone-1')).toEqual(
77 expect.objectContaining({
78 activity: null,
79 deprecatedAt: 1_780_000_000,
80 deprecation: entry('gone-1'),
81 }),
82 )
83 const feed = await d.db
84 .select()
85 .from(changes)
86 .where(eq(changes.type, 'model.deprecated'))
87 expect(feed.map((c) => c.subjectId).sort()).toEqual([
88 modelDbId(p, 'gone-1'),
89 modelDbId(p, 'old-1'),
90 ])
91​
92 // Page failed: no deprecations. Nothing flips back, no new events.
93 result = { models: [live, old] }
94 const outcome = await pollProviderModels(d, provider)
95 expect(outcome.updated).toBe(0)
96 expect((await row('old-1'))?.deprecation).toEqual(entry('old-1'))
97 expect((await row('gone-1'))?.deprecatedAt).toBe(1_780_000_000)
98​
99 // Same page again: no new events.
100 result = {
101 models: [live, old],
102 deprecations: [entry('old-1'), entry('gone-1')],
103 }
104 await pollProviderModels(d, provider)
105 const again = await d.db
106 .select()
107 .from(changes)
108 .where(eq(changes.type, 'model.deprecated'))
109 expect(again).toHaveLength(2)
110​
111 // The listed row leaves the page: it is live again.
112 result = { models: [live, old], deprecations: [entry('gone-1')] }
113 await pollProviderModels(d, provider)
114 expect(await row('old-1')).toEqual(
115 expect.objectContaining({ deprecatedAt: null, deprecation: null }),
116 )
117​
118 // An unlisted row leaves the page: its entry goes, it stays retired.
119 result = { models: [live, old], deprecations: [entry('other-1')] }
120 await pollProviderModels(d, provider)
121 expect(await row('gone-1')).toEqual(
122 expect.objectContaining({
123 deprecatedAt: 1_780_000_000,
124 deprecation: null,
125 }),
126 )
127 const withdrawn = await d.db
128 .select()
129 .from(changes)
130 .where(eq(changes.subjectId, modelDbId(p, 'gone-1')))
131 expect(withdrawn.map((c) => c.summary)).toContain(
132 'Model gone-1 no longer on the deprecations page',
133 )
134 })
135})