// 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(), []); });