// A forum page loader over a DIRECTORY OF SAVED PAGES, for the editor's e2e
// suite (E2E_FORUM_FIXTURE_DIR, set per slot by e2e/servers.ts only): the
// forum-thread fetcher reads `
//page-.html` instead of
// opening a browser, so the channel page's Fetch posts and Full re-walk run end
// to end with no network. A page number past the last file is the last page,
// as XenForo redirects it. `page-..html` (a challenge page, a 403)
// is served in place of page N, with that status, while it exists — a test
// removes it to let the "block" clear. SYNTHETIC pages only.
import { readdir, readFile } from "node:fs/promises";
import path from "node:path";
import type { ForumPageLoader } from "./forumSession";
import { xenforoPageUrl, type XenforoThreadUrl } from "./xenforoParse";
export function forumFixtureDir(env: Readonly> = process.env): string | undefined {
const dir = env.E2E_FORUM_FIXTURE_DIR;
return dir && dir.trim() ? dir : undefined;
}
export function fixtureForumLoader(dir: string, thread: XenforoThreadUrl): ForumPageLoader {
const threadDir = path.join(dir, thread.threadId);
return {
async load(url) {
const files = await readdir(threadDir).catch(() => [] as string[]);
const pages = files
.map((f) => /^page-(\d+)\.html$/.exec(f))
.filter((m): m is RegExpExecArray => m !== null)
.map((m) => Number(m[1]));
if (pages.length === 0) throw new Error(`Could not load ${url}: no fixture pages in ${threadDir}`);
const m = /page-(\d+)\/?$/.exec(new URL(url).pathname);
const wanted = m ? Number(m[1]) : 1;
const page = Math.min(wanted, Math.max(...pages));
const blocked = files.map((f) => new RegExp(`^page-${page}\\.(\\d{3})\\.html$`).exec(f)).find((b) => b);
if (blocked) {
const html = await readFile(path.join(threadDir, blocked[0]), "utf8");
return { html, status: Number(blocked[1]), url: xenforoPageUrl(thread, page) };
}
const html = await readFile(path.join(threadDir, `page-${page}.html`), "utf8").catch(() => "");
if (!html) throw new Error(`Could not load ${url}: no fixture page ${page}`);
return { html, status: 200, url: xenforoPageUrl(thread, page) };
},
async close() {},
};
}