import { afterEach, expect, it, vi } from "vitest"; import { clearConnection, saveConnection } from "../connection"; import { navidromeFetch } from "../../test/navidrome"; import { createCommandLineStore } from "./store"; /** * The line's side of an asynchronous verb. * * `:scan` is the one verb whose answer is a round-trip, and the store is where that * round-trip meets the readout's rules: the placeholder belongs to the press, the * verdict may replace only its own placeholder, and any key in between is the press * being overwritten — feedback that outlives its press is furniture. * * Every read here is one microtask behind every write — a Solid 2 signal's value * commits at the end of the task that wrote it — so each assertion follows its * press by a `flush`, the same wait the component's memos sit behind. */ afterEach(() => { vi.unstubAllGlobals(); clearConnection(); }); const connected = () => saveConnection({ host: "nav.example.com", username: "admin", password: "pw" }); const flush = () => new Promise((r) => setTimeout(r, 0)); /** The fake with its `startScan` held shut until the test opens it. */ function gatedScan() { let release!: () => void; const gate = new Promise((r) => (release = r)); const base = navidromeFetch([]) as unknown as (i: RequestInfo | URL) => Promise; const server = vi.fn(async (input: RequestInfo | URL) => { if (String(input).includes("/rest/startScan")) await gate; return base(input); }) as unknown as typeof fetch; return { server, release }; } it("holds the verb's word on the line until the server answers, then replaces it", async () => { connected(); const { server, release } = gatedScan(); vi.stubGlobal("fetch", server); const line = createCommandLineStore(); line.setCommand("scan"); expect(line.submit()).toBe(true); await flush(); // the press is over, the answer is not: the word and an ellipsis, and nothing else expect(line.hint()).toEqual({ text: "scan…", bad: false }); release(); await flush(); expect(line.hint()).toEqual({ text: "scan done · rows on the way", bad: false }); }); it("lets a key typed mid-flight take the placeholder away — and silences the verdict", async () => { connected(); const { server, release } = gatedScan(); vi.stubGlobal("fetch", server); const line = createCommandLineStore(); line.setCommand("scan"); line.submit(); await flush(); expect(line.hint()?.text).toBe("scan…"); // any other key is what clears a notice; the app's key handler calls this line.clearNotice(); release(); await flush(); // the answer arrived after the press was overwritten: it says nothing expect(line.hint()).toBeNull(); }); it("does not overwrite a newer notice with an older verb's verdict", async () => { connected(); const { server, release } = gatedScan(); vi.stubGlobal("fetch", server); const line = createCommandLineStore(); line.setCommand("scan"); line.submit(); // a second press settles before the first verb's server ever answers line.setCommand("stop"); line.submit(); await flush(); expect(line.hint()).toEqual({ text: "stopped", bad: false }); release(); await flush(); expect(line.hint()).toEqual({ text: "stopped", bad: false }); });