| 1 | import assert from "node:assert/strict"; |
| 2 | import test from "node:test"; |
| 3 | import { collectTranscriptPerformance, decideTranscriptPerformance, formatPerformanceSummary, needsBoundedRetry, TRANSCRIPT_PHASES } from "./transcript-performance.mjs"; |
| 4 | |
| 5 | function attempt(ordinal, overrides = {}) { |
| 6 | const maxima = overrides.maxima ?? {}; |
| 7 | return { |
| 8 | attempt: ordinal, |
| 9 | inputCount: overrides.inputCount ?? 30, |
| 10 | inputP95: overrides.inputP95 ?? 80, |
| 11 | longTaskSupported: true, |
| 12 | errors: overrides.errors ?? [], |
| 13 | phases: Object.fromEntries(TRANSCRIPT_PHASES.map(phase => [phase, { |
| 14 | elapsedMs: 100, |
| 15 | longTasks: [], |
| 16 | longTaskMax: maxima[phase] ?? 100, |
| 17 | }])), |
| 18 | }; |
| 19 | } |
| 20 | |
| 21 | test("a passing first attempt is not retried", async () => { |
| 22 | const first = attempt(1); |
| 23 | assert.equal(needsBoundedRetry(first), false); |
| 24 | assert.equal(decideTranscriptPerformance([first]).status, "passed-first-attempt"); |
| 25 | const calls = []; |
| 26 | const result = await collectTranscriptPerformance(async ordinal => { calls.push(ordinal); return attempt(ordinal); }); |
| 27 | assert.deepEqual(calls, [1]); |
| 28 | assert.equal(result.decision.status, "passed-first-attempt"); |
| 29 | }); |
| 30 | |
| 31 | test("558/420/430 passes after bounded retry and keeps the first sample", () => { |
| 32 | const attempts = [attempt(1, { maxima: { historyPaging: 558 } }), attempt(2, { maxima: { historyPaging: 420 } }), attempt(3, { maxima: { historyPaging: 430 } })]; |
| 33 | const decision = decideTranscriptPerformance(attempts); |
| 34 | assert.equal(decision.status, "passed-after-bounded-retry"); |
| 35 | assert.equal(decision.medians.historyPaging, 430); |
| 36 | assert.equal(decision.inputP95Median, 80); |
| 37 | assert.equal(attempts[0].phases.historyPaging.longTaskMax, 558); |
| 38 | assert.match(formatPerformanceSummary("chromium", 1000, decision, attempts), /#1 \[.*historyPaging=558ms.*#3 \[.*historyPaging=430ms/); |
| 39 | }); |
| 40 | |
| 41 | test("558/520/430 remains a sustained failure", () => { |
| 42 | const decision = decideTranscriptPerformance([ |
| 43 | attempt(1, { maxima: { streaming: 558 } }), |
| 44 | attempt(2, { maxima: { streaming: 520 } }), |
| 45 | attempt(3, { maxima: { streaming: 430 } }), |
| 46 | ]); |
| 47 | assert.equal(decision.status, "failed-sustained-regression"); |
| 48 | assert.equal(decision.medians.streaming, 520); |
| 49 | }); |
| 50 | |
| 51 | test("phases are judged independently", () => { |
| 52 | const decision = decideTranscriptPerformance([ |
| 53 | attempt(1, { maxima: { initialLoad: 530, input: 540 } }), |
| 54 | attempt(2, { maxima: { initialLoad: 400, input: 550 } }), |
| 55 | attempt(3, { maxima: { initialLoad: 420, input: 410 } }), |
| 56 | ]); |
| 57 | assert.equal(decision.medians.initialLoad, 420); |
| 58 | assert.equal(decision.medians.input, 540); |
| 59 | assert.equal(decision.passed, false); |
| 60 | }); |
| 61 | |
| 62 | test("functional errors and missing samples fail immediately", () => { |
| 63 | assert.throws(() => needsBoundedRetry(attempt(1, { errors: ["page error"] })), /browser errors/); |
| 64 | const incomplete = attempt(1); |
| 65 | delete incomplete.phases.streaming; |
| 66 | assert.throws(() => needsBoundedRetry(incomplete), /missing streaming/); |
| 67 | assert.throws(() => decideTranscriptPerformance([attempt(1, { maxima: { historyPaging: 558 } })]), /exactly two retries/); |
| 68 | }); |
| 69 | |
| 70 | test("input P95 uses the same bounded median policy as long tasks", () => { |
| 71 | const recovered = decideTranscriptPerformance([ |
| 72 | attempt(1, { inputP95: 302 }), |
| 73 | attempt(2, { inputP95: 110 }), |
| 74 | attempt(3, { inputP95: 120 }), |
| 75 | ]); |
| 76 | assert.equal(recovered.status, "passed-after-bounded-retry"); |
| 77 | assert.equal(recovered.inputP95Median, 120); |
| 78 | |
| 79 | const sustained = decideTranscriptPerformance([ |
| 80 | attempt(1, { inputP95: 302 }), |
| 81 | attempt(2, { inputP95: 220 }), |
| 82 | attempt(3, { inputP95: 120 }), |
| 83 | ]); |
| 84 | assert.equal(sustained.status, "failed-sustained-regression"); |
| 85 | assert.equal(sustained.inputP95Median, 220); |
| 86 | }); |
| 87 | |
| 88 | test("bounded collection makes exactly two retries after a long-task exceedance", async () => { |
| 89 | const calls = []; |
| 90 | const result = await collectTranscriptPerformance(async ordinal => { |
| 91 | calls.push(ordinal); |
| 92 | return attempt(ordinal, { maxima: { historyPaging: ordinal === 1 ? 558 : 420 } }); |
| 93 | }); |
| 94 | assert.deepEqual(calls, [1, 2, 3]); |
| 95 | assert.equal(result.decision.status, "passed-after-bounded-retry"); |
| 96 | const functionalCalls = []; |
| 97 | await assert.rejects(() => collectTranscriptPerformance(async ordinal => { |
| 98 | functionalCalls.push(ordinal); |
| 99 | throw new Error("functional failure"); |
| 100 | }), /functional failure/); |
| 101 | assert.deepEqual(functionalCalls, [1]); |
| 102 | |
| 103 | const functionalAttemptCalls = []; |
| 104 | await assert.rejects(() => collectTranscriptPerformance(async ordinal => { |
| 105 | functionalAttemptCalls.push(ordinal); |
| 106 | return attempt(ordinal, { |
| 107 | maxima: { historyPaging: ordinal === 1 ? 558 : 420 }, |
| 108 | errors: ordinal === 2 ? ["page error"] : [], |
| 109 | }); |
| 110 | }), /browser errors/); |
| 111 | assert.deepEqual(functionalAttemptCalls, [1, 2]); |
| 112 | |
| 113 | const inputCalls = []; |
| 114 | const inputResult = await collectTranscriptPerformance(async ordinal => { |
| 115 | inputCalls.push(ordinal); |
| 116 | return attempt(ordinal, { inputP95: ordinal === 1 ? 302 : 120 }); |
| 117 | }); |
| 118 | assert.deepEqual(inputCalls, [1, 2, 3]); |
| 119 | assert.equal(inputResult.decision.status, "passed-after-bounded-retry"); |
| 120 | }); |
| 121 |