Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

JavaScript · 89 lines · 3.8 KiB · at b0c0c21

import { test } from "node:test";import assert from "node:assert/strict";import { h, load } from "../../lib/harness.mjs";const { transfer } = await load();const run = (accounts, from, to, amount) => h.res(() => transfer(accounts, from, to, amount));const fail = (error) => ({ ok: false, error });const acct = (balanceCents, frozen = false) => ({ balanceCents, frozen });const book = () => ({ alice: acct(1000), bob: acct(50), carol: acct(0), ice: acct(500, true) });test("moves money", () => {  assert.deepEqual(run(book(), "alice", "bob", 300), {    ok: true,    value: { alice: acct(700), bob: acct(350), carol: acct(0), ice: acct(500, true) },  });});test("balance may reach exactly zero", () => {  const r = h.okOf(run(book(), "bob", "carol", 50));  assert.deepEqual(r.bob, acct(0));  assert.deepEqual(r.carol, acct(50));});test("amount must be a positive integer", () => {  for (const amount of [0, -1, 0.1, 12.5, Number.NaN, Number.POSITIVE_INFINITY]) {    assert.deepEqual(run(book(), "alice", "bob", amount), fail({ kind: "invalid_amount" }), `amount ${amount}`);  }  assert.equal(h.okOf(run(book(), "alice", "bob", 1)).alice.balanceCents, 999);});test("amount is checked before anything else", () => {  assert.deepEqual(run(book(), "ghost", "ghost", 0), fail({ kind: "invalid_amount" }));  assert.deepEqual(run(book(), "ghost", "bob", -3), fail({ kind: "invalid_amount" }));});test("same account", () => {  assert.deepEqual(run(book(), "alice", "alice", 10), fail({ kind: "same_account" }));  assert.deepEqual(run(book(), "ghost", "ghost", 10), fail({ kind: "same_account" }));});test("unknown accounts, from first", () => {  assert.deepEqual(run(book(), "ghost", "bob", 10), fail({ kind: "unknown_account", id: "ghost" }));  assert.deepEqual(run(book(), "alice", "ghost", 10), fail({ kind: "unknown_account", id: "ghost" }));  assert.deepEqual(run(book(), "ghost1", "ghost2", 10), fail({ kind: "unknown_account", id: "ghost1" }));  assert.deepEqual(run(book(), "alice", "ice", 999999), fail({ kind: "frozen", id: "ice" }));});test("inherited object properties are not accounts", () => {  assert.deepEqual(run(book(), "toString", "bob", 10), fail({ kind: "unknown_account", id: "toString" }));  assert.deepEqual(run(book(), "alice", "constructor", 10), fail({ kind: "unknown_account", id: "constructor" }));});test("frozen accounts, from first", () => {  const accounts = { ...book(), ice2: acct(100, true) };  assert.deepEqual(run(accounts, "ice", "alice", 10), fail({ kind: "frozen", id: "ice" }));  assert.deepEqual(run(accounts, "alice", "ice", 10), fail({ kind: "frozen", id: "ice" }));  assert.deepEqual(run(accounts, "ice2", "ice", 10), fail({ kind: "frozen", id: "ice2" }));});test("insufficient funds carries the details", () => {  assert.deepEqual(    run(book(), "bob", "alice", 51),    fail({ kind: "insufficient_funds", id: "bob", balanceCents: 50, amountCents: 51 }),  );  assert.deepEqual(    run(book(), "carol", "alice", 1),    fail({ kind: "insufficient_funds", id: "carol", balanceCents: 0, amountCents: 1 }),  );});test("frozen is reported before insufficient funds", () => {  assert.deepEqual(run(book(), "bob", "ice", 5000), fail({ kind: "frozen", id: "ice" }));});test("input is never modified", () => {  const accounts = book();  const snapshot = JSON.parse(JSON.stringify(accounts));  const r = h.okOf(run(accounts, "alice", "bob", 300));  assert.deepEqual(accounts, snapshot);  assert.notEqual(r, accounts);  run(accounts, "bob", "alice", 9999);  assert.deepEqual(accounts, snapshot);});test("chained transfers use the returned state", () => {  const first = h.okOf(run(book(), "alice", "carol", 400));  const second = h.okOf(run(first, "carol", "bob", 400));  assert.deepEqual(second, { alice: acct(600), bob: acct(450), carol: acct(0), ice: acct(500, true) });});