import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; import LoginDialog from "./LoginDialog"; import { clearConnection, connection } from "../lib/connection"; /** Same string the dialog writes, spelled out so the test pins the storage key. */ const DRAFT_KEY = "hackermusic…t.v1"; function mockPing(payload: Record) { return vi.fn(async () => ({ ok: true, status: 200, json: async () => ({ "subsonic-response": payload }), })); } beforeEach(() => { localStorage.clear(); clearConnection(); vi.unstubAllGlobals(); }); afterEach(() => { cleanup(); // vitest `globals: false` disables testing-library autoloading cleanup }); function fill(host: string, username: string, password: string) { fireEvent.input(screen.getByLabelText("host"), { target: { value: host } }); fireEvent.input(screen.getByLabelText("username"), { target: { value: username } }); fireEvent.input(screen.getByLabelText("password"), { target: { value: password } }); } describe("LoginDialog", () => { it("renders the three fields and connect affordance", () => { render(() => ); expect(screen.getByLabelText("host")).toBeTruthy(); expect(screen.getByLabelText("username")).toBeTruthy(); expect(screen.getByLabelText("password")).toBeTruthy(); expect(screen.getByRole("button", { name: "connect" })).toBeTruthy(); }); it("persists the connection after a successful ping", async () => { const fetchMock = mockPing({ status: "ok", version: "1.16.1", serverVersion: "v0.54.0", openSubsonic: true, }); vi.stubGlobal("fetch", fetchMock); render(() => ); fill("nav.example.com", "admin", "wordpass"); fireEvent.click(screen.getByRole("button", { name: "connect" })); await waitFor(() => { expect(connection()).toEqual({ host: "https://nav.example.com", username: "admin", password: "wordpass", }); }); const url = new URL( String((fetchMock as unknown as ReturnType).mock.calls[0][0]), ); expect(url.pathname).toBe("/rest/ping"); expect(url.searchParams.get("u")).toBe("admin"); expect(url.searchParams.get("t")).toHaveLength(32); // md5 hex token }); it("surfaces server errors and does not persist", async () => { vi.stubGlobal( "fetch", mockPing({ status: "failed", version: "1.16.1", err: { code: 40, message: "Wrong username or password" }, }), ); render(() => ); fill("nav.example.com", "admin", "wrong"); fireEvent.click(screen.getByRole("button", { name: "connect" })); await waitFor(() => { expect(screen.getByRole("alert").textContent).toContain("Wrong username or password"); }); expect(connection()).toBeNull(); }); it("rejects empty host/username client-side without a network call", async () => { const fetchMock = vi.fn(); vi.stubGlobal("fetch", fetchMock); render(() => ); fireEvent.click(screen.getByRole("button", { name: "connect" })); await waitFor(() => { expect(screen.getByRole("alert").textContent).toContain("required"); }); expect(fetchMock).not.toHaveBeenCalled(); }); it("re-seeds host and username after a reload (draft persists; password does not)", async () => { render(() => ); fill("nav.example.com", "admin", "wordpass"); cleanup(); render(() => ); // simulate reload: same storage, fresh mount expect((screen.getByLabelText("host") as HTMLInputElement).value).toBe("nav.example.com"); expect((screen.getByLabelText("username") as HTMLInputElement).value).toBe("admin"); expect((screen.getByLabelText("password") as HTMLInputElement).value).toBe(""); }); it("refuses a draft it cannot fill the form from", () => { // The dialog reads `draftStore.peek().host` into defaultValue, so a stored `null` // used to throw during render — on the one screen with no way out of it. Nothing // in the app writes this value as a string or a number; localStorage did. for (const junk of ["null", '"nav.example.com"', "7", "[]", '{"host":"nav.example.com"}']) { localStorage.setItem(DRAFT_KEY, junk); render(() => ); // All-or-nothing: a draft missing one field is not half-trusted, because the // other half is a host someone may connect to. expect((screen.getByLabelText("host") as HTMLInputElement).value, junk).toBe(""); expect((screen.getByLabelText("username") as HTMLInputElement).value, junk).toBe(""); cleanup(); } }); it("submits via form.requestSubmit (what Enter triggers in browsers)", async () => { const fetchMock = mockPing({ status: "ok", version: "1.16.1" }); vi.stubGlobal("fetch", fetchMock); const { container } = render(() => ); fill("nav.example.com", "admin", "wordpass"); const form = container.querySelector("form")!; form.requestSubmit(); await waitFor(() => { expect(fetchMock).toHaveBeenCalled(); expect(connection()).toBeTruthy(); // wait for the async save, not just the request }); }); });