From 749dd76f9cfb8a68e221bfd43650441e73bb5475 Mon Sep 17 00:00:00 2001 From: snider Date: Wed, 31 Dec 2025 10:17:14 +0000 Subject: [PATCH] fix: Update tests to handle autostart behavior MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - TestStartMiner_Ugly: Add algorithm to config for consistent instance naming, ensuring duplicate detection works correctly - TestListMiners_Good: Account for autostarted miners by checking delta instead of absolute count - TestListMiners: Renamed from TestListMinersEmpty since autostart may add miners - Add defer manager.Stop() to all tests in mining_test.go for proper cleanup 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- pkg/mining/manager_test.go | 21 +++++++++++---------- pkg/mining/mining_test.go | 16 +++++++++++++--- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/pkg/mining/manager_test.go b/pkg/mining/manager_test.go index 390d900..84bde46 100644 --- a/pkg/mining/manager_test.go +++ b/pkg/mining/manager_test.go @@ -86,17 +86,20 @@ func TestStartMiner_Ugly(t *testing.T) { m := setupTestManager(t) defer m.Stop() + // Use an algorithm to get consistent instance naming (xmrig-test_algo) + // Without algo, each start gets a random suffix and won't be detected as duplicate config := &Config{ HTTPPort: 9001, // Use a different port to avoid conflict Pool: "test:1234", Wallet: "testwallet", + Algo: "test_algo", // Consistent algo = consistent instance name } // Case 1: Successfully start a supported miner _, err := m.StartMiner(context.Background(), "xmrig", config) if err != nil { t.Fatalf("Expected to start miner, but got error: %v", err) } - // Case 3: Attempt to start a duplicate miner + // Case 3: Attempt to start a duplicate miner (same algo = same instance name) _, err = m.StartMiner(context.Background(), "xmrig", config) if err == nil { t.Error("Expected an error when starting a duplicate miner, but got nil") @@ -174,22 +177,20 @@ func TestListMiners_Good(t *testing.T) { m := setupTestManager(t) defer m.Stop() - // Case 1: List miners when empty - miners := m.ListMiners() - if len(miners) != 0 { - t.Errorf("Expected 0 miners, but got %d", len(miners)) - } + // Get initial count (may include autostarted miners from config) + initialMiners := m.ListMiners() + initialCount := len(initialMiners) - // Case 2: List miners when not empty + // Case 2: List miners after starting one - should have one more config := &Config{ HTTPPort: 9004, Pool: "test:1234", Wallet: "testwallet", } _, _ = m.StartMiner(context.Background(), "xmrig", config) - miners = m.ListMiners() - if len(miners) != 1 { - t.Errorf("Expected 1 miner, but got %d", len(miners)) + miners := m.ListMiners() + if len(miners) != initialCount+1 { + t.Errorf("Expected %d miners (initial %d + 1), but got %d", initialCount+1, initialCount, len(miners)) } } diff --git a/pkg/mining/mining_test.go b/pkg/mining/mining_test.go index bc9ea32..4a7554c 100644 --- a/pkg/mining/mining_test.go +++ b/pkg/mining/mining_test.go @@ -7,6 +7,8 @@ import ( func TestNewManager(t *testing.T) { manager := NewManager() + defer manager.Stop() + if manager == nil { t.Fatal("NewManager returned nil") } @@ -17,6 +19,7 @@ func TestNewManager(t *testing.T) { func TestStartAndStopMiner(t *testing.T) { manager := NewManager() + defer manager.Stop() config := &Config{ Pool: "pool.example.com", @@ -38,6 +41,7 @@ func TestStartAndStopMiner(t *testing.T) { func TestGetNonExistentMiner(t *testing.T) { manager := NewManager() + defer manager.Stop() _, err := manager.GetMiner("non-existent") if err == nil { @@ -45,16 +49,22 @@ func TestGetNonExistentMiner(t *testing.T) { } } -func TestListMinersEmpty(t *testing.T) { +func TestListMiners(t *testing.T) { manager := NewManager() + defer manager.Stop() + + // ListMiners should return a valid slice (may include autostarted miners) miners := manager.ListMiners() - if len(miners) != 0 { - t.Errorf("Expected 0 miners, got %d", len(miners)) + if miners == nil { + t.Error("ListMiners returned nil") } + // Note: count may be > 0 if autostart is configured } func TestListAvailableMiners(t *testing.T) { manager := NewManager() + defer manager.Stop() + miners := manager.ListAvailableMiners() if len(miners) == 0 { t.Error("Expected at least one available miner")