import type { Database } from "bun:sqlite"; import { beforeAll, describe, expect, test } from "bun:test"; import { mkdir, mkdtemp, unlink, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import type { Hono } from "hono"; import { openBucket } from "../src/bucket/reader.ts"; import { syncAll } from "../src/bucket/sync.ts"; import { openMirror } from "../src/mirror/store.ts"; import { createApp } from "../src/server/app.ts"; import { openKeprDb } from "../src/server/db.ts"; import { Browser, PASSWORD } from "./browser.ts"; import { makeRepos } from "./fixture.ts"; let app: Hono; let kepr: Database; let dataDir: string; let alphaSaves: { id: string; message: string; is_checkpoint: boolean }[]; let vaultSave: string; const body = async (path: string) => (await app.request(path)).text(); // alpha (public): a save, a checkpoint holding a draft no real save keeps, then a second save. // vault (private): one save whose message, category, language and ID must never show. beforeAll(async () => { const repos = await makeRepos(["alpha", "vault"]); const root = join(repos.bucketDir, ".."); await writeFile(join(root, "work/alpha/draft.txt"), "a draft only a checkpoint holds\n"); repos.koh("alpha", "checkpoint", "checkpoint message never shown"); await unlink(join(root, "work/alpha/draft.txt")); await writeFile(join(root, "work/alpha/README.md"), "# alpha\n\nThe alpha project, grown.\n"); await writeFile(join(root, "work/alpha/main.ts"), "export const a = 1;\n"); repos.koh("alpha", "save", "grow alpha"); // HEAD is a checkpoint adding a new language: none of it may reach a public stat. await writeFile(join(root, "work/alpha/tool.py"), "print(1)\n"); repos.koh("alpha", "checkpoint", "checkpoint at head adds python"); repos.koh("alpha", "offer"); await writeFile(join(root, "work/vault/main.zig"), "const x = 1;\n"); repos.koh("vault", "save", "vault message"); alphaSaves = JSON.parse(repos.koh("alpha", "view", "--json")).saves; vaultSave = JSON.parse(repos.koh("vault", "view", "--json")).head_id; dataDir = await mkdtemp(join(tmpdir(), "kepr-front-")); kepr = openKeprDb(dataDir); const bucket = openBucket({ kind: "folder", path: repos.bucketDir }); await syncAll({ bucket, kepr, mirrorDir: join(dataDir, "mirror"), }); kepr.run("UPDATE repos SET public = 1, category = 'tools' WHERE name = 'alpha'"); kepr.run("UPDATE repos SET category = 'secretcat' WHERE name = 'vault'"); for (const [key, value] of [ ["handle", "ada"], ["tagline", "A library of small tools."], ["bio", "Writes small tools."], ["location", "Lisbon"], ["link", "https://ada.example"], ["building_since", "may 2026"], ]) { kepr.query("INSERT INTO settings (key, value) VALUES (?, ?)").run(key, value); } kepr.run( `INSERT INTO issues (repo, number, title, body, type, filed_at, hidden) VALUES ('alpha', 1, 'a', 'b', 'bug', 1, 0), ('alpha', 2, 'c', 'd', 'bug', 1, 1), ('vault', 1, 'e', 'f', 'bug', 1, 0)`, ); kepr.run( `INSERT INTO downloads (repo, version, platform, count) VALUES ('alpha', '1.0.0', 'linux-x86_64', 7), ('alpha', '0.9.0', 'linux-x86_64', 100), ('vault', '1.0.0', 'linux-x86_64', 1000)`, ); kepr.run("INSERT INTO release_settings (repo, version, hidden) VALUES ('alpha', '0.9.0', 1)"); app = createApp( { domain: "kepr.example", dataDir, adminPasswordHash: await Bun.password.hash(PASSWORD, "argon2id"), bucket: { kind: "folder", path: repos.bucketDir }, }, kepr, ); }); const checkpoints = () => alphaSaves.filter((save) => save.is_checkpoint).map((save) => save.id); const realSaves = () => alphaSaves.filter((save) => !save.is_checkpoint); test("only a live operator session can see and open private repo cards", async () => { const operator = new Browser(app); const visitor = new Browser(app); const listings = ["/", "/browse", "/@ada"]; const card = 'class="repo-card" href="/vault"'; visitor.cookies.set("kepr_editor", "1"); kepr.run( "INSERT INTO settings (key, value) VALUES ('umami_src', 'https://stats.example/s.js'), ('umami_id', 'test')", ); kepr.run("INSERT INTO renamed (old, new) VALUES ('oldvault', 'vault')"); try { for (const path of listings) { const response = await visitor.get(path); expect(response.headers.get("vary")).toContain("Cookie"); const markup = await response.text(); expect(markup).not.toContain(card); expect(markup).toContain("https://stats.example/s.js"); } expect((await operator.signIn()).status).toBe(303); for (const path of listings) { const response = await operator.get(path); expect(response.headers.get("cache-control")).toBe("no-store"); const markup = await response.text(); expect(markup).toContain(card); expect(markup).not.toContain("https://stats.example/s.js"); expect(await (await visitor.get(path)).text()).not.toContain(card); } for (const path of [ "/vault", "/vault/timeline", "/vault/atlas/saves", "/vault/atlas/panel?path=main.zig", `/vault/saves/${vaultSave}`, "/vault/files/main.zig", `/vault/raw/main.zig?at=${vaultSave}`, ]) { const response = await operator.get(path); expect([path, response.status]).toEqual([path, 200]); expect(response.headers.get("cache-control")).toBe("no-store"); expect(await response.text()).not.toContain("https://stats.example/s.js"); expect((await visitor.get(path)).status).toBe(404); } for (const path of [ "/oldvault", "/vault/issues", "/vault/releases/latest.json", "/vault/koh/head", ]) { expect((await visitor.get(path)).status).toBe(404); expect((await operator.get(path)).status).toBe(404); } const prefix = await operator.get(`/vault/saves/${vaultSave.slice(0, 7)}`); expect(prefix.status).toBe(302); expect(prefix.headers.get("cache-control")).toBe("no-store"); const token = operator.cookies.get("kepr_session") ?? ""; expect((await operator.post("/admin/sign-out", {})).status).toBe(303); operator.cookies.set("kepr_session", token); for (const path of listings) expect(await (await operator.get(path)).text()).not.toContain(card); expect((await operator.get("/vault")).status).toBe(404); } finally { kepr.run("DELETE FROM settings WHERE key IN ('umami_src', 'umami_id')"); kepr.run("DELETE FROM renamed WHERE old = 'oldvault'"); } }); describe("front page", () => { test("counts distinct objects of public repos' real saves only", async () => { // alpha's real saves hold .kohignore, two READMEs and main.ts; its mirror also holds the // draft and tool.py only checkpoints kept, and the private repo's objects are elsewhere. const mirror = openMirror(join(dataDir, "mirror"), "alpha"); expect(mirror.query("SELECT COUNT(*) AS n FROM objects").get()).toEqual({ n: 6 }); expect(await body("/")).toContain("The library knows
4 things."); }); test("the ticker, cloud and cards show public saves, never checkpoints or private repos", async () => { const html = await body("/"); expect(html).toContain('