2025-11-08 16:52:16 +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-08 16:52:16 +00:00
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestNewManager(t *testing.T) {
|
|
|
|
|
manager := NewManager()
|
2025-12-31 10:17:14 +00:00
|
|
|
defer manager.Stop()
|
|
|
|
|
|
2025-11-08 16:52:16 +00:00
|
|
|
if manager == nil {
|
|
|
|
|
t.Fatal("NewManager returned nil")
|
|
|
|
|
}
|
|
|
|
|
if manager.miners == nil {
|
|
|
|
|
t.Error("Manager miners map is nil")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-09 01:02:31 +00:00
|
|
|
func TestStartAndStopMiner(t *testing.T) {
|
2025-11-08 16:52:16 +00:00
|
|
|
manager := NewManager()
|
2025-12-31 10:17:14 +00:00
|
|
|
defer manager.Stop()
|
2025-11-08 16:52:16 +00:00
|
|
|
|
2025-11-09 01:02:31 +00:00
|
|
|
config := &Config{
|
|
|
|
|
Pool: "pool.example.com",
|
|
|
|
|
Wallet: "wallet123",
|
2025-11-08 16:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-09 01:02:31 +00:00
|
|
|
// We can't fully test StartMiner without a mock miner,
|
|
|
|
|
// but we can test the manager's behavior.
|
|
|
|
|
// This will fail because the miner executable is not present,
|
|
|
|
|
// which is expected in a test environment.
|
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 := manager.StartMiner(context.Background(), "xmrig", config)
|
2025-11-08 16:52:16 +00:00
|
|
|
if err == nil {
|
2025-11-09 01:02:31 +00:00
|
|
|
t.Log("StartMiner did not fail as expected in test environment")
|
2025-11-08 16:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-09 01:02:31 +00:00
|
|
|
// Since we can't start a miner, we can't test stop either.
|
|
|
|
|
// A more complete test suite would use a mock miner.
|
2025-11-08 16:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetNonExistentMiner(t *testing.T) {
|
|
|
|
|
manager := NewManager()
|
2025-12-31 10:17:14 +00:00
|
|
|
defer manager.Stop()
|
2025-11-08 16:52:16 +00:00
|
|
|
|
|
|
|
|
_, err := manager.GetMiner("non-existent")
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Error("Expected error for getting non-existent miner")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-31 10:17:14 +00:00
|
|
|
func TestListMiners(t *testing.T) {
|
2025-11-08 16:52:16 +00:00
|
|
|
manager := NewManager()
|
2025-12-31 10:17:14 +00:00
|
|
|
defer manager.Stop()
|
|
|
|
|
|
|
|
|
|
// ListMiners should return a valid slice (may include autostarted miners)
|
2025-11-08 16:52:16 +00:00
|
|
|
miners := manager.ListMiners()
|
2025-12-31 10:17:14 +00:00
|
|
|
if miners == nil {
|
|
|
|
|
t.Error("ListMiners returned nil")
|
2025-11-08 16:52:16 +00:00
|
|
|
}
|
2025-12-31 10:17:14 +00:00
|
|
|
// Note: count may be > 0 if autostart is configured
|
2025-11-08 16:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-09 01:02:31 +00:00
|
|
|
func TestListAvailableMiners(t *testing.T) {
|
2025-11-08 16:52:16 +00:00
|
|
|
manager := NewManager()
|
2025-12-31 10:17:14 +00:00
|
|
|
defer manager.Stop()
|
|
|
|
|
|
2025-11-09 01:02:31 +00:00
|
|
|
miners := manager.ListAvailableMiners()
|
|
|
|
|
if len(miners) == 0 {
|
|
|
|
|
t.Error("Expected at least one available miner")
|
2025-11-08 16:52:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetVersion(t *testing.T) {
|
|
|
|
|
version := GetVersion()
|
|
|
|
|
if version == "" {
|
|
|
|
|
t.Error("Version is empty")
|
|
|
|
|
}
|
|
|
|
|
}
|