From 78eaecb1198768522550860693587d715e64ed23 Mon Sep 17 00:00:00 2001 From: Sulthan Zaki Date: Mon, 3 Aug 2026 17:00:54 +0700 Subject: [PATCH] Add test coverage for comix coverFromPage() Extend the test harness's document stub with a querySelectorAll("img[alt]") fake (module-level pageImages fixture, mirroring metaTags), then assert on p.cover for a matching alt and for no match. Previously the guard in coverFromPage() always short-circuited under test since querySelectorAll didn't exist on the stub, so the alt-matching loop had zero coverage. --- userscript/test/logic.test.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/userscript/test/logic.test.js b/userscript/test/logic.test.js index 3f79812..2271956 100644 --- a/userscript/test/logic.test.js +++ b/userscript/test/logic.test.js @@ -31,6 +31,9 @@ globalThis.location = { // og: meta tags the adapters read through meta(). Reassigned per test. let metaTags = {}; +// img[alt] elements comix's coverFromPage() scans. Reassigned per test; each +// entry is {alt, src}. +let pageImages = []; globalThis.document = { querySelector(sel) { const m = sel.match(/^meta\[property="([^"]+)"\]$/); @@ -38,6 +41,12 @@ globalThis.document = { const v = metaTags[m[1]]; return v == null ? null : { getAttribute: () => v }; }, + querySelectorAll(sel) { + if (sel !== "img[alt]") return []; + return pageImages.map((img) => ({ + getAttribute: (attr) => img[attr] ?? null, + })); + }, addEventListener() {}, body: undefined, }; @@ -214,6 +223,26 @@ test("comix parses decimal chapter numbers", () => { assert.equal(p.chapterNum, 80.5); }); +test("comix.detect reads the cover from an img whose alt matches the cleaned title", () => { + metaTags = { "og:title": "Dungeons and Crayons ยท Ch.80" }; + pageImages = [ + { alt: "Some Other Series", src: "https://cdn.example/other.jpg" }, + { alt: "Dungeons and Crayons", src: "https://cdn.example/cover.jpg" }, + ]; + const p = comix.detect( + loc("https://comix.to/title/n8we-dungeons-and-crayons/11139891-chapter-80") + ); + assert.equal(p.cover, "https://cdn.example/cover.jpg"); +}); + +test("comix.detect leaves cover empty when no img alt matches the title", () => { + metaTags = { "og:title": "Dungeons and Crayons" }; + pageImages = [{ alt: "Some Other Series", src: "https://cdn.example/other.jpg" }]; + const p = comix.detect(loc("https://comix.to/title/n8we-dungeons-and-crayons")); + assert.equal(p.cover, ""); + pageImages = []; +}); + test("comix ignores unrelated paths", () => { assert.equal(comix.detect(loc("https://comix.to/browse")).type, "other"); });