Files
xarticleaudio/test/convex-state-store.test.js

60 lines
1.5 KiB
JavaScript

"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const { ConvexStateStore } = require("../src/lib/convex-state-store");
test("load returns null when convex query returns null", async () => {
const store = new ConvexStateStore({
client: {
async query() {
return null;
},
async mutation() {},
},
});
const state = await store.load();
assert.equal(state, null);
});
test("load unwraps snapshot envelope payload", async () => {
const store = new ConvexStateStore({
client: {
async query(functionName) {
assert.equal(functionName, "state:getLatestSnapshot");
return {
snapshot: { engine: { jobs: [] } },
};
},
async mutation() {},
},
});
const state = await store.load();
assert.deepEqual(state, { engine: { jobs: [] } });
});
test("save writes state to configured mutation function", async () => {
const calls = [];
const store = new ConvexStateStore({
writeFunction: "state:saveSnapshot",
client: {
async query() {
return null;
},
async mutation(functionName, args) {
calls.push({ functionName, args });
},
},
});
const state = { version: 1 };
await store.save(state);
assert.equal(calls.length, 1);
assert.equal(calls[0].functionName, "state:saveSnapshot");
assert.deepEqual(calls[0].args.snapshot, state);
assert.match(String(calls[0].args.updatedAt), /^\d{4}-\d{2}-\d{2}T/);
});