Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152import { beforeEach, describe, expect, it, vi } from "vitest";
import { clearConnection, connection, isConnected, normalizeHost, saveConnection, testConnection, updateConnection,} from "./connection";
beforeEach(() => { localStorage.clear(); clearConnection();});
describe("normalizeHost", () => { it("prepends https when no scheme", () => { expect(normalizeHost("nav.example.com")).toBe("https://nav.example.com"); }); it("keeps explicit http for LAN servers", () => { expect(normalizeHost("http://192.168.1.5:4533")).toBe("http://192.168.1.5:4533"); }); it("strips trailing slashes, keeps base path", () => { expect(normalizeHost("https://nav.example.com/navidrome/")).toBe( "https://nav.example.com/navidrome", ); }); it("trims whitespace", () => { expect(normalizeHost(" nav.example.com ")).toBe("https://nav.example.com"); }); it("rejects junk", () => { expect(normalizeHost("")).toBeNull(); expect(normalizeHost(" ")).toBeNull(); expect(normalizeHost("not a url at all %%")).toBeNull(); });});
describe("connection store", () => { it("starts unconfigured", () => { expect(connection()).toBeNull(); expect(isConnected()).toBe(false); });
it("saves normalized settings and reports connected", () => { const saved = saveConnection({ host: " nav.example.com ", username: " admin ", password: "wordpass", }); expect(saved.host).toBe("https://nav.example.com"); expect(saved.username).toBe("admin"); expect(connection()?.password).toBe("wordpass"); expect(isConnected()).toBe(true); });
it("throws on invalid host without touching state", () => { expect(() => saveConnection({ host: "", username: "a", password: "b" })).toThrow(); expect(connection()).toBeNull(); });
it("persists to localStorage under a versioned key", () => { saveConnection({ host: "https://nav.example.com", username: "admin", password: "pw" }); const raw = localStorage.getItem("hackermusic.connection.v1"); expect(raw).toBeTruthy(); expect(JSON.parse(raw!)).toEqual({ host: "https://nav.example.com", username: "admin", password: "pw", }); });
it("writes through to localStorage synchronously on save", () => { saveConnection({ host: "https://a.example.com", username: "u", password: "p" }); const raw = localStorage.getItem("hackermusic.connection.v1"); expect(raw).toContain("a.example.com"); expect(JSON.parse(raw!).username).toBe("u"); });
it("updates fields via patch", () => { saveConnection({ host: "https://nav.example.com", username: "admin", password: "pw" }); updateConnection({ password: "newpw" }); expect(connection()?.password).toBe("newpw"); });
it("clearing resets memory and storage", () => { saveConnection({ host: "https://nav.example.com", username: "admin", password: "pw" }); clearConnection(); expect(connection()).toBeNull(); expect(localStorage.getItem("hackermusic.connection.v1")).toBeNull(); });});
describe("testConnection", () => { it("pings without persisting", async () => { const fetchMock = vi.fn(async () => ({ ok: true, status: 200, json: async () => ({ "subsonic-response": { status: "ok", version: "1.16.1", serverVersion: "v0.54.0", openSubsonic: true, }, }), })); vi.stubGlobal("fetch", fetchMock);
const result = await testConnection({ host: "nav.example.com", username: "admin", password: "wordpass", });
expect(result).toMatchObject({ ok: true, serverVersion: "v0.54.0", openSubsonic: true }); expect(connection()).toBeNull(); // test must NOT persist vi.unstubAllGlobals(); });
it("reports failure with error message", async () => { vi.stubGlobal( "fetch", vi.fn(async () => ({ ok: true, status: 200, json: async () => ({ "subsonic-response": { status: "failed", version: "1.16.1", err: { code: 40, message: "Wrong username or password" }, }, }), })), ); const result = await testConnection({ host: "nav.example.com", username: "a", password: "b" }); expect(result.ok).toBe(false); expect(result.error).toContain("Wrong username or password"); vi.unstubAllGlobals(); });
it("rejects invalid host before any network call", async () => { const fetchMock = vi.fn(); vi.stubGlobal("fetch", fetchMock); const result = await testConnection({ host: "!!!", username: "a", password: "b" }); expect(result.ok).toBe(false); expect(fetchMock).not.toHaveBeenCalled(); vi.unstubAllGlobals(); });});