From 177e3bd8dbf7afb92c1d620603b864a2941cba5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Rodr=C3=ADguez?= Date: Fri, 27 Nov 2026 17:01:50 -0600 Subject: [PATCH 1/3] Add controller matching for media cards readMetadata treats an inactive controller as no session and rethrows other errors. mediaSessions pairs each tab with its active metadata, and matchCards maps each card to one session by exact title and artist, so a session is claimed at most once. --- src/controllers.js | 46 +++++++++++ test/controllers.test.js | 161 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 207 insertions(+) create mode 100644 src/controllers.js create mode 100644 test/controllers.test.js diff --git a/src/controllers.js b/src/controllers.js new file mode 100644 index 0000000..c979534 --- /dev/null +++ b/src/controllers.js @@ -0,0 +1,46 @@ +const NOT_AVAILABLE = "NS_ERROR_NOT_AVAILABLE"; +const TITLE_SELECTOR = ".zen-media-title"; +const ARTIST_SELECTOR = ".zen-media-artist"; + +export function readMetadata(controller) { + try { + return controller.getMetadata(); + } catch (error) { + if (error?.name === NOT_AVAILABLE) return null; + throw error; + } +} + +function sessionOf({ linkedBrowser: browser }) { + const controller = browser?.browsingContext?.mediaController; + if (!controller) return null; + const metadata = readMetadata(controller); + return metadata && { browser, metadata }; +} + +export function mediaSessions({ tabs }) { + return [...tabs].map(sessionOf).filter((session) => session !== null); +} + +const labelText = (card, selector) => card.querySelector(selector)?.textContent ?? ""; + +export function readCardTrack(card) { + return { title: labelText(card, TITLE_SELECTOR), artist: labelText(card, ARTIST_SELECTOR) }; +} + +const isSameTrack = ({ title, artist }, metadata) => + title === metadata.title && artist === metadata.artist; + +export function matchCards(cards, sessions) { + const unclaimed = new Set(sessions); + const matches = new Map(); + for (const card of cards) { + const shown = readCardTrack(card); + if (!shown.title) continue; + const session = unclaimed.values().find(({ metadata }) => isSameTrack(shown, metadata)); + if (!session) continue; + unclaimed.delete(session); + matches.set(card, session); + } + return matches; +} diff --git a/test/controllers.test.js b/test/controllers.test.js new file mode 100644 index 0000000..f8c340e --- /dev/null +++ b/test/controllers.test.js @@ -0,0 +1,161 @@ +// @vitest-environment happy-dom +import { describe, expect, it } from "vitest"; +import { matchCards, mediaSessions, readCardTrack, readMetadata } from "../src/controllers.js"; +import report from "./fixtures/zen-1.24.3b.json" with { type: "json" }; + +function notAvailable() { + return Object.assign(new Error(""), { name: "NS_ERROR_NOT_AVAILABLE" }); +} + +function controller(metadata) { + return { getMetadata: () => metadata }; +} + +function inactiveController() { + return { + getMetadata: () => { + throw notAvailable(); + }, + }; +} + +function tab(mediaController) { + return { linkedBrowser: { browsingContext: { mediaController } } }; +} + +function track(title, artist) { + return { title, artist, album: "", artwork: [] }; +} + +function card(title, artist) { + const element = document.createElement("toolbaritem"); + element.className = "zen-media-card"; + element.innerHTML = ``; + element.querySelector(".zen-media-title").textContent = title; + element.querySelector(".zen-media-artist").textContent = artist; + return element; +} + +function session(title, artist) { + return { browser: {}, metadata: track(title, artist) }; +} + +function fixtureCards() { + const toolbar = new DOMParser().parseFromString(report.toolbar.html, "application/xml"); + return [...toolbar.querySelectorAll("toolbar > .zen-media-card")]; +} + +describe("readMetadata", () => { + it("returns the controller's metadata", () => { + const metadata = track("Song", "Band"); + + expect(readMetadata(controller(metadata))).toBe(metadata); + }); + + it("returns null when the controller is inactive", () => { + expect(readMetadata(inactiveController())).toBeNull(); + }); + + it("rethrows other errors", () => { + const broken = { + getMetadata: () => { + throw new TypeError("boom"); + }, + }; + + expect(() => readMetadata(broken)).toThrow(TypeError); + }); +}); + +describe("mediaSessions", () => { + it("pairs each tab's browser with its controller's metadata", () => { + const playing = tab(controller(track("Song", "Band"))); + + expect(mediaSessions({ tabs: [playing] })).toEqual([ + { browser: playing.linkedBrowser, metadata: track("Song", "Band") }, + ]); + }); + + it("skips tabs whose controller is inactive", () => { + const tabs = [tab(inactiveController()), tab(controller(track("Song", "Band")))]; + + expect(mediaSessions({ tabs }).map(({ metadata }) => metadata.title)).toEqual(["Song"]); + }); + + it("skips tabs without a browser, browsing context or controller", () => { + const tabs = [{}, { linkedBrowser: {} }, { linkedBrowser: { browsingContext: {} } }]; + + expect(mediaSessions({ tabs })).toEqual([]); + }); + + it("returns no sessions when there are no tabs", () => { + expect(mediaSessions({ tabs: [] })).toEqual([]); + }); +}); + +describe("readCardTrack", () => { + it("reads the title and artist shown on a card", () => { + expect(readCardTrack(card("Song", "Band"))).toEqual({ title: "Song", artist: "Band" }); + }); + + it("reads empty strings from a card without labels", () => { + expect(readCardTrack(document.createElement("toolbaritem"))).toEqual({ + title: "", + artist: "", + }); + }); + + it("reads the recorded Zen 1.24.3b card", () => { + const [recorded] = fixtureCards(); + + expect(readCardTrack(recorded)).toEqual({ title: "Flames of Fate", artist: "DJ Noriken" }); + }); +}); + +describe("matchCards", () => { + it("matches a card to the session with the same title and artist", () => { + const songCard = card("Song", "Band"); + const other = session("Other", "Band"); + const song = session("Song", "Band"); + + expect(matchCards([songCard], [other, song])).toEqual(new Map([[songCard, song]])); + }); + + it("does not match a session whose artist differs", () => { + expect(matchCards([card("Song", "Band")], [session("Song", "Cover Band")]).size).toBe(0); + }); + + it("gives each session to at most one card", () => { + const first = card("Song", "Band"); + const second = card("Song", "Band"); + const firstSession = session("Song", "Band"); + const secondSession = session("Song", "Band"); + + const matches = matchCards([first, second], [firstSession, secondSession]); + + expect(matches.get(first)).toBe(firstSession); + expect(matches.get(second)).toBe(secondSession); + }); + + it("leaves a card unmatched when every matching session is taken", () => { + const second = card("Song", "Band"); + + const matches = matchCards([card("Song", "Band"), second], [session("Song", "Band")]); + + expect(matches.has(second)).toBe(false); + }); + + it("does not match a card that shows no title yet", () => { + expect(matchCards([card("", "")], [session("", "")]).size).toBe(0); + }); + + it("matches the recorded Zen 1.24.3b card to its tab's controller", () => { + const [recorded] = fixtureCards(); + const [active] = report.controllers.active; + const playing = tab(controller(active.metadata)); + + const matches = matchCards([recorded], mediaSessions({ tabs: [playing] })); + + expect(matches.get(recorded)?.browser).toBe(playing.linkedBrowser); + }); +}); From 9afeaf80dbd605da9fb3845462ff034eb78cdfb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Rodr=C3=ADguez?= Date: Fri, 27 Nov 2026 17:01:50 -0600 Subject: [PATCH 2/3] Mark milestone 3 done and log controller matching Records the controller matching change in the changelog and moves the roadmap to milestone 4 as next. --- CHANGELOG.md | 1 + docs/roadmap.md | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02e5f10..8934b9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - Spike: `npm run spike` prints a read-only Browser Console snippet that reports Zen's media card DOM and media controller metadata ([docs/spike.md](docs/spike.md)). - Test fixture recorded with the spike on Zen 1.24.3b, with tests that pin the media card DOM, metadata and CSP facts the mod relies on. - Artwork selection (`src/artwork.js`): parses Media Session `sizes`, keeps only `https:` and image `data:` sources and picks the smallest image whose shorter side is big enough, falling back to the largest. +- Controller matching (`src/controllers.js`): reads metadata from each tab's media controller, treating an inactive controller (`NS_ERROR_NOT_AVAILABLE`) as no session, and matches each media card to a session by its title and artist, giving each session to at most one card. - Claude Code project skills: `unslop` (code style, summarized in the contributing guide), `mine` (attribution policy) and `ship-it` (commit, review and fix loop). [Unreleased]: https://github.com/rodzy/Zen-Media-Viz/commits/main diff --git a/docs/roadmap.md b/docs/roadmap.md index 446e8e3..c5607a2 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -7,8 +7,8 @@ The first release is built in milestones. Each milestone is one pull request, wr | 0 | Open-source & tooling shell | Test, lint, format and bundle tooling, CI, contributor docs and templates | Done | | 1 | Spike & fixtures | A read-only console snippet that checks Zen's media card DOM and real metadata on the maintainer's Zen version, saved as test fixtures | Done | | 2 | Artwork selection | `src/artwork.js`: parse `sizes`, filter to usable `https:`/`data:` sources, pick the best image | Done | -| 3 | Controller matching | `src/controllers.js`: safely read metadata from each tab's media controller and match it to a media card | Next | -| 4 | Card DOM | `src/card.js`: add, update and remove the thumbnail element in a media card | | +| 3 | Controller matching | `src/controllers.js`: safely read metadata from each tab's media controller and match it to a media card | Done | +| 4 | Card DOM | `src/card.js`: add, update and remove the thumbnail element in a media card | Next | | 5 | Runtime wiring & Sine | Watch cards and metadata changes, clean teardown, `theme.json`, first run inside Zen | | | 6 | Styling & polish | `chrome.css` for the thumbnail, narrow-sidebar and collapsed states, manual checklist | | | 7 | Release v0.1.0 | Install guide, screenshot, version bump and tag | | From 67772499fa0c2f90d4c84f39db4c6a20b99e8a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Rodr=C3=ADguez?= Date: Fri, 27 Nov 2026 17:03:31 -0600 Subject: [PATCH 3/3] Test controller matching across session order and inactive tabs Cover a card matching its own session whatever order the sessions arrive in, and a card staying unmatched when its tab's controller is inactive. --- test/controllers.test.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/controllers.test.js b/test/controllers.test.js index f8c340e..4a0fe4b 100644 --- a/test/controllers.test.js +++ b/test/controllers.test.js @@ -145,6 +145,28 @@ describe("matchCards", () => { expect(matches.has(second)).toBe(false); }); + it("matches each card to its own session whatever order the sessions come in", () => { + const songCard = card("Song", "Band"); + const otherCard = card("Other", "Band"); + const song = session("Song", "Band"); + const other = session("Other", "Band"); + + const matches = matchCards([songCard, otherCard], [other, song]); + + expect(matches).toEqual( + new Map([ + [songCard, song], + [otherCard, other], + ]), + ); + }); + + it("leaves a card unmatched when its tab's controller has gone inactive", () => { + const tabs = [tab(inactiveController()), tab(controller(track("Other", "Band")))]; + + expect(matchCards([card("Song", "Band")], mediaSessions({ tabs })).size).toBe(0); + }); + it("does not match a card that shows no title yet", () => { expect(matchCards([card("", "")], [session("", "")]).size).toBe(0); });