Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { contentHash } from './kv.ts'
4import {
5 CATALOG_CACHE_SECONDS,
6 cachedJson,
7 cachedText,
8 ifNoneMatchSatisfied,
9 versionedSchemaCache,
10} from './http-cache.ts'
11
12const FETCHED = 1_781_150_000
13const STALE = FETCHED + 600
14
15const request = (headers: Record<string, string> = {}) =>
16 new Request('https://example.com/v1/schemas/anthropic', { headers })
17
18describe('cachedJson', () => {
19 it('serves 200 with ETag, Last-Modified, Cache-Control, and X- headers', async () => {
20 const value = { hello: 'world' }
21 const response = await cachedJson(request(), value, {
22 fetchedAt: FETCHED,
23 staleAt: STALE,
24 })
25 expect(response.status).toBe(200)
26 expect(await response.json()).toEqual(value)
27
28 const expectedTag = await contentHash(value)
29 expect(response.headers.get('etag')).toBe(`"${expectedTag}"`)
30 expect(response.headers.get('last-modified')).toBe(
31 new Date(FETCHED * 1000).toUTCString(),
32 )
33 expect(response.headers.get('cache-control')).toBe(
34 `public, max-age=${String(CATALOG_CACHE_SECONDS)}, stale-while-revalidate=600`,
35 )
36 expect(response.headers.get('x-fetched-at')).toBe(String(FETCHED))
37 expect(response.headers.get('x-stale-at')).toBe(String(STALE))
38 })
39
40 it('returns 304 with no body on a matching If-None-Match', async () => {
41 const value = { hello: 'world' }
42 const etag = await contentHash(value)
43 const response = await cachedJson(
44 request({ 'if-none-match': `"${etag}"` }),
45 value,
46 { fetchedAt: FETCHED, staleAt: STALE },
47 )
48 expect(response.status).toBe(304)
49 expect(await response.text()).toBe('')
50 expect(response.headers.get('etag')).toBe(`"${etag}"`)
51 expect(response.headers.get('cache-control')).toBe('private, no-store')
52 expect(response.headers.get('x-stale-at')).toBe(String(STALE))
53 })
54
55 it('returns 200 on a mismatched If-None-Match', async () => {
56 const response = await cachedJson(
57 request({ 'if-none-match': '"deadbeef"' }),
58 { hello: 'world' },
59 { fetchedAt: FETCHED, staleAt: STALE },
60 )
61 expect(response.status).toBe(200)
62 })
63
64 it('uses a provided etag (e.g. the stored schema content hash) verbatim', async () => {
65 const response = await cachedJson(
66 request(),
67 { any: 'body' },
68 { etag: 'a'.repeat(64), fetchedAt: FETCHED, staleAt: STALE },
69 )
70 expect(response.headers.get('etag')).toBe(`"${'a'.repeat(64)}"`)
71 })
72
73 it('honours custom max-age and stale-while-revalidate', async () => {
74 const response = await cachedJson(
75 request(),
76 {},
77 {
78 fetchedAt: FETCHED,
79 staleAt: STALE,
80 maxAge: 5,
81 staleWhileRevalidate: 30,
82 },
83 )
84 expect(response.headers.get('cache-control')).toBe(
85 'public, max-age=5, stale-while-revalidate=30',
86 )
87 })
88
89 it('caches a content-addressed read for a year and marks it immutable', async () => {
90 const response = await cachedJson(
91 request(),
92 {},
93 { etag: 'abc', ...versionedSchemaCache(FETCHED) },
94 )
95 expect(response.headers.get('cache-control')).toBe(
96 'public, max-age=31536000, immutable',
97 )
98 expect(response.headers.get('last-modified')).toBe(
99 new Date(FETCHED * 1000).toUTCString(),
100 )
101 })
102})
103
104describe('cachedText', () => {
105 it('serves a non-JSON body with the given content type and full cache headers', () => {
106 const response = cachedText(
107 request(),
108 'export type X = string\n',
109 'text/typescript; charset=utf-8',
110 { etag: 'hash-types-v1-exact', fetchedAt: FETCHED, staleAt: STALE },
111 )
112 expect(response.status).toBe(200)
113 expect(response.headers.get('content-type')).toBe(
114 'text/typescript; charset=utf-8',
115 )
116 expect(response.headers.get('etag')).toBe('"hash-types-v1-exact"')
117 expect(response.headers.get('x-stale-at')).toBe(String(STALE))
118 })
119
120 it('returns 304 with no body on a matching If-None-Match', async () => {
121 const response = cachedText(
122 request({ 'if-none-match': '"hash-types-v1-exact"' }),
123 'export type X = string\n',
124 'text/typescript; charset=utf-8',
125 { etag: 'hash-types-v1-exact', fetchedAt: FETCHED, staleAt: STALE },
126 )
127 expect(response.status).toBe(304)
128 expect(await response.text()).toBe('')
129 })
130})
131
132describe('ifNoneMatchSatisfied', () => {
133 it('handles lists, weak validators, and the wildcard', () => {
134 expect(ifNoneMatchSatisfied('"abc", "def"', 'def')).toBe(true)
135 expect(ifNoneMatchSatisfied('W/"abc"', 'abc')).toBe(true)
136 expect(ifNoneMatchSatisfied('*', 'anything')).toBe(true)
137 expect(ifNoneMatchSatisfied('"abc"', 'def')).toBe(false)
138 expect(ifNoneMatchSatisfied(null, 'abc')).toBe(false)
139 })
140})