// Unit tests for the Settings destination's pure logic (src/settings.js): the // workspace validation rules, the Save-enabled rule, the `Saved workspace:` line, // the connection status card's four states and the two persisted keys. No DOM, no // socket: `node --test test/settings.test.mjs` proves the logic the page wires up, // so the browser run only has to prove the rendering. // // The wording is asserted against the Android client's own strings // (ui/screens/SettingsScreen.kt, ui/AppViewModel.kt, data/SettingsRepository.kt); // the last test reads those sources so a rename there cannot silently drift past // this one. import { test } from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { SERVER_URL_HELPER, SERVER_URL_PLACEHOLDER, SERVER_URL_STORAGE_KEY, TEMPLATES_CARD_LABEL, TEMPLATES_CARD_SECONDARY, TRUST_BODY, TRUST_TITLE, WORKSPACE_ERROR, WORKSPACE_HELPER, WORKSPACE_PLACEHOLDER, WORKSPACE_STORAGE_KEY, canSaveWorkspace, connectionActionKind, connectionActions, connectionCard, readServerUrl, readWorkspace, savedWorkspaceLine, validateWorkspacePath, workspaceErrorText, writeServerUrl, writeWorkspace, } from "../src/settings.js"; import { bannerState, connectingLabel } from "../src/connection.js"; const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..", ".."); const source = (rel) => readFileSync(resolve(ROOT, rel), "utf8"); const androidSrc = (rel) => source(`android/app/src/main/java/net/awan/pipelines/${rel}`); /** A minimal in-memory Storage, plus a throwing one, for the persistence tests. */ function memoryStorage(seed = {}) { const map = new Map(Object.entries(seed)); return { getItem: (key) => (map.has(key) ? map.get(key) : null), setItem: (key, value) => map.set(key, String(value)), key: (i) => [...map.keys()][i] ?? null, get length() { return map.size; }, }; } // --------------------------------------------------------------------------- // The exact copy constants // --------------------------------------------------------------------------- test("the settings copy is the Android strings verbatim", () => { assert.equal(SERVER_URL_PLACEHOLDER, "ws://192.168.1.20:8081/connect"); assert.equal( SERVER_URL_HELPER, "ws:// on a trusted LAN/VPN, or wss:// through a proxy/VPN. No authentication is used.", ); assert.equal(WORKSPACE_PLACEHOLDER, "/Users/you/repos/pipelines"); assert.equal( WORKSPACE_HELPER, "An absolute directory that already exists on the execution server. For Docker, " + "mount the host directory at the same absolute path with its owner's UID/GID. " + "This is not the phone's storage.", ); assert.equal( WORKSPACE_ERROR, "The workspace must be an absolute path that exists on the execution server.", ); assert.equal(TRUST_TITLE, "Trusted LAN/VPN only"); assert.equal( TRUST_BODY, "The Pipelines API has no authentication and is not internet-safe. Do not " + "expose it to untrusted networks, and never enter provider credentials here \u2014 " + "the app stores none.", ); assert.equal(TEMPLATES_CARD_LABEL, "Browse templates"); assert.equal(TEMPLATES_CARD_SECONDARY, "Reusable pipeline definitions offered by the server."); }); // --------------------------------------------------------------------------- // validateWorkspacePath / workspaceErrorText — AppViewModel.validateWorkspace // --------------------------------------------------------------------------- test("validateWorkspacePath accepts a blank draft and an absolute path", () => { assert.equal(validateWorkspacePath(""), null); assert.equal(validateWorkspacePath(" "), null); assert.equal(validateWorkspacePath(undefined), null); assert.equal(validateWorkspacePath(null), null); assert.equal(validateWorkspacePath("/Users/you/repos/pipelines"), null); assert.equal(validateWorkspacePath(" /srv/pipelines "), null); }); test("validateWorkspacePath names the failure for a relative or bare path", () => { assert.equal(validateWorkspacePath("repos/pipelines"), WORKSPACE_ERROR); assert.equal(validateWorkspacePath("~/pipelines"), WORKSPACE_ERROR); assert.equal(validateWorkspacePath("C:\\pipelines"), WORKSPACE_ERROR); assert.equal(validateWorkspacePath(" relative "), WORKSPACE_ERROR); }); test("workspaceErrorText is the error or empty (Android's `workspaceError ?: helper`)", () => { assert.equal(workspaceErrorText("repos/pipelines"), WORKSPACE_ERROR); assert.equal(workspaceErrorText(""), ""); assert.equal(workspaceErrorText("/srv/pipelines"), ""); }); // --------------------------------------------------------------------------- // canSaveWorkspace — the Save button's enabled state // --------------------------------------------------------------------------- test("canSaveWorkspace enables Save only for a trimmed absolute path", () => { assert.equal(canSaveWorkspace("/srv/pipelines"), true); assert.equal(canSaveWorkspace(" /srv/pipelines "), true); // Deliberately stricter than Android (which also enables Save on a blank // field): the browser has nothing to save for a blank path. assert.equal(canSaveWorkspace(""), false); assert.equal(canSaveWorkspace(" "), false); assert.equal(canSaveWorkspace("repos/pipelines"), false); assert.equal(canSaveWorkspace(undefined), false); }); test("canSaveWorkspace derives from validateWorkspacePath, so the rule has one source", () => { for (const value of ["", " ", "repos/pipelines", "~/x", "C:\\x", "/srv/x", " /srv/x ", "/", "/a/b"]) { const trimmed = String(value).trim(); assert.equal( canSaveWorkspace(value), trimmed !== "" && validateWorkspacePath(value) === null, `disagreement for ${JSON.stringify(value)}`, ); } }); // --------------------------------------------------------------------------- // savedWorkspaceLine // --------------------------------------------------------------------------- test("savedWorkspaceLine reads the path, or (none) when unset", () => { assert.equal(savedWorkspaceLine("/Users/you/repos/pipelines"), "Saved workspace: /Users/you/repos/pipelines"); assert.equal(savedWorkspaceLine(" /srv/x "), "Saved workspace: /srv/x"); assert.equal(savedWorkspaceLine(""), "Saved workspace: (none)"); assert.equal(savedWorkspaceLine(" "), "Saved workspace: (none)"); assert.equal(savedWorkspaceLine(null), "Saved workspace: (none)"); }); // --------------------------------------------------------------------------- // connectionCard — SettingsScreen.kt's ConnectionStatusCard // --------------------------------------------------------------------------- test("connectionCard idle reads Not connected", () => { assert.deepEqual(connectionCard({ kind: "idle" }), ["Not connected"]); assert.deepEqual(connectionCard(undefined), ["Not connected"]); assert.deepEqual(connectionCard({ kind: "something-else" }), ["Not connected"]); }); test("connectionCard connecting names the attempt", () => { assert.deepEqual(connectionCard({ kind: "connecting", attempt: 4 }), ["Connecting (attempt 4)\u2026"]); assert.deepEqual(connectionCard({ kind: "connecting" }), ["Connecting (attempt 0)\u2026"]); }); test("the connecting line is the banner's own, so the app sentence has one source", () => { for (const attempt of [0, 1, 7]) { const state = { kind: "connecting", attempt }; assert.deepEqual(connectionCard(state), [bannerState(state).text]); } assert.equal(connectingLabel(3), "Connecting (attempt 3)\u2026"); }); test("connectionCard connected shows the build and server time when present", () => { assert.deepEqual( connectionCard( { kind: "connected" }, { version: "v1.2.3", commit: "abc123", serverTime: "2026-10-19T08:00:00Z" }, ), ["Connected", "Server build v1.2.3 (abc123)", "Server time 2026-10-19T08:00:00Z"], ); // Missing fields are tolerated: no build line, no time line. assert.deepEqual(connectionCard({ kind: "connected" }, {}), ["Connected"]); assert.deepEqual(connectionCard({ kind: "connected" }, { version: "v1", serverTime: " " }), [ "Connected", "Server build v1", ]); }); test("connectionCard disconnected carries the reason and the retry suffix", () => { assert.deepEqual( connectionCard({ kind: "disconnected", reason: "Connection closed (1006)", retrying: true }), ["Disconnected", "Connection closed (1006)", "Retrying with backoff\u2026"], ); assert.deepEqual( connectionCard({ kind: "disconnected", reason: "Connection closed (1000)", retrying: false }), ["Disconnected", "Connection closed (1000)"], ); // A blank reason renders no empty line; retrying alone still shows. assert.deepEqual(connectionCard({ kind: "disconnected", reason: " ", retrying: true }), [ "Disconnected", "Retrying with backoff\u2026", ]); }); // --------------------------------------------------------------------------- // connectionActions — SettingsScreen.kt's `active` branch // --------------------------------------------------------------------------- test("connectionActions emphasises Disconnect while connected or connecting", () => { assert.deepEqual(connectionActions({ kind: "connected" }), { primary: "Disconnect", secondary: "Connect" }); assert.deepEqual(connectionActions({ kind: "connecting", attempt: 2 }), { primary: "Disconnect", secondary: "Connect", }); }); test("connectionActions emphasises Connect and offers Reconnect otherwise", () => { assert.deepEqual(connectionActions({ kind: "idle" }), { primary: "Connect", secondary: "Reconnect" }); assert.deepEqual(connectionActions({ kind: "disconnected", reason: "x" }), { primary: "Connect", secondary: "Reconnect", }); assert.deepEqual(connectionActions(undefined), { primary: "Connect", secondary: "Reconnect" }); }); test("connectionActionKind keeps Connect connect-only (never the header's toggle)", () => { // Android's connect() validates + persists + opens; only disconnect() closes. assert.equal(connectionActionKind("Connect"), "connect"); assert.equal(connectionActionKind("Reconnect"), "reconnect"); assert.equal(connectionActionKind("Disconnect"), "disconnect"); // Any unknown label is treated as a connect, never as a disconnect. assert.equal(connectionActionKind(""), "connect"); assert.equal(connectionActionKind(undefined), "connect"); for (const state of [{ kind: "connected" }, { kind: "connecting" }, { kind: "idle" }]) { const { primary, secondary } = connectionActions(state); const kinds = [connectionActionKind(primary), connectionActionKind(secondary)]; assert.deepEqual(kinds, state.kind === "idle" ? ["connect", "reconnect"] : ["disconnect", "connect"]); } }); // --------------------------------------------------------------------------- // Persisted keys round-trip // --------------------------------------------------------------------------- test("the two persisted keys are the exact, stable localStorage keys", () => { assert.equal(SERVER_URL_STORAGE_KEY, "pipelines.web.serverUrl.v2"); assert.equal(WORKSPACE_STORAGE_KEY, "pipelines.web.workspace.v1"); }); test("writeWorkspace trims and reads back; readWorkspace is tolerant", () => { const storage = memoryStorage(); assert.equal(writeWorkspace(storage, " /srv/pipelines "), "/srv/pipelines"); assert.equal(storage.getItem(WORKSPACE_STORAGE_KEY), "/srv/pipelines"); assert.equal(storage.length, 1, "only the workspace key is written"); assert.equal(readWorkspace(storage), "/srv/pipelines"); // Blank reads back as "" (the stored value), never throwing. writeWorkspace(storage, " "); assert.equal(readWorkspace(storage), ""); assert.equal(storage.length, 1); }); test("the server-URL key round-trips trimmed, under its own key only", () => { const storage = memoryStorage(); assert.equal(writeServerUrl(storage, " ws://127.0.0.1:8080/connect "), "ws://127.0.0.1:8080/connect"); assert.equal(storage.getItem(SERVER_URL_STORAGE_KEY), "ws://127.0.0.1:8080/connect"); assert.equal(readServerUrl(storage), "ws://127.0.0.1:8080/connect"); assert.equal(storage.length, 1, "only the server-URL key is written"); // The two keys are independent: writing one never disturbs the other. writeWorkspace(storage, "/srv/pipelines"); assert.equal(storage.length, 2); assert.equal(readServerUrl(storage), "ws://127.0.0.1:8080/connect"); assert.equal(readWorkspace(storage), "/srv/pipelines"); }); test("readServerUrl is blank on a missing/unreadable storage and never throws", () => { assert.equal(readServerUrl(memoryStorage()), ""); assert.equal(readServerUrl(undefined), ""); assert.equal(readServerUrl(null), ""); const throwing = { getItem() { throw new Error("denied"); }, setItem() { throw new Error("denied"); }, }; assert.equal(readServerUrl(throwing), ""); assert.equal(writeServerUrl(throwing, " ws://host:1/connect "), "ws://host:1/connect"); }); test("readWorkspace is blank on a missing/unreadable storage and never throws", () => { assert.equal(readWorkspace(memoryStorage()), ""); assert.equal(readWorkspace(undefined), ""); assert.equal(readWorkspace(null), ""); const throwing = { getItem() { throw new Error("denied"); }, setItem() { throw new Error("denied"); }, }; assert.equal(readWorkspace(throwing), ""); assert.equal(writeWorkspace(throwing, "/srv/x"), "/srv/x"); }); // --------------------------------------------------------------------------- // Parity with the Android sources // --------------------------------------------------------------------------- test("the wording still matches the Android client's sources", () => { const settings = androidSrc("ui/screens/SettingsScreen.kt"); for (const line of [ 'Text("Connection"', 'Text("Server URL")', 'Text("ws://192.168.1.20:8081/connect")', "ws:// on a trusted LAN/VPN, or wss:// through a proxy/VPN. No authentication is used.", 'Text("Workspace"', 'Text("Workspace path (on the server)")', 'Text("/Users/you/repos/pipelines")', "An absolute directory that already exists on the execution server. For Docker, ", "This is not the phone's storage.", 'Text("Save workspace")', '"Trusted LAN/VPN only"', "The Pipelines API has no authentication and is not internet-safe. Do not ", "never enter provider credentials here \\u2014 ", 'Text("Browse templates"', "Reusable pipeline definitions offered by the server.", 'Text("Disconnected"', 'Text("Retrying with backoff\\u2026")', 'Text("Connecting (attempt ${state.attempt})\\u2026")', 'Text("Connected"', ]) { assert.ok(settings.includes(line), `SettingsScreen.kt is missing: ${line}`); } const vm = androidSrc("ui/AppViewModel.kt"); assert.ok( vm.includes("The workspace must be an absolute path that exists on the execution server."), "AppViewModel.kt workspace error", ); assert.ok(vm.includes("!trimmed.startsWith(\"/\")"), "AppViewModel.kt absolute-path rule"); // The two Android preference keys the browser analogues stand in for. const repo = androidSrc("data/SettingsRepository.kt"); assert.ok(repo.includes('const val KEY_SERVER_URL = "server_url"'), "KEY_SERVER_URL"); assert.ok(repo.includes('const val KEY_WORKSPACE = "workspace"'), "KEY_WORKSPACE"); // Only non-secret preferences are stored (no credentials). assert.ok(repo.includes("No credentials or tokens are ever stored"), "no-secrets note"); });