From 2d9bcc6c6a2be2bff4e9d99285c2da214467751d Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Sat, 28 Mar 2026 21:12:47 +0100 Subject: [PATCH] chore: engine components tests --- tests/components/engine/audio/test.ts | 181 +++++++++++++++ .../components/engine/repeat-shuffle/test.ts | 176 ++++++++++++++ tests/components/engine/scope/test.ts | 216 ++++++++++++++++++ 3 files changed, 573 insertions(+) create mode 100644 tests/components/engine/audio/test.ts create mode 100644 tests/components/engine/repeat-shuffle/test.ts create mode 100644 tests/components/engine/scope/test.ts diff --git a/tests/components/engine/audio/test.ts b/tests/components/engine/audio/test.ts new file mode 100644 index 00000000..eaf8f369 --- /dev/null +++ b/tests/components/engine/audio/test.ts @@ -0,0 +1,181 @@ +import { describe, it } from "@std/testing/bdd"; +import { expect } from "@std/expect"; + +import { testWeb } from "@tests/common/index.ts"; + +describe("components/engine/audio", () => { + it("has default volume of 0.75", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/audio/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + return engine.volume(); + }); + + expect(result).toBe(0.75); + }); + + it("adjustVolume updates the global volume signal", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/audio/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + engine.adjustVolume({ volume: 0.5 }); + return engine.volume(); + }); + + expect(result).toBe(0.5); + }); + + it("adjustVolume clamps to the provided value", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/audio/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + engine.adjustVolume({ volume: 1.0 }); + return engine.volume(); + }); + + expect(result).toBe(1.0); + }); + + it("isPlaying returns false with no items", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/audio/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + return engine.isPlaying(); + }); + + expect(result).toBe(false); + }); + + it("supply with URL items updates the items signal", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/audio/element.js"); + const { trackA, trackB } = await import("~/testing/sample/tracks.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + + engine.supply({ + audio: [ + { id: "audio-a", url: trackA.uri, isPreload: false, track: trackA }, + { id: "audio-b", url: trackB.uri, isPreload: false, track: trackB }, + ], + }); + + return engine.items().map((i) => i.id); + }); + + expect(result).toEqual(["audio-a", "audio-b"]); + }); + + it("supply with same IDs does not update items signal", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/audio/element.js"); + const { trackA } = await import("~/testing/sample/tracks.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + + const item = { id: "audio-a", url: trackA.uri, isPreload: false, track: trackA }; + + engine.supply({ audio: [item] }); + const itemsAfterFirst = engine.items(); + + engine.supply({ audio: [item] }); + const itemsAfterSecond = engine.items(); + + // Same reference means the signal was not updated + return itemsAfterFirst === itemsAfterSecond; + }); + + expect(result).toBe(true); + }); + + it("supply replaces items when IDs change", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/audio/element.js"); + const { trackA, trackB } = await import("~/testing/sample/tracks.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + + engine.supply({ + audio: [ + { id: "audio-a", url: trackA.uri, isPreload: false, track: trackA }, + ], + }); + + engine.supply({ + audio: [ + { id: "audio-b", url: trackB.uri, isPreload: false, track: trackB }, + ], + }); + + return engine.items().map((i) => i.id); + }); + + expect(result).toEqual(["audio-b"]); + }); + + it("supply with isPreload change triggers items update", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/audio/element.js"); + const { trackA } = await import("~/testing/sample/tracks.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + + engine.supply({ + audio: [ + { id: "audio-a", url: trackA.uri, isPreload: true, track: trackA }, + ], + }); + + engine.supply({ + audio: [ + { id: "audio-a", url: trackA.uri, isPreload: false, track: trackA }, + ], + }); + + return engine.items()[0]?.isPreload; + }); + + expect(result).toBe(false); + }); + + it("persists volume to localStorage", async () => { + const stored = await testWeb(async () => { + const mod = await import("~/components/engine/audio/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + engine.adjustVolume({ volume: 0.3 }); + + for (let i = 0; i < localStorage.length; i++) { + const key = localStorage.key(i)!; + if (key.includes("engine/audio") && key.endsWith("/volume")) { + return localStorage.getItem(key); + } + } + return null; + }); + + expect(stored).toBe("0.3"); + }); + + it("restores volume from localStorage on connect", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/audio/element.js"); + + // Set volume with first engine instance + const engine1 = new mod.CLASS(); + document.body.append(engine1); + engine1.adjustVolume({ volume: 0.4 }); + + // Second instance reads from localStorage + const engine2 = new mod.CLASS(); + document.body.append(engine2); + return engine2.volume(); + }); + + expect(result).toBe(0.4); + }); +}); diff --git a/tests/components/engine/repeat-shuffle/test.ts b/tests/components/engine/repeat-shuffle/test.ts new file mode 100644 index 00000000..ba882af2 --- /dev/null +++ b/tests/components/engine/repeat-shuffle/test.ts @@ -0,0 +1,176 @@ +import { describe, it } from "@std/testing/bdd"; +import { expect } from "@std/expect"; + +import { testWeb } from "@tests/common/index.ts"; + +describe("components/engine/repeat-shuffle", () => { + it("defaults to false for both repeat and shuffle", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/engine/repeat-shuffle/element.js" + ); + const engine = new mod.CLASS(); + document.body.append(engine); + return { repeat: engine.repeat(), shuffle: engine.shuffle() }; + }); + + expect(result.repeat).toBe(false); + expect(result.shuffle).toBe(false); + }); + + it("setRepeat sets repeat to true", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/engine/repeat-shuffle/element.js" + ); + const engine = new mod.CLASS(); + document.body.append(engine); + await engine.setRepeat(true); + return engine.repeat(); + }); + + expect(result).toBe(true); + }); + + it("setRepeat sets repeat back to false", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/engine/repeat-shuffle/element.js" + ); + const engine = new mod.CLASS(); + document.body.append(engine); + await engine.setRepeat(true); + await engine.setRepeat(false); + return engine.repeat(); + }); + + expect(result).toBe(false); + }); + + it("setShuffle sets shuffle to true", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/engine/repeat-shuffle/element.js" + ); + const engine = new mod.CLASS(); + document.body.append(engine); + await engine.setShuffle(true); + return engine.shuffle(); + }); + + expect(result).toBe(true); + }); + + it("setShuffle sets shuffle back to false", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/engine/repeat-shuffle/element.js" + ); + const engine = new mod.CLASS(); + document.body.append(engine); + await engine.setShuffle(true); + await engine.setShuffle(false); + return engine.shuffle(); + }); + + expect(result).toBe(false); + }); + + it("repeat and shuffle are independent", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/engine/repeat-shuffle/element.js" + ); + const engine = new mod.CLASS(); + document.body.append(engine); + await engine.setRepeat(true); + return { repeat: engine.repeat(), shuffle: engine.shuffle() }; + }); + + expect(result.repeat).toBe(true); + expect(result.shuffle).toBe(false); + }); + + it("persists repeat to localStorage", async () => { + const stored = await testWeb(async () => { + const mod = await import( + "~/components/engine/repeat-shuffle/element.js" + ); + const engine = new mod.CLASS(); + document.body.append(engine); + await engine.setRepeat(true); + + // Find the key by iterating localStorage + for (let i = 0; i < localStorage.length; i++) { + const key = localStorage.key(i)!; + if (key.includes("repeat-shuffle") && key.endsWith("/repeat")) { + return localStorage.getItem(key); + } + } + return null; + }); + + expect(stored).toBe("true"); + }); + + it("persists shuffle to localStorage", async () => { + const stored = await testWeb(async () => { + const mod = await import( + "~/components/engine/repeat-shuffle/element.js" + ); + const engine = new mod.CLASS(); + document.body.append(engine); + await engine.setShuffle(true); + + for (let i = 0; i < localStorage.length; i++) { + const key = localStorage.key(i)!; + if (key.includes("repeat-shuffle") && key.endsWith("/shuffle")) { + return localStorage.getItem(key); + } + } + return null; + }); + + expect(stored).toBe("true"); + }); + + it("restores repeat state from localStorage on connect", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/engine/repeat-shuffle/element.js" + ); + + // Set state with first engine instance + const engine1 = new mod.CLASS(); + document.body.append(engine1); + await engine1.setRepeat(true); + + // Second instance reads same localStorage key + const engine2 = new mod.CLASS(); + document.body.append(engine2); + return engine2.repeat(); + }); + + expect(result).toBe(true); + }); + + it("restores shuffle state from localStorage on connect", async () => { + const result = await testWeb(async () => { + const mod = await import( + "~/components/engine/repeat-shuffle/element.js" + ); + + // Set state with first engine instance + const engine1 = new mod.CLASS(); + document.body.append(engine1); + await engine1.setShuffle(true); + + // Second instance reads same localStorage key + const engine2 = new mod.CLASS(); + document.body.append(engine2); + return engine2.shuffle(); + }); + + expect(result).toBe(true); + }); +}); diff --git a/tests/components/engine/scope/test.ts b/tests/components/engine/scope/test.ts new file mode 100644 index 00000000..9cba8a38 --- /dev/null +++ b/tests/components/engine/scope/test.ts @@ -0,0 +1,216 @@ +import { describe, it } from "@std/testing/bdd"; +import { expect } from "@std/expect"; + +import { testWeb } from "@tests/common/index.ts"; + +describe("components/engine/scope", () => { + it("has default sortBy of ['createdAt']", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/scope/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + return engine.sortBy(); + }); + + expect(result).toEqual(["createdAt"]); + }); + + it("has default sortDirection of 'desc'", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/scope/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + return engine.sortDirection(); + }); + + expect(result).toBe("desc"); + }); + + it("has default groupBy of undefined", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/scope/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + return engine.groupBy() ?? null; + }); + + expect(result).toBe(null); + }); + + it("has default playlist of undefined", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/scope/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + return engine.playlist() ?? null; + }); + + expect(result).toBe(null); + }); + + it("has default searchTerm of undefined", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/scope/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + return engine.searchTerm() ?? null; + }); + + expect(result).toBe(null); + }); + + it("setGroupBy updates groupBy", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/scope/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + await engine.setGroupBy("artist"); + return engine.groupBy(); + }); + + expect(result).toBe("artist"); + }); + + it("setGroupBy with undefined clears groupBy", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/scope/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + await engine.setGroupBy("artist"); + await engine.setGroupBy(undefined); + return engine.groupBy() ?? null; + }); + + expect(result).toBe(null); + }); + + it("setPlaylist updates playlist", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/scope/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + await engine.setPlaylist("playlist-123"); + return engine.playlist(); + }); + + expect(result).toBe("playlist-123"); + }); + + it("setSearchTerm updates searchTerm", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/scope/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + await engine.setSearchTerm("hello world"); + return engine.searchTerm(); + }); + + expect(result).toBe("hello world"); + }); + + it("setSortBy updates sortBy", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/scope/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + await engine.setSortBy(["title", "artist"]); + return engine.sortBy(); + }); + + expect(result).toEqual(["title", "artist"]); + }); + + it("setSortDirection updates sortDirection", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/scope/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + await engine.setSortDirection("asc"); + return engine.sortDirection(); + }); + + expect(result).toBe("asc"); + }); + + it("revertToDefaultSort resets sortBy and sortDirection", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/scope/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + await engine.setSortBy(["title"]); + await engine.setSortDirection("asc"); + await engine.revertToDefaultSort(); + return { sortBy: engine.sortBy(), sortDirection: engine.sortDirection() }; + }); + + expect(result.sortBy).toEqual(["createdAt"]); + expect(result.sortDirection).toBe("desc"); + }); + + it("persists groupBy to localStorage", async () => { + const stored = await testWeb(async () => { + const mod = await import("~/components/engine/scope/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + await engine.setGroupBy("album"); + + for (let i = 0; i < localStorage.length; i++) { + const key = localStorage.key(i)!; + if (key.includes("scope") && key.endsWith("/groupBy")) { + return localStorage.getItem(key); + } + } + return null; + }); + + expect(stored).toBe("album"); + }); + + it("persists sortBy to localStorage as JSON", async () => { + const stored = await testWeb(async () => { + const mod = await import("~/components/engine/scope/element.js"); + const engine = new mod.CLASS(); + document.body.append(engine); + await engine.setSortBy(["title", "artist"]); + + for (let i = 0; i < localStorage.length; i++) { + const key = localStorage.key(i)!; + if (key.includes("scope") && key.endsWith("/sortBy")) { + return localStorage.getItem(key); + } + } + return null; + }); + + expect(stored).toBe('["title","artist"]'); + }); + + it("restores values from localStorage on connect", async () => { + const result = await testWeb(async () => { + const mod = await import("~/components/engine/scope/element.js"); + + // Set state with first engine instance + const engine1 = new mod.CLASS(); + document.body.append(engine1); + await engine1.setGroupBy("artist"); + await engine1.setSearchTerm("test"); + await engine1.setSortBy(["title"]); + await engine1.setSortDirection("asc"); + + // Second instance reads same localStorage keys + const engine2 = new mod.CLASS(); + document.body.append(engine2); + return { + groupBy: engine2.groupBy(), + searchTerm: engine2.searchTerm(), + sortBy: engine2.sortBy(), + sortDirection: engine2.sortDirection(), + }; + }); + + expect(result.groupBy).toBe("artist"); + expect(result.searchTerm).toBe("test"); + expect(result.sortBy).toEqual(["title"]); + expect(result.sortDirection).toBe("asc"); + }); +}); -- knotmirror 0.0.1