BlueGit

timfa hfy-novels

Files

hfy-novels / scripts / test-plugin.js

test-plugin.js

test-plugin.js 16.2 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 probeert eerst een mock van Reddit-JSON en
9 * valt terug op Arctic Shift; optioneel een live verzoek als HFY_LIVE=1.
10 */
11
12const fs = require("fs");
13const http = require("http");
14const path = require("path");
15
16const ROOT = path.resolve(__dirname, "..");
17const PLUGIN = fs.readFileSync(path.join(ROOT, "plugin", "hfy-novels.js"), "utf8");
18
19const NovelStatus = {
20 Completed: "Completed",
21 Ongoing: "Ongoing",
22 OnHiatus: "On Hiatus",
23 Unknown: "Unknown",
24};
25const FilterTypes = { Picker: "Picker", Switch: "Switch", CheckboxGroup: "CheckboxGroup" };
26
27function mimeFor(file, asImg) {
28 if (file.endsWith(".svg")) return asImg ? "image/svg+xml" : "text/plain; charset=utf-8";
29 if (file.endsWith(".json")) return "text/plain; charset=utf-8"; // BlueGit serveert raw ook als text/plain
30 if (file.endsWith(".js")) return "text/plain; charset=utf-8";
31 if (file.endsWith(".png")) return "image/png";
32 return "application/octet-stream";
33}
34
35function startServer(rootDir, chapterHandler, redditHandler) {
36 return new Promise((resolve, reject) => {
37 const server = http.createServer((req, res) => {
38 const url = new URL(req.url, "http://127.0.0.1");
39 if (url.pathname.startsWith("/api/posts/ids")) {
40 return chapterHandler(url, res);
41 }
42 if (redditHandler && /\/r\/HFY\/comments\//i.test(url.pathname) && url.pathname.endsWith(".json")) {
43 return redditHandler(url, res, req);
44 }
45 // ?rev=master zoals BlueGit raw-URLs; voor het bestand negeren we de query.
46 let rel = decodeURIComponent(url.pathname).replace(/^\/+/, "");
47 const file = path.join(rootDir, rel);
48 if (!file.startsWith(path.resolve(rootDir))) {
49 res.writeHead(403);
50 res.end("forbidden");
51 return;
52 }
53 fs.readFile(file, (err, data) => {
54 if (err) {
55 res.writeHead(404, { "Content-Type": "text/plain" });
56 res.end("404 " + rel);
57 return;
58 }
59 const asImg = url.searchParams.get("as") === "img";
60 res.writeHead(200, { "Content-Type": mimeFor(file, asImg) });
61 res.end(data);
62 });
63 });
64 server.listen(0, "127.0.0.1", () => {
65 const { port } = server.address();
66 resolve({ server, port, url: "http://127.0.0.1:" + port });
67 });
68 server.on("error", reject);
69 });
70}
71
72function redditListingFor(id) {
73 return [
74 {
75 kind: "Listing",
76 data: {
77 children: [
78 {
79 kind: "t3",
80 data: {
81 id,
82 title: "The Last Terran - Part 1",
83 author: "WriterOne",
84 selftext: "It begins.\n\n[Next](https://www.reddit.com/r/HFY/comments/aaa222/x/)",
85 selftext_html: "<div class=\"md\"><p>It begins.</p></div>",
86 permalink: "/r/HFY/comments/" + id + "/",
87 },
88 },
89 ],
90 },
91 },
92 { kind: "Listing", data: { children: [] } },
93 ];
94}
95
96function loginHtml() {
97 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>";
98}
99
100function blockHtml() {
101 return "<body class=theme-beta><div>Request blocked. reason=lor2</div></body>";
102}
103
104function defaultRedditHandler(mode) {
105 return function redditHandler(url, res) {
106 const m = url.pathname.match(/\/comments\/([0-9a-z]{4,10})\//i);
107 const id = m ? m[1].toLowerCase() : "unknown";
108 if (mode === "ok") {
109 res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
110 res.end(JSON.stringify(redditListingFor(id)));
111 return;
112 }
113 if (mode === "403") {
114 res.writeHead(403, { "Content-Type": "text/html; charset=utf-8" });
115 res.end(blockHtml());
116 return;
117 }
118 if (mode === "login-html") {
119 res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
120 res.end(loginHtml());
121 return;
122 }
123 res.writeHead(500, { "Content-Type": "text/plain" });
124 res.end("reddit mock error");
125 };
126}
127
128function defaultChapterHandler(url, res) {
129 const ids = url.searchParams.get("ids") || "";
130 const id = ids.split(",")[0];
131 const fixture = {
132 data: [
133 {
134 id,
135 title: "The Last Terran - Part 1",
136 author: "WriterOne",
137 selftext: "It begins.\n\n[Next](https://www.reddit.com/r/HFY/comments/aaa222/x/)",
138 selftext_html: "<p>It begins.</p>",
139 permalink: "/r/HFY/comments/" + id + "/",
140 },
141 ],
142 };
143 res.writeHead(200, { "Content-Type": "application/json" });
144 res.end(JSON.stringify(fixture));
145}
146
147function wrapResponse(res) {
148 return {
149 ok: res.ok,
150 status: res.status,
151 headers: res.headers,
152 async text() {
153 return await res.text();
154 },
155 async json() {
156 const text = await res.text();
157 return JSON.parse(text);
158 },
159 url: res.url,
160 };
161}
162
163function makeFetchApi(origin) {
164 return async function fetchApi(url, init) {
165 let target = String(url);
166 // Stuur Reddit-JSON naar de lokale testhost, zodat we 200/403/login kunnen simuleren.
167 if (/^https?:\/\/(www\.|old\.)?reddit\.com\//i.test(target)) {
168 const parsed = new URL(target);
169 target = origin + parsed.pathname + parsed.search;
170 }
171 const res = await fetch(target, init);
172 return wrapResponse(res);
173 };
174}
175
176function loadPlugin(storageMap, fetchApi) {
177 const allow = {
178 "@libs/fetch": { fetchApi, fetchText: async (u, i) => (await fetchApi(u, i)).text() },
179 "@libs/filterInputs": { FilterTypes },
180 "@libs/defaultCover": { defaultCover: "https://example.invalid/cover.png" },
181 "@libs/novelStatus": { NovelStatus },
182 "@libs/storage": {
183 storage: {
184 get(key) {
185 return storageMap[key];
186 },
187 set(key, value) {
188 storageMap[key] = value;
189 },
190 },
191 },
192 };
193 const pluginModule = { exports: {} };
194 const fn = new Function("exports", "require", "module", PLUGIN);
195 fn(pluginModule.exports, (name) => {
196 if (!allow[name]) {
197 throw new Error("blocked require: " + name);
198 }
199 return allow[name];
200 }, pluginModule);
201 const plugin = pluginModule.exports.default;
202 if (!plugin) {
203 throw new Error("geen default export");
204 }
205 for (const method of ["popularNovels", "searchNovels", "parseNovel", "parseChapter"]) {
206 if (typeof plugin[method] !== "function") {
207 throw new Error("plugin mist " + method);
208 }
209 }
210 return plugin;
211}
212
213function assert(cond, msg) {
214 if (!cond) {
215 throw new Error("FAIL: " + msg);
216 }
217}
218
219async function main() {
220 const fixtureRoot = path.join(ROOT, "testdata", "index-fixture");
221 const redditState = { mode: "ok", redditHits: 0, arcticHits: 0 };
222 const chapterHandler = (reqUrl, res) => {
223 redditState.arcticHits += 1;
224 return defaultChapterHandler(reqUrl, res);
225 };
226 const redditHandler = (reqUrl, res) => {
227 redditState.redditHits += 1;
228 return defaultRedditHandler(redditState.mode)(reqUrl, res);
229 };
230 const { server, url } = await startServer(fixtureRoot, chapterHandler, redditHandler);
231 const fetchApi = makeFetchApi(url);
232 const failures = [];
233 try {
234 const storageMap = {
235 indexBaseUrl: url,
236 indexRev: "", // geen ?rev= nodig op de testhost
237 chapterSourceUrl: url,
238 chapterSource: "auto",
239 };
240 const plugin = loadPlugin(storageMap, fetchApi);
241
242 assert(plugin.id === "hfyNovels", "id");
243 assert(plugin.name === "HFY Novels", "name");
244 assert(plugin.version === "0.2.0", "version");
245 assert(plugin.site === "https://www.reddit.com/r/HFY/", "site");
246 assert(plugin.pluginSettings && plugin.pluginSettings.indexBaseUrl, "pluginSettings");
247 assert(plugin.pluginSettings.chapterSource && plugin.pluginSettings.chapterSource.type === "Select", "bron-instelling is Select");
248 assert(typeof plugin.resolveUrl === "function", "resolveUrl");
249 assert(plugin.resolveUrl("chapter/aaa111") === "https://www.reddit.com/r/HFY/comments/aaa111/", "resolveUrl hoofdstuk");
250 assert(plugin.resolveUrl("series/aaa111", true) === "https://www.reddit.com/r/HFY/comments/aaa111/", "resolveUrl reeks");
251
252 const popular = await plugin.popularNovels(1, { showLatestNovels: false, filters: {} });
253 assert(Array.isArray(popular) && popular.length >= 1, "popularNovels geeft items");
254 assert(popular[0].name && popular[0].path, "NovelItem heeft name+path");
255 assert(popular[0].cover && popular[0].cover.indexOf("/covers/") !== -1, "cover-URL wijst naar covers/");
256 const coverIsSvg = /\.svg(\?|$)/i.test(popular[0].cover);
257 if (coverIsSvg) {
258 assert(popular[0].cover.indexOf("as=img") !== -1, "SVG-cover heeft as=img");
259 }
260 const coverRes = await fetch(popular[0].cover);
261 assert(coverRes.ok, "cover is ophaalbaar");
262 const coverBuf = Buffer.from(await coverRes.arrayBuffer());
263 if (coverIsSvg) {
264 const coverBody = coverBuf.toString("utf8");
265 assert(coverBody.indexOf("<svg") === 0, "cover is een SVG");
266 assert(coverBody.indexOf(popular[0].name) !== -1, "cover toont de titel");
267 } else {
268 assert(coverBuf[0] === 0x89 && coverBuf.toString("ascii", 1, 4) === "PNG", "cover is een PNG");
269 }
270 const coverAgain = Buffer.from(await (await fetch(popular[0].cover)).arrayBuffer());
271 assert(Buffer.compare(coverBuf, coverAgain) === 0, "dezelfde titel geeft dezelfde cover");
272 assert(popular.every((n) => !n.path.includes("ccc001")), "NSFW standaard verborgen in catalogus");
273
274 const latest = await plugin.popularNovels(1, { showLatestNovels: true, filters: {} });
275 assert(latest.length >= 1, "showLatestNovels werkt via zoekindex");
276
277 const oneshots = await plugin.popularNovels(1, {
278 filters: { kind: { value: "oneshot" }, nsfw: { value: "hide" } },
279 });
280 assert(oneshots.length === 1 && oneshots[0].name === "A Short Hello", "filter oneshot");
281
282 const nsfw = await plugin.popularNovels(1, {
283 filters: { kind: { value: "all" }, nsfw: { value: "show" } },
284 });
285 assert(nsfw.some((n) => n.path.includes("ccc001")), "NSFW zichtbaar met filter");
286
287 const search = await plugin.searchNovels("terran", 1);
288 assert(search.length === 1 && search[0].path === "series/aaa111", "searchNovels op titel");
289 const byAuthor = await plugin.searchNovels("WriterTwo", 1);
290 assert(byAuthor.length === 1, "searchNovels op auteur");
291 assert((await plugin.searchNovels("", 1)).length === 0, "lege zoekterm");
292
293 const novel = await plugin.parseNovel("series/aaa111");
294 assert(novel.path === "series/aaa111", "SourceNovel.path gelijk aan input");
295 assert(novel.author === "WriterOne", "auteur");
296 assert(novel.cover && novel.cover.indexOf("/covers/") !== -1, "parseNovel heeft een cover-URL");
297 assert(novel.chapters.length === 3, "3 hoofdstukken");
298 assert(novel.chapters[0].path === "chapter/aaa111", "hoofdstukpad");
299 assert(novel.chapters[0].chapterNumber === 1, "hoofdstuknummer");
300 assert(novel.status === NovelStatus.Ongoing || novel.status === NovelStatus.OnHiatus || novel.status === NovelStatus.Unknown, "status enum");
301
302 redditState.mode = "ok";
303 redditState.redditHits = 0;
304 redditState.arcticHits = 0;
305 const html = await plugin.parseChapter("chapter/aaa111");
306 assert(/It begins/.test(html), "parseChapter bevat tekst");
307 assert(/<h1>/.test(html), "parseChapter heeft titel");
308 assert(!/<script/i.test(html), "geen script-tags");
309 assert(/Bron: Reddit/.test(html), "Reddit-ok toont Bron: Reddit");
310 assert(redditState.arcticHits === 0, "bij Reddit-ok geen Arctic Shift-aanroep");
311 assert(redditState.redditHits >= 1, "Reddit-JSON is aangeroepen");
312
313 redditState.mode = "403";
314 redditState.redditHits = 0;
315 redditState.arcticHits = 0;
316 const html403 = await plugin.parseChapter("chapter/aaa111");
317 assert(/It begins/.test(html403), "403 valt terug op Arctic Shift");
318 assert(/Bron: Arctic Shift \(fallback\)/.test(html403), "403 toont fallback-bron");
319 assert(redditState.arcticHits >= 1, "403 roept Arctic Shift aan");
320 assert(redditState.redditHits >= 1, "403 probeert eerst Reddit");
321
322 redditState.mode = "login-html";
323 redditState.redditHits = 0;
324 redditState.arcticHits = 0;
325 const htmlLogin = await plugin.parseChapter("chapter/aaa111");
326 assert(/It begins/.test(htmlLogin), "login-HTML valt terug op Arctic Shift");
327 assert(/Bron: Arctic Shift \(fallback\)/.test(htmlLogin), "login-HTML toont fallback-bron");
328 assert(redditState.arcticHits >= 1, "login-HTML roept Arctic Shift aan");
329
330 storageMap.chapterSource = "reddit";
331 redditState.mode = "403";
332 redditState.arcticHits = 0;
333 let redditOnlyThrew = false;
334 try {
335 await plugin.parseChapter("chapter/aaa111");
336 } catch (err) {
337 redditOnlyThrew = /Reddit/.test(String(err && err.message)) && /WebView/.test(String(err && err.message));
338 }
339 assert(redditOnlyThrew, "modus Reddit faalt zichtbaar bij 403");
340 assert(redditState.arcticHits === 0, "modus Reddit gebruikt geen fallback");
341
342 storageMap.chapterSource = "arctic";
343 redditState.mode = "ok";
344 redditState.redditHits = 0;
345 redditState.arcticHits = 0;
346 const htmlArctic = await plugin.parseChapter("chapter/aaa111");
347 assert(/Bron: Arctic Shift</.test(htmlArctic) && !/fallback/.test(htmlArctic), "modus Arctic Shift slaat Reddit over");
348 assert(redditState.redditHits === 0, "modus Arctic Shift roept Reddit niet aan");
349 assert(redditState.arcticHits >= 1, "modus Arctic Shift gebruikt Arctic Shift");
350 storageMap.chapterSource = "auto";
351
352 let threw = false;
353 try {
354 await plugin.parseChapter("chapter/../etc/passwd");
355 } catch (err) {
356 threw = true;
357 }
358 assert(threw, "ongeldig hoofdstuk-id wordt geweigerd");
359
360 // Manifest-vorm controleren.
361 const manifest = JSON.parse(fs.readFileSync(path.join(ROOT, ".dist", "plugins.json"), "utf8"));
362 const manifestMin = JSON.parse(fs.readFileSync(path.join(ROOT, ".dist", "plugins.min.json"), "utf8"));
363 assert(Array.isArray(manifest) && manifest.length === 1, "plugins.json is een array");
364 assert(manifestMin[0].version === manifest[0].version, "plugins.min.json heeft dezelfde versie");
365 const entry = manifest[0];
366 for (const key of ["id", "name", "site", "lang", "version", "url", "iconUrl"]) {
367 assert(entry[key], "manifest mist " + key);
368 }
369 assert(entry.id === plugin.id && entry.version === plugin.version, "manifest komt overeen met plugin");
370 assert(entry.lang === "English", "lang is de volle taalnaam");
371 assert(entry.hasSettings === true, "hasSettings");
372 assert(entry.url.includes("/-/raw/.dist/hfy-novels.js"), "raw URL voor de plugin-JS");
373 assert(entry.url.includes("rev=master"), "rev=master in de plugin-URL");
374
375 console.log("✔️ plugininterface: popularNovels, searchNovels, parseNovel, parseChapter");
376 console.log("✔️ plugins.json komt overeen met de plugin-export");
377
378 if (process.env.HFY_LIVE === "1") {
379 const liveStorage = {
380 indexBaseUrl: url,
381 indexRev: "",
382 chapterSourceUrl: "https://arctic-shift.photon-reddit.com",
383 };
384 const livePlugin = loadPlugin(liveStorage, async (reqUrl, init) => wrapResponse(await fetch(reqUrl, init)));
385 // Een recent, echt post-id uit de Arctic Shift-probe: willekeurig een
386 // bekend kort id heeft geen zin. We vragen een recente HFY-post op.
387 const listing = await fetch(
388 "https://arctic-shift.photon-reddit.com/api/posts/search?subreddit=HFY&limit=1&sort=desc",
389 { headers: { "User-Agent": "HFY-Novels/0.1 (plugin live test)" } },
390 );
391 if (!listing.ok) {
392 throw new Error("live listing HTTP " + listing.status);
393 }
394 const body = await listing.json();
395 const liveId = body.data[0].id;
396 const liveHtml = await livePlugin.parseChapter("chapter/" + liveId);
397 assert(liveHtml.length > 20, "live parseChapter gaf inhoud");
398 const liveSource = /Bron: ([^<]+)/.exec(liveHtml);
399 console.log("✔️ live parseChapter (" + liveId + ", " + liveHtml.length + " tekens, " + ((liveSource && liveSource[1]) || "bron onbekend") + ")");
400 }
401 } catch (err) {
402 failures.push(err);
403 } finally {
404 server.close();
405 }
406
407 if (failures.length) {
408 for (const err of failures) {
409 console.error(err);
410 }
411 process.exit(1);
412 }
413}
414
415main().catch((err) => {
416 console.error(err);
417 process.exit(1);
418});