repoworkspace.test.ts

8.4 KB · raw

1// Covers §24's selected-repo workspace: the summary, detected and shown languages, where the2// description comes from, and the repo's releases.34import { Database } from "bun:sqlite";5import { beforeAll, describe, expect, test } from "bun:test";6import { mkdtemp, writeFile } from "node:fs/promises";7import { tmpdir } from "node:os";8import { join } from "node:path";9import { openKeprDb } from "../src/server/db.ts";10import { Browser, type Instance, instance } from "./browser.ts";11import { makeRepos, writeRelease } from "./fixture.ts";1213describe("the selected repo's workspace (§24)", () => {14	let inst: Instance;15	let browser: Browser;16	let bucketDir: string;17	const workspace = async () => (await browser.get("/admin/repos?repo=app")).text();18	const save = (fields: Record<string, string>) =>19		browser.post("/admin/repos/app", { public: "1", ...fields });2021	beforeAll(async () => {22		const repos = await makeRepos(["app"]);23		const work = join(repos.bucketDir, "..", "work", "app");24		await writeFile(join(work, "main.ts"), "export const a = 1;\nexport const b = 2;\n");25		await writeFile(join(work, "tool.py"), "print(1)\n");26		repos.koh("app", "save", "add sources");27		repos.koh("app", "offer");28		const head = JSON.parse(repos.koh("app", "view", "--json")).head_id as string;29		for (const version of ["1.0.0", "1.1.0"]) {30			await writeRelease(repos.bucketDir, "app", {31				version,32				save: head,33				files: [{ platform: "linux-x86_64", name: "app", bytes: new Uint8Array([1]) }],34			});35		}36		bucketDir = repos.bucketDir;37		inst = await instance(repos.bucketDir);38		inst.kepr.run(39			`INSERT INTO issues (repo, number, title, body, type, filed_at)40			 VALUES ('app', 1, 'Broken', 'It broke', 'bug', 0)`,41		);42		browser = new Browser(inst.app);43		await browser.signIn();44		await save({});45		await inst.app.request("/app/releases/1.1.0/linux-x86_64");46		await inst.app.request("/app/releases/1.1.0/linux-x86_64");47	});4849	test("summarises saves, lines, the newest version, open issues and downloads", async () => {50		const page = await workspace();51		expect(page).toContain('class="admin-vitals"');52		expect(page).toMatch(/<strong>2<\/strong><span class="mono">saves<\/span>/);53		expect(page).toMatch(/<strong>3<\/strong><span class="mono">lines<\/span>/);54		expect(page).toMatch(/<strong>1\.1\.0<\/strong><span class="mono">newest version<\/span>/);55		expect(page).toMatch(/<strong>1<\/strong><span class="mono">open issues<\/span>/);56		expect(page).toMatch(/<strong>2<\/strong><span class="mono">downloads<\/span>/);57	});5859	test("one click per repo: its tab holds overview, settings, builds and archives together", async () => {60		const page = await workspace();61		expect(page).toContain('href="/admin/repos?repo=app" aria-current="page"');62		for (const mark of [63			'class="admin-vitals"',64			'name="hidden_paths"',65			'action="/admin/releases/app/1.1.0"',66			'action="/admin/repos/app/tarballs/delete"',67		]) {68			expect(page).toContain(mark);69		}70		for (const id of ["overview", "settings", "builds", "archives"]) {71			expect(page).toContain(`<section class="repo-section" id="${id}">`);72		}73		const saved = await save({});74		expect(saved.headers.get("location")).toBe("/admin/repos?repo=app&saved=1");75		const refused = await save({ languages: "a, b, c, d, e, f" });76		expect(await refused.text()).toContain('name="hidden_paths"');77	});7879	test("lists the detected languages with their lines", async () => {80		const page = await workspace();81		expect(page).toContain('TypeScript <span class="muted">2 lines</span>');82		expect(page).toContain('Python <span class="muted">1 line</span>');83	});8485	test("edits the description and details; blank hands them back to the README", async () => {86		expect(await workspace()).toMatch(87			/<textarea[^>]*name="description"[^>]*><\/textarea>/,88		);89		const saved = await save({ description: "Own *words*", details: "## Usage\r\nRun it." });90		expect(saved.status).toBe(303);91		const page = await workspace();92		expect(page).toContain("Own *words*</textarea>");93		expect(page).toContain("## Usage\nRun it.</textarea>");94		const repo = await (await inst.app.request("/app")).text();95		expect(repo).toContain("Own <em>words</em>");96		expect(repo).toContain("Run it.");97		const long = await save({ details: "x".repeat(60_001) });98		expect(long.status).toBe(400);99		expect(await long.text()).toContain("Keep the details under 60,000 characters.");100		await save({ description: "", details: "" });101		expect(inst.kepr.query("SELECT description, details FROM repo_text").get()).toEqual({102			description: "",103			details: "",104		});105	});106107	test("edits its builds in place, with visibility and downloads", async () => {108		inst.kepr.run(109			"INSERT INTO release_settings (repo, version, hidden) VALUES ('app', '1.0.0', 1)",110		);111		const page = await workspace();112		expect(page).toContain('action="/admin/releases/app/1.1.0"');113		expect(page).toContain("<span>shown</span><span>2 downloads</span>");114		expect(page).toContain("<span>hidden</span><span>0 downloads</span>");115		inst.kepr.run("DELETE FROM release_settings");116	});117118	test("shown languages replace the header's detected language; blank brings it back", async () => {119		const header = async () => (await inst.app.request("/app")).text();120		expect(await header()).toContain('<span class="lang-badge">TypeScript</span>');121		expect((await save({ languages: " Zig ,  TypeScript" })).status).toBe(303);122		const shown = await header();123		expect(shown).toContain('<span class="lang-badge">Zig</span>');124		expect(shown).toContain('<span class="lang-badge">TypeScript</span>');125		expect(await workspace()).toContain('value="Zig, TypeScript"');126		expect((await save({ languages: "" })).status).toBe(303);127		const detected = await header();128		expect(detected).not.toContain('<span class="lang-badge">Zig</span>');129		expect(detected).toContain('<span class="lang-badge">TypeScript</span>');130	});131132	test("refuses more than five languages, a long name or a control character", async () => {133		const refusals: [string, string][] = [134			["a, b, c, d, e, f", "up to five languages."],135			["x".repeat(31), "a language name is at most 30 characters."],136			["Zig\u0007", "a language name is printable text."],137		];138		for (const [languages, error] of refusals) {139			const response = await save({ languages });140			expect(response.status).toBe(400);141			expect(await response.text()).toContain(error);142		}143		expect(await workspace()).not.toContain('value="Zig');144	});145146	test("deletes the repo's kept tarballs from the bucket", async () => {147		await save({});148		const head = (await inst.app.request("/app/archive/HEAD.tar.gz")).headers.get("location");149		expect((await inst.app.request(head ?? "")).status).toBe(200);150		const kept = new Bun.Glob("**/*.tar.gz");151		const keptIn = () => Array.fromAsync(kept.scan(join(bucketDir, ".kepr", "tarballs", "app")));152		expect(await keptIn()).toHaveLength(1);153		expect(await workspace()).toContain('action="/admin/repos/app/tarballs/delete"');154		const deleted = await browser.post("/admin/repos/app/tarballs/delete", {});155		expect(deleted.headers.get("location")).toBe("/admin/repos?repo=app&saved=1#archives");156		expect(await keptIn()).toEqual([]);157		expect((await inst.app.request(head ?? "")).status).toBe(200);158	});159});160161test("an older kepr.db gains the shown-languages column when opened", async () => {162	const dir = await mkdtemp(join(tmpdir(), "kepr-old-db-"));163	const old = new Database(join(dir, "kepr.db"));164	old.run("CREATE TABLE repos (name TEXT PRIMARY KEY, public INTEGER NOT NULL DEFAULT 0)");165	old.run("INSERT INTO repos (name) VALUES ('app')");166	old.close();167	const db = openKeprDb(dir);168	expect(db.query<{ languages: string }, []>("SELECT languages FROM repos").get()?.languages).toBe(169		"",170	);171});172173test("an older kepr.db loses card order and install lines, keeping each repo", async () => {174	const dir = await mkdtemp(join(tmpdir(), "kepr-old-db-"));175	const old = new Database(join(dir, "kepr.db"));176	old.run(`CREATE TABLE repos (177		name TEXT PRIMARY KEY, public INTEGER NOT NULL DEFAULT 0, sort_order INTEGER NOT NULL DEFAULT 0,178		install1 TEXT NOT NULL DEFAULT '', install2 TEXT NOT NULL DEFAULT '',179		install3 TEXT NOT NULL DEFAULT '')`);180	old.run("INSERT INTO repos (name, public, install1) VALUES ('app', 1, 'nix profile add app')");181	old.close();182	const db = openKeprDb(dir);183	const columns = db.query<{ name: string }, []>("PRAGMA table_info(repos)").all();184	expect(columns.map((column) => column.name)).not.toContain("install1");185	expect(columns.map((column) => column.name)).not.toContain("sort_order");186	expect(db.query("SELECT name, public FROM repos").all()).toEqual([{ name: "app", public: 1 }]);187});