main
1import assert from 'node:assert/strict'
2import { test } from 'node:test'
3import { judgeCall, type CallHeaders, type Expect, type Saved } from '../scripts/cd/judge.ts'
4import { expectedStatuses, lifecycle } from '../scripts/cd/lifecycle.ts'
5import { FLAGS_RULES_SAVE, HTTP_NOT_FOUND, HTTP_OK, SMOKE_FLAG_KEY, SMOKE_UNIT_ID, USAGE_MARK } from '../scripts/cd/flags-smoke.ts'
6import { setRollout } from '../src/flags/api.ts'
7import { rulesetRequest, serveFlags } from '../src/flags/cache.ts'
8import { parseUnit } from '../src/flags/document.ts'
9import { evaluateDocument } from '../src/flags/eval.ts'
10import { handleFlagHttp } from '../src/flags/http.ts'
11import { memoryStore } from '../src/flags/store.ts'
12import { ABSENT_REVISION, DEFAULT_SALT, USAGE_NOTE_FLAG, USAGE_NOTE_LINE } from '../src/flags/ttl.ts'
13import { CACHE_STATUSES, EMPTY_DOCUMENT, variantKind, variantValue, type CacheStatus, type Flag, type FlagSelection, type Unit } from '../src/flags/types.ts'
15const BUILD = `ply=${'a'.repeat(40)} ply-web=${'b'.repeat(40)}`
16const REQUEST_ID = 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'
18function smokeUnit(): Unit {
19 const parsed = parseUnit(SMOKE_UNIT_ID, { kind: 'absent' })
20 if (parsed.kind !== 'ok') throw new Error(parsed.reason)
21 return { id: parsed.id, attributes: parsed.attributes }
22}
24function smokeKeys(): FlagSelection {
25 return { kind: 'listed', keys: [USAGE_NOTE_FLAG] }
26}
28const note: Flag = {
29 key: SMOKE_FLAG_KEY,
30 salt: DEFAULT_SALT,
31 rollout: { kind: 'rolled-back', reason: 'basis points 0' },
32 on: { kind: 'boolean', value: true },
33 off: { kind: 'boolean', value: false },
34 allow: [],
35 deny: [],
36 rules: [],
37}
39function headers(extra: Partial<CallHeaders> = {}): CallHeaders {
40 return {
41 requestId: REQUEST_ID,
42 build: BUILD,
43 version: null,
44 ray: null,
45 cacheControl: 'no-store',
46 contentType: 'application/json; charset=utf-8',
47 ...extra,
48 }
49}
51function judge(expect: Expect, status: number, body: unknown, saved: Saved = {}, extra: Partial<CallHeaders> = {}) {
52 const bodyText = typeof body === 'string' ? body : JSON.stringify(body)
53 return judgeCall({
54 status,
55 bodyText,
56 headers: headers(extra),
57 ray: 'absent',
58 version: null,
59 expect,
60 secrets: [],
61 }, saved)
62}
64test('flag smoke steps stay off Artifacts and cover the write methods', () => {
65 const steps = lifecycle()
66 const names = steps.map(step => step.name)
67 assert.ok(names.indexOf('flags-unauth') < names.indexOf('ply-unauth'))
68 const unauth = steps.find(step => step.name === 'flags-unauth')
69 assert.equal(unauth?.ply, false)
70 assert.equal(unauth?.git, false)
71 assert.equal(unauth?.ray, 'absent')
72 assert.deepEqual(expectedStatuses(steps.find(step => step.name === 'flags-rules')!), [HTTP_OK, HTTP_NOT_FOUND])
73 assert.deepEqual(expectedStatuses(steps.find(step => step.name === 'flags-eval')!), [HTTP_OK])
74 assert.deepEqual(expectedStatuses(steps.find(step => step.name === 'usage-off')!), [HTTP_OK])
75 const evalStep = steps.find(step => step.name === 'flags-eval')
76 assert.deepEqual(evalStep?.body?.({}), { unit: SMOKE_UNIT_ID, keys: [SMOKE_FLAG_KEY] })
77 for (const name of ['flags-put', 'flags-post', 'flags-patch', 'flags-delete', 'flags-eval-get']) {
78 const step = steps.find(item => item.name === name)
79 assert.equal(step?.git, false)
80 assert.equal(step?.expect.kind, 'code')
81 if (step?.expect.kind === 'code') assert.equal(step.expect.code, 'unsupported')
82 assert.deepEqual([...expectedStatuses(step!)], [405])
83 }
84 assert.equal(steps.find(step => step.name === 'usage-off')?.ply, false)
85})
87test('a missing ruleset is 404 and eval matches the empty rollout', () => {
88 const rules = judge({ kind: 'flags-rules' }, HTTP_NOT_FOUND, { code: 'not_found', message: 'no flag config', retry: 'no' })
89 assert.equal(rules.kind, 'ok')
90 if (rules.kind !== 'ok') return
91 assert.equal(rules.save[FLAGS_RULES_SAVE], JSON.stringify({ kind: 'absent' }))
92 const evaluation = evaluateDocument(EMPTY_DOCUMENT, smokeUnit(), smokeKeys())[0]!
93 const evald = judge({ kind: 'flags-eval' }, HTTP_OK, {
94 revision: null,
95 cache: 'MISS',
96 flags: [{
97 key: SMOKE_FLAG_KEY,
98 variant: evaluation.variant,
99 variantKind: variantKind(evaluation.variant),
100 variantValue: variantValue(evaluation.variant),
101 reason: evaluation.reason,
102 bucket: evaluation.bucket,
103 }],
104 }, rules.save)
105 assert.equal(evald.kind, 'ok', evald.kind === 'fail' ? evald.detail : '')
106 const drifted = judge({ kind: 'flags-eval' }, HTTP_OK, {
107 revision: null,
108 cache: 'MISS',
109 flags: [{
110 key: SMOKE_FLAG_KEY,
111 variant: { kind: 'boolean', value: true },
112 variantKind: 'boolean',
113 variantValue: 'true',
114 reason: 'bucket',
115 bucket: evaluation.bucket,
116 }],
117 }, rules.save)
118 assert.equal(drifted.kind, 'fail')
119})
121test('a present ruleset keeps revision, cache status, and the rollout value', () => {
122 const listed = judge({ kind: 'flags-rules' }, HTTP_OK, {
123 revision: '"rev"',
124 cache: 'HIT',
125 environment: 'production',
126 flags: [note],
127 })
128 assert.equal(listed.kind, 'ok')
129 if (listed.kind !== 'ok') return
130 const evaluation = evaluateDocument({ environment: 'production', flags: [note] }, smokeUnit(), smokeKeys())[0]!
131 assert.equal(evaluation.variant.kind === 'boolean' && evaluation.variant.value, false)
132 const evald = judge({ kind: 'flags-eval' }, HTTP_OK, {
133 revision: '"rev"',
134 cache: 'HIT',
135 flags: [{
136 key: SMOKE_FLAG_KEY,
137 variant: evaluation.variant,
138 variantKind: 'boolean',
139 variantValue: 'false',
140 reason: evaluation.reason,
141 bucket: evaluation.bucket,
142 }],
143 }, listed.save, { cacheControl: 'no-store' })
144 assert.equal(evald.kind, 'ok', evald.kind === 'fail' ? evald.detail : '')
145 const stale = judge({ kind: 'flags-eval' }, HTTP_OK, {
146 revision: '"other"',
147 cache: 'HIT',
148 flags: [{
149 key: SMOKE_FLAG_KEY,
150 variant: evaluation.variant,
151 variantKind: 'boolean',
152 variantValue: 'false',
153 reason: evaluation.reason,
154 bucket: evaluation.bucket,
155 }],
156 }, listed.save)
157 assert.equal(stale.kind, 'fail')
158 const expired = judge({ kind: 'flags-rules' }, HTTP_OK, {
159 revision: '"rev"',
160 cache: 'EXPIRED',
161 environment: 'production',
162 flags: [note],
163 })
164 assert.equal(expired.kind, 'ok', expired.kind === 'fail' ? expired.detail : '')
165 const stored = judge({ kind: 'flags-eval' }, HTTP_OK, {
166 revision: '"rev"',
167 flags: [],
168 }, listed.save, { cacheControl: 'public, max-age=5' })
169 assert.equal(stored.kind, 'fail')
170})
172test('ruleset smoke names every Cache API status', () => {
173 const accepted = new Set<CacheStatus>(['HIT', 'MISS', 'STALE', 'UPDATING', 'EXPIRED', 'REVALIDATED'])
174 for (const status of CACHE_STATUSES) {
175 const judged = judge({ kind: 'flags-rules' }, HTTP_OK, {
176 revision: '"rev"',
177 cache: status,
178 environment: 'production',
179 flags: [note],
180 })
181 assert.equal(judged.kind, accepted.has(status) ? 'ok' : 'fail', `${status} ${judged.kind === 'fail' ? judged.detail : ''}`)
182 }
183 const ignored = judge({ kind: 'flags-rules' }, HTTP_OK, {
184 revision: '"rev"',
185 cache: 'IGNORED',
186 environment: 'production',
187 flags: [note],
188 })
189 assert.equal(ignored.kind, 'fail')
190 if (ignored.kind === 'fail') assert.match(ignored.detail, /not a Cache API status/)
191})
193test('a flag write then an immediate read is EXPIRED and the smoke rule accepts it', async () => {
194 const store = memoryStore()
195 // The empty ruleset is cached before the first seed. Invalidate marks that entry stale.
196 // The next read runs the entrypoint, which answers 200 with the new body, so Workers Cache
197 // reports EXPIRED (a 304 would be REVALIDATED).
198 let phase: 'empty' | 'fresh' | 'invalidated' = 'empty'
199 const cache = {
200 invalidate() {
201 if (phase === 'fresh') phase = 'invalidated'
202 return { kind: 'ok' as const }
203 },
204 async fetch(request: Request) {
205 const response = await serveFlags(request, { FLAGS: { present: true } }, store)
206 const status: CacheStatus = phase === 'invalidated'
207 ? (response.status === 304 ? 'REVALIDATED' : 'EXPIRED')
208 : phase === 'empty' ? 'MISS' : 'HIT'
209 if (phase === 'empty' || phase === 'invalidated') phase = 'fresh'
210 response.headers.set('cf-cache-status', status)
211 return response
212 },
213 }
214 const warm = await cache.fetch(rulesetRequest())
215 assert.equal(warm.status, 404)
216 if (warm.body) await warm.body.cancel()
217 const written = await setRollout(
218 store,
219 { invalidate: async () => cache.invalidate() },
220 SMOKE_FLAG_KEY,
221 0,
222 ABSENT_REVISION,
223 )
224 assert.equal(written.kind, 'ok')
225 if (written.kind !== 'ok') return
226 assert.equal(written.invalidated, true)
227 const response = await handleFlagHttp(
228 new Request('https://ply.example/v1/flags'),
229 { FLAGS: { present: true } },
230 { exports: { FlagsCache: cache } },
231 )
232 assert.equal(response.status, HTTP_OK)
233 const body: unknown = await response.json()
234 assert.ok(body && typeof body === 'object')
235 const listed = body as Record<string, unknown>
236 assert.equal(listed.cache, 'EXPIRED')
237 assert.equal(listed.revision, written.revision)
238 const judged = judge({ kind: 'flags-rules' }, response.status, body)
239 assert.equal(judged.kind, 'ok', judged.kind === 'fail' ? judged.detail : '')
240})
242test('GET / passes without the usage note and fails when the note is on', () => {
243 const off = judge(
244 { kind: 'usage-off' },
245 HTTP_OK,
246 `${USAGE_MARK} a git client\n`,
247 {},
248 { contentType: 'text/plain; charset=utf-8', cacheControl: 'no-store' },
249 )
250 assert.equal(off.kind, 'ok')
251 const on = judge(
252 { kind: 'usage-off' },
253 HTTP_OK,
254 `${USAGE_MARK} a git client\n${USAGE_NOTE_LINE}\n`,
255 {},
256 { contentType: 'text/plain; charset=utf-8' },
257 )
258 assert.equal(on.kind, 'fail')
259})
Diagnostics4 git calls · 33 ms
CallStatusSizeTime033 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=145 git.pkt.payload.max=76 git.ls_refs.count=2 git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020040 KiB · hit7 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=41349 git.response.cache=HIT git.response.duration_ms=7 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=526b305930b8ef382f2d7a1f9c0238730a585921 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=41349 git.pkt.payload.max=41254 git.sideband.band1.bytes=41253 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=61 git.pack.bytes=41253 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c git.pack.full=61Open pack map
fetch2002.9 KiB · hit24 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=24 git.response.bytes=2927 git.response.cache=HIT git.response.duration_ms=24 git.fetch.wants=cf849950b38188b5bd163fd76f4514b870915b43 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2927 git.pkt.payload.max=2906 git.sideband.band1.bytes=2905 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply-web.git
- Ref
- refs/heads/main
- Commit
- 526b305930b8ef382f2d7a1f9c0238730a585921
- cf-ray
- a5e30e799cef8a31
- Request
- 94872225…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format