caelum0x 2b686594a3 feat: Ration phase 1 — full polyglot build for Airwallex Starter Kit 5
Six services, independently built then integrated:
- services/wal (C): crash-safe write-ahead log, CRC32-checksummed
  frames, torn-write recovery via truncate-to-last-valid-frame.
- services/ledger-core (Rust): double-entry ledger over the C WAL via
  FFI, reserve-floor enforcement BEFORE any WAL write, idempotent
  ApplyEntry, full LedgerCore gRPC service.
- services/gateway (Go): Airwallex Issuing/Global-Accounts client
  (mock mode default, live mode ready), the actual rationing math
  (reserve floor, 60% bridge cap, 24h velocity cap, partial-over-decline).
- services/agent (Go): deterministic hard-rule policy envelope computed
  BEFORE any LLM call; Claude only decides within that pre-approved
  envelope; every decision audit-logged even when the LLM call fails.
- services/tui (Go, Bubble Tea) + services/cli (Go, cobra): the only
  UI — no web frontend, per explicit direction.

Integration found and fixed three real bugs, not just compile errors:
- proto regen collision shadowing checked-in pkg/pb packages
- ledger-core/gateway address-variable mismatch (stack would never
  actually connect on default config)
- the big one: MarkDepositReceived never credited the ledger, so every
  tenant's balance was permanently stuck at 0 and bridge-funding could
  only ever decline — the core agentic-rationing feature could never
  produce a real grant. Fixed at the ledger level with idempotent
  mark-and-credit in one WAL frame, covered by 3 new tests.

End-to-end verified live, reproducibly: seed -> simulate a late deposit
-> agent grants a partial bridge with a computed rationale -> second
grant correctly declines once the 24h velocity cap engages. Verified
independently by the orchestrator too: 'make build' and 'make test'
both clean (Rust: 29 tests; Go: all packages; C: WAL test suite).

Airwallex sandbox credentials still pending organizer approval —
tracked as an owner blocker in docs/AIRWALLEX-SETUP.md, not a code gap.
2026-10-19 03:04:13 +03:00

121 lines
3.0 KiB
Go

package format
import (
"testing"
commonpb "github.com/caelum0x/ration/pkg/pb/common"
)
func TestMoney(t *testing.T) {
cases := []struct {
name string
minorUnits int64
currency string
want string
}{
{"usd simple", 1234, "USD", "$12.34"},
{"usd grouped thousands", 123456789, "USD", "$1,234,567.89"},
{"zero", 0, "USD", "$0.00"},
{"negative", -500, "USD", "-$5.00"},
{"eur symbol", 100, "EUR", "€1.00"},
{"unknown currency falls back to code", 100, "XYZ", "XYZ 1.00"},
{"lowercase currency normalizes", 100, "usd", "$1.00"},
{"single digit minor", 1005, "USD", "$10.05"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := Money(tc.minorUnits, tc.currency)
if got != tc.want {
t.Errorf("Money(%d, %q) = %q, want %q", tc.minorUnits, tc.currency, got, tc.want)
}
})
}
}
func TestGroupThousands(t *testing.T) {
cases := []struct {
n int64
want string
}{
{0, "0"},
{5, "5"},
{999, "999"},
{1000, "1,000"},
{12345, "12,345"},
{1234567, "1,234,567"},
}
for _, tc := range cases {
got := groupThousands(tc.n)
if got != tc.want {
t.Errorf("groupThousands(%d) = %q, want %q", tc.n, got, tc.want)
}
}
}
func TestUnix(t *testing.T) {
if got := Unix(0); got != "-" {
t.Errorf("Unix(0) = %q, want %q", got, "-")
}
// 2021-01-01 00:00:00 UTC
if got, want := Unix(1609459200), "2021-01-01 00:00:00"; got != want {
t.Errorf("Unix(1609459200) = %q, want %q", got, want)
}
}
func TestCardStatusLabel(t *testing.T) {
cases := []struct {
s commonpb.CardStatus
want string
}{
{commonpb.CardStatus_CARD_STATUS_ACTIVE, "ACTIVE"},
{commonpb.CardStatus_CARD_STATUS_RATIONED, "RATIONED"},
{commonpb.CardStatus_CARD_STATUS_SUSPENDED, "SUSPENDED"},
{commonpb.CardStatus_CARD_STATUS_CLOSED, "CLOSED"},
{commonpb.CardStatus_CARD_STATUS_UNSPECIFIED, "UNKNOWN"},
}
for _, tc := range cases {
if got := CardStatusLabel(tc.s); got != tc.want {
t.Errorf("CardStatusLabel(%v) = %q, want %q", tc.s, got, tc.want)
}
}
}
func TestDepositStatusLabel(t *testing.T) {
cases := []struct {
s commonpb.DepositStatus
want string
}{
{commonpb.DepositStatus_DEPOSIT_STATUS_EXPECTED, "EXPECTED"},
{commonpb.DepositStatus_DEPOSIT_STATUS_RECEIVED, "RECEIVED"},
{commonpb.DepositStatus_DEPOSIT_STATUS_LATE, "LATE"},
{commonpb.DepositStatus_DEPOSIT_STATUS_UNSPECIFIED, "UNKNOWN"},
}
for _, tc := range cases {
if got := DepositStatusLabel(tc.s); got != tc.want {
t.Errorf("DepositStatusLabel(%v) = %q, want %q", tc.s, got, tc.want)
}
}
}
func TestTruncate(t *testing.T) {
cases := []struct {
s string
width int
want string
}{
{"hello", 10, "hello"},
{"hello", 5, "hello"},
{"hello world", 8, "hello w…"},
{"hello", 1, "h"},
{"hello", 0, ""},
{"hello", -1, ""},
{"héllo world", 8, "héllo w…"},
}
for _, tc := range cases {
got := Truncate(tc.s, tc.width)
if got != tc.want {
t.Errorf("Truncate(%q, %d) = %q, want %q", tc.s, tc.width, got, tc.want)
}
}
}