feat: Increase test coverage for pkg/mining
This commit introduces a number of new tests for the `pkg/mining` package,
increasing the overall test coverage from 8.2% to 40.7%.
The following changes were made:
- Added tests for the `XMRigMiner` struct, including its methods for
installation, starting, stopping, and getting stats.
- Added tests for the `Service` layer, including the API endpoints for
listing, starting, stopping, and getting stats for miners.
- Added tests for the `Manager`, including starting and stopping multiple
miners, collecting stats, and getting hashrate history.
- Introduced a `ManagerInterface` to decouple the `Service` layer from the
concrete `Manager` implementation, facilitating testing with mocks.
2025-11-10 00:06:35 +00:00
|
|
|
package mining
|
|
|
|
|
|
|
|
|
|
import (
|
feat: Add context propagation, state sync, and tests
- Add context.Context to ManagerInterface methods (StartMiner, StopMiner, UninstallMiner)
- Add WebSocket state sync on client connect (sends current miner states)
- Add EventStateSync event type and SetStateProvider method
- Add manager lifecycle tests (idempotent stop, context cancellation, shutdown timeout)
- Add database tests (initialization, hashrate storage, stats)
- Add EventHub tests (creation, broadcast, client count, state provider)
- Update all test files for new context-aware API
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-31 10:10:39 +00:00
|
|
|
"context"
|
2025-11-13 19:17:07 +00:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"runtime"
|
feat: Increase test coverage for pkg/mining
This commit introduces a number of new tests for the `pkg/mining` package,
increasing the overall test coverage from 8.2% to 40.7%.
The following changes were made:
- Added tests for the `XMRigMiner` struct, including its methods for
installation, starting, stopping, and getting stats.
- Added tests for the `Service` layer, including the API endpoints for
listing, starting, stopping, and getting stats for miners.
- Added tests for the `Manager`, including starting and stopping multiple
miners, collecting stats, and getting hashrate history.
- Introduced a `ManagerInterface` to decouple the `Service` layer from the
concrete `Manager` implementation, facilitating testing with mocks.
2025-11-10 00:06:35 +00:00
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-13 19:17:07 +00:00
|
|
|
// setupTestManager creates a new Manager and a dummy executable for tests.
|
|
|
|
|
// It also temporarily modifies the PATH to include the dummy executable's directory.
|
|
|
|
|
func setupTestManager(t *testing.T) *Manager {
|
|
|
|
|
dummyDir := t.TempDir()
|
2025-12-31 16:38:48 +00:00
|
|
|
executableName := "miner"
|
2025-11-13 19:17:07 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
executableName += ".exe"
|
feat: Increase test coverage for pkg/mining
This commit introduces a number of new tests for the `pkg/mining` package,
increasing the overall test coverage from 8.2% to 40.7%.
The following changes were made:
- Added tests for the `XMRigMiner` struct, including its methods for
installation, starting, stopping, and getting stats.
- Added tests for the `Service` layer, including the API endpoints for
listing, starting, stopping, and getting stats for miners.
- Added tests for the `Manager`, including starting and stopping multiple
miners, collecting stats, and getting hashrate history.
- Introduced a `ManagerInterface` to decouple the `Service` layer from the
concrete `Manager` implementation, facilitating testing with mocks.
2025-11-10 00:06:35 +00:00
|
|
|
}
|
2025-11-13 19:17:07 +00:00
|
|
|
dummyPath := filepath.Join(dummyDir, executableName)
|
feat: Increase test coverage for pkg/mining
This commit introduces a number of new tests for the `pkg/mining` package,
increasing the overall test coverage from 8.2% to 40.7%.
The following changes were made:
- Added tests for the `XMRigMiner` struct, including its methods for
installation, starting, stopping, and getting stats.
- Added tests for the `Service` layer, including the API endpoints for
listing, starting, stopping, and getting stats for miners.
- Added tests for the `Manager`, including starting and stopping multiple
miners, collecting stats, and getting hashrate history.
- Introduced a `ManagerInterface` to decouple the `Service` layer from the
concrete `Manager` implementation, facilitating testing with mocks.
2025-11-10 00:06:35 +00:00
|
|
|
|
2025-12-12 12:06:12 +00:00
|
|
|
// Create a script that prints version and exits
|
2025-11-13 19:17:07 +00:00
|
|
|
var script []byte
|
|
|
|
|
if runtime.GOOS == "windows" {
|
2025-12-12 12:06:12 +00:00
|
|
|
script = []byte("@echo off\necho XMRig 6.24.0\n")
|
2025-11-13 19:17:07 +00:00
|
|
|
} else {
|
2025-12-12 12:06:12 +00:00
|
|
|
script = []byte("#!/bin/sh\necho 'XMRig 6.24.0'\n")
|
2025-11-13 19:17:07 +00:00
|
|
|
}
|
feat: Increase test coverage for pkg/mining
This commit introduces a number of new tests for the `pkg/mining` package,
increasing the overall test coverage from 8.2% to 40.7%.
The following changes were made:
- Added tests for the `XMRigMiner` struct, including its methods for
installation, starting, stopping, and getting stats.
- Added tests for the `Service` layer, including the API endpoints for
listing, starting, stopping, and getting stats for miners.
- Added tests for the `Manager`, including starting and stopping multiple
miners, collecting stats, and getting hashrate history.
- Introduced a `ManagerInterface` to decouple the `Service` layer from the
concrete `Manager` implementation, facilitating testing with mocks.
2025-11-10 00:06:35 +00:00
|
|
|
|
2025-11-13 19:17:07 +00:00
|
|
|
if err := os.WriteFile(dummyPath, script, 0755); err != nil {
|
|
|
|
|
t.Fatalf("Failed to create dummy miner executable: %v", err)
|
feat: Increase test coverage for pkg/mining
This commit introduces a number of new tests for the `pkg/mining` package,
increasing the overall test coverage from 8.2% to 40.7%.
The following changes were made:
- Added tests for the `XMRigMiner` struct, including its methods for
installation, starting, stopping, and getting stats.
- Added tests for the `Service` layer, including the API endpoints for
listing, starting, stopping, and getting stats for miners.
- Added tests for the `Manager`, including starting and stopping multiple
miners, collecting stats, and getting hashrate history.
- Introduced a `ManagerInterface` to decouple the `Service` layer from the
concrete `Manager` implementation, facilitating testing with mocks.
2025-11-10 00:06:35 +00:00
|
|
|
}
|
2025-11-13 19:17:07 +00:00
|
|
|
|
|
|
|
|
// Prepend the dummy directory to the PATH
|
|
|
|
|
originalPath := os.Getenv("PATH")
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
os.Setenv("PATH", originalPath)
|
|
|
|
|
})
|
|
|
|
|
os.Setenv("PATH", dummyDir+string(os.PathListSeparator)+originalPath)
|
|
|
|
|
|
|
|
|
|
return NewManager()
|
feat: Increase test coverage for pkg/mining
This commit introduces a number of new tests for the `pkg/mining` package,
increasing the overall test coverage from 8.2% to 40.7%.
The following changes were made:
- Added tests for the `XMRigMiner` struct, including its methods for
installation, starting, stopping, and getting stats.
- Added tests for the `Service` layer, including the API endpoints for
listing, starting, stopping, and getting stats for miners.
- Added tests for the `Manager`, including starting and stopping multiple
miners, collecting stats, and getting hashrate history.
- Introduced a `ManagerInterface` to decouple the `Service` layer from the
concrete `Manager` implementation, facilitating testing with mocks.
2025-11-10 00:06:35 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-13 19:17:07 +00:00
|
|
|
// TestStartMiner tests the StartMiner function
|
2025-11-13 21:24:25 +00:00
|
|
|
func TestStartMiner_Good(t *testing.T) {
|
2025-12-12 12:06:12 +00:00
|
|
|
t.Skip("Skipping test that runs miner process as per request")
|
2025-11-13 21:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestStartMiner_Bad(t *testing.T) {
|
|
|
|
|
m := setupTestManager(t)
|
|
|
|
|
defer m.Stop()
|
|
|
|
|
|
|
|
|
|
config := &Config{
|
|
|
|
|
HTTPPort: 9001, // Use a different port to avoid conflict
|
|
|
|
|
Pool: "test:1234",
|
|
|
|
|
Wallet: "testwallet",
|
|
|
|
|
}
|
2025-11-13 19:17:07 +00:00
|
|
|
|
|
|
|
|
// Case 2: Attempt to start an unsupported miner
|
feat: Add context propagation, state sync, and tests
- Add context.Context to ManagerInterface methods (StartMiner, StopMiner, UninstallMiner)
- Add WebSocket state sync on client connect (sends current miner states)
- Add EventStateSync event type and SetStateProvider method
- Add manager lifecycle tests (idempotent stop, context cancellation, shutdown timeout)
- Add database tests (initialization, hashrate storage, stats)
- Add EventHub tests (creation, broadcast, client count, state provider)
- Update all test files for new context-aware API
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-31 10:10:39 +00:00
|
|
|
_, err := m.StartMiner(context.Background(), "unsupported", config)
|
2025-11-13 19:17:07 +00:00
|
|
|
if err == nil {
|
|
|
|
|
t.Error("Expected an error when starting an unsupported miner, but got nil")
|
|
|
|
|
}
|
2025-11-13 21:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestStartMiner_Ugly(t *testing.T) {
|
2025-12-12 12:06:12 +00:00
|
|
|
t.Skip("Skipping test that runs miner process")
|
feat: Increase test coverage for pkg/mining
This commit introduces a number of new tests for the `pkg/mining` package,
increasing the overall test coverage from 8.2% to 40.7%.
The following changes were made:
- Added tests for the `XMRigMiner` struct, including its methods for
installation, starting, stopping, and getting stats.
- Added tests for the `Service` layer, including the API endpoints for
listing, starting, stopping, and getting stats for miners.
- Added tests for the `Manager`, including starting and stopping multiple
miners, collecting stats, and getting hashrate history.
- Introduced a `ManagerInterface` to decouple the `Service` layer from the
concrete `Manager` implementation, facilitating testing with mocks.
2025-11-10 00:06:35 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-13 19:17:07 +00:00
|
|
|
// TestStopMiner tests the StopMiner function
|
2025-11-13 21:24:25 +00:00
|
|
|
func TestStopMiner_Good(t *testing.T) {
|
2025-12-12 12:06:12 +00:00
|
|
|
t.Skip("Skipping test that runs miner process")
|
2025-11-13 21:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestStopMiner_Bad(t *testing.T) {
|
|
|
|
|
m := setupTestManager(t)
|
|
|
|
|
defer m.Stop()
|
feat: Increase test coverage for pkg/mining
This commit introduces a number of new tests for the `pkg/mining` package,
increasing the overall test coverage from 8.2% to 40.7%.
The following changes were made:
- Added tests for the `XMRigMiner` struct, including its methods for
installation, starting, stopping, and getting stats.
- Added tests for the `Service` layer, including the API endpoints for
listing, starting, stopping, and getting stats for miners.
- Added tests for the `Manager`, including starting and stopping multiple
miners, collecting stats, and getting hashrate history.
- Introduced a `ManagerInterface` to decouple the `Service` layer from the
concrete `Manager` implementation, facilitating testing with mocks.
2025-11-10 00:06:35 +00:00
|
|
|
|
2025-11-13 19:17:07 +00:00
|
|
|
// Case 2: Attempt to stop a non-existent miner
|
feat: Add context propagation, state sync, and tests
- Add context.Context to ManagerInterface methods (StartMiner, StopMiner, UninstallMiner)
- Add WebSocket state sync on client connect (sends current miner states)
- Add EventStateSync event type and SetStateProvider method
- Add manager lifecycle tests (idempotent stop, context cancellation, shutdown timeout)
- Add database tests (initialization, hashrate storage, stats)
- Add EventHub tests (creation, broadcast, client count, state provider)
- Update all test files for new context-aware API
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-31 10:10:39 +00:00
|
|
|
err := m.StopMiner(context.Background(), "nonexistent")
|
feat: Increase test coverage for pkg/mining
This commit introduces a number of new tests for the `pkg/mining` package,
increasing the overall test coverage from 8.2% to 40.7%.
The following changes were made:
- Added tests for the `XMRigMiner` struct, including its methods for
installation, starting, stopping, and getting stats.
- Added tests for the `Service` layer, including the API endpoints for
listing, starting, stopping, and getting stats for miners.
- Added tests for the `Manager`, including starting and stopping multiple
miners, collecting stats, and getting hashrate history.
- Introduced a `ManagerInterface` to decouple the `Service` layer from the
concrete `Manager` implementation, facilitating testing with mocks.
2025-11-10 00:06:35 +00:00
|
|
|
if err == nil {
|
2025-11-13 19:17:07 +00:00
|
|
|
t.Error("Expected an error when stopping a non-existent miner, but got nil")
|
feat: Increase test coverage for pkg/mining
This commit introduces a number of new tests for the `pkg/mining` package,
increasing the overall test coverage from 8.2% to 40.7%.
The following changes were made:
- Added tests for the `XMRigMiner` struct, including its methods for
installation, starting, stopping, and getting stats.
- Added tests for the `Service` layer, including the API endpoints for
listing, starting, stopping, and getting stats for miners.
- Added tests for the `Manager`, including starting and stopping multiple
miners, collecting stats, and getting hashrate history.
- Introduced a `ManagerInterface` to decouple the `Service` layer from the
concrete `Manager` implementation, facilitating testing with mocks.
2025-11-10 00:06:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-13 19:23:33 +00:00
|
|
|
|
|
|
|
|
// TestGetMiner tests the GetMiner function
|
2025-11-13 21:24:25 +00:00
|
|
|
func TestGetMiner_Good(t *testing.T) {
|
2025-11-13 19:23:33 +00:00
|
|
|
m := setupTestManager(t)
|
|
|
|
|
defer m.Stop()
|
|
|
|
|
|
2025-12-12 12:06:12 +00:00
|
|
|
// Case 1: Get an existing miner (manually injected)
|
|
|
|
|
miner := NewXMRigMiner()
|
|
|
|
|
// Set name to match what StartMiner would produce usually ("xmrig")
|
|
|
|
|
// Since we inject it, we can use the default name or set one.
|
|
|
|
|
miner.Name = "xmrig-test"
|
|
|
|
|
m.mu.Lock()
|
|
|
|
|
m.miners["xmrig-test"] = miner
|
|
|
|
|
m.mu.Unlock()
|
2025-11-13 19:23:33 +00:00
|
|
|
|
2025-12-12 12:06:12 +00:00
|
|
|
retrievedMiner, err := m.GetMiner("xmrig-test")
|
2025-11-13 19:23:33 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Expected to get miner, but got error: %v", err)
|
|
|
|
|
}
|
2025-12-12 12:06:12 +00:00
|
|
|
if retrievedMiner.GetName() != "xmrig-test" {
|
|
|
|
|
t.Errorf("Expected to get miner 'xmrig-test', but got %s", retrievedMiner.GetName())
|
2025-11-13 19:23:33 +00:00
|
|
|
}
|
2025-11-13 21:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetMiner_Bad(t *testing.T) {
|
|
|
|
|
m := setupTestManager(t)
|
|
|
|
|
defer m.Stop()
|
2025-11-13 19:23:33 +00:00
|
|
|
|
|
|
|
|
// Case 2: Attempt to get a non-existent miner
|
2025-11-13 21:24:25 +00:00
|
|
|
_, err := m.GetMiner("nonexistent")
|
2025-11-13 19:23:33 +00:00
|
|
|
if err == nil {
|
|
|
|
|
t.Error("Expected an error when getting a non-existent miner, but got nil")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestListMiners tests the ListMiners function
|
2025-11-13 21:24:25 +00:00
|
|
|
func TestListMiners_Good(t *testing.T) {
|
2025-11-13 19:23:33 +00:00
|
|
|
m := setupTestManager(t)
|
|
|
|
|
defer m.Stop()
|
|
|
|
|
|
2025-12-31 10:17:14 +00:00
|
|
|
// Get initial count (may include autostarted miners from config)
|
|
|
|
|
initialMiners := m.ListMiners()
|
|
|
|
|
initialCount := len(initialMiners)
|
2025-11-13 19:23:33 +00:00
|
|
|
|
2025-12-12 12:06:12 +00:00
|
|
|
// Case 2: List miners when not empty (manually injected)
|
|
|
|
|
miner := NewXMRigMiner()
|
|
|
|
|
miner.Name = "xmrig-test"
|
|
|
|
|
m.mu.Lock()
|
|
|
|
|
m.miners["xmrig-test"] = miner
|
|
|
|
|
m.mu.Unlock()
|
feat: Add context propagation, state sync, and tests
- Add context.Context to ManagerInterface methods (StartMiner, StopMiner, UninstallMiner)
- Add WebSocket state sync on client connect (sends current miner states)
- Add EventStateSync event type and SetStateProvider method
- Add manager lifecycle tests (idempotent stop, context cancellation, shutdown timeout)
- Add database tests (initialization, hashrate storage, stats)
- Add EventHub tests (creation, broadcast, client count, state provider)
- Update all test files for new context-aware API
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-31 10:10:39 +00:00
|
|
|
|
2026-02-02 06:12:37 +00:00
|
|
|
finalMiners := m.ListMiners()
|
|
|
|
|
expectedCount := initialCount + 1
|
|
|
|
|
if len(finalMiners) != expectedCount {
|
|
|
|
|
t.Errorf("Expected %d miners, but got %d", expectedCount, len(finalMiners))
|
feat: Add context propagation, state sync, and tests
- Add context.Context to ManagerInterface methods (StartMiner, StopMiner, UninstallMiner)
- Add WebSocket state sync on client connect (sends current miner states)
- Add EventStateSync event type and SetStateProvider method
- Add manager lifecycle tests (idempotent stop, context cancellation, shutdown timeout)
- Add database tests (initialization, hashrate storage, stats)
- Add EventHub tests (creation, broadcast, client count, state provider)
- Update all test files for new context-aware API
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-31 10:10:39 +00:00
|
|
|
}
|
|
|
|
|
}
|