97 lines
2.0 KiB
Go
97 lines
2.0 KiB
Go
package fleet
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"forge-mesh/internal/api/types"
|
|
"forge-mesh/internal/db"
|
|
)
|
|
|
|
func TestStoreUpsertHeartbeat(t *testing.T) {
|
|
conn, err := db.Open(t.TempDir() + "/test.db")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
store := NewStore(conn)
|
|
host, err := store.UpsertHeartbeat(types.HeartbeatPayload{
|
|
Hostname: "test-host",
|
|
HashrateHps: 1234.5,
|
|
CurrentTier: 2,
|
|
TierType: "xmrig",
|
|
TierState: "active",
|
|
Arch: "amd64",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if host.ID == "" {
|
|
t.Fatal("expected host id")
|
|
}
|
|
if host.HashrateHps != 1234.5 {
|
|
t.Fatalf("hashrate_hps: got %v", host.HashrateHps)
|
|
}
|
|
if host.CurrentTier != 2 {
|
|
t.Fatalf("current_tier: got %d", host.CurrentTier)
|
|
}
|
|
|
|
host2, err := store.UpsertHeartbeat(types.HeartbeatPayload{
|
|
HostID: host.ID,
|
|
Hostname: "test-host",
|
|
HashrateHps: 5000,
|
|
CurrentTier: 3,
|
|
TierType: "gpu",
|
|
TierState: "probing",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if host2.HashrateHps != 5000 {
|
|
t.Fatalf("hashrate: got %v", host2.HashrateHps)
|
|
}
|
|
if host2.CurrentTier != 3 {
|
|
t.Fatalf("tier: got %d", host2.CurrentTier)
|
|
}
|
|
}
|
|
|
|
func TestMiningProfileRoundTrip(t *testing.T) {
|
|
conn, err := db.Open(t.TempDir() + "/test.db")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
store := NewStore(conn)
|
|
host, err := store.UpsertHeartbeat(types.HeartbeatPayload{Hostname: "miner-1"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
profile := &types.MiningProfile{
|
|
ID: "prof-1",
|
|
Name: "test",
|
|
WalletAddress: "wallet-pin-xyz",
|
|
Tiers: []types.MiningTierSpec{
|
|
{Type: "xmrig", Duration: 5},
|
|
},
|
|
PolicyFromServer: true,
|
|
}
|
|
ctx := context.Background()
|
|
if err := store.SaveMiningProfile(ctx, profile); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.AssignMiningProfile(ctx, host.ID, profile.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got, err := store.GetHostMiningProfile(ctx, host.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.WalletAddress != "wallet-pin-xyz" {
|
|
t.Fatalf("wallet: %q", got.WalletAddress)
|
|
}
|
|
}
|