"use strict"; // Zero-dependency browser calculation regressions (run: node tests/cache_outcomes.cjs). const assert = require("node:assert/strict"); const fs = require("node:fs"); const path = require("node:path"); const vm = require("node:vm"); const root = path.join(__dirname, ".."); const src = fs.readFileSync(path.join(root, "src/admin/web/app/runtime.js"), "utf8"); const begin = src.indexOf("function cacheOutcomeMix(deltas) {"); const end = src.indexOf("\nfunction renderCacheMix(mix) {", begin); assert.ok(begin >= 0 && end > begin, "production cacheOutcomeMix function exists"); const mix = vm.runInNewContext(`${src.slice(begin, end)}; cacheOutcomeMix;`); const result = mix([10, 4, 2, 3]); assert.deepEqual(Array.from(result.counts), [4, 2, 3, 1]); assert.deepEqual(Array.from(result.percentages), [40, 20, 30, 10]); assert.equal(result.requests, 10); assert.deepEqual(Array.from(mix([12, 0, 0, 0]).counts), [0, 0, 0, 12]); assert.deepEqual(Array.from(mix([9, 0, 0, 9]).counts), [0, 0, 9, 0]); for (const invalid of [[0, 0, 0, 0], [1, 2, 0, 0], [10, -1, 0, 0], [NaN, 0, 0, 0], [], null]) { assert.equal(mix(invalid), null, `invalid sample ${invalid} is not charted`); } const widgets = JSON.parse(fs.readFileSync(path.join(root, "src/admin/web/dashboard-widgets.json"), "utf8")); const defaults = JSON.parse(fs.readFileSync(path.join(root, "src/admin/web/dashboard-defaults.json"), "utf8")); assert.equal(widgets.filter(widget => widget.id === "cache_outcomes").length, 1); assert.equal(defaults.dashboard_widgets.filter(id => id === "cache_outcomes").length, 1); assert.equal(widgets.find(widget => widget.id === "cache_outcomes").default_hidden, true, "cache outcomes is opt-in"); assert.equal(defaults.dashboard_hidden_widgets.filter(id => id === "cache_outcomes").length, 1, "hidden in factory layout"); const html = fs.readFileSync(path.join(root, "src/admin/web/partials/overview-metrics.html"), "utf8"); assert.match(html, /data-dashboard-widget="cache_outcomes"/); for (const key of ["hit", "miss", "bypass", "other"]) { assert.match(html, new RegExp(`id="cache-mix-${key}"`)); assert.match(html, new RegExp(`id="cache-mix-${key}-percent"`)); } const strings = ["en_US", "pl_pl"].map(language => JSON.parse(fs.readFileSync(path.join(root, `langs/${language}.json`), "utf8")).translations); for (const translation of strings) { for (const key of ["cacheMixTitle", "cacheMixHelp", "cacheMixOther", "cacheMixSample", "cacheMixWaiting", "cacheMixAria", "h3_udp_receive_buffer_mib", "h3_udp_send_buffer_mib", "http3ApplyPolicy"]) { assert.ok(translation[key], `translation missing: ${key}`); } } console.log("Cache outcomes, manifest and locale regression checks passed.");