import { afterEach, beforeEach, expect, it } from "vitest";
import { cleanup, fireEvent, render } from "@solidjs/testing-library";
import { Show, type VoidComponent } from "solid-js";
import { captureArtifact, expectNoDiagnostics } from "../test/diagnostics";
import { LadderAuditModal } from "./LadderAuditModal";
import {
annotateLadder,
beginLadder,
clearLadderAudits,
endLadder,
hideLadderAudit,
ladderAuditModal,
noteStripped,
recordRung,
recordUnasked,
setLadderAuditEnabled,
showLadderAudit,
type LadderAudit,
} from "../lib/lyrics/audit";
/**
* The ladder's account, drawn.
*
* The recorder has its own tests; these are about the *reading*, and specifically the one thing a
* debugging table is allowed to be and still fail: complete but useless. A rung that says `nothing`
* is a wall. The same rung saying `2 hits · 2 timed · 2 refused`, with the refused ones named and
* the seconds that condemned them beside each, is the difference between a modal somebody closes and
* a modal that changes the code.
*
* Everything is built through the recorder rather than with hand-made objects, so what is asserted on
* screen is what the client actually writes — a fixture would let the component and the recorder drift
* apart and both keep passing.
*/
const tags = {
trackName: "Partners For Life ft. Pharrell Williams & Jamie Foxx",
artistName: "The Neptunes",
albumName: "The Neptunes: The Other Side Of The Planet (Disc 12)",
duration: 272,
};
const Host: VoidComponent = () => (
{(audit) => }
);
const modal = () => document.querySelector(".la-modal");
const rows = () => [...document.querySelectorAll(".la-rung")];
const refusals = () =>
[...document.querySelectorAll(".la-veto")].map((n) => n.textContent);
/** A bootleg that took all four questions and was refused by the ceiling on the loose two. */
function recordRefusedAttempt(): LadderAudit {
const audit = beginLadder("tr-1", tags, 0)!;
recordRung(audit, {
n: 1,
name: "the signature",
endpoint: "/api/get",
asked: { track_name: tags.trackName, duration: "272" },
at: 0,
ms: 40,
status: "none",
hits: 0,
timed: 0,
vetoed: [],
});
noteStripped(audit, "Partners For Life", ["Pharrell Williams", "Jamie Foxx"]);
recordRung(audit, {
n: 3,
name: "the title without its credit",
endpoint: "/api/search",
asked: { track_name: "Partners For Life", duration: "272" },
at: 45,
ms: 31,
status: "records",
hits: 2,
timed: 2,
ceiling: 3,
vetoed: [
{
id: 9212306,
trackName: "Partners For Life",
artistName: "Jeff Beal",
offSeconds: 112,
timed: true,
why: "length",
},
{
id: 44,
trackName: "Partners For Life",
artistName: "Unknown Artist",
offSeconds: null,
timed: false,
why: "no length",
},
],
});
endLadder(audit, 80, "none", { says: "2 timed records, none within 3s" });
annotateLadder("tr-1", "the answer arrived after the listener had moved on");
return audit;
}
beforeEach(() => {
setLadderAuditEnabled(true);
clearLadderAudits();
});
afterEach(() => {
hideLadderAudit();
cleanup();
});
it("is not on screen until it is asked for", async () => {
recordRefusedAttempt();
const { container } = render(() => );
expect(container.querySelector(".la-modal")).toBeNull();
// Raised by the recorder, the only way the bar can raise it too: nothing here opens itself.
showLadderAudit();
await waitForModal();
expect(container.querySelector(".la-modal")).not.toBeNull();
});
it("shows the tags the library sent, and what the ladder asked with", async () => {
recordRefusedAttempt();
render(() => );
const { artifact } = await captureArtifact(async () => {
showLadderAudit();
await waitForModal();
// The header is the raw tag — the thing that was wrong — and the line under it is the edit the
// loose questions are built on, so the two can be compared without opening a second tool.
expect(document.querySelector(".la-title")?.textContent).toContain("ft. Pharrell Williams");
expect(document.querySelector(".la-by")?.textContent).toContain("The Neptunes");
expect(document.querySelector(".la-stripped-title")?.textContent).toBe("Partners For Life");
expect(document.querySelector(".la-stripped-credited")?.textContent).toContain(
"Pharrell Williams, Jamie Foxx",
);
});
expectNoDiagnostics(artifact);
});
it("draws one row per question, with the query that went out in the host's field names", async () => {
recordRefusedAttempt();
render(() => );
await captureArtifact(async () => {
showLadderAudit();
await waitForModal();
expect(rows()).toHaveLength(2);
const chips = [...rows()[1]!.querySelectorAll(".la-chip")].map(
(n) => n.textContent,
);
expect(chips).toEqual(["track_namePartners For Life", "duration272"]);
expect(rows()[1]!.querySelector(".la-endpoint")?.textContent).toBe("/api/search");
expect(rows()[1]!.querySelector(".la-ceiling")?.textContent).toBe("±3s");
});
});
it("names the records the ceiling refused, with the seconds that refused them", async () => {
// The assertion the modal exists for. A rung that refused two records has to say which two, how far
// off they were, and whether they had timings — otherwise the next move is a guess.
recordRefusedAttempt();
render(() => );
showLadderAudit();
await waitForModal();
expect(refusals()).toEqual([
"Jeff Beal — Partners For Life · 112s off · timed · refused for its length",
"Unknown Artist — Partners For Life · no length published · no timings · refused for its no length",
]);
// The timed one is the lyric that exists and was not taken, so it is the row the eye should land on.
expect(document.querySelectorAll('.la-veto[data-timed="yes"]')).toHaveLength(1);
});
it("says the verdict in the word the bar uses, and carries what the store added", async () => {
recordRefusedAttempt();
render(() => );
showLadderAudit();
await waitForModal();
expect(document.querySelector(".la-verdict")?.textContent).toBe(
"none · 2 timed records, none within 3s",
);
expect(document.querySelector(".la-verdict")?.getAttribute("data-outcome")).toBe("none");
expect([...document.querySelectorAll(".la-note")].map((n) => n.textContent)).toEqual([
"the answer arrived after the listener had moved on",
]);
});
it("explains an attempt that never asked anything, instead of drawing an empty table", async () => {
recordUnasked("tr-2", tags, "cached", 0, "1 timed record, none within 3s");
render(() => );
showLadderAudit();
await waitForModal();
expect(rows()).toHaveLength(0);
expect(document.querySelector(".la-empty")?.textContent).toContain(
"answered from what the app already remembered",
);
});
it("closes on Escape and walks the attempts on the arrows", async () => {
recordRefusedAttempt();
recordUnasked("tr-0", tags, "found", 0);
render(() => );
showLadderAudit();
await waitForModal();
expect(document.querySelector(".la-title")?.textContent).toContain("Partners For Life");
fireEvent.keyDown(window, { key: "ArrowRight" });
await waitForModal();
// The newest is the bootleg; the arrow walks back to the track before it.
expect(document.querySelector(".la-verdict")?.getAttribute("data-outcome")).toBe("found");
fireEvent.keyDown(window, { key: "Escape" });
await waitForModal();
expect(modal()).toBeNull();
});
it("counts the attempts it is showing, and says so", async () => {
recordRefusedAttempt();
recordUnasked("tr-0", tags, "found", 0);
render(() => );
showLadderAudit();
await waitForModal();
expect(document.querySelector(".la-count")?.textContent).toBe("attempt 1 of 2");
});
/**
* Between the write and the read.
*
* A signal written from a test body is committed at the end of the task under Solid 2, so a
* synchronous read straight after `showLadderAudit()` is reading the screen from before the modal was
* asked to open — the same wait the load graph's tests strike.
*/
async function waitForModal(): Promise {
for (let i = 0; i < 20 && !document.querySelector(".la-modal"); i++) {
await new Promise((resolve) => setTimeout(resolve, 0));
}
if (!document.querySelector(".la-modal")) throw new Error("the table never opened");
}