store.test.js
1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// RED/GREEN check for the JSON CRUD store shared by characters/garments/
// dance-refs (dev-plan.md T1). Run: node --test store.test.js
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const { createStore } = require("./store.js");
function tempFile() {
return path.join(os.tmpdir(), `store-test-${Date.now()}-${Math.random().toString(36).slice(2)}.json`);
}
test("create/list/get/update/remove round-trip", () => {
const file = tempFile();
const store = createStore(file);
const created = store.create({ name: "test" });
assert.equal(typeof created.id, "string");
assert.equal(created.name, "test");
assert.equal(typeof created.createdAt, "number");
assert.deepEqual(store.list(), [created]);
assert.deepEqual(store.get(created.id), created);
const updated = store.update(created.id, { name: "updated" });
assert.equal(updated.name, "updated");
assert.equal(updated.id, created.id);
assert.equal(store.remove(created.id), true);
assert.deepEqual(store.list(), []);
assert.equal(store.remove(created.id), false);
fs.unlinkSync(file);
});
test("data survives a fresh store instance pointed at the same file (simulated process restart)", () => {
const file = tempFile();
const store1 = createStore(file);
const created = store1.create({ name: "persisted" });
const store2 = createStore(file); // new instance, no shared in-memory state
assert.deepEqual(store2.get(created.id), created);
fs.unlinkSync(file);
});
test("list() on a missing file returns an empty array, not a throw", () => {
const file = tempFile(); // never created
const store = createStore(file);
assert.deepEqual(store.list(), []);
});