import { beforeEach, describe, expect, it, vi } from "vitest"; /** * Load-time validation of the connection, in a file of its own and with a fresh * module graph per case. * * The store is a module-level singleton seeded from localStorage at import, so the * only way to ask "what happens when the stored value is junk" is to import the * module again after planting the value — which `vi.resetModules()` only does * cleanly while nothing else in the worker is holding the previous instance. Its own * file keeps that true (vitest runs with `isolate: false`). */ const KEY = "hackermusic.connection.v1"; async function reloadWith(raw: string | null) { localStorage.clear(); if (raw !== null) localStorage.setItem(KEY, raw); vi.resetModules(); return import("./connection"); } beforeEach(() => { localStorage.clear(); }); describe("a stored connection", () => { it("is adopted when it is what saveConnection wrote", async () => { localStorage.clear(); const first = await reloadWith(null); first.saveConnection({ host: "nav.example.com", username: "admin", password: "pw" }); const raw = localStorage.getItem(KEY)!; const again = await reloadWith(raw); expect(again.connection()).toEqual({ host: "https://nav.example.com", username: "admin", password: "pw", }); expect(again.isConnected()).toBe(true); }); it("is dropped when its host was edited into something normalizeHost would never produce", async () => { // The host is the stored string this app turns into a URL it attaches the // username and the password hash to. Being a string is not the invariant; being // the output of normalizeHost is. for (const host of [ "javascript:alert(1)", "//evil.example", "https://nav.example.com/", // a trailing slash from an older build "ftp://nav.example.com", "https://user:pw@nav.example.com", ]) { const mod = await reloadWith(JSON.stringify({ host, username: "admin", password: "pw" })); expect(mod.connection(), host).toBeNull(); expect(mod.isConnected(), host).toBe(false); } }); it("is dropped when a field went missing rather than used half of it", async () => { const mod = await reloadWith( JSON.stringify({ host: "https://nav.example.com", username: "admin" }), ); expect(mod.connection()).toBeNull(); }); it("is kept with empty credentials — that is 'not connected', not corruption", async () => { // An anonymous server has both empty, and isConnected is what decides that is // useless. A schema that demanded a non-empty password would log the user out of // a server that never had a password. const mod = await reloadWith( JSON.stringify({ host: "https://nav.example.com", username: "", password: "" }), ); expect(mod.connection()).toEqual({ host: "https://nav.example.com", username: "", password: "", }); expect(mod.isConnected()).toBe(false); }); it("is absent, rather than broken, when the key holds a null", async () => { // "not configured yet" is stored as null, so the schema must accept it as a value // and not report the very first launch as a failed load. const mod = await reloadWith("null"); expect(mod.connection()).toBeNull(); expect(mod.isConnected()).toBe(false); }); it("is dropped when the key holds something another script wrote", async () => { for (const raw of [ '"https://nav.example.com"', '{"host":null,"username":"a","password":"b"}', '[{"host":"https://nav.example.com","username":"a","password":"b"}]', ]) { const mod = await reloadWith(raw); expect(mod.connection(), raw).toBeNull(); } }); });