| 1 | test("questions are created in the next free minute, and found by type, date, and reference", async () => { |
| 2 | // the module opens its database as it loads |
| 3 | process.env.CLOVER_DB = fs.mkdtempSync(path.join(os.tmpdir(), "questions-")); |
| 4 | const questions = await import("./questions.ts"); |
| 5 | const minute = new Date(Date.UTC(2025, 0, 1, 12, 30)); |
| 6 | |
| 7 | const first = questions.create("normal", "q: one\n\nsee @2501010730", minute); |
| 8 | const second = questions.create("normal", "q: two", new Date(minute.getTime() + 59_000)); |
| 9 | const pending = questions.create("pending", JSON.stringify({ prompt: "three?", sourceName: "fox" }), minute); |
| 10 | assert.deepEqual([first, second, pending].map((date) => (date.getTime() - minute.getTime()) / 60_000), [0, 1, 2]); |
| 11 | |
| 12 | assert.deepEqual(questions.getAll().map((q) => [q.id, q.type, q.text.split("\n")[0]]), [ |
| 13 | ["2501010731", "normal", "q: two"], |
| 14 | ["2501010730", "normal", "q: one"], |
| 15 | ]); |
| 16 | assert.deepEqual(questions.getAllPending().map((q) => [q.id, q.prompt, q.sourceName, q.date]), [ |
| 17 | ["2501010732", "three?", "fox", pending], |
| 18 | ]); |
| 19 | assert.equal(questions.getByDate(new Date(first.getTime() + 30_000))?.text, "q: one\n\nsee @2501010730"); |
| 20 | assert.deepEqual(questions.search("2501010730").map((q) => q.id), ["2501010730"]); |
| 21 | |
| 22 | questions.updateByQmid(first.getTime() / 60_000, "q: one, edited", "normal"); |
| 23 | questions.rejectByQmid(second.getTime() / 60_000); |
| 24 | questions.deleteByQmid(pending.getTime() / 60_000); |
| 25 | assert.deepEqual(questions.getAll().map((q) => q.text), ["q: one, edited"]); |
| 26 | assert.equal(questions.getByDate(second)?.type, "reject"); |
| 27 | assert.equal(questions.getByDate(pending), null); |
| 28 | }); |
| 29 | |
| 30 | import assert from "node:assert/strict"; |
| 31 | import * as fs from "node:fs"; |
| 32 | import * as os from "node:os"; |
| 33 | import * as path from "node:path"; |
| 34 | import { test } from "vitest"; |