import { afterEach, expect, test } from "bun:test"; import { mkdirSync, rmSync } from "node:fs"; import { join } from "node:path"; import { columnKey, exportName, numberIn, pageHtml, readTables, renderDataFile, toDataTable } from "./data-table"; const made: string[] = []; afterEach(() => { for (const dir of made.splice(0)) rmSync(dir, { recursive: true, force: true }); }); function scratch(): string { const dir = join(import.meta.dir, "..", ".scratch", `data-table-${crypto.randomUUID()}`); mkdirSync(dir, { recursive: true }); made.push(dir); return dir; } // A wiki stats table as ark.wiki.gg lays it out: a two-row heading where "Health" spans its columns, // a footnote marker, a hidden sort key, entities, a thousands separator and an empty cell. const PAGE = `
Base stats
Creature Health Base Damage
Level 1+W
zzRaptor[1]2004015
Rex & friends1,100-10 / 40
`; test("reads a page's tables, nested navigation boxes included, in page order", async () => { const tables = await readTables(PAGE); expect(tables.length).toBe(2); expect(tables[1]!.caption).toBe("Base stats"); }); test("a two-row heading names each column, numbers are typed, and a mixed column stays text", async () => { const table = toDataTable((await readTables(PAGE))[1]!); expect(table.columns.map((column) => [column.key, column.type, column.nullable])).toEqual([ ["creature", "string", false], ["healthLevel1", "number", false], ["healthW", "number", true], ["baseDamage", "string", false], ]); expect(table.rows).toEqual([ ["Raptor", 200, 40, "15"], ["Rex & friends", 1100, null, "10 / 40"], ]); }); test("the written file is TypeScript a game imports, and it names its source on line 1", async () => { const table = toDataTable((await readTables(PAGE))[1]!); const text = renderDataFile({ url: "https://wiki.test/Stats", index: 1, name: "creatureStats", table, fetched: "2026-10-26" }); expect(text.split("\n")[0]).toBe("// GENERATED by `bun run data-table` from https://wiki.test/Stats (table 1). Re-run it with --refresh to update; do not edit."); const dir = scratch(); const path = join(dir, "creatureStats.ts"); await Bun.write(path, text); const loaded = (await import(path)) as { creatureStats: { creature: string; healthLevel1: number; healthW: number | null; baseDamage: string }[]; creatureStatsHeadings: Record }; expect(loaded.creatureStats[1]).toEqual({ creature: "Rex & friends", healthLevel1: 1100, healthW: null, baseDamage: "10 / 40" }); expect(loaded.creatureStatsHeadings["healthW"]).toBe("Health +W"); }); test("a page is fetched once: the second read comes from the cache, --refresh goes back to the network", async () => { const cache = scratch(); let fetches = 0; const fetchPage = async () => { fetches += 1; return `

fetch ${fetches}

`; }; expect(await pageHtml("https://wiki.test/a", { refresh: false, cache, fetchPage })).toBe("

fetch 1

"); expect(await pageHtml("https://wiki.test/a", { refresh: false, cache, fetchPage })).toBe("

fetch 1

"); expect(fetches).toBe(1); expect(await pageHtml("https://wiki.test/a", { refresh: true, cache, fetchPage })).toBe("

fetch 2

"); expect(await pageHtml("https://wiki.test/b", { refresh: false, cache, fetchPage })).toBe("

fetch 3

"); }); test("names and numbers", () => { expect(columnKey("Melee Damage (Wild)")).toBe("meleeDamageWild"); expect(columnKey("2nd Attack")).toBe("col2ndAttack"); expect(exportName("creatureStats")).toBe("creatureStats"); expect(exportName("creature-stats")).toBe("creatureStats"); expect([numberIn("1,200"), numberIn("+5"), numberIn("12.5%"), numberIn("−3"), numberIn("1-2"), numberIn("N/A")]).toEqual([1200, 5, 12.5, -3, null, null]); });