Open History to see the last commit
1import { afterEach, describe, expect, it } from 'vitest'
2import { fetchText } from './types.ts'
3import {
4 cachedGoogleDocs,
5 googleDocsFailures,
6 resetGoogleDocsLimit,
7} from './google-docs.ts'
8import { docsCacheKey } from './model-facts.ts'
9
10const PAGE_A =
11 'https://docs.cloud.google.com/gemini-enterprise-agent-platform/a'
12const PAGE_B =
13 'https://docs.cloud.google.com/gemini-enterprise-agent-platform/b'
14const originalFetch = globalThis.fetch
15
16function memoryKv(): {
17 kv: KVNamespace
18 dropFresh: () => void
19} {
20 const store = new Map<string, string>()
21 const kv = {
22 get: (key: string) => Promise.resolve(store.get(key) ?? null),
23 put: (key: string, value: string) => {
24 store.set(key, value)
25 return Promise.resolve()
26 },
27 } as KVNamespace
28 const dropFresh = (): void => {
29 for (const key of store.keys()) {
30 if (key.startsWith('docs:') && !key.startsWith('docs-stale:')) {
31 store.delete(key)
32 }
33 }
34 }
35 return { kv, dropFresh }
36}
37
38function ok(body: string): Response {
39 return new Response(body, { status: 200 })
40}
41
42function limited(): Response {
43 return new Response('slow down', {
44 status: 429,
45 statusText: 'Too Many Requests',
46 })
47}
48
49afterEach(() => {
50 globalThis.fetch = originalFetch
51 resetGoogleDocsLimit()
52})
53
54describe('cachedGoogleDocs', () => {
55 it('serves the stored page after a 429 and does not fetch the next page', async () => {
56 const { kv, dropFresh } = memoryKv()
57 const pages: Record<string, string> = {
58 [PAGE_A]: 'alpha',
59 [PAGE_B]: 'beta',
60 }
61 globalThis.fetch = ((url: string) =>
62 Promise.resolve(ok(pages[String(url)] ?? ''))) as typeof fetch
63
64 await cachedGoogleDocs(kv, PAGE_A, () => fetchText(PAGE_A))
65 await cachedGoogleDocs(kv, PAGE_B, () => fetchText(PAGE_B))
66 dropFresh()
67
68 const calls: Array<string> = []
69 globalThis.fetch = ((url: string) => {
70 calls.push(String(url))
71 return Promise.resolve(limited())
72 }) as typeof fetch
73
74 await expect(
75 cachedGoogleDocs(kv, PAGE_A, () => fetchText(PAGE_A)),
76 ).resolves.toBe('alpha')
77 await expect(
78 cachedGoogleDocs(kv, PAGE_B, () => fetchText(PAGE_B)),
79 ).resolves.toBe('beta')
80 expect(calls).toEqual([PAGE_A])
81 const failures = googleDocsFailures()
82 expect(failures).toMatchObject({ failed: 1, skipped: 1 })
83 expect(failures.first.map((item) => item.source)).toEqual([PAGE_A])
84 expect(failures.failed - failures.first.length).toBeGreaterThanOrEqual(0)
85 })
86
87 it('counts cooling skips without putting them in the failure list', async () => {
88 const { kv, dropFresh } = memoryKv()
89 const pages = Array.from({ length: 7 }, (_, index) => `${PAGE_A}/${index}`)
90 globalThis.fetch = ((url: string) =>
91 Promise.resolve(ok(String(url)))) as typeof fetch
92 for (const page of pages) {
93 await cachedGoogleDocs(kv, page, () => fetchText(page))
94 }
95 dropFresh()
96 resetGoogleDocsLimit()
97
98 globalThis.fetch = () => Promise.resolve(limited())
99 for (const page of pages) {
100 await cachedGoogleDocs(kv, page, () => fetchText(page))
101 }
102 const failures = googleDocsFailures()
103 expect(failures.failed).toBe(1)
104 expect(failures.skipped).toBe(6)
105 expect(failures.first).toHaveLength(1)
106 expect(failures.first[0]?.source).toBe(pages[0])
107 })
108
109 it('fails the load when a 429 has nothing stored', async () => {
110 globalThis.fetch = () => Promise.resolve(limited())
111 await expect(
112 cachedGoogleDocs(undefined, PAGE_A, () => fetchText(PAGE_A)),
113 ).rejects.toThrow('→ 429')
114 })
115
116 it('does not serve a stored page when the status is not 429', async () => {
117 const { kv, dropFresh } = memoryKv()
118 globalThis.fetch = () => Promise.resolve(ok('alpha'))
119 await cachedGoogleDocs(kv, PAGE_A, () => fetchText(PAGE_A))
120 dropFresh()
121 globalThis.fetch = () =>
122 Promise.resolve(new Response('down', { status: 503 }))
123 await expect(
124 cachedGoogleDocs(kv, PAGE_A, () => fetchText(PAGE_A)),
125 ).rejects.toThrow('503')
126 })
127
128 it('reads a fresh page from the same key as cachedDocs and does not fetch again', async () => {
129 const { kv } = memoryKv()
130 let calls = 0
131 globalThis.fetch = () => {
132 calls += 1
133 return Promise.resolve(ok('alpha'))
134 }
135 await cachedGoogleDocs(kv, PAGE_A, () => fetchText(PAGE_A))
136 await cachedGoogleDocs(kv, PAGE_A, () => fetchText(PAGE_A))
137 expect(calls).toBe(1)
138 expect(docsCacheKey(PAGE_A)).toMatch(/^docs:v\d+:/)
139 expect(googleDocsFailures()).toEqual({ failed: 0, skipped: 0, first: [] })
140 })
141
142 it('does not store an assembly built from a stale page as a fresh hit', async () => {
143 const { kv, dropFresh } = memoryKv()
144 globalThis.fetch = () => Promise.resolve(ok('page'))
145 await cachedGoogleDocs(kv, PAGE_B, () => fetchText(PAGE_B))
146 await cachedGoogleDocs(kv, PAGE_A, async () => {
147 const inner = await cachedGoogleDocs(kv, PAGE_B, () => fetchText(PAGE_B))
148 return `bundle:${inner}`
149 })
150 dropFresh()
151
152 let calls = 0
153 globalThis.fetch = () => {
154 calls += 1
155 return Promise.resolve(limited())
156 }
157 await expect(
158 cachedGoogleDocs(kv, PAGE_A, async () => {
159 const inner = await cachedGoogleDocs(kv, PAGE_B, () =>
160 fetchText(PAGE_B),
161 )
162 return `bundle:${inner}`
163 }),
164 ).resolves.toBe('bundle:page')
165 expect(calls).toBe(1)
166 expect(googleDocsFailures()).toMatchObject({
167 failed: 1,
168 skipped: 0,
169 first: [{ source: PAGE_B }],
170 })
171
172 resetGoogleDocsLimit()
173 calls = 0
174 globalThis.fetch = ((url: string) => {
175 calls += 1
176 return Promise.resolve(ok(String(url) === PAGE_B ? 'page' : 'index'))
177 }) as typeof fetch
178 await cachedGoogleDocs(kv, PAGE_A, async () => {
179 const inner = await cachedGoogleDocs(kv, PAGE_B, () => fetchText(PAGE_B))
180 return `bundle:${inner}`
181 })
182 expect(calls).toBeGreaterThan(0)
183 expect(googleDocsFailures()).toEqual({ failed: 0, skipped: 0, first: [] })
184 })
185})