GitHubivarvong

ply

main
ply6 git calls136 ms27 KiB3 cachedprotocol v2
236 lines14 KiBRaw
1// The ratchet scripts, run as CI runs them (a process, an exit code) against a scratch tree whose counts are known
2// by construction. Expected values are counted by hand from the fixture text below, never taken from the script.
3import { test } from 'vitest';
4import assert from 'node:assert/strict';
5import { spawnSync } from 'node:child_process';
6import { mkdtempSync, mkdirSync, writeFileSync, existsSync, readFileSync, rmSync, cpSync, symlinkSync, appendFileSync } from 'node:fs';
7import { tmpdir } from 'node:os';
8import { join } from 'node:path';
9import { fileURLToPath } from 'node:url';
11const SCRIPT = fileURLToPath(new URL('../scripts/throw-ratchet.mjs', import.meta.url));
12const BASELINE = 'scripts/throw-baseline.json';
14// A tree with exactly two throw statements (one per function; the word in the comment is prose, not a statement).
15function tree() {
16 const dir = mkdtempSync(join(tmpdir(), 'ply-ratchet-'));
17 mkdirSync(join(dir, 'src'));
18 mkdirSync(join(dir, 'scripts'));
19 writeFileSync(join(dir, 'src/a.ts'), [
20 '// this file may throw twice',
21 'export function a(): never { throw new Error("a"); }',
22 'export function b(): never { throw new Error("b"); }',
23 '',
24 ].join('\n'));
25 return dir;
27const run = (dir, ...args) => spawnSync(process.execPath, [SCRIPT, ...args], { cwd: dir, encoding: 'utf8' });
29test('throw ratchet: a missing baseline fails and is not written; only --write creates it', () => {
30 const dir = tree();
31 try {
32 // Check mode (what `npm run typecheck` and CI run): no baseline is a failure that names the file and the count.
33 const check = run(dir);
34 assert.equal(check.status, 1, `check mode with no baseline must fail, got exit ${check.status}: ${check.stdout}${check.stderr}`);
35 assert.match(check.stderr, /no scripts\/throw-baseline\.json \(src has 2 throw statements\)/u);
36 assert.equal(existsSync(join(dir, BASELINE)), false, 'check mode must not write a baseline');
37 // The explicit flag writes it, with the hand-counted number.
38 const write = run(dir, '--write');
39 assert.equal(write.status, 0, write.stderr);
40 assert.deepEqual(JSON.parse(readFileSync(join(dir, BASELINE), 'utf8')), { throw: 2 });
41 assert.equal(run(dir).status, 0);
42 // A third throw: check mode fails, and --write refuses to raise the baseline.
43 writeFileSync(join(dir, 'src/c.ts'), 'export function c(): never { throw new Error("c"); }\n');
44 assert.equal(run(dir).status, 1);
45 const raise = run(dir, '--write');
46 assert.equal(raise.status, 1);
47 assert.match(raise.stderr, /throw rose 2 -> 3/u);
48 assert.deepEqual(JSON.parse(readFileSync(join(dir, BASELINE), 'utf8')), { throw: 2 });
49 } finally {
50 rmSync(dir, { recursive: true, force: true });
51 }
52});
54const ABSENCE = fileURLToPath(new URL('../scripts/absence-ratchet.mjs', import.meta.url));
55const ZERO = { boundary: 'src/absent.ts', undefined: 0, optional: 0, chain: 0, coalesce: 0 };
56// A tree whose baseline says: no absence syntax outside the boundary.
57function absenceTree(files) {
58 const dir = mkdtempSync(join(tmpdir(), 'ply-absence-'));
59 mkdirSync(join(dir, 'src'));
60 mkdirSync(join(dir, 'scripts'));
61 writeFileSync(join(dir, 'src/absent.ts'), 'export const absent = undefined;\n');
62 writeFileSync(join(dir, 'scripts/absence-baseline.json'), `${JSON.stringify(ZERO, null, 2)}\n`);
63 for (const [name, text] of Object.entries(files)) writeFileSync(join(dir, name), text);
64 return dir;
66const absence = dir => spawnSync(process.execPath, [ABSENCE], { cwd: dir, encoding: 'utf8' });
68test('absence ratchet: a regex group, a string, a template and a comment are not absence syntax', () => {
69 // Hand count: this file has no optional member, no optional chain, no coalescing operator and no missing value.
70 // Every `?:`, `?.`, `??` and `undefined` below is inside a regex literal, a string, template text or a comment
71 // (the regex is the one the textual count failed at 018d58b: src/push.ts:257).
72 const dir = absenceTree({ 'src/a.ts': [
73 '// undefined is only named here: a?.b, c ?? d, e?: f',
74 '/* and here: undefined ?. ?? ?: */',
75 'export const R = /^push is ([0-9]+(?:\\.[0-9]+)?) MiB, limit is ([0-9]+(?:\\.[0-9]+)?) MiB$/u;',
76 "export const S = 'x?.y ?? z?: undefined';",
77 'export const T = `t?.u ?? v?: undefined ${1} ?? undefined`;',
78 'export const C = R.test(S) ? 1 : 2;',
79 '',
80 ].join('\n') });
81 try {
82 const r = absence(dir);
83 assert.equal(r.status, 0, `a file with no absence syntax must pass a zero baseline: ${r.stdout}${r.stderr}`);
84 assert.match(r.stdout, /undefined 0, optional 0, chain 0, coalesce 0/u);
85 } finally {
86 rmSync(dir, { recursive: true, force: true });
87 }
88});
90test('absence ratchet: spacing does not hide syntax; each kind is counted once per node', () => {
91 // Hand count, one line each:
92 // optional 4: `a? : number`, `m?(): void`, the parameter `p?: number`, the mapped `[K in keyof T]?: T[K]`
93 // (`-?` on the next line removes optionality: not counted; `c ? 1 : 2` is a conditional: not counted)
94 // undefined 3: `void 0`, the type keyword in `number | undefined`, the identifier in `= undefined`
95 // chain 3: `o?.a`, `o?.["a"]`, `f?.()`
96 // coalesce 2: `??`, `??=`
97 const dir = absenceTree({ 'src/b.ts': [
98 'export type O = { a? : number; m?(): void };',
99 'export type P<T> = { [K in keyof T]?: T[K] };',
100 'export type Q<T> = { [K in keyof T]-?: T[K] };',
101 'export function g(o: O | null, f: (() => number) | null, p?: number): number {',
102 ' let x: number | undefined = undefined;',
103 ' const v = void 0;',
104 ' x ??= 1;',
105 ' const c = v ? 1 : 2;',
106 ' return (o?.a ?? 0) + Number(o?.["a"]) + Number(f?.()) + Number(p) + x + c;',
107 '}',
108 '',
109 ].join('\n') });
110 try {
111 const r = absence(dir);
112 assert.equal(r.status, 1, `absence syntax over a zero baseline must fail: ${r.stdout}${r.stderr}`);
113 assert.match(r.stderr, /undefined rose 0 -> 3;/u);
114 assert.match(r.stderr, /optional rose 0 -> 4;/u);
115 assert.match(r.stderr, /chain rose 0 -> 3;/u);
116 assert.match(r.stderr, /coalesce rose 0 -> 2;/u);
117 } finally {
118 rmSync(dir, { recursive: true, force: true });
119 }
120});
122// The guard CI runs beside the ratchets: a baseline that moved the unsafe way against the base commit fails unless a
123// commit says `Ratchet-Change: <file>: <reason>`. The scratch repository's history is written here, commit by
124// commit, so every expected exit code follows from what the commit did, not from the guard.
125const GUARD = fileURLToPath(new URL('../scripts/ratchet-guard.mjs', import.meta.url));
126test('ci runs the ratchet guard against the pull request base (or the previous commit on main)', () => {
127 const ci = readFileSync(new URL('../.github/workflows/ci.yml', import.meta.url), 'utf8');
128 assert.match(ci, /^ {10}RATCHET_BASE: \$\{\{ github\.event\.pull_request\.base\.sha \|\| github\.event\.before \}\}\n {8}run: node scripts\/ratchet-guard\.mjs$/mu);
129});
130test('ratchet guard: a loosened baseline needs a Ratchet-Change line naming that file', () => {
131 const dir = mkdtempSync(join(tmpdir(), 'ply-guard-'));
132 const env = { ...process.env, GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_SYSTEM: '/dev/null', GIT_AUTHOR_NAME: 't', GIT_AUTHOR_EMAIL: 't@example.com', GIT_COMMITTER_NAME: 't', GIT_COMMITTER_EMAIL: 't@example.com' };
133 delete env.RATCHET_BASE;
134 const git = (...args) => { const r = spawnSync('git', args, { cwd: dir, env, encoding: 'utf8' }); assert.equal(r.status, 0, `git ${args.join(' ')}: ${r.stderr}`); return r.stdout.trim(); };
135 const put = (file, value) => writeFileSync(join(dir, file), `${JSON.stringify(value)}\n`);
136 const commit = (message) => { git('add', '-A'); git('commit', '-q', '-m', message); return git('rev-parse', 'HEAD'); };
137 const guard = (base) => spawnSync(process.execPath, [GUARD, base], { cwd: dir, env, encoding: 'utf8' });
138 const holes = (...ids) => ({ categories: { decisions: { count: ids.length, ids } } });
139 try {
140 mkdirSync(join(dir, 'scripts'));
141 mkdirSync(join(dir, 'test/space'), { recursive: true });
142 git('init', '-q', '-b', 'main');
143 put('scripts/throw-baseline.json', { throw: 2 });
144 put('scripts/absence-baseline.json', { boundary: 'src/absent.ts', optional: 0 });
145 put('test/space/ratchet.json', holes('a', 'b'));
146 put('test/durations.json', { 'test/x.test.js': 1 });
147 writeFileSync(join(dir, 'test/x.test.js'), '');
148 const base = commit('base');
149 assert.equal(guard(base).status, 0, 'nothing changed');
151 // throw 2 -> 3 with no marker: refused, naming the file, the move and the line to add.
152 put('scripts/throw-baseline.json', { throw: 3 });
153 commit('raise the throw baseline quietly');
154 const quiet = guard(base);
155 assert.equal(quiet.status, 1, quiet.stdout);
156 assert.match(quiet.stderr, /scripts\/throw-baseline\.json moved the unsafe way[^]*throw rose 2 -> 3[^]*Ratchet-Change: scripts\/throw-baseline\.json: <reason>/u);
157 // A marker for another file, or one with no reason, does not cover it.
158 git('commit', '-q', '--amend', '-m', 'raise\n\nRatchet-Change: test/space/ratchet.json: wrong file\nRatchet-Change: scripts/throw-baseline.json:');
159 assert.equal(guard(base).status, 1);
160 git('commit', '-q', '--amend', '-m', 'raise\n\nRatchet-Change: scripts/throw-baseline.json: c() must throw until #1');
161 assert.equal(guard(base).status, 0);
163 // Lowering is the ratchet working: no marker needed. (2 holes -> 1, absence stays, throw back to 2.)
164 put('scripts/throw-baseline.json', { throw: 2 });
165 put('test/space/ratchet.json', holes('a'));
166 const lowered = commit('lower');
167 assert.equal(guard(lowered).status, 0);
168 assert.equal(guard(base).status, 0);
170 // Each other baseline's unsafe move, one at a time against `lowered`; `git checkout` restores the file.
171 const refused = (file, value, why) => {
172 put(file, value);
173 commit(`loosen ${file}`);
174 const r = guard(lowered);
175 assert.equal(r.status, 1, `${file}: ${r.stdout}`);
176 assert.match(r.stderr, why);
177 git('reset', '-q', '--hard', lowered);
178 };
179 refused('test/space/ratchet.json', holes('a', 'c'), /decisions gained hole c/u);
180 refused('test/space/ratchet.json', { categories: {} }, /category decisions removed/u);
181 refused('scripts/absence-baseline.json', { boundary: 'src/absent.ts', optional: 2 }, /optional rose 0 -> 2/u);
182 refused('scripts/absence-baseline.json', { boundary: 'src/other.ts', optional: 0 }, /boundary src\/absent\.ts -> src\/other\.ts/u);
183 refused('test/durations.json', {}, /test\/x\.test\.js lost its weight/u);
184 // A base that cannot be resolved fails closed.
185 assert.equal(guard('no-such-ref').status, 1);
186 } finally {
187 rmSync(dir, { recursive: true, force: true });
188 }
189});
191// A copy of what the space scripts read, with one decision point the registry does not know. The copy differs from
192// the tree by exactly that function, so exactly two holes are new (an exported function is a point, its fail() is
193// another), with ids of the shape test/space/ratchet.json stores: `<file>#<function>:export` and `<file>#<function>:<code>:<message>`.
194const ROOT = fileURLToPath(new URL('..', import.meta.url));
195const SNEAKY = 'src/sign.ts#decideSneaky:protocol:an unregistered decision';
196const SNEAKY_EXPORT = 'src/sign.ts#decideSneaky:export';
197// The suite's NODE_OPTIONS hook and PLY_FAILSITES belong to the tree under test, not to a child running in a copy.
198const CLEAN_ENV = { ...process.env, NODE_OPTIONS: '', PLY_FAILSITES: '', PLY_FAILSITES_GATE: '' };
199test('spaces --ratchet --init adds a hole only with --reason, and prints the Ratchet-Change line the commit needs', () => {
200 const dir = mkdtempSync(join(tmpdir(), 'ply-spaces-'));
201 try {
202 for (const p of ['src', 'scripts', 'test/space', 'test/ts-resolve.mjs', 'package.json']) cpSync(join(ROOT, p), join(dir, p), { recursive: true });
203 symlinkSync(join(ROOT, 'node_modules'), join(dir, 'node_modules'));
204 appendFileSync(join(dir, 'src/sign.ts'), "\nexport function decideSneaky(n: number): unknown {\n return n > 1 ? fail('protocol', 'an unregistered decision') : null;\n}\n");
205 const file = join(dir, 'test/space/ratchet.json');
206 const ids = () => JSON.parse(readFileSync(file, 'utf8')).categories.decisions.ids;
207 const spaces = (...args) => spawnSync(process.execPath, ['--import', './test/ts-resolve.mjs', 'scripts/spaces.mjs', ...args], { cwd: dir, encoding: 'utf8', env: CLEAN_ENV });
208 const had = ids();
209 assert.ok(!had.includes(SNEAKY));
211 // Lowering never adds.
212 const lowered = spaces('--ratchet');
213 assert.equal(lowered.status, 0, lowered.stderr);
214 assert.deepEqual(ids().filter(id => !had.includes(id)), []);
216 // --init with no reason (or a blank one): refused, ratchet.json and SPACES.md not written, the hole and the line named.
217 const before = readFileSync(file, 'utf8');
218 for (const args of [['--ratchet', '--init'], ['--ratchet', '--init', '--md'], ['--ratchet', '--init', '--reason', ' ']]) {
219 const refused = spaces(...args);
220 assert.equal(refused.status, 1, `${args.join(' ')} must fail, got exit ${refused.status}: ${refused.stdout}${refused.stderr}`);
221 assert.equal(readFileSync(file, 'utf8'), before);
222 assert.ok(!existsSync(join(dir, 'SPACES.md')));
223 assert.ok(refused.stderr.includes(SNEAKY), refused.stderr);
224 assert.ok(refused.stderr.includes('Ratchet-Change: test/space/ratchet.json: '), refused.stderr);
225 }
227 // With a reason: exactly those holes are added, and the output is the line ci's ratchet-guard looks for.
228 const added = spaces('--ratchet', '--init', '--reason', 'decideSneaky is tracked in #999');
229 assert.equal(added.status, 0, added.stderr);
230 assert.deepEqual(ids().filter(id => !had.includes(id)), [SNEAKY_EXPORT, SNEAKY]);
231 assert.ok(added.stdout.includes(SNEAKY), added.stdout);
232 assert.ok(added.stdout.includes('Ratchet-Change: test/space/ratchet.json: decideSneaky is tracked in #999'), added.stdout);
233 } finally {
234 rmSync(dir, { recursive: true, force: true });
235 }
236});
Diagnostics6 git calls · 136 ms
CallStatusSizeTime0136 ms
discover200191 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=191
git.server.agent=git/github-9922ec520325-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=191
git.pkt.payload.max=36
ls-refs20012 KiB2 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=11990
git.response.cache=UPDATING
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=152
git.pkt.data=151
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=11990
git.pkt.payload.max=122
git.ls_refs.count=151
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 102004.2 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=4347
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4
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=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4347
git.pkt.payload.max=4247
git.sideband.band1.bytes=4247
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=10
git.pack.bytes=4247
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=a6caf8e129bea82c9bae4cbc7c130c9205add400
git.pack.full=10
Open pack map
fetchtree:0200919 B · hit4 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=919
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588
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=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=919
git.pkt.payload.max=892
git.sideband.band1.bytes=892
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=892
git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76e
Open pack map
fetchtree:02005.6 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=123
git.response.bytes=5724
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=58fabb6768b3f737ee9cd8bac647bdc27405fb4d
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=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=5724
git.pkt.payload.max=5697
git.sideband.band1.bytes=5697
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=5697
git.pack.trailer=be7e0453974a5fe832f9065a33bb7e3434c10e9b
Open pack map
fetch2004.5 KiB124 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=4564
git.response.cache=MISS
git.response.duration_ms=124
git.fetch.wants=e452f95401eda596e484e2cd3de705b120b32001
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=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=4564
git.pkt.payload.max=4537
git.sideband.band1.bytes=4537
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://github.com/ivarvong/ply
Ref
refs/heads/main
Commit
19912563f540228593df0c489082e5e2508e21e4
cf-ray
a5e1a05a28753cd0
Request
d7316301…
Protocol
v2 · sha1
Agent
git/github-9922ec520325-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format