// Covers §24's selected-repo workspace: the summary, detected and shown languages, where the // description comes from, and the repo's releases. import { Database } from "bun:sqlite"; import { beforeAll, describe, expect, test } from "bun:test"; import { mkdtemp, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { openKeprDb } from "../src/server/db.ts"; import { Browser, type Instance, instance } from "./browser.ts"; import { makeRepos, writeRelease } from "./fixture.ts"; describe("the selected repo's workspace (§24)", () => { let inst: Instance; let browser: Browser; const workspace = async () => (await browser.get("/admin/repos?repo=app")).text(); const save = (fields: Record) => browser.post("/admin/repos/app", { public: "1", ...fields }); beforeAll(async () => { const repos = await makeRepos(["app"]); const work = join(repos.bucketDir, "..", "work", "app"); await writeFile(join(work, "main.ts"), "export const a = 1;\nexport const b = 2;\n"); await writeFile(join(work, "tool.py"), "print(1)\n"); repos.koh("app", "save", "add sources"); repos.koh("app", "offer"); const head = JSON.parse(repos.koh("app", "view", "--json")).head_id as string; for (const version of ["1.0.0", "1.1.0"]) { await writeRelease(repos.bucketDir, "app", { version, save: head, files: [{ platform: "linux-x86_64", name: "app", bytes: new Uint8Array([1]) }], }); } inst = await instance(repos.bucketDir); inst.kepr.run( `INSERT INTO issues (repo, number, title, body, type, filed_at) VALUES ('app', 1, 'Broken', 'It broke', 'bug', 0)`, ); browser = new Browser(inst.app); await browser.signIn(); await save({}); await inst.app.request("/app/releases/1.1.0/linux-x86_64"); await inst.app.request("/app/releases/1.1.0/linux-x86_64"); }); test("summarises saves, lines, the newest version, open issues and downloads", async () => { const page = await workspace(); expect(page).toContain('class="admin-vitals"'); expect(page).toMatch(/2<\/strong>saves<\/span>/); expect(page).toMatch(/3<\/strong>lines<\/span>/); expect(page).toMatch(/1\.1\.0<\/strong>newest version<\/span>/); expect(page).toMatch(/1<\/strong>open issues<\/span>/); expect(page).toMatch(/2<\/strong>downloads<\/span>/); }); test("lists the detected languages with their lines", async () => { const page = await workspace(); expect(page).toContain('TypeScript 2 lines'); expect(page).toContain('Python 1 line'); }); test("edits the description and details; blank hands them back to the README", async () => { expect(await workspace()).toMatch(/]*name="description"[^>]*><\/textarea>/); const saved = await save({ description: "Own *words*", details: "## Usage\r\nRun it." }); expect(saved.status).toBe(303); const page = await workspace(); expect(page).toContain("Own *words*"); expect(page).toContain("## Usage\nRun it."); const repo = await (await inst.app.request("/app")).text(); expect(repo).toContain("Own words"); expect(repo).toContain("Run it."); const long = await save({ details: "x".repeat(60_001) }); expect(long.status).toBe(400); expect(await long.text()).toContain("Keep the details under 60,000 characters."); await save({ description: "", details: "" }); expect(inst.kepr.query("SELECT description, details FROM repo_text").get()).toEqual({ description: "", details: "", }); }); test("edits its builds in place, with visibility and downloads", async () => { inst.kepr.run( "INSERT INTO release_settings (repo, version, hidden) VALUES ('app', '1.0.0', 1)", ); const page = await workspace(); expect(page).toContain('action="/admin/releases/app/1.1.0"'); expect(page).toContain("shown2 downloads"); expect(page).toContain("hidden0 downloads"); inst.kepr.run("DELETE FROM release_settings"); }); test("shown languages replace the header's detected language; blank brings it back", async () => { const header = async () => (await inst.app.request("/app")).text(); expect(await header()).toContain('TypeScript'); expect((await save({ languages: " Zig , TypeScript" })).status).toBe(303); const shown = await header(); expect(shown).toContain('Zig'); expect(shown).toContain('TypeScript'); expect(await workspace()).toContain('value="Zig, TypeScript"'); expect((await save({ languages: "" })).status).toBe(303); const detected = await header(); expect(detected).not.toContain('Zig'); expect(detected).toContain('TypeScript'); }); test("refuses more than five languages, a long name or a control character", async () => { const refusals: [string, string][] = [ ["a, b, c, d, e, f", "up to five languages."], ["x".repeat(31), "a language name is at most 30 characters."], ["Zig\u0007", "a language name is printable text."], ]; for (const [languages, error] of refusals) { const response = await save({ languages }); expect(response.status).toBe(400); expect(await response.text()).toContain(error); } expect(await workspace()).not.toContain('value="Zig'); }); }); test("an older kepr.db gains the shown-languages column when opened", async () => { const dir = await mkdtemp(join(tmpdir(), "kepr-old-db-")); const old = new Database(join(dir, "kepr.db")); old.run("CREATE TABLE repos (name TEXT PRIMARY KEY, public INTEGER NOT NULL DEFAULT 0)"); old.run("INSERT INTO repos (name) VALUES ('app')"); old.close(); const db = openKeprDb(dir); expect(db.query<{ languages: string }, []>("SELECT languages FROM repos").get()?.languages).toBe( "", ); }); test("an older kepr.db loses card order and install lines, keeping each repo", async () => { const dir = await mkdtemp(join(tmpdir(), "kepr-old-db-")); const old = new Database(join(dir, "kepr.db")); old.run(`CREATE TABLE repos ( name TEXT PRIMARY KEY, public INTEGER NOT NULL DEFAULT 0, sort_order INTEGER NOT NULL DEFAULT 0, install1 TEXT NOT NULL DEFAULT '', install2 TEXT NOT NULL DEFAULT '', install3 TEXT NOT NULL DEFAULT '')`); old.run("INSERT INTO repos (name, public, install1) VALUES ('app', 1, 'nix profile add app')"); old.close(); const db = openKeprDb(dir); const columns = db.query<{ name: string }, []>("PRAGMA table_info(repos)").all(); expect(columns.map((column) => column.name)).not.toContain("install1"); expect(columns.map((column) => column.name)).not.toContain("sort_order"); expect(db.query("SELECT name, public FROM repos").all()).toEqual([{ name: "app", public: 1 }]); });