import { afterEach, beforeEach, expect, it, vi } from "vitest"; import { cleanup, fireEvent, render, waitFor } from "@solidjs/testing-library"; import { captureArtifact, expectNoDiagnostics } from "../test/diagnostics"; import App from "../App"; import { clearConnection, saveConnection } from "../lib/connection"; import { library } from "../lib/harvest"; import { player } from "../lib/player"; import { mediaFiles, navidromeFetch, type FakeMediaFile } from "../test/navidrome"; import { stubResizeObserver } from "../test/env"; /** * The line above the transport: `/` for the needle, `:` for a command. * * Mounted the way the app mounts it — under the connection gate, beside the * transport, outside the routed view — because where it sits is half of what it is: a * field inside the list could not address the player, and a field beside the player * has to ask the list (through its host) what Enter means. */ const songs: FakeMediaFile[] = [ ...mediaFiles({ album: "Heavy Loop", albumArtist: "Autechre", albumId: "al-1" }, 3), ...mediaFiles({ album: "Amber", albumArtist: "Autechre", albumId: "al-2" }, 2, 3), ]; const line = () => document.querySelector(".cmd-line")!; const box = () => document.querySelector(".lib-query-input")!; const prompt = () => document.querySelector(".lib-query-prompt"); const readout = () => document.querySelector(".lib-query-count"); const rows = () => document.querySelectorAll(".lib-row--track").length; /** What the reader types at a live field: the input event a browser fires. */ const type = (value: string) => { fireEvent.input(box(), { target: { value } }); }; const press = (key: string, el: Window | HTMLElement = window) => { fireEvent.keyDown(el as HTMLElement, { key }); }; /** The caret into the line, whichever half `key` opens. */ const arm = async (key: string) => { press(key); await waitFor(() => expect(document.activeElement).toBe(box())); }; async function bootConnected() { vi.stubGlobal("fetch", navidromeFetch(songs)); saveConnection({ host: "https://nav.example.com", username: "admin", password: "wordpass", }); render(() => ); await waitFor(() => expect(library().status()).toBe("ready"), { timeout: 3000, }); await waitFor(() => expect(rows()).toBeGreaterThan(0)); } beforeEach(() => { localStorage.clear(); clearConnection(); library().reset(); player().stop(); player().setVolume(0.8); stubResizeObserver({ width: 1000, height: 0 }); }); afterEach(() => { cleanup(); vi.unstubAllGlobals(); }); it("sits below the routed view, outside it", async () => { const { artifact } = await captureArtifact(async () => { await bootConnected(); const view = document.querySelector(".lib")!; // App chrome, not view chrome: a sibling of the list rather than a descendant of // it, stacked after it — which is where the transport goes too, right underneath. expect(line().parentElement).toBe(view.parentElement); expect(view.compareDocumentPosition(line()) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy(); // the field left the toolbar with it, and took the search wash along expect(document.querySelector(".lib-toolbar .lib-query-input")).toBeNull(); expect(document.querySelector(".lib-toolbar .lib-query-prompt")).toBeNull(); }); expectNoDiagnostics(artifact); }); it("arms with `/` without a prompt glyph, and types straight into the filter", async () => { const { artifact } = await captureArtifact(async () => { await bootConnected(); // nothing armed, nothing typed: the line is furniture, and it says so by wearing // no mark at all — the idle `/` hint is gone, because it advertised a key rather // than a state expect(prompt()).toBeNull(); expect(line().dataset.mode).toBe("search"); await arm("/"); // still no glyph: the search half is a caret and a wash, nothing more expect(prompt()).toBeNull(); expect(line().dataset.typing).toBe("true"); type("amber"); await waitFor(() => expect(rows()).toBe(2)); // the count lives beside the field now, and the wash follows the query rather than // the caret — an accepted filter is still a search in progress expect(readout()?.textContent).toBe("2 tracks"); expect(line().dataset.filtered).toBe("true"); }); expectNoDiagnostics(artifact); }); it("says the readout in the number the needle actually found", async () => { // `2 tracks` has been asserted since the readout existed, and it is the only plural form a // five-track fixture can ever print: `1 tracks` needs a query with exactly one hit under it, // which is the one screen nobody screenshots. const { artifact } = await captureArtifact(async () => { await bootConnected(); await arm("/"); type("track 5"); await waitFor(() => expect(rows()).toBe(1)); expect(readout()?.textContent).toBe("1 track"); // and back to the plural, so the memo is seen to agree in both directions rather than // having been pinned to one string type("track"); await waitFor(() => expect(rows()).toBe(5)); expect(readout()?.textContent).toBe("5 tracks"); }); expectNoDiagnostics(artifact); }); it("a second `/` starts a new search: the old needle is cleared, not offered for editing", async () => { const { artifact } = await captureArtifact(async () => { await bootConnected(); await arm("/"); type("amber"); await waitFor(() => expect(rows()).toBe(2)); press("Enter", box()); // accept: caret out, filter stays await waitFor(() => expect(document.activeElement).not.toBe(box())); expect(rows()).toBe(2); // `/` again is "ask again", not "keep asking": the needle goes with the key, so // the field comes up empty and the list behind it is the whole library again await arm("/"); expect(box().value).toBe(""); expect(readout()).toBeNull(); expect(line().dataset.filtered).toBe("false"); expect(line().dataset.typing).toBe("true"); await waitFor(() => expect(rows()).toBe(5)); // and the next thing typed is a new query, not the old one with a character on it type("heavy"); await waitFor(() => expect(rows()).toBe(3)); expect(box().value).toBe("heavy"); }); expectNoDiagnostics(artifact); }); it("arms with `:` into command mode, without touching the needle", async () => { const { artifact } = await captureArtifact(async () => { await bootConnected(); await arm("/"); type("amber"); await waitFor(() => expect(rows()).toBe(2)); press("Enter", box()); // accept: caret out, filter stays await waitFor(() => expect(document.activeElement).not.toBe(box())); await arm(":"); expect(line().dataset.mode).toBe("command"); // the prompt says `:`, and it says it because command mode is on expect(prompt()?.textContent).toBe(":"); // what the field holds is the command, not the query — and the query is still // narrowing the list behind it, unread and unchanged expect(box().value).toBe(""); expect(rows()).toBe(2); }); expectNoDiagnostics(artifact); }); it("says what the word being typed means, and refuses the ones that mean nothing", async () => { const { artifact } = await captureArtifact(async () => { await bootConnected(); await arm(":"); type("vol"); await waitFor(() => expect(readout()?.textContent).toBe("the master gain")); expect(readout()?.dataset.bad).toBeUndefined(); type("volupme"); await waitFor(() => expect(readout()?.dataset.bad).toBe("true")); expect(readout()?.textContent).toContain("volupme"); // Enter on a word nobody knows leaves the line alone: the text stays where it is, // the caret stays in it, and the volume is where it was. press("Enter", box()); expect(box().value).toBe("volupme"); expect(document.activeElement).toBe(box()); expect(player().peekVolume()).toBe(0.8); expect(line().dataset.mode).toBe("command"); }); expectNoDiagnostics(artifact); }); it("runs a command, prints what it did, and gives the keyboard back", async () => { const { artifact } = await captureArtifact(async () => { await bootConnected(); await arm(":"); type("vol 40"); press("Enter", box()); await waitFor(() => expect(player().peekVolume()).toBeCloseTo(0.4, 5)); // out of the field, back to being a search line — one press, one answer await waitFor(() => expect(document.activeElement).not.toBe(box())); expect(line().dataset.mode).toBe("search"); // ...and the answer stays on the line until the next key takes it, as the toolbar's // share line does expect(readout()?.textContent).toBe("40%"); expect(box().value).toBe(""); // the command was not left in the field for `/` to find press("j"); await waitFor(() => expect(readout()).toBeNull()); }); expectNoDiagnostics(artifact); }); it("walks the history with the arrows, and only in command mode", async () => { const { artifact } = await captureArtifact(async () => { await bootConnected(); await arm(":"); type("vol 40"); press("Enter", box()); await waitFor(() => expect(player().peekVolume()).toBeCloseTo(0.4, 5)); await arm(":"); press("ArrowUp", box()); await waitFor(() => expect(box().value).toBe("vol 40")); expect(line().dataset.mode).toBe("command"); // ...and in the search half the same arrows are the caret's, which is the same // guard that keeps them out of the list's cursor while the caret is in the line press("Escape", box()); await arm("/"); const before = box().value; press("ArrowUp", box()); expect(box().value).toBe(before); }); expectNoDiagnostics(artifact); }); it("leaves command mode on Escape with nothing half-typed behind it", async () => { const { artifact } = await captureArtifact(async () => { await bootConnected(); await arm(":"); type("seek 1:2"); press("Escape", box()); await waitFor(() => expect(document.activeElement).not.toBe(box())); expect(line().dataset.mode).toBe("search"); expect(prompt()).toBeNull(); // the half-typed command is gone rather than waiting for the next `:` — a command // is a press, not a draft await arm(":"); expect(box().value).toBe(""); }); expectNoDiagnostics(artifact); }); it("lets `/` and `:` through as characters while the caret is in the line", async () => { const { artifact } = await captureArtifact(async () => { await bootConnected(); await arm("/"); // a band with a slash in it, and a time with a colon in it, both have to be // typeable — the key that opens the line cannot also be eating its own text press("/", box()); type("cap/"); expect(box().value).toBe("cap/"); expect(line().dataset.mode).toBe("search"); await arm(":"); press(":", box()); type(":seek 1:20"); expect(box().value).toBe(":seek 1:20"); expect(line().dataset.mode).toBe("command"); }); expectNoDiagnostics(artifact); }); it("opens the prompt when the first keystroke in an empty field is a colon", async () => { const { artifact } = await captureArtifact(async () => { await bootConnected(); // The trap: the caret is already in the line — put there by `/`, by a click, by anything — and // the reader types the command they came to type. `:` means the prompt everywhere else on this // line, and an empty field has no needle for a colon to be a character *in*, so the character // and the key have stopped being the same thing. Getting to a command used to require leaving // the field and pressing the key again: a rule nobody reads and everybody trips over. await arm("/"); press(":", box()); await waitFor(() => expect(line().dataset.mode).toBe("command")); expect(prompt()?.textContent).toBe(":"); // The colon opened the prompt rather than being typed into it, and the needle the field was // holding (nothing) is undisturbed. expect(box().value).toBe(""); // And the verb it was opened for is reachable by its alias, from here, in one motion. type("lrclibaudit"); await waitFor(() => expect(readout()?.textContent).toContain("why the lyric was not found")); press("Enter", box()); await waitFor(() => expect(readout()?.textContent).toContain("nothing asked of lrclib yet")); expect(readout()?.dataset.bad).toBe("true"); }); expectNoDiagnostics(artifact); }); it("types a colon into a needle that already has text in it", async () => { await bootConnected(); await arm("/"); type("Blade Runner"); // The guard is the empty field, not the key: with text in front of the caret the colon is editing // a needle — `Blade Runner: The Final Cut` is a title — and stays a character. press(":", box()); expect(line().dataset.mode).toBe("search"); expect(box().value).toBe("Blade Runner"); });