const assert = require("node:assert/strict"); const adaptive = require("./adaptive-brightness.js"); const defaults = adaptive.normalizeConfig(null); assert.deepEqual(defaults, {enabled: true, thresholds: [25, 100, 300], targets: {}}); for (const input of [null, [], "bad", {enabled: 0, thresholds: [25, 25, 300]}, {thresholds: [0, 2, 3]}, {thresholds: [1, 2, Infinity]}]) assert.deepEqual(adaptive.normalizeConfig(input), defaults); assert.deepEqual(adaptive.normalizeConfig({enabled: false, targets: {"ddc:DP-1": [0, -1, NaN, 1], invalid: [1, 1, 1, 1]}}), {enabled: false, thresholds: [25, 100, 300], targets: {"ddc:DP-1": [0, 0.5, 0.75, 1]}}); for (const [value, expected] of [[0, 0], [24, 0], [25, 1], [100, 2], [300, 3]]) assert.equal(adaptive.bucket(value, defaults.thresholds, -1), expected); assert.equal(adaptive.bucket(27, defaults.thresholds, 0), 0); assert.equal(adaptive.bucket(28, defaults.thresholds, 0), 1); assert.equal(adaptive.bucket(22, defaults.thresholds, 1), 1); assert.equal(adaptive.bucket(21, defaults.thresholds, 1), 0); assert.equal(adaptive.bucket(109, defaults.thresholds, 1), 1); assert.equal(adaptive.bucket(110, defaults.thresholds, 1), 2); assert.equal(adaptive.bucket(330, defaults.thresholds, 0), 3); assert.equal(adaptive.bucket(null, defaults.thresholds, -1), -1); assert.equal(adaptive.smooth(null, 100), 100); assert.equal(adaptive.smooth(100, 200), 125); assert.deepEqual(adaptive.editThreshold([25, 100, 300], 1, 1), [25, 26, 300]); assert.deepEqual(adaptive.editThreshold([25, 100, 300], 0, 900), [99, 100, 300]); assert.deepEqual(adaptive.editThreshold([25, 100, 300], 2, 20), [25, 100, 101]); for (const [index, value] of [[-1, 1], [3, 1], [0, 1.2], [1, Infinity], [2, 0], [1, "50"]]) assert.equal(adaptive.editThreshold(defaults.thresholds, index, value), null); for (const [from, to] of [[0.2, 0.9], [0.9, 0.2], [0.5, 0.5]]) { assert.equal(adaptive.easedLevel(from, to, 0), from); assert.ok(Math.abs(adaptive.easedLevel(from, to, 1) - to) < 1e-12); assert.ok(Math.abs(adaptive.easedLevel(from, to, 0.5) - (from + to) / 2) < 1e-12); assert.equal(adaptive.easedLevel(from, to, -1), from); assert.ok(Math.abs(adaptive.easedLevel(from, to, 2) - to) < 1e-12); let previous = from; for (let step = 1; step <= 20; step++) { const level = adaptive.easedLevel(from, to, step / 20); assert.ok(to >= from ? level >= previous : level <= previous); assert.ok(level >= Math.min(from, to) - 1e-12 && level <= Math.max(from, to) + 1e-12); previous = level; } assert.ok(Math.abs(adaptive.easedLevel(from, to, 0.1) - from) <= Math.abs(to - from) * 0.1); } const learned = adaptive.learn(defaults, "ddc:DP-1", 1, 3 / 7); assert.equal(adaptive.target(learned, "ddc:DP-1", 1), 3 / 7); assert.equal(adaptive.target(learned, "ddc:DP-2", 1), 0.5); assert.deepEqual(defaults.targets, {}); assert.deepEqual(adaptive.learn(learned, "ddc:DP-1", -1, 0.9), learned); assert.deepEqual(adaptive.normalizeConfig(JSON.parse(JSON.stringify(learned))), learned); assert.deepEqual(adaptive.buckets(learned).map(row => row.label), ["dark", "dim", "bright", "sunlight"]); assert.equal(adaptive.buckets(learned)[1].targets["ddc:DP-1"], 3 / 7); console.log("adaptive-brightness.test.js: PASS (pure config, thresholds, EMA, hysteresis, per-display learning)"); import("./adaptive-brightness-runtime.test.mjs").catch(error => { console.error(error); process.exitCode = 1; });