| 1 | const dir = vi.hoisted(() => { |
| 2 | const fs = process.getBuiltinModule("node:fs"); |
| 3 | const os = process.getBuiltinModule("node:os"); |
| 4 | const dir = fs.mkdtempSync(os.tmpdir() + "/clover-databases-test-"); |
| 5 | Object.assign(process.env, { |
| 6 | CLOVER_DB: dir + "/db", |
| 7 | CLOVER_FILE_DERIVED: dir + "/derived", |
| 8 | CLOVER_SOT_KEY: "test", |
| 9 | }); |
| 10 | return dir; |
| 11 | }); |
| 12 | afterAll(() => fs.rmSync(dir, { recursive: true, force: true })); |
| 13 | // |
| 14 | for (const path of ["/2025/song.mp3", "/2025/.date", "/_unlisted/font.ttf", "/friends/2025/photo.jpg"]) { |
| 15 | const file = mediaFiles.createFile({ |
| 16 | path, |
| 17 | date: new Date(0), |
| 18 | hash: crypto.createHash("sha1").update(path).digest("hex"), |
| 19 | size: 0, |
| 20 | duration: 0, |
| 21 | dimensions: "", |
| 22 | contents: path === "/2025/song.mp3" ? "" : "PRIVATE " + path, |
| 23 | }); |
| 24 | const root = mediaFiles.db.get<{ id: number }>( |
| 25 | sql`insert into derived_roots (date, key) values (0, ${file.hash + "/opus"}) returning id`, |
| 26 | ).id; |
| 27 | mediaFiles.db.run(sql`insert into derived_files (file, root, size, hash) values ('a.webm', ${root}, 0, '')`); |
| 28 | mediaFiles.db.run(sql`insert into derived_refs (file, root) values (${file.id}, ${root})`); |
| 29 | } |
| 30 | permissions.set("/friends", 1); |
| 31 | questions.create("normal", "q: public"); |
| 32 | questions.create("reject", "q: PRIVATE rejected"); |
| 33 | questions.create("pending", JSON.stringify({ prompt: "PRIVATE pending", sourceName: "fox" })); |
| 34 | // a table no copy lists, like the search index questions once had |
| 35 | getDatabase("questions.sqlite").run(sql`create virtual table leftover using fts5(text)`); |
| 36 | getDatabase("questions.sqlite").run(sql`insert into leftover values ('PRIVATE rejected')`); |
| 37 | |
| 38 | async function download(name: string, headers?: Record<string, string>) { |
| 39 | const response = await app.fetch(new Request(`http://localhost/db/${name}`, { headers })); |
| 40 | assert.equal(response.status, 200); |
| 41 | const bytes = Buffer.from(await response.arrayBuffer()); |
| 42 | const file = path.join(dir, `${name}.${crypto.randomUUID()}`); |
| 43 | fs.writeFileSync(file, bytes); |
| 44 | const db = new DatabaseSync(file, { readOnly: true }); |
| 45 | const column = (query: string) => db.prepare(query).all().map((row) => Object.values(row)[0]); |
| 46 | return { bytes, column }; |
| 47 | } |
| 48 | |
| 49 | test("a public copy has only what listings show anyone, and nothing else in its bytes", async () => { |
| 50 | const cache = await download("cache.sqlite"); |
| 51 | assert.ok(!cache.bytes.includes("PRIVATE") && !cache.bytes.includes("friends")); |
| 52 | assert.deepEqual(cache.column("select path from media_files order by path"), ["/", "/2025", "/2025/song.mp3"]); |
| 53 | assert.deepEqual(cache.column("select count(*) from permissions"), [0]); |
| 54 | assert.deepEqual(cache.column("select count(*) from derived_roots"), [1]); |
| 55 | assert.deepEqual(cache.column("select count(*) from derived_files"), [1]); |
| 56 | |
| 57 | const qa = await download("questions.sqlite"); |
| 58 | assert.ok(!qa.bytes.includes("PRIVATE")); |
| 59 | assert.deepEqual(qa.column("select text from questions"), ["q: public"]); |
| 60 | assert.deepEqual(qa.column("select name from sqlite_master where type = 'table' order by name"), [ |
| 61 | "clover_migrations", |
| 62 | "questions", |
| 63 | ]); |
| 64 | }); |
| 65 | |
| 66 | test("the key gets a whole copy, and a wrong key gets nothing", async () => { |
| 67 | const qa = await download("questions.sqlite", { Authorization: "test" }); |
| 68 | assert.deepEqual(qa.column("select count(*) from questions"), [3]); |
| 69 | const wrong = await app.fetch( |
| 70 | new Request("http://localhost/db/questions.sqlite", { headers: { Authorization: "?" } }), |
| 71 | ); |
| 72 | assert.equal(wrong.status, 401); |
| 73 | assert.equal((await app.fetch(new Request("http://localhost/db/constructor"))).status, 404); |
| 74 | }); |
| 75 | |
| 76 | test("a copy is written again once its database changes", async () => { |
| 77 | questions.create("normal", "q: newer", new Date(Date.UTC(2030, 0))); |
| 78 | const qa = await download("questions.sqlite"); |
| 79 | assert.deepEqual(qa.column("select text from questions order by qmid"), ["q: public", "q: newer"]); |
| 80 | }); |
| 81 | |
| 82 | test("a restored copy replaces every row, for connections that have the database open", async () => { |
| 83 | const db = getDatabase("restored.sqlite"); |
| 84 | db.run(sql`create table questions (text text)`); |
| 85 | const response = await app.fetch(new Request("http://localhost/db/questions.sqlite")); |
| 86 | await db.restore(UNWRAP(response.body)); |
| 87 | const rows = db.all<{ text: string }>(sql`select text from questions order by qmid`); |
| 88 | assert.deepEqual(rows.map((row) => row.text), ["q: public", "q: newer"]); |
| 89 | }); |
| 90 | |
| 91 | import { getDatabase } from "#sitegen/drizzle"; |
| 92 | import { UNWRAP } from "@clo/lib/assert"; |
| 93 | import { sql } from "drizzle-orm"; |
| 94 | import { strict as assert } from "node:assert"; |
| 95 | import * as crypto from "node:crypto"; |
| 96 | import * as fs from "node:fs"; |
| 97 | import * as path from "node:path"; |
| 98 | import { DatabaseSync } from "node:sqlite"; |
| 99 | import { afterAll, test, vi } from "vitest"; |
| 100 | import { app } from "./databases.ts"; |
| 101 | import "./file-viewer/models/derived.ts"; |
| 102 | import * as mediaFiles from "./file-viewer/models/media-files.ts"; |
| 103 | import * as permissions from "./file-viewer/models/permissions.ts"; |
| 104 | import * as questions from "./q+a/models/questions.ts"; |