diff --git a/tests/js/extremes.test.js b/tests/js/extremes.test.js index 2dce9960..fbc5edac 100644 --- a/tests/js/extremes.test.js +++ b/tests/js/extremes.test.js @@ -1,8 +1,10 @@ /* Session extremes: the age clause, and the gap/restart split. Executes the shipped - noteSample / triple / acceptTriple / resetWindows / newEngineRun, sliced by line + noteSample / triple / acceptTriple / resetWindows / newEngineRun / poll, sliced by line range out of the page and CALLED -- defining a sliced function proves nothing. - pct() is stubbed (not under test); nowS is declared here because in the page it is - module-level and so invisible inside new Function. */ + pctn/pct are the SHIPPED formatters rather than a re-typed stand-in: a copy of a formatter + drifts from the real one, and an assertion on its output then silently means nothing. + nowS is declared here because in the page it is module-level, so it is invisible inside + new Function. */ const src = require("./page.js"); const L = src.split("\n"); let fails = 0, checks = 0; @@ -16,15 +18,15 @@ function lines(a, b) { if (j < 0) throw new Error("no end line: " + b); return L.slice(i, j + 1).join("\n"); } -const BLOCK = [ +const COMMON = [ "let nowS = 0;", - /* resetWindows() clears these module-level collaborators; they are not under - test, but it READS hist, so they have to exist for it to run at all. */ - "const hist = {}, moveHist = { up: [], down: [] };", - "let prev = null, prevAt = 0, accPrev = null;", 'const DASH = "\\u2013";', "const finite = (v) => v != null && isFinite(v);", - "const pct = (f, d = 1) => (f == null ? DASH : (f * 100).toFixed(d) + '%');", + /* resetWindows() clears these; they are not under test but it READS them. */ + "const hist = {}, moveHist = { up: [], down: [] };", + "let prev = null, prevAt = 0, accPrev = null;", + lines("function pctn(", "}"), + lines("const pct =", "const pct ="), lines("function dur(", "}"), lines("const RECENT_S =", "const RECENT_PREFILL_S"), lines("const extAt =", "let lastActiveC"), @@ -35,12 +37,15 @@ const BLOCK = [ lines("function resetWindows", "}"), lines("function newEngineRun", "}"), ].join("\n"); + function fresh() { const box = {}; - new Function("box", BLOCK + "\nbox.api={noteSample,triple,acceptTriple,resetWindows,newEngineRun,extAt,winAt,extAtS,setNow:(v)=>{nowS=v;}};")(box); + new Function("box", COMMON + "\nbox.api={noteSample,triple,acceptTriple,resetWindows,newEngineRun,extAt,winAt,extAtS,accPool,setNow:(v)=>{nowS=v;}};")(box); return box.api; } const F = (v) => v + " t/s"; + +async function main() { console.log("== the age clause appears only when the extreme stands alone =="); let b = fresh(); b.setNow(100); b.noteSample("dec", 1, 500, 15, true); @@ -52,11 +57,41 @@ out = b.triple("dec", 1, "best", F); ok(/best 500 t\/s · 50\.0 s ago$/.test(out), "extreme alone: age names the span (got " + JSON.stringify(out) + ")"); b.setNow(100.4); ok(!b.triple("dec", 1, "best", F).includes("ago"), "an extreme set 0.4s ago is current: no clause"); + console.log("== a stale extreme is distinguishable from a fresh one =="); b = fresh(); b.setNow(100); b.noteSample("dec", 1, 500, 15, false); b.setNow(1000); out = b.triple("dec", 1, "best", F); ok(out.includes("15m 00s ago"), "15-minute-old extreme says so (got " + JSON.stringify(out) + ")"); ok(out.includes("best 500"), "and still shows the figure itself"); + +console.log("== the stamp marks when the EXTREME was set, not when anything was sampled =="); +/* Re-sampling a key with a WORSE value must not refresh the stamp: the age belongs to the + figure on the left. Refreshing it on every sample makes every age read ~0 -- wrong output, + not missing output, which is the hardest kind of defect for a label to survive. */ +b = fresh(); b.setNow(100); b.noteSample("dec", 1, 500, 15, false); +b.setNow(200); b.noteSample("dec", 1, 400, 15, false); /* not a new extreme */ +b.setNow(260); +out = b.triple("dec", 1, "best", F); +ok(out.includes("best 500"), "the extreme is still the earlier, better figure (got " + JSON.stringify(out) + ")"); +ok(out.includes("2m 40s ago"), "age is measured from the extreme, not the last sample (got " + JSON.stringify(out) + ")"); +b.setNow(300); b.noteSample("dec", 1, 900, 15, false); /* a NEW extreme */ +b.setNow(330); +out = b.triple("dec", 1, "best", F); +ok(out.includes("best 900") && out.includes("30.0 s ago"), "a new extreme restamps (got " + JSON.stringify(out) + ")"); + +console.log("== acceptTriple's age clause is exercised on a LIVE extreme =="); +/* The restart case below clears extAt first, so acceptTriple returns at its guard and never + reaches the clause. Nothing else drove it, so the clause could be deleted unnoticed. */ +b = fresh(); b.setNow(100); b.noteSample("accept", 1, 0.75, 15, false); +b.setNow(100.5); +ok(!b.acceptTriple(1).includes("ago"), "an acceptance extreme set 0.5s ago is current: no clause (got " + JSON.stringify(b.acceptTriple(1)) + ")"); +b.setNow(400); +out = b.acceptTriple(1); +ok(/^highest 75% · 5m 00s ago$/.test(out), "once it stands alone it names its age (got " + JSON.stringify(out) + ")"); +b.accPool[1] = [{ dr: 10, da: 8 }, { dr: 10, da: 6 }]; +out = b.acceptTriple(1); +ok(out.includes("recent") && !out.includes("ago"), "with a ring: window figures, no age clause (got " + JSON.stringify(out) + ")"); + console.log("== a GAP keeps the extreme, and says how old it is =="); b = fresh(); b.setNow(100); b.noteSample("dec", 1, 500, 15, true); b.noteSample("accept", 1, 0.9, 15, false); b.setNow(400); b.resetWindows(); @@ -64,6 +99,7 @@ out = b.triple("dec", 1, "best", F); ok(out.includes("best 500"), "the session extreme survives a gap (got " + JSON.stringify(out) + ")"); ok(out.includes("5m 00s ago"), "and names its age (got " + JSON.stringify(out) + ")"); eq(Object.keys(b.extAt).length, 2, "extAt untouched by a gap"); + console.log("== a RESTART drops the extremes: a new process inherits nothing =="); b = fresh(); b.setNow(100); b.noteSample("dec", 1, 500, 15, true); b.noteSample("accept", 1, 0.9, 15, false); b.setNow(400); b.newEngineRun(); @@ -75,11 +111,66 @@ b.setNow(200); b.noteSample("dec", 1, 700, 15, false); out = b.triple("dec", 1, "best", F); ok(!out.includes("ago"), "a fresh extreme carries no age clause (got " + JSON.stringify(out) + ")"); ok(/best 700 t\/s$/.test(out), "and reads as the new run's own figure"); -console.log("== the age tracks the NEWER extreme, not the first =="); -b = fresh(); b.setNow(100); b.noteSample("dec", 1, 500, 15, false); -b.setNow(400); b.noteSample("dec", 1, 900, 15, false); b.setNow(460); -out = b.triple("dec", 1, "best", F); -ok(out.includes("best 900"), "the surviving figure is the newest extreme (got " + JSON.stringify(out) + ")"); -ok(out.includes("1m 00s ago"), "and its age is the newer stamp (got " + JSON.stringify(out) + ")"); -console.log(fails ? "\n" + fails + " FAILURE(S) of " + checks : "\nALL " + checks + " CHECKS PASS on the shipped extreme code"); -process.exit(fails ? 1 : 0); + +/* ------------------------------------------------------------------ poll() itself + * The restart DETECTION lives in poll(), not in newEngineRun(). Calling newEngineRun() directly + * proves only that the function clears things -- deleting the detection line in poll() would + * leave every such assertion green. So drive poll() with a scripted /stats sequence and assert + * what it decides. resetWindows / newEngineRun / update are replaced with counters here on + * purpose: this is testing the decision, not the clearing (covered above). */ +const POLL = lines("async function poll() {", "}"); +function makePoller(seq, clock) { + const body = COMMON + "\n" + [ + "let lastPollAt = 0;", + "const GAP_S = 60; let pollIdleMs = 500;", + "const calls = { run: 0, gap: 0, update: 0 };", + "function resetWindows() { calls.gap++; }", + "function newEngineRun() { calls.run++; }", + "function update() { calls.update++; }", + "function schedulePoll() {}", + "function pollCadence() { return 500; }", + "const els = {};", + "function $(id) { return els[id] || (els[id] = { hidden: false, className: \"\", classList: { remove() {} } }); }", + "let i = 0;", + "async function api() { const s = seq[Math.min(i++, seq.length - 1)]; return { ok: true, json: async () => JSON.parse(JSON.stringify(s)) }; }", + POLL, + "return { poll, calls };", + ].join("\n"); + return new Function("document", "performance", "Date", "seq", "clock", body)( + { hidden: false }, { now: () => 0 }, { now: () => clock.wall }, seq, clock); +} +const fixed = { wall: 1000 }; /* a fixed clock keeps the GAP path out of these assertions */ +console.log("== poll() detects a new engine process =="); +let p = makePoller([ + { started_at: 100, uptime_s: 10 }, /* first poll: baseline, clears nothing */ + { started_at: 100, uptime_s: 20 }, /* same process, uptime rising: nothing happens */ + { started_at: 200, uptime_s: 5 }, /* started_at moved: a new process */ +], fixed); +await p.poll(); eq(p.calls.run, 0, "the first poll is a baseline, not a restart"); +await p.poll(); eq(p.calls.run, 0, "same started_at with a rising uptime is the same process"); +await p.poll(); eq(p.calls.run, 1, "a changed started_at clears the extremes"); + +console.log("== poll() catches a restart that lands in the SAME second =="); +/* started_at is a whole-second clock, so a restart inside the same second leaves it unchanged. + Without the uptime comparison the dead process's extremes render WITH A TRUE AGE, which reads + as honest provenance and is worse than not labelling it at all. */ +p = makePoller([ + { started_at: 100, uptime_s: 40 }, + { started_at: 100, uptime_s: 2 }, /* same started_at, uptime went backwards */ +], fixed); +await p.poll(); await p.poll(); +eq(p.calls.run, 1, "a same-second restart is caught by the uptime comparison"); + +console.log("== poll() takes the GAP path, not the restart path, when only the clock moved =="); +const slow = { wall: 1000 }; +p = makePoller([{ started_at: 100, uptime_s: 10 }, { started_at: 100, uptime_s: 130 }], slow); +await p.poll(); +slow.wall = 1000 + 90 * 1000; /* 90s with nothing polled: a gap */ +await p.poll(); +eq(p.calls.gap, 1, "a long quiet span clears the windows"); +eq(p.calls.run, 0, "and is NOT read as a restart: the extremes survive it"); +} +main().then(() => { + console.log(fails ? "\n" + fails + " FAILURE(S) of " + checks : "\nALL " + checks + " CHECKS PASS on the shipped extreme code"); + process.exit(fails ? 1 : 0); +}); diff --git a/tests/js/poll_spark.test.js b/tests/js/poll_spark.test.js index ff657716..1d8ae413 100644 --- a/tests/js/poll_spark.test.js +++ b/tests/js/poll_spark.test.js @@ -85,7 +85,9 @@ console.log("== the prune is the SHIPPED setMetric, actually called =="); function shippedPrune(seed, nowSVal, value) { const Mm = { m: { n: { firstChild: { nodeValue: "" } }, line: { setAttribute: () => {} } } }; const hh = { m: seed.slice() }; - new Function("M", "hist", "nowS", "spark", "HIST_S", SETMETRIC + "\nsetMetric('m', " + value + ", 'x');\nreturn hist;")(Mm, hh, nowSVal, () => {}, HS); + /* v is passed as a parameter, not interpolated into the source: interpolating the string + "null" and the literal null produce the same text, so the null case would not be null. */ + new Function("M", "hist", "nowS", "spark", "HIST_S", "v", SETMETRIC + "\nsetMetric('m', v, 'x');\nreturn hist;")(Mm, hh, nowSVal, () => {}, HS, value); return hh.m; } let fast = []; for (let i = 0; i < 600; i++) fast.push({ t: i * 0.1, v: 1 }); @@ -99,7 +101,7 @@ eq(r[0].t, 3000 - HS, "the oldest survivor is the first sample inside the window let edge = []; for (let i = 0; i < 5; i++) edge.push({ t: 1000 + i, v: 1 }); eq(shippedPrune(edge, 1000 + HS, 1).length, 6, "a sample exactly at nowS-HIST_S survives (exclusive <)"); eq(shippedPrune(edge, 1001 + HS, 1).length, 5, "one second later that same sample is pruned"); -r = shippedPrune([{ t: 10, v: 1 }], 1005, "null"); +r = shippedPrune([{ t: 10, v: 1 }], 1005, null); eq(r.length, 1, "a null reading records nothing and prunes nothing"); eq(r[0].t, 10, "the earlier sample is untouched"); console.log(fails ? "\n" + fails + " FAILURE(S) of " + checks : "\nALL " + checks + " CHECKS PASS on the shipped page JS");