// A SYNTHETIC forum thread that changes between runs (release 37 lane G): posts // arrive at its end, posts are deleted and the pages shift down, a page is // served as a challenge. The walk's tests read it through a loader, the // controller's and the e2e suite's through the saved-pages directory // E2E_FORUM_FIXTURE_DIR names (social/forumFixtureLoader.ts). Invented forum, // members and words (xenforoPages.ts). import { mkdir, readdir, rm, writeFile } from "node:fs/promises"; import path from "node:path"; import { threadPage, type FakePost } from "./xenforoPages"; export const FAKE_THREAD_ID = "4242"; const T0 = 1_760_000_000; export class FakeForumThread { // The thread's posts as the forum shows them now, oldest first. posts: FakePost[] = []; private nextId: number; constructor( count: number, readonly perPage = 3, idBase = 9000, ) { this.nextId = idBase + 1; this.add(count); } // `n` new posts at the thread's end. add(n: number): void { for (let i = 0; i < n; i++) { const id = this.nextId++; this.posts.push({ id, author: `Member${id % 4}`, userId: 50 + (id % 4), ts: T0 + id * 600, position: 0, body: `Words of post ${id}.` }); } } // Delete posts (by id): every later post moves back, pages shift down. remove(ids: Iterable): void { const gone = new Set(ids); this.posts = this.posts.filter((p) => !gone.has(p.id)); } get last(): number { return Math.max(1, Math.ceil(this.posts.length / this.perPage)); } // The posts of page n (1-based), positions as the forum numbers them now. pagePosts(n: number): FakePost[] { const page = Math.min(Math.max(1, n), this.last); return this.posts .slice((page - 1) * this.perPage, page * this.perPage) .map((p, i) => ({ ...p, position: (page - 1) * this.perPage + i + 1 })); } html(n: number): string { const page = Math.min(Math.max(1, n), this.last); return threadPage({ page, last: this.last, posts: this.pagePosts(page) }); } ids(): string[] { return this.posts.map((p) => String(p.id)); } // The thread as E2E_FORUM_FIXTURE_DIR's saved pages: `/4242/page-.html`, // pages past the end removed (block overrides are left alone). async writeTo(dir: string): Promise { const threadDir = path.join(dir, FAKE_THREAD_ID); await mkdir(threadDir, { recursive: true }); for (const f of await readdir(threadDir)) { const m = /^page-(\d+)\.html$/.exec(f); if (m && Number(m[1]) > this.last) await rm(path.join(threadDir, f)); } for (let n = 1; n <= this.last; n++) await writeFile(path.join(threadDir, `page-${n}.html`), this.html(n)); } }