package db import ( "encoding/json" "testing" ) func TestOathLedgerRoundTrip(t *testing.T) { d, err := New(t.TempDir()) if err != nil { t.Fatal(err) } defer d.Close() why := map[string]string{"tier": "dns_txt", "lane": "spread"} hash := HashWhyJSON(why) entry, err := d.InsertOathLedger( "operator", OathSpreadTierEscalation, "agent-1", "#aabbcc", hash, OathOutcomeSuccess, map[string]string{"command_type": "reorder_tiers"}, ) if err != nil { t.Fatal(err) } if entry == nil || entry.ID == 0 { t.Fatalf("expected inserted entry, got %+v", entry) } if entry.WhyHash != hash { t.Fatalf("why_hash = %q want %q", entry.WhyHash, hash) } rows, err := d.ListOathLedger(10) if err != nil { t.Fatal(err) } if len(rows) != 1 { t.Fatalf("rows = %+v", rows) } if rows[0].ActionType != OathSpreadTierEscalation || rows[0].Actor != "operator" { t.Fatalf("unexpected row: %+v", rows[0]) } } func TestHashWhyJSONStable(t *testing.T) { src := map[string]interface{}{"verdict": "retry spread", "clearance": 4} h1 := HashWhyJSON(src) h2 := HashWhyJSON(src) if h1 == "" || h1 != h2 { t.Fatalf("hash unstable: %q %q", h1, h2) } if len(h1) != 64 { t.Fatalf("expected sha256 hex length 64, got %d", len(h1)) } _, _ = json.Marshal(src) }