M1: contracts and vectors (SPEC v0.1, schema 1)

contracts/openapi.yaml (OpenAPI 3.1, E-1..E-33, error shape and the
exhaustive error code enum, device bearer scheme), contracts/schema/
server-v1.sql (SPEC §14.1 verbatim), seven test-vector files per §14.5.5,
80 examples (every JSON body of §14.3.4, every error code, sync pull/push,
every FCM push type), and tools/contracts-check.py (stdlib-only reference
of §9.1, §9.3, §9.4 and §14.5.4 plus shape checks; the M1 gate).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-17 20:57:59 +02:00
parent cab3e935d6
commit c6df2ab8f5
90 changed files with 8931 additions and 0 deletions
@@ -0,0 +1,141 @@
{
"format": 1,
"function": "compute_balances",
"cases": [
{
"name": "empty: no accounts, no rows",
"input": { "user_ids": ["a", "b"], "account_ids": [], "expenses": [], "settlements": [] },
"expected": { "ok": { "per_account": {}, "global": { "a": 0, "b": 0 } } }
},
{
"name": "one account, (1001, payer a, 50/50) and (100, payer b, a 6000 bp): a +440, b -440",
"input": {
"user_ids": ["a", "b"], "account_ids": ["x"],
"expenses": [
{ "account_id": "x", "amount_ore": 1001, "payer_user_id": "a", "shares": { "a": 501, "b": 500 } },
{ "account_id": "x", "amount_ore": 100, "payer_user_id": "b", "shares": { "a": 60, "b": 40 } }
],
"settlements": []
},
"expected": { "ok": { "per_account": { "x": { "a": 440, "b": -440 } }, "global": { "a": 440, "b": -440 } } }
},
{
"name": "same account plus settlement b -> a 440: all zero",
"input": {
"user_ids": ["a", "b"], "account_ids": ["x"],
"expenses": [
{ "account_id": "x", "amount_ore": 1001, "payer_user_id": "a", "shares": { "a": 501, "b": 500 } },
{ "account_id": "x", "amount_ore": 100, "payer_user_id": "b", "shares": { "a": 60, "b": 40 } }
],
"settlements": [ { "from_user_id": "b", "to_user_id": "a", "allocations": { "x": 440 } } ]
},
"expected": { "ok": { "per_account": { "x": { "a": 0, "b": 0 } }, "global": { "a": 0, "b": 0 } } }
},
{
"name": "two accounts with opposite signs: global is the sum",
"input": {
"user_ids": ["a", "b"], "account_ids": ["x", "y"],
"expenses": [
{ "account_id": "x", "amount_ore": 2000, "payer_user_id": "a", "shares": { "a": 1000, "b": 1000 } },
{ "account_id": "y", "amount_ore": 600, "payer_user_id": "b", "shares": { "a": 300, "b": 300 } }
],
"settlements": []
},
"expected": { "ok": { "per_account": { "x": { "a": 1000, "b": -1000 }, "y": { "a": -300, "b": 300 } }, "global": { "a": 700, "b": -700 } } }
},
{
"name": "an account in account_ids with no rows has zeros",
"input": {
"user_ids": ["a", "b"], "account_ids": ["x", "y"],
"expenses": [ { "account_id": "x", "amount_ore": 1001, "payer_user_id": "a", "shares": { "a": 501, "b": 500 } } ],
"settlements": []
},
"expected": { "ok": { "per_account": { "x": { "a": 500, "b": -500 }, "y": { "a": 0, "b": 0 } }, "global": { "a": 500, "b": -500 } } }
},
{
"name": "archived accounts are included: the archived account is in account_ids and counts toward global",
"input": {
"user_ids": ["a", "b"], "account_ids": ["live", "arch"],
"expenses": [
{ "account_id": "live", "amount_ore": 1000, "payer_user_id": "a", "shares": { "a": 500, "b": 500 } },
{ "account_id": "arch", "amount_ore": 5000, "payer_user_id": "b", "shares": { "a": 2500, "b": 2500 } }
],
"settlements": []
},
"expected": { "ok": { "per_account": { "live": { "a": 500, "b": -500 }, "arch": { "a": -2500, "b": 2500 } }, "global": { "a": -2000, "b": 2000 } } }
},
{
"name": "account ids referenced by rows but not in account_ids still count",
"input": {
"user_ids": ["a", "b"], "account_ids": ["x"],
"expenses": [ { "account_id": "z", "amount_ore": 100, "payer_user_id": "b", "shares": { "a": 100, "b": 0 } } ],
"settlements": [ { "from_user_id": "a", "to_user_id": "b", "allocations": { "w": 30 } } ]
},
"expected": { "ok": { "per_account": { "x": { "a": 0, "b": 0 }, "z": { "a": -100, "b": 100 }, "w": { "a": 30, "b": -30 } }, "global": { "a": -70, "b": 70 } } }
},
{
"name": "settlement with allocations to two accounts",
"input": {
"user_ids": ["a", "b"], "account_ids": ["x", "y"],
"expenses": [
{ "account_id": "x", "amount_ore": 10000, "payer_user_id": "a", "shares": { "a": 5000, "b": 5000 } },
{ "account_id": "y", "amount_ore": 3000, "payer_user_id": "a", "shares": { "a": 0, "b": 3000 } }
],
"settlements": [ { "from_user_id": "b", "to_user_id": "a", "allocations": { "x": 5000, "y": 1000 } } ]
},
"expected": { "ok": { "per_account": { "x": { "a": 0, "b": 0 }, "y": { "a": 2000, "b": -2000 } }, "global": { "a": 2000, "b": -2000 } } }
},
{
"name": "over-settling flips the account's sign",
"input": {
"user_ids": ["a", "b"], "account_ids": ["x"],
"expenses": [ { "account_id": "x", "amount_ore": 1000, "payer_user_id": "a", "shares": { "a": 500, "b": 500 } } ],
"settlements": [ { "from_user_id": "b", "to_user_id": "a", "allocations": { "x": 800 } } ]
},
"expected": { "ok": { "per_account": { "x": { "a": -300, "b": 300 } }, "global": { "a": -300, "b": 300 } } }
},
{
"name": "partial settlement in the other direction",
"input": {
"user_ids": ["a", "b"], "account_ids": ["x"],
"expenses": [ { "account_id": "x", "amount_ore": 1000, "payer_user_id": "b", "shares": { "a": 500, "b": 500 } } ],
"settlements": [ { "from_user_id": "a", "to_user_id": "b", "allocations": { "x": 200 } } ]
},
"expected": { "ok": { "per_account": { "x": { "a": -300, "b": 300 } }, "global": { "a": -300, "b": 300 } } }
},
{
"name": "expense fully borne by the payer leaves the balance unchanged",
"input": {
"user_ids": ["a", "b"], "account_ids": ["x"],
"expenses": [
{ "account_id": "x", "amount_ore": 1001, "payer_user_id": "a", "shares": { "a": 501, "b": 500 } },
{ "account_id": "x", "amount_ore": 7000, "payer_user_id": "a", "shares": { "a": 7000, "b": 0 } }
],
"settlements": []
},
"expected": { "ok": { "per_account": { "x": { "a": 500, "b": -500 } }, "global": { "a": 500, "b": -500 } } }
},
{
"name": "tombstoned rows excluded: callers pass only non-deleted rows (§14.5.2); the account's deleted expense and deleted settlement are absent from the input",
"input": {
"user_ids": ["a", "b"], "account_ids": ["x"],
"expenses": [ { "account_id": "x", "amount_ore": 2000, "payer_user_id": "b", "shares": { "a": 1000, "b": 1000 } } ],
"settlements": []
},
"expected": { "ok": { "per_account": { "x": { "a": -1000, "b": 1000 } }, "global": { "a": -1000, "b": 1000 } } }
},
{
"name": "maximum amounts stay exact in 64-bit integers",
"input": {
"user_ids": ["a", "b"], "account_ids": ["x"],
"expenses": [
{ "account_id": "x", "amount_ore": 1000000000, "payer_user_id": "a", "shares": { "a": 0, "b": 1000000000 } },
{ "account_id": "x", "amount_ore": 1000000000, "payer_user_id": "a", "shares": { "a": 0, "b": 1000000000 } },
{ "account_id": "x", "amount_ore": 1000000000, "payer_user_id": "a", "shares": { "a": 0, "b": 1000000000 } }
],
"settlements": []
},
"expected": { "ok": { "per_account": { "x": { "a": 3000000000, "b": -3000000000 } }, "global": { "a": 3000000000, "b": -3000000000 } } }
}
]
}
+216
View File
@@ -0,0 +1,216 @@
{
"format": 1,
"function": "compute_split",
"cases": [
{
"name": "§9.1 row 1: 1001, payer a, 5000/5000: payer gets the extra öre",
"input": { "amount_ore": 1001, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 5000, "b": 5000 } } },
"expected": { "ok": { "a": 501, "b": 500 } }
},
{
"name": "§9.1 row 2: 1, payer a, 5000/5000: the single öre goes to the payer",
"input": { "amount_ore": 1, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 5000, "b": 5000 } } },
"expected": { "ok": { "a": 1, "b": 0 } }
},
{
"name": "§9.1 row 3: 100, payer b, 4000/6000",
"input": { "amount_ore": 100, "payer_user_id": "b", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 6000, "b": 4000 } } },
"expected": { "ok": { "a": 60, "b": 40 } }
},
{
"name": "§9.1 row 4: 999, payer a, 3333/6667",
"input": { "amount_ore": 999, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 3333, "b": 6667 } } },
"expected": { "ok": { "a": 333, "b": 666 } }
},
{
"name": "§9.1 row 5: 999, payer b, 6667/3333",
"input": { "amount_ore": 999, "payer_user_id": "b", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 3333, "b": 6667 } } },
"expected": { "ok": { "a": 332, "b": 667 } }
},
{
"name": "§9.1 row 6: 50000, payer a, 0/10000",
"input": { "amount_ore": 50000, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 0, "b": 10000 } } },
"expected": { "ok": { "a": 0, "b": 50000 } }
},
{
"name": "§9.1 row 7: 50000, payer a, 10000/0",
"input": { "amount_ore": 50000, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 10000, "b": 0 } } },
"expected": { "ok": { "a": 50000, "b": 0 } }
},
{
"name": "user_ids order does not matter: payer b listed second, remainder still to payer",
"input": { "amount_ore": 1001, "payer_user_id": "b", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 5000, "b": 5000 } } },
"expected": { "ok": { "a": 500, "b": 501 } }
},
{
"name": "user_ids given in reverse order",
"input": { "amount_ore": 999, "payer_user_id": "a", "user_ids": ["b", "a"], "spec": { "mode": "ratio", "bp": { "a": 3333, "b": 6667 } } },
"expected": { "ok": { "a": 333, "b": 666 } }
},
{
"name": "non-payer exact share 0.9 öre: rounded down to 0, payer bears the whole amount",
"input": { "amount_ore": 9, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 9000, "b": 1000 } } },
"expected": { "ok": { "a": 9, "b": 0 } }
},
{
"name": "full on the payer",
"input": { "amount_ore": 1001, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "full", "bearer_user_id": "a" } },
"expected": { "ok": { "a": 1001, "b": 0 } }
},
{
"name": "full on the non-payer",
"input": { "amount_ore": 1001, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "full", "bearer_user_id": "b" } },
"expected": { "ok": { "a": 0, "b": 1001 } }
},
{
"name": "exact valid: shares sum exactly to the amount",
"input": { "amount_ore": 1001, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "exact", "ore": { "a": 300, "b": 701 } } },
"expected": { "ok": { "a": 300, "b": 701 } }
},
{
"name": "exact valid with a zero share",
"input": { "amount_ore": 41200, "payer_user_id": "b", "user_ids": ["a", "b"], "spec": { "mode": "exact", "ore": { "a": 41200, "b": 0 } } },
"expected": { "ok": { "a": 41200, "b": 0 } }
},
{
"name": "exact sum mismatch: one öre short",
"input": { "amount_ore": 1001, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "exact", "ore": { "a": 300, "b": 700 } } },
"expected": { "error": "exact_sum_mismatch" }
},
{
"name": "exact sum mismatch: one öre over",
"input": { "amount_ore": 1001, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "exact", "ore": { "a": 300, "b": 702 } } },
"expected": { "error": "exact_sum_mismatch" }
},
{
"name": "exact negative share (sum would match)",
"input": { "amount_ore": 1001, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "exact", "ore": { "a": -100, "b": 1101 } } },
"expected": { "error": "exact_negative" }
},
{
"name": "amount 0",
"input": { "amount_ore": 0, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 5000, "b": 5000 } } },
"expected": { "error": "amount_out_of_range" }
},
{
"name": "amount negative",
"input": { "amount_ore": -1, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 5000, "b": 5000 } } },
"expected": { "error": "amount_out_of_range" }
},
{
"name": "amount 1 000 000 001",
"input": { "amount_ore": 1000000001, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 5000, "b": 5000 } } },
"expected": { "error": "amount_out_of_range" }
},
{
"name": "amount 1 000 000 000 with 3333 bp: 64-bit intermediate, no overflow",
"input": { "amount_ore": 1000000000, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 6667, "b": 3333 } } },
"expected": { "ok": { "a": 666700000, "b": 333300000 } }
},
{
"name": "amount 999 999 999 with 3333 bp on the non-payer: remainder to payer at the top of the range",
"input": { "amount_ore": 999999999, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 6667, "b": 3333 } } },
"expected": { "ok": { "a": 666700000, "b": 333299999 } }
},
{
"name": "amount 1 000 000 000 exactly, full on the non-payer",
"input": { "amount_ore": 1000000000, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "full", "bearer_user_id": "b" } },
"expected": { "ok": { "a": 0, "b": 1000000000 } }
},
{
"name": "bp 10001",
"input": { "amount_ore": 1000, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 10001, "b": 0 } } },
"expected": { "error": "bp_out_of_range" }
},
{
"name": "ratio does not sum to 10000",
"input": { "amount_ore": 1000, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 5000, "b": 4000 } } },
"expected": { "error": "bp_sum_not_10000" }
},
{
"name": "ratio sums to 10001",
"input": { "amount_ore": 1000, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 5001, "b": 5000 } } },
"expected": { "error": "bp_sum_not_10000" }
},
{
"name": "three user ids",
"input": { "amount_ore": 1000, "payer_user_id": "a", "user_ids": ["a", "b", "c"], "spec": { "mode": "ratio", "bp": { "a": 5000, "b": 5000 } } },
"expected": { "error": "users_invalid" }
},
{
"name": "one user id",
"input": { "amount_ore": 1000, "payer_user_id": "a", "user_ids": ["a"], "spec": { "mode": "ratio", "bp": { "a": 10000 } } },
"expected": { "error": "users_invalid" }
},
{
"name": "two user ids that are not distinct",
"input": { "amount_ore": 1000, "payer_user_id": "a", "user_ids": ["a", "a"], "spec": { "mode": "ratio", "bp": { "a": 10000 } } },
"expected": { "error": "users_invalid" }
},
{
"name": "payer not in users",
"input": { "amount_ore": 1000, "payer_user_id": "x", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 5000, "b": 5000 } } },
"expected": { "error": "payer_unknown" }
},
{
"name": "bearer not in users",
"input": { "amount_ore": 1000, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "full", "bearer_user_id": "x" } },
"expected": { "error": "bearer_unknown" }
},
{
"name": "ratio map keys do not match users",
"input": { "amount_ore": 1000, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 5000, "x": 5000 } } },
"expected": { "error": "share_users_mismatch" }
},
{
"name": "ratio map has an extra zero entry",
"input": { "amount_ore": 1000, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 5000, "b": 5000, "x": 0 } } },
"expected": { "error": "share_users_mismatch" }
},
{
"name": "ratio map misses one user",
"input": { "amount_ore": 1000, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 10000 } } },
"expected": { "error": "share_users_mismatch" }
},
{
"name": "exact map keys do not match users",
"input": { "amount_ore": 1000, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "exact", "ore": { "a": 500, "x": 500 } } },
"expected": { "error": "share_users_mismatch" }
},
{
"name": "error order: amount checked before users",
"input": { "amount_ore": 0, "payer_user_id": "x", "user_ids": ["a", "b", "c"], "spec": { "mode": "ratio", "bp": { "a": 5000, "b": 4000 } } },
"expected": { "error": "amount_out_of_range" }
},
{
"name": "error order: users checked before payer",
"input": { "amount_ore": 1000, "payer_user_id": "x", "user_ids": ["a", "b", "c"], "spec": { "mode": "ratio", "bp": { "a": 5000, "b": 5000 } } },
"expected": { "error": "users_invalid" }
},
{
"name": "error order: payer checked before bp range",
"input": { "amount_ore": 1000, "payer_user_id": "x", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 10001, "b": 0 } } },
"expected": { "error": "payer_unknown" }
},
{
"name": "error order: bp range checked before bp sum",
"input": { "amount_ore": 1000, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 10001, "b": 5000 } } },
"expected": { "error": "bp_out_of_range" }
},
{
"name": "error order: bp sum checked before share users",
"input": { "amount_ore": 1000, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "ratio", "bp": { "a": 5000, "x": 4000 } } },
"expected": { "error": "bp_sum_not_10000" }
},
{
"name": "error order: exact negative checked before exact sum",
"input": { "amount_ore": 1000, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "exact", "ore": { "a": -1, "b": 500 } } },
"expected": { "error": "exact_negative" }
},
{
"name": "error order: exact sum checked before share users",
"input": { "amount_ore": 1000, "payer_user_id": "a", "user_ids": ["a", "b"], "spec": { "mode": "exact", "ore": { "a": 500, "x": 400 } } },
"expected": { "error": "exact_sum_mismatch" }
}
]
}
@@ -0,0 +1,61 @@
{
"format": 1,
"function": "over_settled_accounts",
"cases": [
{
"name": "none: every allocation within the debt (equal to the debt is not over)",
"input": {
"allocations": [ { "account_id": "X", "amount_ore": 30000 }, { "account_id": "Y", "amount_ore": 5000 } ],
"accounts": [
{ "account_id": "X", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": -30000 },
{ "account_id": "Y", "created_at": "2025-02-01T00:00:00.000Z", "archived": false, "payer_net_ore": -20000 }
]
},
"expected": { "ok": [] }
},
{
"name": "one: Y gets one öre more than owed",
"input": {
"allocations": [ { "account_id": "X", "amount_ore": 30000 }, { "account_id": "Y", "amount_ore": 20001 } ],
"accounts": [
{ "account_id": "X", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": -30000 },
{ "account_id": "Y", "created_at": "2025-02-01T00:00:00.000Z", "archived": false, "payer_net_ore": -20000 }
]
},
"expected": { "ok": ["Y"] }
},
{
"name": "an account where the payer is owed: any allocation is over (max(0, -net) = 0)",
"input": {
"allocations": [ { "account_id": "Z", "amount_ore": 1 } ],
"accounts": [ { "account_id": "Z", "created_at": "2024-12-01T00:00:00.000Z", "archived": false, "payer_net_ore": 10000 } ]
},
"expected": { "ok": ["Z"] }
},
{
"name": "an unknown account counts as net 0",
"input": {
"allocations": [ { "account_id": "X", "amount_ore": 100 }, { "account_id": "U", "amount_ore": 100 } ],
"accounts": [ { "account_id": "X", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": -30000 } ]
},
"expected": { "ok": ["U"] }
},
{
"name": "several: reported in allocation order, not account order",
"input": {
"allocations": [ { "account_id": "Y", "amount_ore": 25000 }, { "account_id": "X", "amount_ore": 100 }, { "account_id": "Z", "amount_ore": 1 } ],
"accounts": [
{ "account_id": "X", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": -30000 },
{ "account_id": "Y", "created_at": "2025-02-01T00:00:00.000Z", "archived": false, "payer_net_ore": -20000 },
{ "account_id": "Z", "created_at": "2024-12-01T00:00:00.000Z", "archived": true, "payer_net_ore": 0 }
]
},
"expected": { "ok": ["Y", "Z"] }
},
{
"name": "no allocations",
"input": { "allocations": [], "accounts": [ { "account_id": "X", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": -30000 } ] },
"expected": { "ok": [] }
}
]
}
@@ -0,0 +1,168 @@
{
"format": 1,
"function": "propose_allocation",
"cases": [
{
"name": "owing X (-30000, oldest owing) and Y (-20000), owed in Z (+10000): 40000 gives X 30000, Y 10000",
"input": {
"amount_ore": 40000,
"accounts": [
{ "account_id": "X", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": -30000 },
{ "account_id": "Y", "created_at": "2025-02-01T00:00:00.000Z", "archived": false, "payer_net_ore": -20000 },
{ "account_id": "Z", "created_at": "2024-12-01T00:00:00.000Z", "archived": false, "payer_net_ore": 10000 }
]
},
"expected": { "ok": { "allocations": [ { "account_id": "X", "amount_ore": 30000 }, { "account_id": "Y", "amount_ore": 10000 } ], "excess_ore": 0, "over_settled_account_ids": [] } }
},
{
"name": "same accounts, 50000 = exactly the total debt: no excess",
"input": {
"amount_ore": 50000,
"accounts": [
{ "account_id": "X", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": -30000 },
{ "account_id": "Y", "created_at": "2025-02-01T00:00:00.000Z", "archived": false, "payer_net_ore": -20000 },
{ "account_id": "Z", "created_at": "2024-12-01T00:00:00.000Z", "archived": false, "payer_net_ore": 10000 }
]
},
"expected": { "ok": { "allocations": [ { "account_id": "X", "amount_ore": 30000 }, { "account_id": "Y", "amount_ore": 20000 } ], "excess_ore": 0, "over_settled_account_ids": [] } }
},
{
"name": "same accounts, 60000: excess 10000 added to the last owing account Y, over-settled [Y]",
"input": {
"amount_ore": 60000,
"accounts": [
{ "account_id": "X", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": -30000 },
{ "account_id": "Y", "created_at": "2025-02-01T00:00:00.000Z", "archived": false, "payer_net_ore": -20000 },
{ "account_id": "Z", "created_at": "2024-12-01T00:00:00.000Z", "archived": false, "payer_net_ore": 10000 }
]
},
"expected": { "ok": { "allocations": [ { "account_id": "X", "amount_ore": 30000 }, { "account_id": "Y", "amount_ore": 30000 } ], "excess_ore": 10000, "over_settled_account_ids": ["Y"] } }
},
{
"name": "same accounts, 20000 is less than the first debt: only X",
"input": {
"amount_ore": 20000,
"accounts": [
{ "account_id": "X", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": -30000 },
{ "account_id": "Y", "created_at": "2025-02-01T00:00:00.000Z", "archived": false, "payer_net_ore": -20000 },
{ "account_id": "Z", "created_at": "2024-12-01T00:00:00.000Z", "archived": false, "payer_net_ore": 10000 }
]
},
"expected": { "ok": { "allocations": [ { "account_id": "X", "amount_ore": 20000 } ], "excess_ore": 0, "over_settled_account_ids": [] } }
},
{
"name": "input order does not matter: accounts listed newest first",
"input": {
"amount_ore": 40000,
"accounts": [
{ "account_id": "Y", "created_at": "2025-02-01T00:00:00.000Z", "archived": false, "payer_net_ore": -20000 },
{ "account_id": "Z", "created_at": "2024-12-01T00:00:00.000Z", "archived": false, "payer_net_ore": 10000 },
{ "account_id": "X", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": -30000 }
]
},
"expected": { "ok": { "allocations": [ { "account_id": "X", "amount_ore": 30000 }, { "account_id": "Y", "amount_ore": 10000 } ], "excess_ore": 0, "over_settled_account_ids": [] } }
},
{
"name": "no owing accounts: 5000 with only Z (owed) goes to Z, over-settled [Z]",
"input": {
"amount_ore": 5000,
"accounts": [ { "account_id": "Z", "created_at": "2024-12-01T00:00:00.000Z", "archived": false, "payer_net_ore": 10000 } ]
},
"expected": { "ok": { "allocations": [ { "account_id": "Z", "amount_ore": 5000 } ], "excess_ore": 5000, "over_settled_account_ids": ["Z"] } }
},
{
"name": "no owing accounts: a net of exactly 0 is not owing; target is the oldest non-archived account",
"input": {
"amount_ore": 700,
"accounts": [
{ "account_id": "A", "created_at": "2024-01-01T00:00:00.000Z", "archived": true, "payer_net_ore": 0 },
{ "account_id": "B", "created_at": "2024-06-01T00:00:00.000Z", "archived": false, "payer_net_ore": 500 },
{ "account_id": "C", "created_at": "2024-03-01T00:00:00.000Z", "archived": false, "payer_net_ore": 0 }
]
},
"expected": { "ok": { "allocations": [ { "account_id": "C", "amount_ore": 700 } ], "excess_ore": 700, "over_settled_account_ids": ["C"] } }
},
{
"name": "all accounts archived and none owing: the oldest account",
"input": {
"amount_ore": 1200,
"accounts": [
{ "account_id": "P", "created_at": "2025-05-01T00:00:00.000Z", "archived": true, "payer_net_ore": 0 },
{ "account_id": "Q", "created_at": "2025-03-01T00:00:00.000Z", "archived": true, "payer_net_ore": 300 },
{ "account_id": "R", "created_at": "2025-04-01T00:00:00.000Z", "archived": true, "payer_net_ore": 0 }
]
},
"expected": { "ok": { "allocations": [ { "account_id": "Q", "amount_ore": 1200 } ], "excess_ore": 1200, "over_settled_account_ids": ["Q"] } }
},
{
"name": "archived owing accounts are still settled (archived never affects money)",
"input": {
"amount_ore": 1500,
"accounts": [
{ "account_id": "old", "created_at": "2024-01-01T00:00:00.000Z", "archived": true, "payer_net_ore": -1000 },
{ "account_id": "new", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": -1000 }
]
},
"expected": { "ok": { "allocations": [ { "account_id": "old", "amount_ore": 1000 }, { "account_id": "new", "amount_ore": 500 } ], "excess_ore": 0, "over_settled_account_ids": [] } }
},
{
"name": "equal created_at among owing accounts: ordered by id ascending",
"input": {
"amount_ore": 1500,
"accounts": [
{ "account_id": "q", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": -1000 },
{ "account_id": "p", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": -1000 }
]
},
"expected": { "ok": { "allocations": [ { "account_id": "p", "amount_ore": 1000 }, { "account_id": "q", "amount_ore": 500 } ], "excess_ore": 0, "over_settled_account_ids": [] } }
},
{
"name": "equal created_at with no owing account: target chosen by id ascending",
"input": {
"amount_ore": 100,
"accounts": [
{ "account_id": "n", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": 0 },
{ "account_id": "m", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": 50 }
]
},
"expected": { "ok": { "allocations": [ { "account_id": "m", "amount_ore": 100 } ], "excess_ore": 100, "over_settled_account_ids": ["m"] } }
},
{
"name": "single owing account, amount above its debt: excess to it",
"input": {
"amount_ore": 1000,
"accounts": [
{ "account_id": "Z", "created_at": "2024-12-01T00:00:00.000Z", "archived": false, "payer_net_ore": 10000 },
{ "account_id": "X", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": -1 }
]
},
"expected": { "ok": { "allocations": [ { "account_id": "X", "amount_ore": 1000 } ], "excess_ore": 999, "over_settled_account_ids": ["X"] } }
},
{
"name": "no accounts",
"input": { "amount_ore": 1000, "accounts": [] },
"expected": { "error": "no_accounts" }
},
{
"name": "amount 0",
"input": {
"amount_ore": 0,
"accounts": [ { "account_id": "X", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": -30000 } ]
},
"expected": { "error": "amount_out_of_range" }
},
{
"name": "amount 1 000 000 001",
"input": {
"amount_ore": 1000000001,
"accounts": [ { "account_id": "X", "created_at": "2025-01-01T00:00:00.000Z", "archived": false, "payer_net_ore": -30000 } ]
},
"expected": { "error": "amount_out_of_range" }
},
{
"name": "error order: amount checked before no accounts",
"input": { "amount_ore": 0, "accounts": [] },
"expected": { "error": "amount_out_of_range" }
}
]
}
+214
View File
@@ -0,0 +1,214 @@
{
"format": 1,
"function": "resolve_range",
"cases": [
{
"name": "this_month",
"input": {
"preset": "this_month",
"today": "2026-10-17",
"earliest_expense": "2024-03-05",
"custom_start": null,
"custom_end": null
},
"expected": {
"ok": {
"start": "2026-10-01",
"end_inclusive": "2026-10-17"
}
}
},
{
"name": "last_3_months: the current month and the two before",
"input": {
"preset": "last_3_months",
"today": "2026-10-17",
"earliest_expense": "2024-03-05",
"custom_start": null,
"custom_end": null
},
"expected": {
"ok": {
"start": "2026-08-01",
"end_inclusive": "2026-10-17"
}
}
},
{
"name": "last_6_months",
"input": {
"preset": "last_6_months",
"today": "2026-10-17",
"earliest_expense": "2024-03-05",
"custom_start": null,
"custom_end": null
},
"expected": {
"ok": {
"start": "2026-05-01",
"end_inclusive": "2026-10-17"
}
}
},
{
"name": "last_12_months: crosses into the previous year",
"input": {
"preset": "last_12_months",
"today": "2026-10-17",
"earliest_expense": "2024-03-05",
"custom_start": null,
"custom_end": null
},
"expected": {
"ok": {
"start": "2025-11-01",
"end_inclusive": "2026-10-17"
}
}
},
{
"name": "this_year",
"input": {
"preset": "this_year",
"today": "2026-10-17",
"earliest_expense": "2024-03-05",
"custom_start": null,
"custom_end": null
},
"expected": {
"ok": {
"start": "2026-01-01",
"end_inclusive": "2026-10-17"
}
}
},
{
"name": "all_time with an earliest expense",
"input": {
"preset": "all_time",
"today": "2026-10-17",
"earliest_expense": "2024-03-05",
"custom_start": null,
"custom_end": null
},
"expected": {
"ok": {
"start": "2024-03-05",
"end_inclusive": "2026-10-17"
}
}
},
{
"name": "all_time without expenses: today to today",
"input": {
"preset": "all_time",
"today": "2026-10-17",
"earliest_expense": null,
"custom_start": null,
"custom_end": null
},
"expected": {
"ok": {
"start": "2026-10-17",
"end_inclusive": "2026-10-17"
}
}
},
{
"name": "custom in order, future dates allowed",
"input": {
"preset": "custom",
"today": "2026-10-17",
"earliest_expense": null,
"custom_start": "2026-11-01",
"custom_end": "2026-12-31"
},
"expected": {
"ok": {
"start": "2026-11-01",
"end_inclusive": "2026-12-31"
}
}
},
{
"name": "custom swapped when start is after end",
"input": {
"preset": "custom",
"today": "2026-10-17",
"earliest_expense": null,
"custom_start": "2026-12-31",
"custom_end": "2026-11-01"
},
"expected": {
"ok": {
"start": "2026-11-01",
"end_inclusive": "2026-12-31"
}
}
},
{
"name": "custom single day",
"input": {
"preset": "custom",
"today": "2026-10-17",
"earliest_expense": null,
"custom_start": "2026-02-28",
"custom_end": "2026-02-28"
},
"expected": {
"ok": {
"start": "2026-02-28",
"end_inclusive": "2026-02-28"
}
}
},
{
"name": "last_3_months in January crosses the year",
"input": {
"preset": "last_3_months",
"today": "2027-01-01",
"earliest_expense": null,
"custom_start": null,
"custom_end": null
},
"expected": {
"ok": {
"start": "2026-11-01",
"end_inclusive": "2027-01-01"
}
}
},
{
"name": "last_12_months on 31 March: first of April the year before",
"input": {
"preset": "last_12_months",
"today": "2027-03-31",
"earliest_expense": null,
"custom_start": null,
"custom_end": null
},
"expected": {
"ok": {
"start": "2026-04-01",
"end_inclusive": "2027-03-31"
}
}
},
{
"name": "this_month on the first day of the month",
"input": {
"preset": "this_month",
"today": "2026-11-01",
"earliest_expense": null,
"custom_start": null,
"custom_end": null
},
"expected": {
"ok": {
"start": "2026-11-01",
"end_inclusive": "2026-11-01"
}
}
}
]
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff