diff --git a/package.json b/package.json --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "build:viz": "bun build public/viz/viz-hydrate.ts --outfile public/viz/dist.js", "build:ui": "bun build public/ui.ts --outfile public/ui.js", "build:htmx": "cp node_modules/htmx.org/dist/htmx.min.js public/htmx.min.js", "backfill": "bun run scripts/backfill.ts", + "export-contributors": "bun run scripts/export-contributors.ts", "test": "bun test", "lint": "biome check src/ public/ tests/", "lint:fix": "biome check --write src/ public/ tests/", diff --git a/scripts/export-contributors.ts b/scripts/export-contributors.ts new file mode 100644 --- /dev/null +++ b/scripts/export-contributors.ts @@ -0,0 +1,10 @@ +#!/usr/bin/env bun + +// Print the contributor seed list. Newline-delimited, or --json for an array. +// Pipe into backfill: bun run backfill $(bun run export-contributors) + +import { listContributors } from "../src/registry/index.ts"; + +const json = process.argv.slice(2).includes("--json"); +const dids = listContributors(); +console.log(json ? JSON.stringify(dids) : dids.join("\n")); diff --git a/tests/registry/export-contributors.test.ts b/tests/registry/export-contributors.test.ts new file mode 100644 --- /dev/null +++ b/tests/registry/export-contributors.test.ts @@ -0,0 +1,55 @@ +import { Database } from "bun:sqlite"; +import { afterAll, beforeAll, describe, expect, test } from "bun:test"; +import { rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +const dbPath = join(tmpdir(), `lichen-registry-export-${Date.now()}.db`); + +beforeAll(() => { + const db = new Database(dbPath); + db.run( + `CREATE TABLE contributors (did TEXT PRIMARY KEY, first_seen TEXT NOT NULL, last_seen TEXT NOT NULL)`, + ); + db.run( + `INSERT INTO contributors (did, first_seen, last_seen) VALUES + ('did:plc:ccc','x','x'), ('did:plc:aaa','x','x'), ('did:plc:bbb','x','x')`, + ); + db.close(); +}); + +afterAll(() => { + for (const suffix of ["", "-shm", "-wal"]) { + rmSync(dbPath + suffix, { force: true }); + } +}); + +async function runExport(args: string[]): Promise { + const proc = Bun.spawn(["bun", "scripts/export-contributors.ts", ...args], { + env: { ...process.env, REGISTRY_DB_PATH: dbPath }, + stdout: "pipe", + }); + const out = await new Response(proc.stdout).text(); + await proc.exited; + return out; +} + +describe("export-contributors CLI", () => { + test("emits the distinct DID set, sorted, one per line", async () => { + const out = await runExport([]); + expect(out.trim().split("\n")).toEqual([ + "did:plc:aaa", + "did:plc:bbb", + "did:plc:ccc", + ]); + }); + + test("--json emits a sorted array", async () => { + const out = await runExport(["--json"]); + expect(JSON.parse(out)).toEqual([ + "did:plc:aaa", + "did:plc:bbb", + "did:plc:ccc", + ]); + }); +});