BlueGit

timfa hfy-novels

Files

hfy-novels / scripts / test-plugin.js

test-plugin.js

test-plugin.js 37.6 KiB Download Raw
1#!/usr/bin/env node
2"use strict";
3
4/**
5 * Laadt de plugin zoals LNReader dat doet: custom require-allowlist,
6 * `new Function`, en daarna popularNovels / searchNovels / parseNovel /
7 * parseChapter. De index komt van een lokale HTTP-server (testdata of
8 * public/). parseChapter, searchNovels, popularNovels en parseNovel
9 * gebruiken gemockte Reddit-JSON, vallen terug op Arctic Shift en daarna
10 * op de statische index; optioneel een live verzoek als HFY_LIVE=1.
11 */
12
13const fs = require("fs");
14const http = require("http");
15const path = require("path");
16const zlib = require("zlib");
17
18const ROOT = path.resolve(__dirname, "..");
19const PLUGIN = fs.readFileSync(path.join(ROOT, "plugin", "hfy-novels.js"), "utf8");
20
21const LIVE_COVER_REDDIT =
22 "https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/covers/reddit.png?rev=master";
23const LIVE_COVER_ARCTIC =
24 "https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/covers/arctic-shift.png?rev=master";
25const LIVE_COVER_INDEX =
26 "https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/covers/index.png?rev=master";
27
28const NovelStatus = {
29 Completed: "Completed",
30 Ongoing: "Ongoing",
31 OnHiatus: "On Hiatus",
32 Unknown: "Unknown",
33};
34const FilterTypes = { Picker: "Picker", Switch: "Switch", CheckboxGroup: "CheckboxGroup" };
35
36function mimeFor(file, asImg) {
37 if (file.endsWith(".svg")) return asImg ? "image/svg+xml" : "text/plain; charset=utf-8";
38 if (file.endsWith(".json")) return "text/plain; charset=utf-8"; // BlueGit serveert raw ook als text/plain
39 if (file.endsWith(".js")) return "text/plain; charset=utf-8";
40 if (file.endsWith(".png")) return "image/png";
41 return "application/octet-stream";
42}
43
44function startServer(rootDir, chapterHandler, redditHandler) {
45 return new Promise((resolve, reject) => {
46 const server = http.createServer((req, res) => {
47 const url = new URL(req.url, "http://127.0.0.1");
48 if (url.pathname.startsWith("/api/posts/ids") || url.pathname.startsWith("/api/posts/search") || url.pathname.startsWith("/api/subreddits/wikis")) {
49 return chapterHandler(url, res);
50 }
51 if (redditHandler && /\/r\/HFY\//i.test(url.pathname)) {
52 return redditHandler(url, res, req);
53 }
54 if (redditHandler && url.pathname.startsWith("/api/info")) {
55 return redditHandler(url, res, req);
56 }
57 // ?rev=master zoals BlueGit raw-URLs; voor het bestand negeren we de query.
58 let rel = decodeURIComponent(url.pathname).replace(/^\/+/, "");
59 const file = path.join(rootDir, rel);
60 if (!file.startsWith(path.resolve(rootDir))) {
61 res.writeHead(403);
62 res.end("forbidden");
63 return;
64 }
65 fs.readFile(file, (err, data) => {
66 if (err) {
67 res.writeHead(404, { "Content-Type": "text/plain" });
68 res.end("404 " + rel);
69 return;
70 }
71 const asImg = url.searchParams.get("as") === "img";
72 res.writeHead(200, { "Content-Type": mimeFor(file, asImg) });
73 res.end(data);
74 });
75 });
76 server.listen(0, "127.0.0.1", () => {
77 const { port } = server.address();
78 resolve({ server, port, url: "http://127.0.0.1:" + port });
79 });
80 server.on("error", reject);
81 });
82}
83
84function redditListingFor(id) {
85 return [
86 {
87 kind: "Listing",
88 data: {
89 children: [
90 {
91 kind: "t3",
92 data: {
93 id,
94 title: "The Last Terran - Part 1",
95 author: "WriterOne",
96 selftext: "It begins.\n\n[Next](https://www.reddit.com/r/HFY/comments/aaa222/x/)",
97 selftext_html: "<div class=\"md\"><p>It begins.</p></div>",
98 permalink: "/r/HFY/comments/" + id + "/",
99 },
100 },
101 ],
102 },
103 },
104 { kind: "Listing", data: { children: [] } },
105 ];
106}
107
108function loginHtml() {
109 return "<!DOCTYPE html><html><head><title>reddit.com: Log in</title></head><body>Log in to Reddit to continue. <a href=\"https://www.reddit.com/login\">log in</a></body></html>";
110}
111
112function blockHtml() {
113 return "<body class=theme-beta><div>Request blocked. reason=lor2</div></body>";
114}
115
116function defaultRedditHandler(mode) {
117 return function redditHandler(url, res) {
118 const m = url.pathname.match(/\/comments\/([0-9a-z]{4,10})\//i);
119 if (!m) {
120 res.writeHead(404, { "Content-Type": "text/plain" });
121 res.end("no default reddit mock for " + url.pathname);
122 return;
123 }
124 const id = m[1].toLowerCase();
125 if (mode === "ok") {
126 res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
127 res.end(JSON.stringify(redditListingFor(id)));
128 return;
129 }
130 if (mode === "403") {
131 res.writeHead(403, { "Content-Type": "text/html; charset=utf-8" });
132 res.end(blockHtml());
133 return;
134 }
135 if (mode === "login-html") {
136 res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
137 res.end(loginHtml());
138 return;
139 }
140 res.writeHead(500, { "Content-Type": "text/plain" });
141 res.end("reddit mock error");
142 };
143}
144
145function defaultChapterHandler(url, res) {
146 if (!url.pathname.startsWith("/api/posts/ids")) {
147 res.writeHead(404, { "Content-Type": "text/plain" });
148 res.end("no default arctic mock for " + url.pathname);
149 return;
150 }
151 const ids = url.searchParams.get("ids") || "";
152 const id = ids.split(",")[0];
153 const fixture = {
154 data: [
155 {
156 id,
157 title: "The Last Terran - Part 1",
158 author: "WriterOne",
159 selftext: "It begins.\n\n[Next](https://www.reddit.com/r/HFY/comments/aaa222/x/)",
160 selftext_html: "<p>It begins.</p>",
161 permalink: "/r/HFY/comments/" + id + "/",
162 },
163 ],
164 };
165 res.writeHead(200, { "Content-Type": "application/json" });
166 res.end(JSON.stringify(fixture));
167}
168
169function wrapResponse(res) {
170 return {
171 ok: res.ok,
172 status: res.status,
173 headers: res.headers,
174 async text() {
175 return await res.text();
176 },
177 async json() {
178 const text = await res.text();
179 return JSON.parse(text);
180 },
181 url: res.url,
182 };
183}
184
185function makeFetchApi(origin) {
186 return async function fetchApi(url, init) {
187 let target = String(url);
188 // Stuur Reddit-JSON naar de lokale testhost, zodat we 200/403/login kunnen simuleren.
189 if (/^https?:\/\/(www\.|old\.)?reddit\.com\//i.test(target)) {
190 const parsed = new URL(target);
191 target = origin + parsed.pathname + parsed.search;
192 }
193 const res = await fetch(target, init);
194 return wrapResponse(res);
195 };
196}
197
198function loadPlugin(storageMap, fetchApi) {
199 const allow = {
200 "@libs/fetch": { fetchApi, fetchText: async (u, i) => (await fetchApi(u, i)).text() },
201 "@libs/filterInputs": { FilterTypes },
202 "@libs/defaultCover": { defaultCover: "https://example.invalid/cover.png" },
203 "@libs/novelStatus": { NovelStatus },
204 "@libs/storage": {
205 storage: {
206 get(key) {
207 return storageMap[key];
208 },
209 set(key, value, expires) {
210 storageMap[key] = value;
211 if (expires != null) {
212 storageMap[key + "__expires"] = expires;
213 }
214 },
215 },
216 },
217 };
218 const pluginModule = { exports: {} };
219 const fn = new Function("exports", "require", "module", PLUGIN);
220 fn(pluginModule.exports, (name) => {
221 if (!allow[name]) {
222 throw new Error("blocked require: " + name);
223 }
224 return allow[name];
225 }, pluginModule);
226 const plugin = pluginModule.exports.default;
227 if (!plugin) {
228 throw new Error("geen default export");
229 }
230 for (const method of ["popularNovels", "searchNovels", "parseNovel", "parseChapter"]) {
231 if (typeof plugin[method] !== "function") {
232 throw new Error("plugin mist " + method);
233 }
234 }
235 return plugin;
236}
237
238function assert(cond, msg) {
239 if (!cond) {
240 throw new Error("FAIL: " + msg);
241 }
242}
243
244function wikiIndexPayload(markdown) {
245 return { kind: "wikipage", data: { content_md: markdown, revision_date: 1700000000 } };
246}
247
248function listingOf(posts) {
249 return {
250 kind: "Listing",
251 data: {
252 children: posts.map((p) => ({ kind: "t3", data: p })),
253 after: null,
254 },
255 };
256}
257
258function samplePost(id, title, author, extras) {
259 const extra = extras || {};
260 return Object.assign(
261 {
262 id,
263 title,
264 author,
265 created_utc: extra.created_utc || 1700000000,
266 permalink: "/r/HFY/comments/" + id + "/",
267 selftext: extra.selftext || "",
268 selftext_html: extra.selftext_html || "",
269 link_flair_text: extra.flair || "OC-Series",
270 over_18: !!extra.over_18,
271 is_self: true,
272 },
273 extra.more || {},
274 );
275}
276
277async function testLiveCatalog(origin, fixtureRoot, fetchApi, assert) {
278 const wikiIndex = fs.readFileSync(path.join(ROOT, "testdata", "wiki", "index.md"), "utf8");
279 const wikiTerran = fs.readFileSync(path.join(ROOT, "testdata", "wiki", "pages", "the_last_terran.md"), "utf8");
280 const state = { redditMode: "ok", arcticMode: "ok", wikiHits: 0, searchHits: 0, arcticSearchHits: 0, arcticWikiHits: 0, infoHits: 0 };
281
282 function redditHandler(url, res) {
283 if (state.redditMode === "403") {
284 res.writeHead(403, { "Content-Type": "text/html; charset=utf-8" });
285 res.end(blockHtml());
286 return;
287 }
288 if (state.redditMode === "error") {
289 res.writeHead(500, { "Content-Type": "text/plain" });
290 res.end("reddit mock error");
291 return;
292 }
293 if (/\/wiki\/series\/the_last_terran/i.test(url.pathname)) {
294 state.wikiHits += 1;
295 res.writeHead(200, { "Content-Type": "application/json" });
296 res.end(JSON.stringify(wikiIndexPayload(wikiTerran)));
297 return;
298 }
299 if (/\/wiki\/series/i.test(url.pathname)) {
300 state.wikiHits += 1;
301 res.writeHead(200, { "Content-Type": "application/json" });
302 res.end(JSON.stringify(wikiIndexPayload(wikiIndex)));
303 return;
304 }
305 if (/\/search\.json$/i.test(url.pathname)) {
306 state.searchHits += 1;
307 const q = (url.searchParams.get("q") || "").toLowerCase();
308 const posts = [];
309 if (q.indexOf("terran") !== -1 || q.indexOf("writerone") !== -1) {
310 posts.push(
311 samplePost("aaa111", "[OC] The Last Terran - Part 1", "WriterOne", {
312 flair: "OC-FirstOfSeries",
313 selftext: "[Next](https://www.reddit.com/r/HFY/comments/aaa222/x/)",
314 }),
315 );
316 }
317 if (q.indexOf("star") !== -1 || q.indexOf("boat") !== -1) {
318 posts.push(
319 samplePost("ddd001", "[OC] Star Boat - Part 1", "Skipper", {
320 flair: "OC-FirstOfSeries",
321 selftext: "[Next](https://www.reddit.com/r/HFY/comments/ddd002/x/)",
322 created_utc: 1700345600,
323 }),
324 );
325 posts.push(
326 samplePost("ddd002", "Star Boat - Part 2", "Skipper", {
327 flair: "OC-Series",
328 selftext: "[Prev](https://www.reddit.com/r/HFY/comments/ddd001/x/)",
329 created_utc: 1700432000,
330 }),
331 );
332 }
333 res.writeHead(200, { "Content-Type": "application/json" });
334 res.end(JSON.stringify(listingOf(posts)));
335 return;
336 }
337 if (/\/new\.json$/i.test(url.pathname)) {
338 state.searchHits += 1;
339 res.writeHead(200, { "Content-Type": "application/json" });
340 res.end(
341 JSON.stringify(
342 listingOf([
343 samplePost("ddd001", "[OC] Star Boat - Part 1", "Skipper", {
344 flair: "OC-FirstOfSeries",
345 created_utc: 1700345600,
346 }),
347 samplePost("meta999", "[META] Weekly thread", "ModAccount", { flair: "Meta", created_utc: 1700345700 }),
348 samplePost("aaa444", "The Last Terran - Part 4", "WriterOne", {
349 flair: "OC-Series",
350 created_utc: 1700259200,
351 }),
352 ]),
353 ),
354 );
355 return;
356 }
357 if (url.pathname.startsWith("/api/info") || /\/comments\/[0-9a-z]+\//i.test(url.pathname)) {
358 state.infoHits += 1;
359 const ids = [];
360 if (url.searchParams.get("id")) {
361 url.searchParams.get("id").split(",").forEach((raw) => ids.push(raw.replace(/^t3_/, "").toLowerCase()));
362 } else {
363 const m = url.pathname.match(/\/comments\/([0-9a-z]{4,10})\//i);
364 if (m) ids.push(m[1].toLowerCase());
365 }
366 const posts = ids.map((id) => {
367 if (id === "aaa222") {
368 return samplePost(id, "The Last Terran - Part 2", "WriterOne", {
369 selftext: "[Prev](https://www.reddit.com/r/HFY/comments/aaa111/x/) | [Next](https://www.reddit.com/r/HFY/comments/aaa333/x/)",
370 });
371 }
372 if (id === "aaa333") {
373 return samplePost(id, "The Last Terran Chapter 3", "WriterOne", {
374 selftext: "[Previous](/r/HFY/comments/aaa222/)",
375 });
376 }
377 return samplePost(id, "The Last Terran - Part 1", "WriterOne", {
378 flair: "OC-FirstOfSeries",
379 selftext: "[Next](https://www.reddit.com/r/HFY/comments/aaa222/x/)",
380 });
381 });
382 res.writeHead(200, { "Content-Type": "application/json" });
383 if (/\/comments\//i.test(url.pathname)) {
384 res.end(JSON.stringify(redditListingFor(ids[0] || "aaa111")));
385 } else {
386 res.end(JSON.stringify(listingOf(posts)));
387 }
388 return;
389 }
390 res.writeHead(404, { "Content-Type": "text/plain" });
391 res.end("no reddit mock for " + url.pathname);
392 }
393
394 function arcticHandler(url, res) {
395 if (url.pathname.startsWith("/api/subreddits/wikis")) {
396 state.arcticWikiHits += 1;
397 if (state.arcticMode === "fail") {
398 res.writeHead(500, { "Content-Type": "text/plain" });
399 res.end("arctic wiki fail");
400 return;
401 }
402 const paths = url.searchParams.get("paths") || "";
403 const content = /the_last_terran/.test(paths) ? wikiTerran : wikiIndex;
404 res.writeHead(200, { "Content-Type": "application/json" });
405 res.end(JSON.stringify({ data: [{ path: paths, content }] }));
406 return;
407 }
408 if (url.pathname.startsWith("/api/posts/search")) {
409 state.arcticSearchHits += 1;
410 if (state.arcticMode === "fail") {
411 res.writeHead(500, { "Content-Type": "text/plain" });
412 res.end("arctic search fail");
413 return;
414 }
415 const q = ((url.searchParams.get("query") || "") + " " + (url.searchParams.get("title") || "")).toLowerCase();
416 const posts = [];
417 if (!q.trim() || q.indexOf("star") !== -1 || q.indexOf("boat") !== -1) {
418 posts.push(
419 samplePost("ddd001", "[OC] Star Boat - Part 1", "Skipper", {
420 flair: "OC-FirstOfSeries",
421 created_utc: 1700345600,
422 }),
423 );
424 }
425 res.writeHead(200, { "Content-Type": "application/json" });
426 res.end(JSON.stringify({ data: posts }));
427 return;
428 }
429 if (url.pathname.startsWith("/api/posts/ids")) {
430 return defaultChapterHandler(url, res);
431 }
432 res.writeHead(404);
433 res.end("no arctic mock");
434 }
435
436 const started = await startServer(fixtureRoot, arcticHandler, redditHandler);
437 const liveFetch = makeFetchApi(started.url);
438 try {
439 const storageMap = {
440 indexBaseUrl: origin,
441 indexRev: "",
442 chapterSourceUrl: started.url,
443 chapterSource: "auto",
444 };
445 const plugin = loadPlugin(storageMap, liveFetch);
446
447 state.wikiHits = 0;
448 const popular = await plugin.popularNovels(1, { showLatestNovels: false, filters: {} });
449 assert(popular.some((n) => n.name === "The Last Terran"), "live wiki-index levert The Last Terran");
450 assert(popular.some((n) => n.path.indexOf("the_last_terran") !== -1 || n.path === "wiki/the_last_terran"), "wiki-pad zonder first-id");
451 const hitsAfterFirst = state.wikiHits;
452 assert(hitsAfterFirst >= 1, "wiki-index is bij Reddit opgehaald");
453 const popularAgain = await plugin.popularNovels(1, { filters: {} });
454 assert(popularAgain.some((n) => n.name === "The Last Terran"), "cache levert opnieuw de wiki-index");
455 assert(state.wikiHits === hitsAfterFirst, "tweede popularNovels gebruikt de wiki-cache");
456 const liveCover = popular.find((n) => n.name === "The Last Terran").cover;
457 assertLiveCover(liveCover, "reddit", "live wiki-cover");
458 assert(
459 popularAgain.find((n) => n.name === "The Last Terran").cover === liveCover,
460 "gecachte wiki-index houdt de Reddit-cover",
461 );
462
463 const latest = await plugin.popularNovels(1, { showLatestNovels: true, filters: {} });
464 assert(latest.some((n) => /Star Boat/i.test(n.name)), "Latest/Nieuw toont recente OC-reeks");
465 assertLiveCover(latest.find((n) => /Star Boat/i.test(n.name)).cover, "reddit", "Latest-cover");
466 assert(latest.every((n) => !/Weekly thread/i.test(n.name)), "Meta-posts blijven buiten Nieuw");
467 const viaFilter = await plugin.popularNovels(1, { filters: { catalog: { value: "new" } } });
468 assert(viaFilter.some((n) => /Star Boat/i.test(n.name)), "filter Nieuw toont recente OC-posts");
469
470 const search = await plugin.searchNovels("terran", 1);
471 assert(search.some((n) => /Last Terran/i.test(n.name)), "searchNovels groepeert wiki + Part 1");
472 const star = await plugin.searchNovels("Star Boat", 1);
473 assert(star.length === 1 && /Star Boat/i.test(star[0].name), "losse Part 1+2 worden één novel");
474 assert(star[0].path === "series/ddd001", "nieuwe reeks gebruikt het eerste deel als id");
475 assertLiveCover(star[0].cover, "reddit", "live zoek-cover zonder index-PNG");
476
477 const novel = await plugin.parseNovel("wiki/the_last_terran");
478 assert(novel.name === "The Last Terran", "parseNovel gebruikt de wiki-titel");
479 assert(novel.author === "WriterOne", "parseNovel leest de auteur uit de wiki");
480 assert(novel.chapters.length === 3, "parseNovel volgt de wiki-TOC");
481 assert(novel.chapters[0].path === "chapter/aaa111", "eerste hoofdstukpad");
482 assert(novel.chapters[2].path === "chapter/aaa333", "laatste TOC-hoofdstuk");
483 assertLiveCover(novel.cover, "reddit", "parseNovel live-cover (wiki, geen statische PNG)");
484 const indexed = await plugin.parseNovel("series/aaa111");
485 assertLiveCover(indexed.cover, "reddit", "parseNovel van een indexreeks gebruikt Reddit-cover als live Reddit slaagt");
486
487 clearWikiCache(storageMap);
488 state.redditMode = "403";
489 state.arcticMode = "ok";
490 state.arcticWikiHits = 0;
491 const plugin403 = loadPlugin(storageMap, liveFetch);
492 const popular403 = await plugin403.popularNovels(1, { filters: {} });
493 assert(popular403.some((n) => n.name === "The Last Terran"), "403 valt terug op Arctic Shift-wiki");
494 assert(state.arcticWikiHits >= 1, "403 roept Arctic Shift-wiki aan");
495 assertLiveCover(
496 popular403.find((n) => n.name === "The Last Terran").cover,
497 "arctic",
498 "403-wiki-cover",
499 );
500
501 const search403 = await plugin403.searchNovels("Star Boat", 1);
502 assert(search403.some((n) => /Star Boat/i.test(n.name)), "zoeken valt bij 403 terug op Arctic Shift");
503 assertLiveCover(search403.find((n) => /Star Boat/i.test(n.name)).cover, "arctic", "403-zoek-cover");
504
505 const novel403 = await plugin403.parseNovel("wiki/the_last_terran");
506 assertLiveCover(novel403.cover, "arctic", "parseNovel 403-cover");
507
508 clearWikiCache(storageMap);
509 state.redditMode = "403";
510 state.arcticMode = "fail";
511 const pluginStatic = loadPlugin(storageMap, liveFetch);
512 const popularStatic = await pluginStatic.popularNovels(1, { filters: {} });
513 assert(popularStatic.some((n) => n.path === "series/aaa111"), "na Reddit+Arctic Shift volgt de statische index");
514 const staticItem = popularStatic.find((n) => n.path === "series/aaa111");
515 assertLiveCover(staticItem.cover, "index", "statische catalogus na live-fout gebruikt index.png");
516 const searchStatic = await pluginStatic.searchNovels("terran", 1);
517 assert(searchStatic.length === 1 && searchStatic[0].path === "series/aaa111", "zoeken valt terug op de statische index");
518
519 console.log("✔️ live catalogus: wiki, Nieuw, zoeken, parseNovel, 403→Arctic Shift→index");
520 } finally {
521 started.server.close();
522 }
523}
524
525
526
527function clearWikiCache(storageMap) {
528 delete storageMap.wikiSeriesIndex;
529 delete storageMap["wikiSeriesIndex.v2"];
530 delete storageMap.wikiSeriesIndex__expires;
531 delete storageMap["wikiSeriesIndex.v2__expires"];
532}
533
534function assertLiveCover(uri, source, label) {
535 const expected =
536 source === "arctic" ? LIVE_COVER_ARCTIC : source === "index" ? LIVE_COVER_INDEX : LIVE_COVER_REDDIT;
537 assert(typeof uri === "string" && uri === expected, label + " is de vaste " + source + "-cover-URL");
538 assert(/^https:\/\//.test(uri), label + " is een https-URL");
539 assert(uri.indexOf("data:") === -1, label + " is geen data-URI");
540}
541
542function assertPngFile(file, label) {
543 const buf = fs.readFileSync(file);
544 assert(buf[0] === 0x89 && buf.toString("ascii", 1, 4) === "PNG", label + " heeft PNG-signatuur");
545 assert(buf[4] === 0x0d && buf[5] === 0x0a && buf[6] === 0x1a && buf[7] === 0x0a, label + " heeft geldige PNG-header");
546 const width = buf.readUInt32BE(16);
547 const height = buf.readUInt32BE(20);
548 assert(width === 240 && height === 360, label + " is 240x360");
549 assert(buf.length < 20 * 1024, label + " is een klein bestand");
550}
551
552function testLiveCoverFiles() {
553 for (const name of ["reddit.png", "arctic-shift.png", "index.png"]) {
554 const src = path.join(ROOT, "plugin", "covers", name);
555 const dist = path.join(ROOT, ".dist", "covers", name);
556 assert(fs.existsSync(src), name + " staat in plugin/covers");
557 assert(fs.existsSync(dist), name + " staat in .dist/covers");
558 assertPngFile(src, "bron " + name);
559 assertPngFile(dist, "dist " + name);
560 assert(Buffer.compare(fs.readFileSync(src), fs.readFileSync(dist)) === 0, name + " is gelijk gekopieerd");
561 }
562 console.log("✔️ vaste broncovers: reddit.png, arctic-shift.png en index.png (240x360, klein PNG)");
563}
564
565
566
567
568async function testCoverSourceAfterLoginAndCommentsFallback(origin, fixtureRoot, assert) {
569 // 1) Wiki via Arctic cachen, daarna Reddit weer beschikbaar: geen 6-uur-wacht.
570 // 2) /api/info.json 403, /comments/{id}/.json 200: parseNovel moet Reddit-cover geven.
571 const wikiIndex = fs.readFileSync(path.join(ROOT, "testdata", "wiki", "index.md"), "utf8");
572 const wikiTerran = fs.readFileSync(path.join(ROOT, "testdata", "wiki", "pages", "the_last_terran.md"), "utf8");
573 const state = { redditMode: "403", blockInfo: false, wikiHits: 0, infoHits: 0, commentHits: 0, arcticWikiHits: 0 };
574
575 function redditHandler(url, res) {
576 const isInfo = url.pathname.startsWith("/api/info");
577 const isComments = /\/comments\/[0-9a-z]+\//i.test(url.pathname);
578 const isWiki = /\/wiki\//i.test(url.pathname);
579 if (state.redditMode === "403" || (state.blockInfo && (isInfo || isWiki) && !isComments)) {
580 if (isComments && state.redditMode !== "403") {
581 // comments blijven doorgaan in blockInfo-modus
582 } else {
583 if (isWiki) state.wikiHits += 1;
584 if (isInfo) state.infoHits += 1;
585 res.writeHead(403, { "Content-Type": "text/html; charset=utf-8" });
586 res.end(blockHtml());
587 return;
588 }
589 }
590 if (isWiki) {
591 state.wikiHits += 1;
592 const md = /the_last_terran/.test(url.pathname) ? wikiTerran : wikiIndex;
593 res.writeHead(200, { "Content-Type": "application/json" });
594 res.end(JSON.stringify(wikiIndexPayload(md)));
595 return;
596 }
597 if (isInfo) {
598 state.infoHits += 1;
599 res.writeHead(403, { "Content-Type": "text/html; charset=utf-8" });
600 res.end(blockHtml());
601 return;
602 }
603 if (isComments) {
604 state.commentHits += 1;
605 const m = url.pathname.match(/\/comments\/([0-9a-z]+)\//i);
606 const id = m ? m[1].toLowerCase() : "aaa111";
607 res.writeHead(200, { "Content-Type": "application/json" });
608 res.end(JSON.stringify(redditListingFor(id)));
609 return;
610 }
611 res.writeHead(404, { "Content-Type": "text/plain" });
612 res.end("no mock for " + url.pathname);
613 }
614
615 function arcticHandler(url, res) {
616 if (url.pathname.startsWith("/api/subreddits/wikis")) {
617 state.arcticWikiHits += 1;
618 const paths = url.searchParams.get("paths") || "";
619 const content = /the_last_terran/.test(paths) ? wikiTerran : wikiIndex;
620 res.writeHead(200, { "Content-Type": "application/json" });
621 res.end(JSON.stringify({ data: [{ path: paths, content }] }));
622 return;
623 }
624 if (url.pathname.startsWith("/api/posts/ids")) {
625 res.writeHead(200, { "Content-Type": "application/json" });
626 res.end(JSON.stringify({ data: [samplePost("aaa111", "[OC] The Last Terran - Part 1", "WriterOne")] }));
627 return;
628 }
629 res.writeHead(404);
630 res.end("no arctic mock");
631 }
632
633 const started = await startServer(fixtureRoot, arcticHandler, redditHandler);
634 const fetchApi = makeFetchApi(started.url);
635 try {
636 const storageMap = {
637 indexBaseUrl: origin,
638 indexRev: "",
639 chapterSourceUrl: started.url,
640 chapterSource: "auto",
641 };
642 const plugin = loadPlugin(storageMap, fetchApi);
643
644 const popularArctic = await plugin.popularNovels(1, { filters: {} });
645 assertLiveCover(
646 popularArctic.find((n) => n.name === "The Last Terran").cover,
647 "arctic",
648 "eerst Arctic-wiki-cover",
649 );
650 assert(state.arcticWikiHits >= 1, "Arctic-wiki is gebruikt");
651 const hitsBeforeRetry = state.wikiHits;
652
653 state.redditMode = "ok";
654 const popularRetry = await plugin.popularNovels(1, { filters: {} });
655 assert(state.wikiHits > hitsBeforeRetry, "Arctic-cache triggert een nieuwe Reddit-poging (geen 6 uur wachten)");
656 assertLiveCover(
657 popularRetry.find((n) => n.name === "The Last Terran").cover,
658 "reddit",
659 "na inloggen Reddit-cover op de wiki-lijst",
660 );
661
662 // Nieuwe plugin, geen cache: wiki 403, api/info 403, comments 200.
663 clearWikiCache(storageMap);
664 state.redditMode = "ok";
665 state.blockInfo = true;
666 state.infoHits = 0;
667 state.commentHits = 0;
668 const pluginComments = loadPlugin(storageMap, fetchApi);
669 const novel = await pluginComments.parseNovel("series/aaa111");
670 assert(state.commentHits >= 1, "parseNovel valt terug op comments/.json");
671 assertLiveCover(novel.cover, "reddit", "comments-fallback geeft Reddit-cover, niet Arctic");
672 // Hoofdstuk uit Reddit: dezelfde cookies/endpoint.
673 const html = await pluginComments.parseChapter("chapter/aaa111");
674 assert(/Bron: Reddit/.test(html), "parseChapter toont Bron: Reddit");
675
676 // Zonder bron → Reddit-cover (bij twijfel).
677 assertLiveCover(plugin.coverFor({ name: "X" }), "reddit", "ontbrekende bron prefereert Reddit");
678 assertLiveCover(plugin.coverFor({ name: "X", source: "index" }), "index", "index-bron");
679 assertLiveCover(plugin.coverFor({ name: "X", source: "arctic" }), "arctic", "arctic-bron");
680
681 console.log("✔️ covers: bron volgt Reddit na login, comments-fallback, index/reddit/arctic");
682 } finally {
683 started.server.close();
684 }
685}
686
687async function testParseNovelWhenLiveFails(origin, fixtureRoot, assert) {
688 // Reddit 403 + Arctic Shift 502 zonder posts-array: dit is de fout uit 0.3.2
689 // ("unable to load novel" overal, ook voor verhalen in de statische index).
690 const redditHandler = (url, res) => {
691 res.writeHead(403, { "Content-Type": "text/html; charset=utf-8" });
692 res.end("<html><body>blocked</body></html>");
693 };
694 const arcticHandler = (url, res) => {
695 res.writeHead(502, { "Content-Type": "text/html; charset=utf-8" });
696 res.end("<html><body>Bad Gateway</body></html>");
697 };
698 const started = await startServer(fixtureRoot, arcticHandler, redditHandler);
699 const fetchApi = makeFetchApi(started.url);
700 try {
701 const storageMap = {
702 indexBaseUrl: origin,
703 indexRev: "",
704 chapterSourceUrl: started.url,
705 chapterSource: "auto",
706 };
707 const plugin = loadPlugin(storageMap, fetchApi);
708
709 const live = await plugin.liveParseNovel("series/aaa111", "auto");
710 assert(live && live.ok === false, "liveParseNovel geeft ok:false bij 403+502, geen throw");
711 assert(!live.novel, "geen half novel-object bij live-fout");
712
713 const novel = await plugin.parseNovel("series/aaa111");
714 assert(novel.path === "series/aaa111", "parseNovel houdt het aangevraagde pad");
715 assert(novel.name === "The Last Terran", "parseNovel valt terug op de statische index");
716 assert(novel.chapters && novel.chapters.length === 3, "statische hoofdstuklijst blijft beschikbaar");
717 assertLiveCover(novel.cover, "index", "indexcover bij live-fout");
718
719 let wikiError = "";
720 try {
721 await plugin.parseNovel("wiki/the_last_terran");
722 } catch (err) {
723 wikiError = String(err && err.message || err);
724 }
725 assert(/niet live bepalen/i.test(wikiError), "wiki zonder index krijgt een duidelijke fout");
726 assert(!/Cannot read/i.test(wikiError) && !/undefined/i.test(wikiError), "geen TypeError op .posts.length");
727
728 console.log("✔️ parseNovel: 403+502 TypeError-vrij, statische fallback werkt");
729 } finally {
730 started.server.close();
731 }
732}
733
734async function main() {
735 const fixtureRoot = path.join(ROOT, "testdata", "index-fixture");
736 const redditState = { mode: "ok", redditHits: 0, arcticHits: 0 };
737 const chapterHandler = (reqUrl, res) => {
738 redditState.arcticHits += 1;
739 return defaultChapterHandler(reqUrl, res);
740 };
741 const redditHandler = (reqUrl, res) => {
742 redditState.redditHits += 1;
743 return defaultRedditHandler(redditState.mode)(reqUrl, res);
744 };
745 const { server, url } = await startServer(fixtureRoot, chapterHandler, redditHandler);
746 const fetchApi = makeFetchApi(url);
747 const failures = [];
748 try {
749 const storageMap = {
750 indexBaseUrl: url,
751 indexRev: "", // geen ?rev= nodig op de testhost
752 chapterSourceUrl: url,
753 chapterSource: "auto",
754 };
755 const plugin = loadPlugin(storageMap, fetchApi);
756
757 assert(plugin.id === "hfyNovels", "id");
758 assert(plugin.name === "HFY Novels", "name");
759 assert(plugin.version === "0.3.4", "version");
760 assert(plugin.site === "https://www.reddit.com/r/HFY/", "site");
761 assert(plugin.pluginSettings && plugin.pluginSettings.indexBaseUrl, "pluginSettings");
762 assert(plugin.filters && plugin.filters.catalog, "filter Nieuw/wiki");
763 assert(plugin.pluginSettings.chapterSource && plugin.pluginSettings.chapterSource.type === "Select", "bron-instelling is Select");
764 assert(typeof plugin.resolveUrl === "function", "resolveUrl");
765 assert(plugin.resolveUrl("chapter/aaa111") === "https://www.reddit.com/r/HFY/comments/aaa111/", "resolveUrl hoofdstuk");
766 assert(plugin.resolveUrl("series/aaa111", true) === "https://www.reddit.com/r/HFY/comments/aaa111/", "resolveUrl reeks");
767 assert(plugin.resolveUrl("wiki/the_last_terran", true) === "https://www.reddit.com/r/HFY/wiki/series/the_last_terran/", "resolveUrl wiki");
768
769 testLiveCoverFiles();
770
771 const popular = await plugin.popularNovels(1, { showLatestNovels: false, filters: {} });
772 assert(Array.isArray(popular) && popular.length >= 1, "popularNovels geeft items");
773 assert(popular[0].name && popular[0].path, "NovelItem heeft name+path");
774 assertLiveCover(popular[0].cover, "index", "statische cataloguscover");
775 assert(popular.every((n) => n.cover === LIVE_COVER_INDEX), "alle index-items delen index.png");
776 assert(popular.every((n) => !n.path.includes("ccc001")), "NSFW standaard verborgen in catalogus");
777
778 const latest = await plugin.popularNovels(1, { showLatestNovels: true, filters: {} });
779 assert(latest.length >= 1, "showLatestNovels werkt via zoekindex");
780
781 const oneshots = await plugin.popularNovels(1, {
782 filters: { kind: { value: "oneshot" }, nsfw: { value: "hide" } },
783 });
784 assert(oneshots.length === 1 && oneshots[0].name === "A Short Hello", "filter oneshot");
785
786 const nsfw = await plugin.popularNovels(1, {
787 filters: { kind: { value: "all" }, nsfw: { value: "show" } },
788 });
789 assert(nsfw.some((n) => n.path.includes("ccc001")), "NSFW zichtbaar met filter");
790
791 const search = await plugin.searchNovels("terran", 1);
792 assert(search.length === 1 && search[0].path === "series/aaa111", "searchNovels op titel");
793 const byAuthor = await plugin.searchNovels("WriterTwo", 1);
794 assert(byAuthor.length === 1, "searchNovels op auteur");
795 assert((await plugin.searchNovels("", 1)).length === 0, "lege zoekterm");
796
797 const novel = await plugin.parseNovel("series/aaa111");
798 assert(novel.path === "series/aaa111", "SourceNovel.path gelijk aan input");
799 assert(novel.author === "WriterOne", "auteur");
800 assertLiveCover(novel.cover, "reddit", "parseNovel gebruikt Reddit-cover via comments/.json");
801 assert(novel.chapters.length === 3, "3 hoofdstukken");
802 assert(novel.chapters[0].path === "chapter/aaa111", "hoofdstukpad");
803 assert(novel.chapters[0].chapterNumber === 1, "hoofdstuknummer");
804 assert(novel.status === NovelStatus.Ongoing || novel.status === NovelStatus.OnHiatus || novel.status === NovelStatus.Unknown, "status enum");
805
806 redditState.mode = "ok";
807 redditState.redditHits = 0;
808 redditState.arcticHits = 0;
809 const html = await plugin.parseChapter("chapter/aaa111");
810 assert(/It begins/.test(html), "parseChapter bevat tekst");
811 assert(/<h1>/.test(html), "parseChapter heeft titel");
812 assert(!/<script/i.test(html), "geen script-tags");
813 assert(/Bron: Reddit/.test(html), "Reddit-ok toont Bron: Reddit");
814 assert(redditState.arcticHits === 0, "bij Reddit-ok geen Arctic Shift-aanroep");
815 assert(redditState.redditHits >= 1, "Reddit-JSON is aangeroepen");
816
817 redditState.mode = "403";
818 redditState.redditHits = 0;
819 redditState.arcticHits = 0;
820 const html403 = await plugin.parseChapter("chapter/aaa111");
821 assert(/It begins/.test(html403), "403 valt terug op Arctic Shift");
822 assert(/Bron: Arctic Shift \(fallback\)/.test(html403), "403 toont fallback-bron");
823 assert(redditState.arcticHits >= 1, "403 roept Arctic Shift aan");
824 assert(redditState.redditHits >= 1, "403 probeert eerst Reddit");
825
826 redditState.mode = "login-html";
827 redditState.redditHits = 0;
828 redditState.arcticHits = 0;
829 const htmlLogin = await plugin.parseChapter("chapter/aaa111");
830 assert(/It begins/.test(htmlLogin), "login-HTML valt terug op Arctic Shift");
831 assert(/Bron: Arctic Shift \(fallback\)/.test(htmlLogin), "login-HTML toont fallback-bron");
832 assert(redditState.arcticHits >= 1, "login-HTML roept Arctic Shift aan");
833
834 storageMap.chapterSource = "reddit";
835 redditState.mode = "403";
836 redditState.arcticHits = 0;
837 let redditOnlyThrew = false;
838 try {
839 await plugin.parseChapter("chapter/aaa111");
840 } catch (err) {
841 redditOnlyThrew = /Reddit/.test(String(err && err.message)) && /WebView/.test(String(err && err.message));
842 }
843 assert(redditOnlyThrew, "modus Reddit faalt zichtbaar bij 403");
844 assert(redditState.arcticHits === 0, "modus Reddit gebruikt geen fallback");
845
846 storageMap.chapterSource = "arctic";
847 redditState.mode = "ok";
848 redditState.redditHits = 0;
849 redditState.arcticHits = 0;
850 const htmlArctic = await plugin.parseChapter("chapter/aaa111");
851 assert(/Bron: Arctic Shift</.test(htmlArctic) && !/fallback/.test(htmlArctic), "modus Arctic Shift slaat Reddit over");
852 assert(redditState.redditHits === 0, "modus Arctic Shift roept Reddit niet aan");
853 assert(redditState.arcticHits >= 1, "modus Arctic Shift gebruikt Arctic Shift");
854 storageMap.chapterSource = "auto";
855
856 let threw = false;
857 try {
858 await plugin.parseChapter("chapter/../etc/passwd");
859 } catch (err) {
860 threw = true;
861 }
862 assert(threw, "ongeldig hoofdstuk-id wordt geweigerd");
863
864 // --- Live catalogus: wiki, zoeken, parseNovel, met fallbacks ---
865 await testLiveCatalog(url, fixtureRoot, fetchApi, assert);
866 await testParseNovelWhenLiveFails(url, fixtureRoot, assert);
867 await testCoverSourceAfterLoginAndCommentsFallback(url, fixtureRoot, assert);
868
869
870 // Manifest-vorm controleren.
871 const manifest = JSON.parse(fs.readFileSync(path.join(ROOT, ".dist", "plugins.json"), "utf8"));
872 const manifestMin = JSON.parse(fs.readFileSync(path.join(ROOT, ".dist", "plugins.min.json"), "utf8"));
873 assert(Array.isArray(manifest) && manifest.length === 1, "plugins.json is een array");
874 assert(manifestMin[0].version === manifest[0].version, "plugins.min.json heeft dezelfde versie");
875 const entry = manifest[0];
876 for (const key of ["id", "name", "site", "lang", "version", "url", "iconUrl"]) {
877 assert(entry[key], "manifest mist " + key);
878 }
879 assert(entry.id === plugin.id && entry.version === plugin.version, "manifest komt overeen met plugin");
880 assert(entry.lang === "English", "lang is de volle taalnaam");
881 assert(entry.hasSettings === true, "hasSettings");
882 assert(entry.url.includes("/-/raw/.dist/hfy-novels.js"), "raw URL voor de plugin-JS");
883 assert(entry.url.includes("rev=master"), "rev=master in de plugin-URL");
884
885 console.log("✔️ plugininterface: popularNovels, searchNovels, parseNovel, parseChapter");
886 console.log("✔️ plugins.json komt overeen met de plugin-export");
887
888 if (process.env.HFY_LIVE === "1") {
889 const liveStorage = {
890 indexBaseUrl: url,
891 indexRev: "",
892 chapterSourceUrl: "https://arctic-shift.photon-reddit.com",
893 };
894 const livePlugin = loadPlugin(liveStorage, async (reqUrl, init) => wrapResponse(await fetch(reqUrl, init)));
895 // Een recent, echt post-id uit de Arctic Shift-probe: willekeurig een
896 // bekend kort id heeft geen zin. We vragen een recente HFY-post op.
897 const listing = await fetch(
898 "https://arctic-shift.photon-reddit.com/api/posts/search?subreddit=HFY&limit=1&sort=desc",
899 { headers: { "User-Agent": "HFY-Novels/0.1 (plugin live test)" } },
900 );
901 if (!listing.ok) {
902 throw new Error("live listing HTTP " + listing.status);
903 }
904 const body = await listing.json();
905 const liveId = body.data[0].id;
906 const liveHtml = await livePlugin.parseChapter("chapter/" + liveId);
907 assert(liveHtml.length > 20, "live parseChapter gaf inhoud");
908 const liveSource = /Bron: ([^<]+)/.exec(liveHtml);
909 console.log("✔️ live parseChapter (" + liveId + ", " + liveHtml.length + " tekens, " + ((liveSource && liveSource[1]) || "bron onbekend") + ")");
910 }
911 } catch (err) {
912 failures.push(err);
913 } finally {
914 server.close();
915 }
916
917 if (failures.length) {
918 for (const err of failures) {
919 console.error(err);
920 }
921 process.exit(1);
922 }
923}
924
925main().catch((err) => {
926 console.error(err);
927 process.exit(1);
928});