Remember the interval and the pinned rows across reloads (closes #212)
check / check (push) Successful in 4m26s

The page keeps the interval and the names of the pinned WAN hosts in
localStorage and starts with them on the next load. A browser with no
usable storage, or a saved value the page cannot use, starts at 3s with
datavi.be pinned. The interval menu's options are now built from one
list, so the saved interval can be marked selected.

Model: opus-5-5
This commit is contained in:
clawbot committed 2026-11-19 06:28:41 +00:00
1 parent dd119a0e46
commit 1e85a210f5
3 files changed
+175 -13

No files matched your search

+91
View File
@@ -17,6 +17,10 @@ import {
log,
measureLatency,
Reporter,
savedInterval,
savedPinnedNames,
saveInterval,
savePinnedNames,
tick,
updateClocks,
updateSummary,
@@ -1054,3 +1058,90 @@ for (const { timedOut, unreachable = 0, answered, latency, health } of [
assert.equal(state.healthStatus(), health);
});
}
// Gives test t a browser storage that keeps its values in saved, or, with
// saved null, one that throws on every read and write, as a browser with
// storage turned off does. It is removed when the test ends.
function mockStorage(t, saved) {
const fail = () => {
throw new Error("The operation is insecure.");
};
globalThis.localStorage =
saved === null
? { getItem: fail, setItem: fail }
: {
getItem: (key) => saved[key] ?? null,
setItem: (key, value) => {
saved[key] = value;
},
};
t.after(() => delete globalThis.localStorage);
}
// The interval menu's selected option in the page buildUI draws.
function selectedInterval(t) {
const app = {};
t.mock.method(globalThis.document, "getElementById", () => app);
buildUI(new AppState([]));
return app.innerHTML.match(/<option value="(\d+)" selected>/)[1];
}
// The names of the pinned WAN targets in state.
function pinnedNames(state) {
return state.wan.filter((h) => h.pinned).map((h) => h.name);
}
test("after the user sets the interval to 10s, unpins datavi.be and pins GitHub, a reload starts at 10s with only GitHub pinned", (t) => {
mockStorage(t, {});
CONFIG.updateInterval = 3000;
const before = new AppState([], savedPinnedNames());
saveInterval(10000);
for (const host of before.wan) host.pinned = host.name === "GitHub";
savePinnedNames(before);
CONFIG.updateInterval = savedInterval();
const after = new AppState([], savedPinnedNames());
assert.equal(CONFIG.updateInterval, 10000);
assert.equal(selectedInterval(t), "10000");
assert.deepEqual(pinnedNames(after), ["GitHub"]);
});
test("after the user unpins every target, a reload starts with none pinned", (t) => {
mockStorage(t, {});
const before = new AppState([], savedPinnedNames());
for (const host of before.wan) host.pinned = false;
savePinnedNames(before);
const after = new AppState([], savedPinnedNames());
assert.deepEqual(pinnedNames(after), []);
});
for (const [browser, saved] of [
["that has saved nothing", {}],
[
"whose saved interval is not in the menu and whose saved pins are not a list of names",
{ "netwatch-interval": "7000", "netwatch-pinned": '{"GitHub":true}' },
],
["whose saved pins are not JSON", { "netwatch-pinned": "GitHub" }],
["with storage turned off", null],
]) {
test(`a browser ${browser} starts at 3s with only datavi.be pinned`, (t) => {
mockStorage(t, saved);
CONFIG.updateInterval = 3000;
CONFIG.updateInterval = savedInterval();
const state = new AppState([], savedPinnedNames());
assert.equal(CONFIG.updateInterval, 3000);
assert.equal(selectedInterval(t), "3000");
assert.deepEqual(pinnedNames(state), ["datavi.be"]);
});
}
test("in a browser with storage turned off, changing the interval or a pin throws nothing", (t) => {
mockStorage(t, null);
saveInterval(10000);
savePinnedNames(new AppState([]));
});