diff --git a/tests/components/configurator/input/test.ts b/tests/components/configurator/input/test.ts new file mode 100644 index 00000000..d8b34aec --- /dev/null +++ b/tests/components/configurator/input/test.ts @@ -0,0 +1,147 @@ +import { describe, it } from "@std/testing/bdd"; +import { expect } from "@std/expect"; + +import { testWeb } from "@tests/common/index.ts"; +import type { Track } from "~/definitions/types.d.ts"; + +describe("components/configurator/input", () => { + it("listCached returns an empty array initially", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/input/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + return configurator.listCached(); + }); + + expect(result).toEqual([]); + }); + + it("consult with an unsupported scheme returns supported: false", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/input/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + return configurator.consult("ipfs://QmSomeCid"); + }); + + expect(result.supported).toBe(false); + }); + + it("resolve with an unsupported scheme returns undefined", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/input/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + const r = await configurator.resolve({ uri: "ipfs://QmSomeCid" }); + return r ?? null; + }); + + expect(result).toBe(null); + }); + + it("detach with no matching input returns all tracks unchanged", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/input/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + + const tracks: Track[] = [ + { $type: "sh.diffuse.output.track", id: "t1", uri: "ipfs://Qm1" }, + { $type: "sh.diffuse.output.track", id: "t2", uri: "ipfs://Qm2" }, + ]; + + return configurator.detach({ fileUriOrScheme: "ipfs", tracks }); + }); + + expect(result.map((t) => t.id)).toEqual(["t1", "t2"]); + }); + + it("groupConsult marks unsupported schemes as unavailable", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/input/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + + return configurator.groupConsult([ + "ipfs://QmA", + "ipfs://QmB", + ]); + }); + + expect(result["ipfs"]?.available).toBe(false); + expect(result["ipfs"]?.uris).toEqual(["ipfs://QmA", "ipfs://QmB"]); + }); + + it("inputs maps child input elements by their SCHEME", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/input/element.js" + ); + const HttpsInput = await import( + "~/components/input/https/element.js" + ); + + const configurator = new mod.CLASS(); + const httpsInput = new HttpsInput.CLASS(); + configurator.appendChild(httpsInput); + document.body.append(configurator); + + return Object.keys(configurator.inputs()); + }); + + expect(result).toContain("https"); + }); + + it("consult with an HTTPS URI delegates to the HTTPS input", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/input/element.js" + ); + const HttpsInput = await import( + "~/components/input/https/element.js" + ); + + const configurator = new mod.CLASS(); + const httpsInput = new HttpsInput.CLASS(); + configurator.appendChild(httpsInput); + document.body.append(configurator); + + return configurator.consult("https://example.com/audio.mp3"); + }); + + expect(result.supported).toBe(true); + }); + + it("resolve with an HTTPS URI returns a url via the HTTPS input", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/input/element.js" + ); + const HttpsInput = await import( + "~/components/input/https/element.js" + ); + + const configurator = new mod.CLASS(); + const httpsInput = new HttpsInput.CLASS(); + configurator.appendChild(httpsInput); + document.body.append(configurator); + + return configurator.resolve({ uri: "https://example.com/audio.mp3" }); + }); + + expect(result).not.toBe(null); + if (result && "url" in result) { + expect(result.url).toBe("https://example.com/audio.mp3"); + } + }); +}); diff --git a/tests/components/configurator/output/test.ts b/tests/components/configurator/output/test.ts new file mode 100644 index 00000000..037e6f0e --- /dev/null +++ b/tests/components/configurator/output/test.ts @@ -0,0 +1,178 @@ +import { describe, it } from "@std/testing/bdd"; +import { expect } from "@std/expect"; + +import { testWeb } from "@tests/common/index.ts"; + +describe("components/configurator/output", () => { + it("selected is null initially", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/output/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + return configurator.selected(); + }); + + expect(result).toBe(null); + }); + + it("activated is empty initially", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/output/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + return Array.from(configurator.activated()); + }); + + expect(result).toEqual([]); + }); + + it("hasSelected returns false when nothing is stored in localStorage", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/output/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + return configurator.hasSelected(); + }); + + expect(result).toBe(false); + }); + + it("hasDefault returns false without a default attribute", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/output/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + return configurator.hasDefault(); + }); + + expect(result).toBe(false); + }); + + it("hasDefault returns true when the default attribute is set", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/output/element.js" + ); + const configurator = new mod.CLASS(); + configurator.setAttribute("default", "my-output"); + document.body.append(configurator); + return configurator.hasDefault(); + }); + + expect(result).toBe(true); + }); + + it("select persists the id to localStorage", async () => { + const stored = await testWeb(async () => { + const mod = await import( + "~/components/configurator/output/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + await configurator.select("my-output"); + return localStorage.getItem("diffuse/configurator/output/selected/id"); + }); + + expect(stored).toBe("my-output"); + }); + + it("hasSelected returns true after select", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/output/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + await configurator.select("my-output"); + return configurator.hasSelected(); + }); + + expect(result).toBe(true); + }); + + it("activated includes id after select", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/output/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + await configurator.select("my-output"); + return Array.from(configurator.activated()); + }); + + expect(result).toContain("my-output"); + }); + + it("deselect removes the id from localStorage", async () => { + const stored = await testWeb(async () => { + const mod = await import( + "~/components/configurator/output/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + await configurator.select("my-output"); + await configurator.deselect(); + return localStorage.getItem("diffuse/configurator/output/selected/id"); + }); + + expect(stored).toBe(null); + }); + + it("hasSelected returns false after deselect", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/output/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + await configurator.select("my-output"); + await configurator.deselect(); + return configurator.hasSelected(); + }); + + expect(result).toBe(false); + }); + + it("options lists child elements by id and label", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/output/element.js" + ); + const configurator = new mod.CLASS(); + + const child = document.createElement("div"); + child.id = "output-a"; + child.setAttribute("label", "Output A"); + configurator.appendChild(child); + + document.body.append(configurator); + + const opts = await configurator.options(); + return opts.map((o) => ({ id: o.id, label: o.label })); + }); + + expect(result).toEqual([{ id: "output-a", label: "Output A" }]); + }); + + it("options returns an empty array when there are no children", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/output/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + return configurator.options(); + }); + + expect(result).toEqual([]); + }); +}); diff --git a/tests/components/configurator/scrobbles/test.ts b/tests/components/configurator/scrobbles/test.ts new file mode 100644 index 00000000..b0771d46 --- /dev/null +++ b/tests/components/configurator/scrobbles/test.ts @@ -0,0 +1,174 @@ +import { describe, it } from "@std/testing/bdd"; +import { expect } from "@std/expect"; + +import { testWeb } from "@tests/common/index.ts"; + +describe("components/configurator/scrobbles", () => { + it("scrobblers returns empty array when there are no children", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/scrobbles/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + return configurator.scrobblers().length; + }); + + expect(result).toBe(0); + }); + + it("nowPlaying resolves without throwing when there are no active scrobblers", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/scrobbles/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + + const track = { + $type: "sh.diffuse.output.track" as const, + id: "t1", + uri: "https://example.com/audio.mp3", + }; + + await configurator.nowPlaying(track); + return true; + }); + + expect(result).toBe(true); + }); + + it("scrobble resolves without throwing when there are no active scrobblers", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/scrobbles/element.js" + ); + const configurator = new mod.CLASS(); + document.body.append(configurator); + + const track = { + $type: "sh.diffuse.output.track" as const, + id: "t1", + uri: "https://example.com/audio.mp3", + }; + + await configurator.scrobble(track, Date.now()); + return true; + }); + + expect(result).toBe(true); + }); + + it("scrobblers includes child elements that have the scrobble interface", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/scrobbles/element.js" + ); + const configurator = new mod.CLASS(); + + // Mock scrobbler with the required interface + const mock = document.createElement("div"); + (mock as any).isAuthenticated = () => true; + (mock as any).nowPlaying = async () => {}; + (mock as any).scrobble = async () => {}; + configurator.appendChild(mock); + + document.body.append(configurator); + + return configurator.scrobblers().length; + }); + + expect(result).toBe(1); + }); + + it("scrobblers excludes children that do not have the scrobble interface", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/scrobbles/element.js" + ); + const configurator = new mod.CLASS(); + + // Plain element without scrobble methods + const plain = document.createElement("div"); + configurator.appendChild(plain); + + document.body.append(configurator); + + return configurator.scrobblers().length; + }); + + expect(result).toBe(0); + }); + + it("nowPlaying calls all authenticated scrobblers", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/scrobbles/element.js" + ); + const configurator = new mod.CLASS(); + + const calls: string[] = []; + + const auth = document.createElement("div"); + (auth as any).isAuthenticated = () => true; + (auth as any).nowPlaying = async () => { calls.push("auth"); }; + (auth as any).scrobble = async () => {}; + + const unauth = document.createElement("div"); + (unauth as any).isAuthenticated = () => false; + (unauth as any).nowPlaying = async () => { calls.push("unauth"); }; + (unauth as any).scrobble = async () => {}; + + configurator.appendChild(auth); + configurator.appendChild(unauth); + document.body.append(configurator); + + const track = { + $type: "sh.diffuse.output.track" as const, + id: "t1", + uri: "https://example.com/audio.mp3", + }; + + await configurator.nowPlaying(track); + return calls; + }); + + expect(result).toEqual(["auth"]); + }); + + it("scrobble calls all authenticated scrobblers", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/configurator/scrobbles/element.js" + ); + const configurator = new mod.CLASS(); + + const calls: string[] = []; + + const auth = document.createElement("div"); + (auth as any).isAuthenticated = () => true; + (auth as any).nowPlaying = async () => {}; + (auth as any).scrobble = async () => { calls.push("auth"); }; + + const unauth = document.createElement("div"); + (unauth as any).isAuthenticated = () => false; + (unauth as any).nowPlaying = async () => {}; + (unauth as any).scrobble = async () => { calls.push("unauth"); }; + + configurator.appendChild(auth); + configurator.appendChild(unauth); + document.body.append(configurator); + + const track = { + $type: "sh.diffuse.output.track" as const, + id: "t1", + uri: "https://example.com/audio.mp3", + }; + + await configurator.scrobble(track, Date.now()); + return calls; + }); + + expect(result).toEqual(["auth"]); + }); +});