feat: add engine and store snapshot/restore for restart resilience

This commit is contained in:
Codex
2026-02-18 12:57:25 +00:00
parent 76f673fe4c
commit 9b3cdbff1a
6 changed files with 117 additions and 15 deletions

View File

@@ -61,3 +61,27 @@ test("is idempotent by idempotency key", () => {
assert.equal(first.id, second.id);
assert.equal(wallet.getBalance("u1"), 4);
});
test("can restore state from previous snapshot", () => {
const original = new WalletStore();
original.applyTransaction({
userId: "u1",
type: "credit",
amount: 3,
reason: "topup",
idempotencyKey: "evt-r1",
});
const restored = new WalletStore(original.exportState());
assert.equal(restored.getBalance("u1"), 3);
const duplicate = restored.applyTransaction({
userId: "u1",
type: "credit",
amount: 3,
reason: "topup",
idempotencyKey: "evt-r1",
});
assert.equal(duplicate.amount, 3);
assert.equal(restored.getBalance("u1"), 3);
});