diff --git a/test/unit/main.test.js b/test/unit/main.test.js index 7e7f0e2..6e1e3bb 100644 --- a/test/unit/main.test.js +++ b/test/unit/main.test.js @@ -1107,6 +1107,17 @@ test("after the user sets the interval to 10s, unpins datavi.be and pins GitHub, assert.deepEqual(pinnedNames(after), ["GitHub"]); }); +test("a reload with Google and GitHub pinned starts with them at the top, by name", (t) => { + mockStorage(t, { "netwatch-pinned": '["Google","GitHub"]' }); + + const state = new AppState([], savedPinnedNames()); + + assert.deepEqual( + state.wan.slice(0, 2).map((h) => h.name), + ["GitHub", "Google"], + ); +}); + test("after the user unpins every target, a reload starts with none pinned", (t) => { mockStorage(t, {}); const before = new AppState([], savedPinnedNames()); diff --git a/web/src/main.js b/web/src/main.js index 6ad47ed..dc5fb9b 100644 --- a/web/src/main.js +++ b/web/src/main.js @@ -358,8 +358,10 @@ export class HostState { export class AppState { // pinnedNames: the names of the WAN hosts to pin. constructor(localHosts, pinnedNames = DEFAULT_PINNED) { - this.wan = WAN_HOSTS.map( - (h) => new HostState(h, pinnedNames.includes(h.name)), + this.wan = sortedWAN( + WAN_HOSTS.map( + (h) => new HostState(h, pinnedNames.includes(h.name)), + ), ); this.local = localHosts.map((h) => new HostState(h)); this.paused = false; @@ -1269,21 +1271,25 @@ function appendDebugLogLine(entry) { // --- Sorting ----------------------------------------------------------------- -// Sort WAN hosts: pinned hosts first (alphabetically), then unpinned hosts -// by last latency ascending, with unreachable hosts at the bottom. -function sortAndRebuildWAN(state) { - log.debug("WAN hosts re-sorted"); - const pinned = state.wan +// The WAN hosts sorted: pinned hosts first (alphabetically), then unpinned +// hosts by last latency ascending, with unreachable hosts at the bottom. +function sortedWAN(hosts) { + const pinned = hosts .filter((h) => h.pinned) .sort((a, b) => a.name.localeCompare(b.name)); - const rest = state.wan.filter((h) => !h.pinned); + const rest = hosts.filter((h) => !h.pinned); rest.sort((a, b) => { if (a.lastLatency === null && b.lastLatency === null) return 0; if (a.lastLatency === null) return 1; if (b.lastLatency === null) return -1; return a.lastLatency - b.lastLatency; }); - state.wan = [...pinned, ...rest]; + return [...pinned, ...rest]; +} + +function sortAndRebuildWAN(state) { + log.debug("WAN hosts re-sorted"); + state.wan = sortedWAN(state.wan); const container = document.getElementById("wan-hosts"); container.innerHTML = wanRowsHTML(state.wan);