| 1 | import assert from "node:assert/strict"; |
| 2 | import { readFileSync, readdirSync } from "node:fs"; |
| 3 | import { DatabaseSync } from "node:sqlite"; |
| 4 | import { test } from "node:test"; |
| 5 | |
| 6 | const dir = new URL("../migrations/", import.meta.url); |
| 7 | const MIGRATION = "0011_work_items_dataset.sql"; |
| 8 | |
| 9 | test("dashboard templates that read issues read work items after the migration, run once or twice", () => { |
| 10 | const db = new DatabaseSync(":memory:"); |
| 11 | for (const file of readdirSync(dir).sort()) { |
| 12 | if (file >= MIGRATION) break; |
| 13 | db.exec(readFileSync(new URL(file, dir), "utf8")); |
| 14 | } |
| 15 | const insert = db.prepare( |
| 16 | "INSERT INTO folio_templates (id, workspace_id, kind, name, body, created_by, created_at) VALUES (?, 'wsp_1', ?, ?, ?, 'user:usr_1', 'now')", |
| 17 | ); |
| 18 | const tiles = [ |
| 19 | { id: "t1", type: "stat", query: { dataset: "issues", measure: { op: "count" } } }, |
| 20 | { id: "t2", type: "markdown", query: null, markdown: "Notes" }, |
| 21 | { id: "t3", type: "line", query: { dataset: "pull_requests", measure: { op: "count" } } }, |
| 22 | ]; |
| 23 | insert.run("tpl_1", "dashboard", "Triage", JSON.stringify({ tiles })); |
| 24 | insert.run("tpl_2", "doc", "Notes", "# issues"); |
| 25 | insert.run("tpl_3", "dashboard", "Not JSON", "not json"); |
| 26 | const sql = readFileSync(new URL(MIGRATION, dir), "utf8"); |
| 27 | db.exec(sql); |
| 28 | db.exec(sql); |
| 29 | const body = (id: string) => (db.prepare("SELECT body FROM folio_templates WHERE id = ?").get(id) as { body: string }).body; |
| 30 | const migrated = JSON.parse(body("tpl_1")) as { tiles: { id: string; query: { dataset: string } | null }[] }; |
| 31 | assert.deepEqual( |
| 32 | migrated.tiles.map((tile) => [tile.id, tile.query?.dataset ?? null]), |
| 33 | [ |
| 34 | ["t1", "work_items"], |
| 35 | ["t2", null], |
| 36 | ["t3", "pull_requests"], |
| 37 | ], |
| 38 | ); |
| 39 | assert.equal(body("tpl_2"), "# issues"); |
| 40 | assert.equal(body("tpl_3"), "not json"); |
| 41 | }); |