portofel / client / test / model.test.mjs
 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// The pure wallet decisions (lib/model), black-box over the compiled testlib.

import test from "node:test";
import assert from "node:assert/strict";
import * as L from "../test-dist/testlib.js";

const S = "A".repeat(43);
const B = "B".repeat(43);
const TOKEN = `${S}.${B}`;
const TOKEN2 = `${S}.${B}.2`;

test("token-of-paste: whole capabilities only", () => {
  // the four ways a link actually arrives
  assert.equal(L.tokenOfPaste(TOKEN), TOKEN, "bare token");
  assert.equal(L.tokenOfPaste(`#${TOKEN2}`), TOKEN2, "fragment");
  assert.equal(L.tokenOfPaste(`https://banca.ardegazu.ro/#${TOKEN}`), TOKEN, "whole URL");
  assert.equal(L.tokenOfPaste(`  ${TOKEN}  `), TOKEN, "whitespace trimmed");

  // half a capability is not a shorter capability
  assert.equal(L.tokenOfPaste(S), null, "a bare secret is a room, not a bank");
  assert.equal(L.tokenOfPaste(`${S}.${B.slice(0, 42)}`), null, "truncated banker half");

  // an unanchored run must not fish a "link" out of arbitrary text
  assert.equal(L.tokenOfPaste(`x${TOKEN}`), null, "prefixed run");
  assert.equal(L.tokenOfPaste(`${TOKEN}y`), null, "suffixed run");
  assert.equal(L.tokenOfPaste(""), null);
  assert.equal(L.tokenOfPaste(null), null);
});

test("v1-token?: the version suffix decides", () => {
  assert.equal(L.isV1Token(TOKEN), true, "no suffix = BANK/1");
  assert.equal(L.isV1Token(TOKEN2), false, "the .2 suffix = BANK/2");
});

const acct = (id, state) => ({ id, state, name: "", seq: 0, bal: 0, balExact: "0", locked: 0, lockedExact: "0" });

test("payable-partition: friends with accounts / others / friends without", () => {
  const me = "M".repeat(43);
  const f1 = "F".repeat(43); // friend, open account
  const f2 = "G".repeat(43); // friend, no account
  const o1 = "O".repeat(43); // stranger, open account
  const p1 = "P".repeat(43); // stranger, pending — NOT payable
  const snap = {
    ok: true,
    accounts: [acct(me, "open"), acct(f1, "open"), acct(o1, "open"), acct(p1, "pending")],
  };
  const split = L.payablePartition(snap, me, [f1, f2, me]);
  assert.deepEqual([...split.here], [f1], "friend with an open account");
  assert.deepEqual([...split.away], [f2], "friend without one");
  assert.deepEqual([...split.others], [o1], "open stranger only — pending is not payable, me is excluded");
});

test("payable-partition: order is the friend list's / the fold's", () => {
  const me = "M".repeat(43);
  const a = "A".repeat(42) + "a";
  const b = "B".repeat(42) + "b";
  const c = "C".repeat(42) + "c";
  const snap = { ok: true, accounts: [acct(c, "open"), acct(b, "open"), acct(a, "open")] };
  const split = L.payablePartition(snap, me, [a, b]);
  assert.deepEqual([...split.here], [a, b], "friend order preserved");
  assert.deepEqual([...split.others], [c], "fold order preserved");
});

test("track!: pending window opens before the dial and closes on settle", async () => {
  const pending = new Set();
  const edges = [];
  const onChange = () => edges.push(pending.has("tok"));
  const out = await L.trackPending(pending, "tok", onChange, () => Promise.resolve(null));
  assert.equal(out, null, "the settled value passes through untouched");
  assert.deepEqual(edges, [true, false], "marked before, cleared after");
});

test("track!: a refusal (catalog key) passes through and still clears", async () => {
  const pending = new Set();
  const out = await L.trackPending(pending, "tok", () => {}, () => Promise.resolve("err.bank.open"));
  assert.equal(out, "err.bank.open");
  assert.equal(pending.has("tok"), false);
});

static mirror of HEAD · about · clone: git clone https://git.ardegazu.ro/portofel.git