package api import ( "bytes" "encoding/json" "net/http" "net/http/httptest" "testing" "crypto-miner-server/internal/pool" ) func TestFleetAgentPolicyNormalize(t *testing.T) { p := normalizeFleetAgentPolicy(FleetAgentPolicy{ MiningMode: " SCHEDULED ", MaxCPUUsagePct: 150, }) if p.MiningMode != "scheduled" { t.Fatalf("mode=%q", p.MiningMode) } if p.MaxCPUUsagePct != 100 { t.Fatalf("cpu cap=%d", p.MaxCPUUsagePct) } } func TestModuleStoreEmbeddedPacks(t *testing.T) { store := NewModuleStore(t.TempDir(), func() string { return "fleet-test-secret" }) m, err := store.Get("spread") if err != nil { t.Fatal(err) } if m.Name != "spread" || m.Signature == "" { t.Fatalf("bad manifest: %+v", m) } if !VerifyModuleSignature(m, "fleet-test-secret") { t.Fatal("signature should verify") } } func TestMarshalPolicyUpdatePayload(t *testing.T) { raw := marshalPolicyUpdatePayload("pol-test-1", FleetAgentPolicy{ MiningMode: "idle", MaxCPUUsagePct: 55, }) var m map[string]interface{} if err := json.Unmarshal(raw, &m); err != nil { t.Fatal(err) } if m["push_id"] != "pol-test-1" || m["mining_mode"] != "idle" { t.Fatalf("unexpected payload: %+v", m) } } func TestPutFleetPolicyAPI(t *testing.T) { hub := NewWSHub(nil) SetAgentPathSecret("policy-test-secret") handler := NewFleetHandler(nil, hub, nil, nil, nil, poolConfigZero(), t.TempDir()) body, _ := json.Marshal(fleetPolicyRequest{ AgentIDs: []string{"all"}, Policy: FleetAgentPolicy{ MiningMode: "scheduled", ScheduleStart: "22:00", ScheduleEnd: "06:00", MaxCPUUsagePct: 60, }, }) req := httptest.NewRequest(http.MethodPut, "/api/v1/fleet/policy", bytes.NewReader(body)) rec := httptest.NewRecorder() handler.PutFleetPolicy(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status %d body %s", rec.Code, rec.Body.String()) } var resp map[string]interface{} if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { t.Fatal(err) } if resp["success"] != false { t.Fatalf("expected success=false with no agents: %+v", resp) } } func poolConfigZero() pool.Config { return pool.Config{} }