pricing.test.js
1.62 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
// RED/GREEN check for pricing_config (dev-plan-4.md T23). Run: node --test pricing.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 { createPricingStore } = require("./pricing.js");
function tmpPricingPath() {
return path.join(fs.mkdtempSync(path.join(os.tmpdir(), "pricing-test-")), "pricing.json");
}
test("a fresh file is seeded with placeholder defaults, including spend_garment", () => {
const store = createPricingStore(tmpPricingPath());
const spend = store.get("spend_garment");
assert.ok(spend);
assert.equal(spend.kind, "spend");
assert.ok(spend.credits > 0);
});
test("upsert creates a new key and updates an existing one in place", () => {
const store = createPricingStore(tmpPricingPath());
const created = store.upsert("spend_character", { kind: "spend", credits: 50 });
assert.equal(created.credits, 50);
assert.equal(store.list().filter((p) => p.key === "spend_character").length, 1);
const updated = store.upsert("spend_garment", { credits: 15 });
assert.equal(updated.credits, 15);
assert.equal(store.get("spend_garment").credits, 15);
// upsert didn't duplicate the row
assert.equal(store.list().filter((p) => p.key === "spend_garment").length, 1);
});
test("changes persist across a fresh store instance pointed at the same file", () => {
const filePath = tmpPricingPath();
createPricingStore(filePath).upsert("spend_garment", { credits: 25 });
const reopened = createPricingStore(filePath);
assert.equal(reopened.get("spend_garment").credits, 25);
});