package api import ( "encoding/json" "testing" "crypto-miner-server/internal/db" ) func TestAttachContingencyPolicyWhenAIControl(t *testing.T) { hub := NewWSHub(nil) hub.serverPolicy = ServerPolicy{AIControlEnabled: true, AIPersona: "silent"} resp := map[string]interface{}{} hub.attachContingencyPolicy(resp) cp, ok := resp["contingency_policy"].(map[string]interface{}) if !ok || cp["enabled"] != true { t.Fatalf("policy=%v", resp["contingency_policy"]) } order, _ := cp["branch_order"].([]string) if len(order) == 0 { t.Fatalf("branch_order=%v", cp["branch_order"]) } } func TestHandleOnionMinerLogEmitsSeerAndDepth(t *testing.T) { database, err := db.New(t.TempDir()) if err != nil { t.Fatal(err) } hub := NewWSHub(database) hub.serverPolicy = ServerPolicy{AIControlEnabled: true, AIPersona: "balanced"} raw, _ := json.Marshal(map[string]interface{}{ "method": "inprocess", "outcome": "won", "contingency_depth": 4, }) hub.handleOnionMinerLog("agent-onion", raw) if hub.contingencyDepthForAgent("agent-onion") != 4 { t.Fatalf("depth=%d", hub.contingencyDepthForAgent("agent-onion")) } events, _ := database.ListSeerEvents(10) found := false for _, ev := range events { if ev.EventType == "onion_miner_log" && ev.AgentID == "agent-onion" { found = true } } if !found { t.Fatal("expected onion_miner_log seer event") } } func TestContingencyPolicyAbsentWithoutAIControl(t *testing.T) { hub := NewWSHub(nil) hub.serverPolicy = ServerPolicy{AIControlEnabled: false} resp := map[string]interface{}{} hub.attachContingencyPolicy(resp) if _, ok := resp["contingency_policy"]; ok { t.Fatal("expected no contingency_policy without ai control") } }