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

102 lines
2.4 KiB
Go

package activity
import (
"testing"
"time"
)
func TestBus_PublishDeliversToMatchingSubscriber(t *testing.T) {
b := NewBus()
ch, unsubscribe := b.Subscribe("tenant_1")
defer unsubscribe()
b.Publish(Event{ID: "e1", TenantID: "tenant_1", Kind: "card", Summary: "issued"})
b.Publish(Event{ID: "e2", TenantID: "tenant_2", Kind: "card", Summary: "other tenant"})
select {
case got := <-ch:
if got.ID != "e1" {
t.Errorf("ID = %q, want e1", got.ID)
}
case <-time.After(time.Second):
t.Fatal("timed out waiting for matching event")
}
select {
case got := <-ch:
t.Fatalf("received unexpected second event %+v (should have been filtered out)", got)
case <-time.After(50 * time.Millisecond):
// expected: no more events for this tenant filter.
}
}
func TestBus_WildcardSubscriberReceivesEverything(t *testing.T) {
b := NewBus()
ch, unsubscribe := b.Subscribe("")
defer unsubscribe()
b.Publish(Event{ID: "e1", TenantID: "tenant_1"})
b.Publish(Event{ID: "e2", TenantID: "tenant_2"})
got := map[string]bool{}
for i := 0; i < 2; i++ {
select {
case e := <-ch:
got[e.ID] = true
case <-time.After(time.Second):
t.Fatal("timed out waiting for events")
}
}
if !got["e1"] || !got["e2"] {
t.Errorf("got = %v, want both e1 and e2", got)
}
}
func TestBus_UnsubscribeStopsDelivery(t *testing.T) {
b := NewBus()
ch, unsubscribe := b.Subscribe("")
unsubscribe()
b.Publish(Event{ID: "e1", TenantID: "tenant_1"})
if _, open := <-ch; open {
t.Error("channel should be closed after unsubscribe")
}
if b.SubscriberCount() != 0 {
t.Errorf("SubscriberCount() = %d, want 0", b.SubscriberCount())
}
}
func TestBus_SlowSubscriberDoesNotBlockPublisher(t *testing.T) {
b := NewBus()
_, unsubscribe := b.Subscribe("") // never drained
defer unsubscribe()
done := make(chan struct{})
go func() {
for i := 0; i < subscriberBufferSize*2; i++ {
b.Publish(Event{ID: "e", TenantID: "t"})
}
close(done)
}()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("Publish blocked on a slow/undrained subscriber")
}
}
func TestNewEvent(t *testing.T) {
e := NewEvent("tenant_1", "card", "issued card")
if e.ID == "" {
t.Error("NewEvent() ID is empty")
}
if e.TenantID != "tenant_1" || e.Kind != "card" || e.Summary != "issued card" {
t.Errorf("NewEvent() = %+v, unexpected fields", e)
}
if e.AtUnix == 0 {
t.Error("NewEvent() AtUnix is zero")
}
}