main
1// Orders taken from git's own version-sort tests (t7004) and from for-each-ref on this corpus. The space test
2// (test/space/version-refname.test.js) is the oracle over the whole product; this file is the fast check mutation runs.
3import { test } from 'vitest';
4import assert from 'node:assert/strict';
5import { compareVersionRefnames } from '../src/refs.js';
6import { errorKindAttr } from '../src/result.js';
7import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
9// Success cells are a number. An Err here is a test bug: these names are byte strings.
10function cmp(a, b, order) {
11 const r = compareVersionRefnames(a, b, order);
12 if (!r.ok) throw r.error;
13 return r.value;
14}
16const RELEASE = ['-alpha', '-beta', '', '-gamma', '-delta'];
18/** @type {readonly { readonly suffixes: readonly string[], readonly order: readonly string[] }[]} */
19const ORDERS = Object.freeze([
20 { suffixes: [], order: ['foo1.3', 'foo1.6', 'foo1.10'] },
21 { suffixes: ['-rc'], order: ['foo1.3', 'foo1.6-rc1', 'foo1.6-rc2', 'foo1.6', 'foo1.10'] },
22 { suffixes: ['-before'], order: ['foo1.7-before1', 'foo1.7', 'foo1.7-after1'] },
23 { suffixes: ['-before', '-after'], order: ['foo1.7-before1', 'foo1.7-after1', 'foo1.7'] },
24 { suffixes: ['-bar', '-foo-baz', '-foo-bar'], order: ['foo1.8-foo-baz', 'foo1.8-foo-bar', 'foo1.8'] },
25 { suffixes: ['-pre', '-prerelease'], order: ['foo1.9-pre1', 'foo1.9-pre2', 'foo1.9-prerelease1'] },
26 { suffixes: RELEASE, order: ['foo1.10-alpha', 'foo1.10-beta', 'foo1.10', 'foo1.10-unlisted-suffix', 'foo1.10-gamma', 'foo1.10-delta'] },
27 { suffixes: [], order: ['01', '1', '9', '10'] },
28 { suffixes: [], order: ['v1.01', 'v1.1', 'v1.9', 'v1.10'] },
29 { suffixes: [], order: ['1.01', '1.02', '1.1', '1.9', '1.10'] },
30 { suffixes: [], order: ['99', '100'] },
31 // Leading zeros are fractional (strverscmp S_Z): 00 < 01 < 0 < 1.
32 { suffixes: [], order: ['00', '01', '0', '1'] },
33 // UTF-8 bytes, not UTF-16 code units: U+E000 (EE 80 80) before U+10000 (F0 90 80 80). Code units put the surrogate first.
34 { suffixes: [], order: ['a1', 'é1', '\uE0001', '\u{10000}1'] },
35 { suffixes: ['-rc', ''], order: ['v1-rc1', 'v1-rc2', 'v1', 'v1-beta'] },
36 { suffixes: ['', '-rc'], order: ['v1', 'v1-rc1', 'v1-rc2'] },
37]);
39function sorted(names, suffixes) {
40 return [...names].sort((a, b) => cmp(a, b, { suffixes }));
41}
43test('compareVersionRefnames matches the version-sort orders git publishes', () => {
44 for (const { suffixes, order } of ORDERS) {
45 const shuffled = [...order].reverse();
46 assert.deepEqual(sorted(shuffled, suffixes), [...order], `${JSON.stringify(suffixes)} ${order.join(' ')}`);
47 for (let i = 0; i < order.length; i++) {
48 assert.equal(cmp(order[i], order[i], { suffixes }), 0, order[i]);
49 for (let j = i + 1; j < order.length; j++) {
50 const delta = cmp(order[i], order[j], { suffixes });
51 const back = cmp(order[j], order[i], { suffixes });
52 assert.ok(delta < 0, `${order[i]} before ${order[j]} (${delta})`);
53 assert.ok(back > 0, `${order[j]} after ${order[i]} (${back})`);
54 assert.equal(Math.sign(delta), -Math.sign(back), `${order[i]} vs ${order[j]}`);
55 }
56 }
57 }
58});
60// Magnitudes, not only signs: versioncmp returns the byte difference or the distance in the suffix list.
61test('compareVersionRefnames returns versioncmp\'s difference, not only its sign', () => {
62 assert.equal(cmp('b', 'a'), 1);
63 assert.equal(cmp('a', 'c'), -2);
64 assert.equal(cmp('10', '9'), 1);
65 assert.equal(cmp('9', '10'), -1);
66 assert.equal(cmp('100', '99'), 1);
67 assert.equal(cmp('99', '100'), -1);
68 assert.equal(cmp('12', '13'), -1);
69 assert.equal(cmp('01', '1'), -1);
70 assert.equal(cmp('1a', '12'), -1);
71 assert.equal(cmp('12', '1a'), 1);
72 // The length scan stops at '9' and at the first byte past '9'. 'a' and a missing '9' both change which run is longer.
73 assert.equal(cmp('119', '12a'), 1);
74 assert.equal(cmp('12a', '119'), -1);
75 assert.equal(cmp('199', '20'), 1);
76 assert.equal(cmp('19', '2'), 1);
77 assert.equal(cmp('2', '19'), -1);
78 const suffixes = RELEASE;
79 assert.equal(cmp('v1-alpha', 'v1-gamma', { suffixes }), -3);
80 assert.equal(cmp('v1-gamma', 'v1-alpha', { suffixes }), 3);
81 // Both sides match a suffix (alpha at 0, the empty release at 2), so the value is the distance, not the one-sided ±1.
82 assert.equal(cmp('v1-alpha', 'v1', { suffixes }), -2);
83 assert.equal(cmp('v1', 'v1-alpha', { suffixes }), 2);
84 assert.equal(cmp('v1', 'v1-gamma', { suffixes }), -1);
85 assert.equal(cmp('v1-gamma', 'v1', { suffixes }), 1);
86 // No suffix list: the byte difference, '-' against the end of the string.
87 assert.equal(cmp('v1-alpha', 'v1'), 45);
88 assert.equal(cmp('v1', 'v1-alpha'), -45);
89 assert.equal(cmp('v1-alpha', 'v1', {}), 45);
90 assert.equal(cmp('v1-alpha', 'v1', { suffixes: [] }), 45);
91 // An equal-length duplicate does not replace the suffix already found, so the distance stays 0 against 2.
92 assert.equal(cmp('v1-alpha', 'v1-gamma', { suffixes: ['-alpha', '-alpha', '-gamma'] }), -2);
93 // versioncmp is not a strict weak order once the release sits between prereleases.
94 assert.equal(cmp('00.1-gamma', '00.2-alpha', { suffixes }), -1);
95 assert.equal(cmp('00.2-alpha', '00.1-gamma', { suffixes }), 1);
96 assert.equal(cmp('00.2-alpha', '00.10-alpha', { suffixes }), -1);
97 assert.equal(cmp('00.10-alpha', '00.2-alpha', { suffixes }), 1);
98 assert.equal(cmp('00.1-gamma', '00.10-alpha', { suffixes }), 1);
99 assert.equal(cmp('00.10-alpha', '00.1-gamma', { suffixes }), -1);
100 const releaseFirst = ['', '-rc'];
101 assert.equal(cmp('00.1-rc1', '00.2', { suffixes: releaseFirst }), -1);
102 assert.equal(cmp('00.2', '00.10', { suffixes: releaseFirst }), -1);
103 assert.equal(cmp('00.1-rc1', '00.10', { suffixes: releaseFirst }), 1);
104 assert.equal(cmp('00.10', '00.1-rc1', { suffixes: releaseFirst }), -1);
105 // versioncmp stops at the C string's NUL.
106 assert.equal(cmp('a\0b', 'a\0c'), 0);
107 assert.equal(cmp('a\0b', 'a'), 0);
108 assert.equal(cmp('a\0', 'b'), -1);
109 assert.equal(cmp('v1', 'v1'), 0);
110 assert.equal(cmp('', ''), 0);
111 assert.equal(cmp('', 'a'), -97);
112 // The omitted suffix list is empty. A placeholder suffix in that default would put this name first.
113 assert.equal(cmp('xStryker was here', 'x'), 83);
114 assert.equal(cmp('x', 'xStryker was here'), -83);
115});
117// Pure, so the function has no span. The caller stamps error.kind; that is the attribute the operation span will carry.
118function stamped(r) {
119 assert.equal(r.ok, false);
120 const ex = arrayExporter();
121 const span = new Tracer({ exporter: ex }).start('git.refs.version');
122 span.set(errorKindAttr(r.error)).error(r.error, r.error.code).end();
123 const flat = flattenLine(ex.lines[0])[0];
124 assert.equal(flat.name, 'git.refs.version');
125 assert.equal(flat.attrs['error.kind'], r.error.kind);
126 assert.equal(flat.attrs['error.type'], 'bad_request');
127 return r.error;
128}
130test('compareVersionRefnames refuses a value that is not a ref byte string', () => {
131 const cases = [
132 [1, 'a', undefined, 'not_string', 'left', 'refname'],
133 ['a', 1, undefined, 'not_string', 'right', 'refname'],
134 ['a', 'b', [1], 'not_string', 'suffix', 'versionsort.suffix'],
135 ['\uD800', 'a', undefined, 'not_bytes', 'left', 'refname'],
136 ['a', '\uD800', undefined, 'not_bytes', 'right', 'refname'],
137 ['a', 'b', ['\uD800'], 'not_bytes', 'suffix', 'versionsort.suffix'],
138 // Escaped bytes that already spell UTF-8. The characters themselves compare.
139 ['\udcc3\udca9', 'a', undefined, 'not_bytes', 'left', 'refname'],
140 ];
141 for (const [a, b, suffixes, kind, which, what] of cases) {
142 const error = stamped(compareVersionRefnames(a, b, suffixes === undefined ? undefined : { suffixes }));
143 assert.equal(error.kind, kind, `${kind} ${which}`);
144 assert.equal(error.which, which);
145 assert.equal(error.code, 'bad_request');
146 assert.equal(error.what, what);
147 assert.equal(error instanceof Error, true);
148 }
149 // The byte string a caller should have passed still compares. é is the spelling the escaped form refused.
150 assert.equal(typeof cmp('é', 'a'), 'number');
151 assert.equal(typeof cmp('1', 'a'), 'number');
152});
Diagnostics4 git calls · 29 ms
CallStatusSizeTime029 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 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 B2 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=UPDATING 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 1020038 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=23 git.response.bytes=38434 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=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=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=38434 git.pkt.payload.max=38339 git.sideband.band1.bytes=38338 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=63 git.pack.bytes=38338 git.pack.ofs_delta=0 git.pack.ref_delta=8 git.pack.chain.max=8 git.pack.trailer=0bc36ab7adf82d3109177f11f1ea93f1ce071f77 git.pack.full=55Open pack map
fetch2002.5 KiB · hit23 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2594 git.response.cache=HIT git.response.duration_ms=23 git.fetch.wants=904e3348a7e98fa09af9c8a2d4a937c6b60e7ceb 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=2594 git.pkt.payload.max=2573 git.sideband.band1.bytes=2572 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.git
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e22c7ab80f2a83
- Request
- 2ce3e80e…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format