import { afterAll, beforeAll, describe, expect, setSystemTime, test } from "bun:test"; import { fileIssue } from "../src/issues/store.ts"; import { Browser, type Instance, instance } from "./browser.ts"; import { makeRepos, writeRelease } from "./fixture.ts"; let clock = Date.parse("2026-09-26T12:00:00Z"); const tick = (seconds: number) => { clock += seconds * 1000; setSystemTime(new Date(clock)); }; describe("repos and visibility", () => { let inst: Instance; let browser: Browser; let bucketDir: string; let save: string; const publicPaths = () => [ "/app", "/app/files/README.md", "/app/raw/README.md", `/app/saves/${save}`, `/app/archive/${save}.tar.gz`, "/app/archive/HEAD.tar.gz", "/app/archives", "/app/releases", "/app/releases/latest", "/app/releases/latest.json", "/app/releases/history.json", "/app/releases/latest/version.txt", "/app/releases/latest/linux-x86_64", "/app/releases/1.0.0/checksums.txt", "/app/releases/1.0.0/linux-x86_64", "/app/install.sh", "/api/app/releases/latest", "/api/app/releases/history", "/app/update-info", "/app/meta", "/app/issues", "/app/issues/new", "/app/issues/1", ]; const statuses = async () => { const out: string[] = []; for (const path of publicPaths()) out.push(`${path} ${(await inst.app.request(path)).status}`); return out; }; const text = async (path: string) => (await inst.app.request(path)).text(); const publish = (repo: string, fields: Record) => browser.post(`/admin/repos/${repo}`, { sort_order: "0", ...fields }); beforeAll(async () => { setSystemTime(new Date(clock)); const repos = await makeRepos(["app", "other"]); bucketDir = repos.bucketDir; save = repos.saves.app as string; await writeRelease(bucketDir, "app", { version: "1.0.0", save, files: [{ platform: "linux-x86_64", name: "app", bytes: new Uint8Array([1]) }], }); inst = await instance(bucketDir); inst.kepr.run("INSERT INTO settings (key, value) VALUES ('handle', 'june')"); fileIssueDirect(inst, "app"); browser = new Browser(inst.app); await browser.signIn(); }); afterAll(() => setSystemTime()); test("every repo the sync found is listed, private, with its last sync", async () => { const page = await (await browser.get("/admin/repos")).text(); expect(page).toContain('id="repo-app"'); expect(page).toContain('id="repo-other"'); expect(page.match(/private<\/span>/g)?.length).toBe(2); expect(page).toContain("last sync 2026-09-26 12:00 UTC"); expect((await statuses()).every((line) => line.endsWith(" 404"))).toBe(true); }); test("bad settings are refused and change nothing", async () => { for (const fields of []>[ { homepage: "javascript:alert(1)" }, { sort_order: "first" }, { install1: "one\ntwo" }, { category: "x".repeat(501) }, ]) { expect((await publish("app", { public: "1", ...fields })).status).toBe(400); } expect((await inst.app.request("/app")).status).toBe(404); expect((await browser.post("/admin/repos/nowhere", { public: "1" })).status).toBe(404); }); test("publishing opens every public surface, with the settings shown", async () => { await publish("other", { public: "1" }); const response = await publish("app", { public: "1", category: "tools", homepage: "https://app.example", install1: "nix profile add app", }); expect(response.status).toBe(303); const lines = await statuses(); expect(lines.filter((line) => line.endsWith(" 404"))).toEqual([]); expect(await text("/app")).toContain("https://app.example"); expect(await text("/app")).toContain("nix profile add app"); expect(await text("/")).toContain('href="/app"'); expect(await text("/browse")).toContain('href="/app"'); expect(await text("/@june")).toContain('href="/app"'); expect(await text("/sitemap.xml")).toContain("/app"); }); test("flipping a repo private 404s every surface and drops it from lists and counts", async () => { const stat = async () => [...(await text("/@june")).matchAll(/profile-stat-number">([^<]*) m[1]); // Both download routes above counted; the issue is the repo's one. expect(await stat()).toEqual(["2", "2", "1", "2"]); await publish("app", { category: "tools" }); expect((await statuses()).filter((line) => !line.endsWith(" 404"))).toEqual([]); for (const path of ["/", "/browse", "/@june"]) expect(await text(path)).not.toContain("/app"); expect(await text("/sitemap.xml")).not.toContain("/app<"); expect(await text("/")).not.toContain("tools"); expect(await stat()).toEqual(["1", "1", "0", "0"]); // Settings survive the flip. expect(await (await browser.get("/admin/repos")).text()).toContain('value="tools"'); }); test("sync now reads the bucket at once and records the time", async () => { await writeRelease(bucketDir, "other", { version: "2.0.0", save: save, files: [{ platform: "linux-x86_64", name: "other", bytes: new Uint8Array([2]) }], }); tick(60); expect((await inst.app.request("/other/releases/latest/version.txt")).status).toBe(404); const response = await browser.post("/admin/sync", {}); expect(response.status).toBe(303); expect(await text("/other/releases/latest/version.txt")).toBe("2.0.0"); expect(await (await browser.get("/admin/repos")).text()).toContain( "last sync 2026-09-26 12:01 UTC", ); }); }); describe("issues: filed, answered and closed end to end through real routes", () => { let inst: Instance; let operator: Browser; let save: string; const text = async (path: string) => (await inst.app.request(path)).text(); const act = (fields: Record) => operator.post("/admin/issues/app/1", fields); beforeAll(async () => { setSystemTime(new Date(clock)); const repos = await makeRepos(["app", "hush"]); save = repos.saves.app as string; inst = await instance(repos.bucketDir); operator = new Browser(inst.app); await operator.signIn(); await operator.post("/admin/repos/app", { public: "1", sort_order: "0" }); // A reporter files through the public form, as a browser would. const reporter = new Browser(inst.app, "198.51.100.40"); const form = await (await reporter.request("/app/issues/new")).text(); const stamp = /name="stamp" value="([^"]+)"/.exec(form)?.[1] ?? ""; tick(4); const filed = await reporter.request("/app/issues/new", { method: "POST", body: new URLSearchParams({ stamp, website: "", title: "Crash start", body: "It crashed.", type: "bug", version: "", name: "Ada", email: "ada@private.example", }), }); expect(filed.status).toBe(200); fileIssueDirect(inst, "hush"); }); afterAll(() => setSystemTime()); test("the console lists issues across repos, private ones too, with the contact detail", async () => { const list = await (await operator.get("/admin/issues")).text(); expect(list).toContain('href="/admin/issues/app/1"'); expect(list).toContain('href="/admin/issues/hush/1"'); expect(list).toContain("Crash <on> start"); const page = await (await operator.get("/admin/issues/app/1")).text(); expect(page).toContain("contact: ada@private.example"); expect(await text("/app/issues/1")).not.toContain("ada@private.example"); }); test("an operator reply shows on the public thread, marked", async () => { expect((await act({ action: "reply", body: "Fixed ." })).status).toBe(303); expect((await act({ action: "reply", body: " " })).status).toBe(400); const thread = await text("/app/issues/1"); expect(thread).toContain('operator'); expect(thread).toContain("Fixed <soon>."); }); test("close with a fixed-in save prefix, then reopen", async () => { expect((await act({ action: "close", fixed_in: "zzzzzz" })).status).toBe(400); expect((await act({ action: "close", fixed_in: "0123456789ab" })).status).toBe(400); expect((await act({ action: "close", fixed_in: save.slice(0, 6) })).status).toBe(303); const thread = await text("/app/issues/1"); expect(thread).toContain("◈ closed"); expect(thread).toContain(`fixed in `); expect(await text("/app/issues?status=closed")).toContain('href="/app/issues/1"'); expect(await (await operator.get("/admin/issues?filter=closed")).text()).toContain( 'href="/admin/issues/app/1"', ); expect((await act({ action: "reopen" })).status).toBe(303); expect(await text("/app/issues/1")).toContain("◉ open"); expect(await text("/app/issues/1")).not.toContain("fixed in"); }); test("set type, and hide for spam: gone publicly, kept in the console", async () => { expect((await act({ action: "type", type: "nonsense" })).status).toBe(400); expect((await act({ action: "type", type: "idea" })).status).toBe(303); expect(await text("/app/issues")).toContain("#1 · idea"); expect((await act({ action: "hide" })).status).toBe(303); expect((await inst.app.request("/app/issues/1")).status).toBe(404); expect(await (await operator.get("/admin/issues")).text()).not.toContain("/admin/issues/app/1"); expect(await (await operator.get("/admin/issues?filter=hidden")).text()).toContain( 'href="/admin/issues/app/1"', ); expect((await act({ action: "unhide" })).status).toBe(303); expect((await inst.app.request("/app/issues/1")).status).toBe(200); expect((await act({ action: "delete" })).status).toBe(400); expect((await operator.post("/admin/issues/app/9", { action: "reopen" })).status).toBe(404); }); }); function fileIssueDirect(inst: Instance, repo: string): void { fileIssue(inst.kepr, { repo, title: `A ${repo} issue`, body: "b", type: "bug", version: "", reporterName: "", contactEmail: "", ipHash: "x", filedAt: 1, }); } describe("releases: counts, displayed notes and hiding", () => { let inst: Instance; let operator: Browser; const text = async (path: string) => (await inst.app.request(path)).text(); const edit = (version: string, fields: Record) => operator.post(`/admin/releases/app/${version}`, fields); beforeAll(async () => { const repos = await makeRepos(["app"]); const save = repos.saves.app as string; for (const version of ["1.0.0", "1.1.0"]) { await writeRelease(repos.bucketDir, "app", { version, save, notes: `notes for ${version}`, files: [{ platform: "linux-x86_64", name: "app", bytes: new Uint8Array([1]) }], }); } inst = await instance(repos.bucketDir); operator = new Browser(inst.app); await operator.signIn(); await operator.post("/admin/repos/app", { public: "1", sort_order: "0" }); await inst.app.request("/app/releases/1.1.0/linux-x86_64"); }); test("every version is listed with its download counts and notes", async () => { const page = await (await operator.get("/admin/releases")).text(); expect(page).toContain('id="app-1.1.0"'); expect(page).toContain('id="app-1.0.0"'); expect(page.indexOf('id="app-1.1.0"')).toBeLessThan(page.indexOf('id="app-1.0.0"')); expect(page).toContain('1 downloadlinux-x86_64 1'); expect(page).toContain(">notes for 1.1.0"); }); test("edited notes replace the record's everywhere; the record's own text clears the edit", async () => { expect((await edit("1.1.0", { notes: "Better ." })).status).toBe(303); expect(JSON.parse(await text("/api/app/releases/latest")).notes).toBe("Better ."); expect(await text("/app/releases")).toContain("Better <notes>."); expect(await (await operator.get("/admin/releases")).text()).toContain("(edited)"); expect((await edit("1.1.0", { notes: "notes for 1.1.0" })).status).toBe(303); const row = inst.kepr.query("SELECT notes FROM release_settings WHERE version = '1.1.0'").get(); expect(row).toEqual({ notes: null }); expect((await edit("1.1.0", { notes: "x".repeat(20_001) })).status).toBe(400); }); test("a hidden version leaves pages, latest and every feed, and stays in the console", async () => { expect((await edit("1.1.0", { notes: "notes for 1.1.0", hidden: "1" })).status).toBe(303); expect(await text("/app/releases/latest/version.txt")).toBe("1.0.0"); expect(JSON.parse(await text("/api/app/releases/latest")).version).toBe("1.0.0"); expect(await text("/api/app/releases/history")).not.toContain("1.1.0"); expect(await text("/app/releases")).not.toContain("1.1.0"); expect((await inst.app.request("/app/releases/1.1.0/linux-x86_64")).status).toBe(404); const page = await (await operator.get("/admin/releases")).text(); expect(page).toContain('id="app-1.1.0"'); expect(page).toContain('value="1" checked> hidden'); expect((await edit("1.1.0", { notes: "notes for 1.1.0" })).status).toBe(303); expect(await text("/app/releases/latest/version.txt")).toBe("1.1.0"); expect((await edit("9.9.9", { notes: "" })).status).toBe(404); expect((await operator.post("/admin/releases/nowhere/1.0.0", { notes: "" })).status).toBe(404); }); });