Files
pipelines/web/test/connection.test.mjs
T

233 lines
11 KiB
JavaScript

// Unit tests for the pure connection helpers (src/connection.js): the banner
// state derivation, the return-to-foreground policy, the ping-reply protocol
// comparison, the server build label and the live badge. No DOM, no socket, no
// timer: `node --test test/connection.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
// (ConnectionBanner.kt, ConnectionRecovery.kt, SettingsScreen.kt,
// LiveBadge.kt/RunStream.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 {
ForegroundAction,
IDLE_BANNER_TEXT,
RETRYING_SUFFIX,
RunStream,
bannerState,
foregroundAction,
liveBadgeState,
protocolMismatch,
serverBuildLabel,
stateBadge,
} 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}`);
// ---------------------------------------------------------------------------
// bannerState
// ---------------------------------------------------------------------------
test("idle banner uses the app's settings wording and offers Reconnect with an endpoint", () => {
const withEndpoint = bannerState({ kind: "idle", attempt: 0, reason: "", retrying: false }, { hasEndpoint: true });
assert.equal(withEndpoint.tone, "idle");
assert.equal(withEndpoint.text, "Not connected. Enter the server URL in Settings, then connect.");
assert.equal(withEndpoint.action, "Reconnect");
const without = bannerState({ kind: "idle" }, { hasEndpoint: false });
assert.equal(without.text, IDLE_BANNER_TEXT);
assert.equal(without.action, null);
});
test("connecting banner names the attempt and offers no action", () => {
const banner = bannerState({ kind: "connecting", attempt: 4, reason: "", retrying: false }, { hasEndpoint: true });
assert.equal(banner.tone, "connecting");
assert.equal(banner.text, "Connecting (attempt 4)\u2026");
assert.equal(banner.action, null);
});
test("disconnected banner carries the reason, the retry suffix and Reconnect", () => {
const retrying = bannerState(
{ kind: "disconnected", attempt: 2, reason: "Connection closed (1006)", retrying: true },
{ hasEndpoint: true },
);
assert.equal(retrying.tone, "error");
assert.equal(retrying.text, `Disconnected: Connection closed (1006)${RETRYING_SUFFIX}`);
assert.equal(retrying.action, "Reconnect");
const settled = bannerState(
{ kind: "disconnected", reason: "Connection closed (1000)", retrying: false },
{ hasEndpoint: true },
);
assert.equal(settled.text, "Disconnected: Connection closed (1000)");
});
test("disconnected banner offers no action without a configured endpoint", () => {
const banner = bannerState({ kind: "disconnected", reason: "boom", retrying: false }, { hasEndpoint: false });
assert.equal(banner.action, null);
});
test("disconnected banner falls back when the reason is blank", () => {
const banner = bannerState({ kind: "disconnected", reason: " ", retrying: false }, { hasEndpoint: true });
assert.equal(banner.text, "Disconnected: connection closed");
});
test("connected banner is an explicit green state with no reconnect action", () => {
const banner = bannerState({ kind: "connected", attempt: 0, reason: "", retrying: false }, { hasEndpoint: true });
assert.equal(banner.tone, "ok");
assert.equal(banner.text, "Connected");
assert.equal(banner.action, null);
});
test("an unknown or missing connection state reads as idle", () => {
assert.equal(bannerState(undefined, { hasEndpoint: false }).tone, "idle");
assert.equal(bannerState({ kind: "something-else" }, { hasEndpoint: false }).tone, "idle");
});
// ---------------------------------------------------------------------------
// stateBadge — the header badge, which alone carries the retry attempt
// ---------------------------------------------------------------------------
test("stateBadge names the disconnected reason, retry and attempt", () => {
const retrying = stateBadge({ kind: "disconnected", attempt: 3, reason: "Connection closed (1006)", retrying: true });
assert.equal(retrying.tone, "bad");
assert.equal(retrying.text, "disconnected — Connection closed (1006) (attempt 3, retrying)");
const settled = stateBadge({ kind: "disconnected", attempt: 2, reason: "boom", retrying: false });
assert.equal(settled.text, "disconnected — boom (attempt 2)");
const attemptsZero = stateBadge({ kind: "disconnected", attempt: 0, reason: "boom", retrying: true });
assert.equal(attemptsZero.text, "disconnected — boom (retrying)");
const bare = stateBadge({ kind: "disconnected", reason: "", retrying: false });
assert.equal(bare.text, "disconnected");
});
test("stateBadge covers the other connection states", () => {
assert.deepEqual(stateBadge({ kind: "connected" }), { tone: "ok", text: "connected" });
assert.deepEqual(stateBadge({ kind: "connecting", attempt: 4 }), { tone: "warn", text: "connecting… (attempt 4)" });
assert.deepEqual(stateBadge({ kind: "idle" }), { tone: "idle", text: "idle" });
assert.deepEqual(stateBadge(undefined), { tone: "idle", text: "idle" });
});
// ---------------------------------------------------------------------------
// foregroundAction — the ConnectionRecovery.kt truth table
// ---------------------------------------------------------------------------
test("foregroundAction leaves Idle alone (a deliberate Disconnect)", () => {
assert.equal(foregroundAction({ kind: "idle" }, false), ForegroundAction.LeaveAlone);
assert.equal(foregroundAction({ kind: "idle" }, true), ForegroundAction.LeaveAlone);
});
test("foregroundAction leaves Connected alone only when confirmed alive", () => {
assert.equal(foregroundAction({ kind: "connected" }, true), ForegroundAction.LeaveAlone);
assert.equal(foregroundAction({ kind: "connected" }, false), ForegroundAction.Reconnect);
});
test("foregroundAction reconnects Connecting and Disconnected whatever the probe says", () => {
assert.equal(foregroundAction({ kind: "connecting", attempt: 2 }, false), ForegroundAction.Reconnect);
assert.equal(foregroundAction({ kind: "disconnected", reason: "x" }, false), ForegroundAction.Reconnect);
assert.equal(foregroundAction({ kind: "disconnected" }, true), ForegroundAction.Reconnect);
});
// ---------------------------------------------------------------------------
// protocolMismatch
// ---------------------------------------------------------------------------
test("protocolMismatch is silent when the server matches or omits the protocol", () => {
assert.equal(protocolMismatch(1, 1), null);
assert.equal(protocolMismatch(1, null), null);
assert.equal(protocolMismatch(1, undefined), null);
assert.equal(protocolMismatch(1, ""), null);
assert.equal(protocolMismatch(null, 2), null);
assert.equal(protocolMismatch(undefined, undefined), null);
});
test("protocolMismatch names both values when they differ", () => {
const warning = protocolMismatch(1, 2);
assert.equal(typeof warning, "string");
assert.ok(warning.includes("1"), warning);
assert.ok(warning.includes("2"), warning);
});
// ---------------------------------------------------------------------------
// serverBuildLabel
// ---------------------------------------------------------------------------
test("serverBuildLabel renders version and commit, SettingsScreen.kt's shape", () => {
assert.equal(serverBuildLabel({ version: "v1.2.3", commit: "abc123", dirty: false }), "Server build v1.2.3 (abc123)");
// A dirty tree is already folded into `version` by internal/buildinfo (-dirty),
// so there is deliberately no second `+dirty` marker on the commit.
assert.equal(serverBuildLabel({ version: "v1.2.3-dirty", commit: "abc123", dirty: true }), "Server build v1.2.3-dirty (abc123)");
assert.equal(serverBuildLabel({ version: "v1.2.3", commit: "abc123", dirty: true }), "Server build v1.2.3 (abc123)");
});
test("serverBuildLabel tolerates missing fields", () => {
assert.equal(serverBuildLabel({ version: "v1" }), "Server build v1");
assert.equal(serverBuildLabel({ commit: "abc" }), "Server build (abc)");
assert.equal(serverBuildLabel({ dirty: true }), "");
assert.equal(serverBuildLabel({}), "");
assert.equal(serverBuildLabel(null), "");
assert.equal(serverBuildLabel(undefined), "");
});
// ---------------------------------------------------------------------------
// liveBadgeState — RunStream.kt words
// ---------------------------------------------------------------------------
test("liveBadgeState maps the explicit stream state to RunStream.kt's words", () => {
// The badge says whether pushes are arriving, independent of any executing run.
assert.deepEqual(liveBadgeState(RunStream.Live), { tone: "live", label: "live" });
assert.deepEqual(liveBadgeState(RunStream.Connecting), { tone: "reconnecting", label: "reconnecting" });
assert.deepEqual(liveBadgeState(RunStream.Offline), { tone: "not-live", label: "not live" });
// An unknown/missing state is honestly not live.
assert.deepEqual(liveBadgeState(undefined), { tone: "not-live", label: "not live" });
assert.deepEqual(liveBadgeState("something-else"), { tone: "not-live", label: "not live" });
});
// ---------------------------------------------------------------------------
// Parity with the Android sources
// ---------------------------------------------------------------------------
test("the wording still matches the Android client's sources", () => {
const banner = androidSrc("ui/components/ConnectionBanner.kt");
assert.ok(banner.includes("Not connected. Enter the server URL in Settings, then connect."), banner.slice(0, 200));
assert.ok(banner.includes('"Connecting (attempt ${state.attempt}') && banner.includes("…"), "connecting copy");
assert.ok(banner.includes('append(" · retrying with backoff")'), "retry suffix");
assert.ok(banner.includes('"Reconnect"'), "reconnect label");
const settings = androidSrc("ui/screens/SettingsScreen.kt");
assert.ok(settings.includes('Text("Connected"'), "connected label");
assert.ok(settings.includes("Server build"), "build label");
// buildinfo folds a dirty tree into `version` via a `-dirty` suffix, which is
// why serverBuildLabel adds no separate dirty marker (SettingsScreen.kt does
// not render one either).
const buildinfo = source("internal/buildinfo/buildinfo.go");
assert.ok(buildinfo.includes('info.Version += "-dirty"'), "buildinfo -dirty suffix");
assert.ok(!settings.includes("dirty"), "SettingsScreen.kt renders no dirty marker");
const recovery = androidSrc("data/transport/ConnectionRecovery.kt");
for (const line of [
"ConnectionState.Idle -> ForegroundAction.LeaveAlone",
"if (confirmedAlive) ForegroundAction.LeaveAlone else ForegroundAction.Reconnect",
"is ConnectionState.Connecting -> ForegroundAction.Reconnect",
"is ConnectionState.Disconnected -> ForegroundAction.Reconnect",
]) {
assert.ok(recovery.includes(line), `ConnectionRecovery.kt is missing: ${line}`);
}
const stream = androidSrc("ui/RunStream.kt");
for (const label of ['Live("live")', 'Connecting("reconnecting")', 'Offline("not live")']) {
assert.ok(stream.includes(label), `RunStream.kt is missing: ${label}`);
}
});