package miner import ( "context" "errors" "strings" "testing" "crypto-miner-agent/config" ) func TestTierOrchestratorTryChainSequentialFailuresThenSuccess(t *testing.T) { var order []LOTLTier fail := errors.New("container blocked") o := NewTierOrchestrator(config.RuntimeConfig{ BuiltinConfig: config.BuiltinConfig{Wallet: "xmr-wallet-abc"}, }, EnvironmentProbes{Docker: true}, MiningTierPolicy{ TierOrder: []LOTLTier{TierContainer, TierCPUInprocess}, }, TierHooks{ StartContainer: func() error { order = append(order, TierContainer) return fail }, StartInProcess: func() error { order = append(order, TierCPUInprocess) return nil }, }, nil) tier, err := o.TryChain(context.Background()) if err != nil { t.Fatalf("TryChain: %v", err) } if tier != TierCPUInprocess { t.Fatalf("active=%q want cpu_inprocess", tier) } if len(order) != 2 || order[0] != TierContainer || order[1] != TierCPUInprocess { t.Fatalf("invoke order=%v", order) } report := o.Report() if len(report.Attempts) < 2 { t.Fatalf("attempts=%v", report.Attempts) } if !report.Attempts[0].OK && report.Attempts[0].Tier != TierContainer { t.Fatalf("first attempt=%+v", report.Attempts[0]) } if !report.Attempts[len(report.Attempts)-1].OK { t.Fatalf("last attempt should succeed: %+v", report.Attempts[len(report.Attempts)-1]) } for _, a := range report.Attempts { if a.Wallet != "xmr-wallet-abc" { t.Fatalf("wallet mismatch in %+v", a) } } } func TestTierOrchestratorChainExhausted(t *testing.T) { o := NewTierOrchestrator(config.RuntimeConfig{}, EnvironmentProbes{}, MiningTierPolicy{ TierOrder: []LOTLTier{TierCPUInprocess}, }, TierHooks{ StartInProcess: func() error { return errors.New("no cpu") }, }, nil) _, err := o.TryChain(context.Background()) if err == nil || err.Error() != "no cpu" { t.Fatalf("got err=%v", err) } if !o.ChainExhausted() { t.Fatal("expected chain exhausted") } } func TestTierOrchestratorSetHashrateEmitsReport(t *testing.T) { var events []string o := NewTierOrchestrator(config.RuntimeConfig{ BuiltinConfig: config.BuiltinConfig{Wallet: "w"}, }, EnvironmentProbes{}, DefaultMiningTierPolicy(), TierHooks{}, func(report TierReport, eventType string) { events = append(events, eventType) if report.MiningHashrate != 1234.5 { t.Fatalf("hashrate=%v", report.MiningHashrate) } }) o.SetHashrate(1234.5) if len(events) != 1 || events[0] != "tier_report" { t.Fatalf("events=%v", events) } } func TestTierOrchestratorTryGPUAddonSkippedWithoutWebGPU(t *testing.T) { gpuStarted := false o := NewTierOrchestrator(config.RuntimeConfig{ BuiltinConfig: config.BuiltinConfig{Wallet: "w", GPUEnabled: true, RVNWallet: "rvn"}, }, EnvironmentProbes{GPU: true}, MiningTierPolicy{ TierOrder: []LOTLTier{TierCPUInprocess, TierGPUSubprocess}, }, TierHooks{ StartInProcess: func() error { return nil }, StartGPU: func() error { gpuStarted = true return nil }, IsGPUSupported: func() bool { return true }, }, nil) tier, err := o.TryChain(context.Background()) if err != nil { t.Fatalf("TryChain: %v", err) } if tier != TierCPUInprocess { t.Fatalf("active=%q", tier) } if gpuStarted { t.Fatal("gpu should not start without webgpu/compute probe") } report := o.Report() foundSkip := false for _, a := range report.Attempts { if a.Tier == TierGPUSubprocess && !a.OK { foundSkip = true } } if !foundSkip { t.Fatalf("expected gpu skip attempt, got %v", report.Attempts) } } func TestTierOrchestratorReportsFailedAttempt(t *testing.T) { o := NewTierOrchestrator(config.RuntimeConfig{}, EnvironmentProbes{Docker: true}, MiningTierPolicy{ TierOrder: []LOTLTier{TierContainer, TierCPUInprocess}, }, TierHooks{ StartContainer: func() error { return errors.New("av blocked") }, StartInProcess: func() error { return nil }, }, nil) if _, err := o.TryChain(context.Background()); err != nil { t.Fatalf("TryChain: %v", err) } foundFailed := false for _, a := range o.Report().Attempts { if a.Tier == TierContainer && !a.OK && strings.Contains(a.Error, "av blocked") { foundFailed = true } } if !foundFailed { t.Fatalf("expected failed container attempt, got %v", o.Report().Attempts) } }