GitHubivarvong
ply
main
1// Option laws (map, flatMap, getOrElse) and the platform-absence boundary. PROP_N shrinks the loop under mutation.
2import { test } from 'vitest';
3import assert from 'node:assert/strict';
4import { execFileSync } from 'node:child_process';
5import { some, none, mapOption, flatMapOption, getOrElse, matchOption } from '../src/result.js';
6import {
7 fromNullable, fromNullish, at, atOr, mapGet, readKey, jsonDecode, byteInRange,
8 asOption, unwrapOrAbsent,
9} from '../src/absent.js';
10import { Rng } from './fuzz/prng.js';
11import { compareCounts } from '../scripts/absence-ratchet.mjs';
13const N = parseInt(process.env.PROP_N ?? '200', 10);
14const SEED = 1;
15const genInt = rng => rng.int(-30, 30);
16const genOpt = rng => (rng.bool() ? some(genInt(rng)) : none);
17const genPlain = rng => { const k = rng.int(-4, 4); return n => n + k; };
18const genFn = rng => {
19 const k = rng.int(-4, 4);
20 const drop = rng.bool();
21 return n => (drop && n % 2 === 0 ? none : some(n + k));
22};
24test('Option: none skips the continuation; some applies it', () => {
25 const boom = () => { throw new Error('ran'); };
26 assert.equal(mapOption(none, boom), none);
27 assert.equal(flatMapOption(none, boom), none);
28 assert.equal(getOrElse(none, 7), 7);
29 assert.deepEqual(mapOption(some(2), n => n + 1), some(3));
30 assert.deepEqual(flatMapOption(some(2), n => some(n + 1)), some(3));
31 assert.equal(flatMapOption(some(2), () => none), none);
32 assert.equal(getOrElse(some(2), 7), 2);
33 assert.equal(matchOption(some(2), { some: n => n + 1, none: () => 0 }), getOrElse(mapOption(some(2), n => n + 1), 0));
34});
36test('Option laws: map identity and composition, flatMap identities and associativity, getOrElse', () => {
37 for (let i = 0; i < N; i++) {
38 const rng = new Rng(SEED * 1000003 + i);
39 const o = genOpt(rng);
40 const f = genPlain(rng);
41 const g = genPlain(rng);
42 assert.deepEqual(mapOption(o, x => x), o);
43 assert.deepEqual(mapOption(mapOption(o, f), g), mapOption(o, x => g(f(x))));
44 const ff = genFn(rng);
45 const gg = genFn(rng);
46 assert.deepEqual(flatMapOption(flatMapOption(o, ff), gg), flatMapOption(o, x => flatMapOption(ff(x), gg)));
47 const a = genInt(rng);
48 assert.deepEqual(flatMapOption(some(a), ff), ff(a));
49 assert.deepEqual(flatMapOption(o, some), o);
50 if (o.tag === 'none') {
51 assert.equal(mapOption(o, f), none);
52 assert.equal(flatMapOption(o, ff), none);
53 }
54 const fallback = genInt(rng);
55 assert.equal(getOrElse(o, fallback), matchOption(o, { some: v => v, none: () => fallback }));
56 }
57});
59test('boundary: platform absence becomes Option, null stays a value, indexes do not wrap', () => {
60 assert.deepEqual(fromNullable(0), some(0));
61 assert.deepEqual(fromNullable(false), some(false));
62 assert.deepEqual(fromNullable(''), some(''));
63 assert.equal(fromNullable(undefined), none);
64 assert.deepEqual(fromNullable(null), some(null));
65 assert.equal(fromNullish(undefined), none);
66 assert.equal(fromNullish(null), none);
67 assert.deepEqual(fromNullish(0), some(0));
68 assert.deepEqual(fromNullish(false), some(false));
69 assert.deepEqual(fromNullish(''), some(''));
70 assert.equal(asOption(undefined), none);
71 assert.equal(asOption(none), none);
72 assert.deepEqual(asOption(some(1)), some(1));
73 assert.equal(unwrapOrAbsent(some(4)), 4);
74 assert.equal(unwrapOrAbsent(none), undefined);
75 assert.deepEqual(at([10, 20], 1), some(20));
76 assert.equal(at([10], 1), none);
77 assert.equal(at([10], -1), none);
78 assert.equal(at([10, 20], 1.5), none);
79 assert.equal(at([10, 20], Number.NaN), none);
80 const hole = [10, 20, 30];
81 hole[1] = undefined;
82 assert.equal(at(hole, 1), none);
83 assert.equal(atOr(hole, 1, 9), 9);
84 assert.deepEqual(at(Uint8Array.of(4, 5), 0), some(4));
85 assert.equal(atOr([1], 5, 9), 9);
86 assert.equal(atOr([1], 0, 9), 1);
87 assert.equal(atOr([1], -1, 9), 9);
88 assert.equal(atOr([1, 2], 1.5, 9), 9);
89 const table = new Map([['a', 1]]);
90 assert.deepEqual(mapGet(table, 'a'), some(1));
91 assert.equal(mapGet(table, 'b'), none);
92 assert.deepEqual(readKey({ a: 1 }, 'a'), some(1));
93 assert.equal(readKey({}, 'a'), none);
94 assert.deepEqual(jsonDecode('{"a":1}'), some({ a: 1 }));
95 assert.deepEqual(jsonDecode('null'), some(null));
96 assert.deepEqual(jsonDecode('0'), some(0));
97 assert.equal(jsonDecode('nope'), none);
98 assert.equal(jsonDecode(''), none);
99 const bytes = Uint8Array.of(0, 7, 255);
100 assert.equal(byteInRange(bytes, 0, 0, 0), true);
101 assert.equal(byteInRange(bytes, 1, 0, 6), false);
102 assert.equal(byteInRange(bytes, 1, 7, 7), true);
103 assert.equal(byteInRange(bytes, 3, 0, 255), false);
104 assert.equal(byteInRange(bytes, -1, 0, 255), false);
105 assert.equal(byteInRange(bytes, 1.5, 0, 255), false);
106});
108test('absence ratchet holds, and a rise or a silent drop is rejected', () => {
109 // Stryker's dry run instruments src/, so the committed baseline does not match that tree.
110 if (process.env.MUTATION !== '1') execFileSync(process.execPath, ['scripts/absence-ratchet.mjs'], { stdio: 'pipe' });
111 const base = { boundary: 'src/absent.ts', undefined: 3, optional: 2, chain: 1, coalesce: 1 };
112 assert.deepEqual(compareCounts({ ...base }, base), []);
113 assert.ok(compareCounts({ ...base, undefined: 4 }, base).some(line => line.includes('rose')));
114 assert.ok(compareCounts({ ...base, optional: 1 }, base).some(line => line.includes('fell')));
115 assert.ok(compareCounts({ ...base, boundary: 'src/other.ts' }, base).length > 0);
116});
Diagnostics6 git calls · 141 ms
CallStatusSizeTime0141 ms
discover200191 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=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 KiB116 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=112 git.response.bytes=11990 git.response.cache=EXPIRED git.response.duration_ms=114 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=2 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=10Open pack map
fetchtree:0200919 B · 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=919 git.response.cache=HIT git.response.duration_ms=3 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=5323b9266eea5ded637285d1c5320699f12be76eOpen pack map
fetchtree:02005.6 KiB · hit4 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=5724 git.response.cache=HIT git.response.duration_ms=4 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=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetch2001.7 KiB · hit16 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=16 git.response.bytes=1734 git.response.cache=HIT git.response.duration_ms=16 git.fetch.wants=e517ce1151ce041a9da35e7feccdc6b3059c22f9 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=1734 git.pkt.payload.max=1707 git.sideband.band1.bytes=1707 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
- a5e19c140b283cd0
- Request
- bd288eda…
- 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