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.
This commit is contained in:
2026-08-03 17:00:54 +07:00
parent e392ec3de0
commit 78eaecb119
+29
View File
@@ -31,6 +31,9 @@ globalThis.location = {
// og: meta tags the adapters read through meta(). Reassigned per test. // og: meta tags the adapters read through meta(). Reassigned per test.
let metaTags = {}; let metaTags = {};
// img[alt] elements comix's coverFromPage() scans. Reassigned per test; each
// entry is {alt, src}.
let pageImages = [];
globalThis.document = { globalThis.document = {
querySelector(sel) { querySelector(sel) {
const m = sel.match(/^meta\[property="([^"]+)"\]$/); const m = sel.match(/^meta\[property="([^"]+)"\]$/);
@@ -38,6 +41,12 @@ globalThis.document = {
const v = metaTags[m[1]]; const v = metaTags[m[1]];
return v == null ? null : { getAttribute: () => v }; return v == null ? null : { getAttribute: () => v };
}, },
querySelectorAll(sel) {
if (sel !== "img[alt]") return [];
return pageImages.map((img) => ({
getAttribute: (attr) => img[attr] ?? null,
}));
},
addEventListener() {}, addEventListener() {},
body: undefined, body: undefined,
}; };
@@ -214,6 +223,26 @@ test("comix parses decimal chapter numbers", () => {
assert.equal(p.chapterNum, 80.5); 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", () => { test("comix ignores unrelated paths", () => {
assert.equal(comix.detect(loc("https://comix.to/browse")).type, "other"); assert.equal(comix.detect(loc("https://comix.to/browse")).type, "other");
}); });