main
1import { describe, expect, it, vi } from 'vitest';
2import { setRollout, setTargeting } from '../../src/flags/api.ts';
3import { memoryStore, r2Store, type R2BucketLike } from '../../src/flags/store.ts';
4import { ABSENT_REVISION, FLAGS_OBJECT_KEY } from '../../src/flags/ttl.ts';
5import type { InvalidateResult } from '../../src/flags/store.ts';
7/** One property. A null receiver and a missing key are holes. A stored null stays null. */
8function slot(receiver: unknown, key: string): unknown {
9 if (receiver === null || receiver === void 0) return void 0;
10 if (typeof receiver !== 'object' && typeof receiver !== 'function') return void 0;
11 return (receiver as Record<string, unknown>)[key];
12}
14const okInvalidate = async (): Promise<InvalidateResult> => ({ kind: 'ok' });
16describe('compare-and-swap', () => {
17 it('creates on the absent revision and conflicts on a stale etag', async () => {
18 const store = memoryStore();
19 const created = await setRollout(store, { invalidate: okInvalidate }, 'ply.usage.note', 1, ABSENT_REVISION);
20 expect(created.kind).toBe('ok');
21 if (created.kind !== 'ok') return;
22 const lost = await setRollout(store, { invalidate: okInvalidate }, 'ply.usage.note', 5, ABSENT_REVISION);
23 expect(lost).toEqual({ kind: 'conflict', expected: ABSENT_REVISION, actual: created.revision });
24 const stale = await setRollout(store, { invalidate: okInvalidate }, 'ply.usage.note', 5, '"not-the-etag"');
25 expect(stale.kind).toBe('conflict');
26 if (stale.kind !== 'conflict') return;
27 expect(stale.actual).toBe(created.revision);
28 expect(slot(store.snapshot(), 'etag')).toBe(created.revision);
29 const next = await setRollout(store, { invalidate: okInvalidate }, 'ply.usage.note', 5, created.revision);
30 expect(next.kind).toBe('ok');
31 if (next.kind !== 'ok') return;
32 expect(next.config.flags[0]!.rollout).toEqual({ kind: 'ramping', basisPoints: 5 });
33 });
35 it('does not write an invalid edit', async () => {
36 const store = memoryStore();
37 const created = await setRollout(store, { invalidate: okInvalidate }, 'ply.usage.note', 1, ABSENT_REVISION);
38 if (created.kind !== 'ok') throw new Error(created.kind);
39 const rejected = await setRollout(store, { invalidate: okInvalidate }, 'ply.usage.note', 10_001, created.revision);
40 expect(rejected.kind).toBe('invalid');
41 expect(slot(store.snapshot(), 'etag')).toBe(created.revision);
42 });
44 it('logs the revision pair and the diff, and reports a failed invalidate', async () => {
45 const store = memoryStore();
46 const lines: string[] = [];
47 const spy = vi.spyOn(console, 'log').mockImplementation(line => { if (typeof line === 'string') lines.push(line); });
48 const created = await setRollout(store, { invalidate: okInvalidate }, 'ply.usage.note', 1, ABSENT_REVISION);
49 expect(created.kind).toBe('ok');
50 if (created.kind !== 'ok') return;
51 const write = lines.map(line => JSON.parse(line) as { msg: string; oldRevision: string | null; newRevision: string; diff: { op: string }[] }).find(entry => entry.msg === 'flags.write');
52 expect(write).toMatchObject({ msg: 'flags.write', op: 'setRollout', flag: 'ply.usage.note', oldRevision: null, newRevision: created.revision });
53 expect(slot(write == null ? void 0 : write.diff[0], 'op')).toBe('create');
54 const targeted = await setTargeting(store, { invalidate: async () => ({ kind: 'failed', reason: 'tag busy' }) }, 'ply.usage.note', { allow: ['user-1'], deny: [], rules: [] }, created.revision);
55 expect(targeted).toMatchObject({ kind: 'ok', invalidated: false });
56 spy.mockRestore();
57 });
59 it('passes etagMatches unquoted and If-None-Match * to R2', async () => {
60 const puts: { key: string; onlyIf: { etagMatches: string } | Headers }[] = [];
61 let body = '';
62 let etag = '';
63 const bucket: R2BucketLike = {
64 async get(key) {
65 expect(key).toBe(FLAGS_OBJECT_KEY);
66 if (!body) return null;
67 return { etag, httpEtag: `"${etag}"`, text: async () => body };
68 },
69 async put(key, value, options) {
70 puts.push({ key, onlyIf: options.onlyIf });
71 const onlyIf = options.onlyIf;
72 if (onlyIf instanceof Headers) {
73 if (onlyIf.get('if-none-match') === '*' && body) return null;
74 } else if (!onlyIf || onlyIf.etagMatches !== etag) return null;
75 body = value;
76 etag = 'abc';
77 return { etag, httpEtag: '"abc"' };
78 },
79 };
80 const store = r2Store(bucket);
81 const created = await setRollout(store, { invalidate: okInvalidate }, 'ply.usage.note', 1, ABSENT_REVISION);
82 expect(created.kind).toBe('ok');
83 if (created.kind !== 'ok') return;
84 expect(created.revision).toBe('"abc"');
85 const createdPut = puts[0];
86 const onlyIf = slot(createdPut, 'onlyIf');
87 expect(slot(createdPut, 'key')).toBe(FLAGS_OBJECT_KEY);
88 expect(onlyIf).toBeInstanceOf(Headers);
89 if (!(onlyIf instanceof Headers)) return;
90 expect(onlyIf.get('if-none-match')).toBe('*');
91 const updated = await setRollout(store, { invalidate: okInvalidate }, 'ply.usage.note', 5, created.revision);
92 expect(updated.kind).toBe('ok');
93 expect(puts[1]).toMatchObject({ key: FLAGS_OBJECT_KEY, onlyIf: { etagMatches: 'abc' } });
94 const lost = await setRollout(store, { invalidate: okInvalidate }, 'ply.usage.note', 10, '"stale"');
95 expect(lost.kind).toBe('conflict');
96 expect(puts).toHaveLength(2);
97 });
98});
Diagnostics4 git calls · 30 ms
CallStatusSizeTime030 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 B3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=145 git.response.cache=UPDATING git.response.duration_ms=3 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 · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=41349 git.response.cache=HIT git.response.duration_ms=3 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
fetch2001.6 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=1656 git.response.cache=HIT git.response.duration_ms=24 git.fetch.wants=ccbf22dc4076a3327acb8283707fdcacfabf073e 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=1656 git.pkt.payload.max=1635 git.sideband.band1.bytes=1634 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
- a5e2ef9a39ebfda9
- Request
- 574709c7…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format