package idlewatt import ( "strings" "testing" "time" "gno.land/p/nt/avl/v0" "gno.land/p/nt/testutils/v0" "gno.land/p/nt/uassert/v0" "gno.land/p/nt/urequire/v0" ) var ( alice = testutils.TestAddress("alice") bob = testutils.TestAddress("bob") carol = testutils.TestAddress("carol") boss = testutils.TestAddress("boss") who address // the account the next call is signed by ) // reset puts the realm back to a clean state between tests. func reset() { admin = boss paused = false onlineWindow = 30 * time.Minute nextNodeID = 1 nextJobID = 1 nodes = avl.NewTree() byOperator = avl.NewTree() leaderboard = avl.NewTree() jobs = avl.NewTree() nodeJobs = avl.NewTree() totalGrossCents = 0 poolCents = 0 poolPaidCents = 0 bonusRounds = 0 lastDistribution = time.Time{} } func registerGPU(cur realm, op address, name string, rate int64) int { who = op testing.SetRealm(testing.NewUserRealm(who)) return RegisterNode(cross(cur), name, KindHomeGPU, "RTX 4090 24GB", "US-NY Buffalo", PowerGrid, rate) } // bringOnline heartbeats a node and makes it available. func bringOnline(cur realm, op address, id int) { who = op testing.SetRealm(testing.NewUserRealm(who)) Heartbeat(cross(cur), id, "") SetAvailability(cross(cur), id, ModeAvailable) } func TestRegisterNode(cur realm, t *testing.T) { reset() who = alice testing.SetRealm(testing.NewUserRealm(who)) id := RegisterNode(cross(cur), "Tonawanda Cybertruck (simulated)", KindVehicle, "AMD Ryzen infotainment + AI4", "US-NY Tonawanda", PowerPowershare, 30) uassert.Equal(t, 1, id) uassert.Equal(t, 1, NodeCount()) n := mustGetNode(id) uassert.Equal(t, alice, n.Operator) uassert.Equal(t, KindVehicle, n.Kind) uassert.Equal(t, ModePaused, n.Mode, "new nodes start paused") uassert.Equal(t, int64(30), n.RateCents) uassert.True(t, n.LastHeartbeat.IsZero()) uassert.Equal(t, 1, operatorNodeCount(alice)) uassert.Equal(t, 1, leaderboard.Size()) id2 := registerGPU(cur, bob, "Garage 4090", 45) uassert.Equal(t, 2, id2, "ids are sequential") } func TestRegisterValidation(cur realm, t *testing.T) { reset() who = alice testing.SetRealm(testing.NewUserRealm(who)) cases := []struct { name, kind, hw, region, power string rate int64 want string }{ {"ab", KindCPU, "Xeon", "US", PowerGrid, 10, "name must be 3-48 characters"}, {strings.Repeat("x", 49), KindCPU, "Xeon", "US", PowerGrid, 10, "name must be 3-48 characters"}, {" lead", KindCPU, "Xeon", "US", PowerGrid, 10, "name must not start or end with a space"}, {"bad|pipe", KindCPU, "Xeon", "US", PowerGrid, 10, "name contains an unsupported character: only letters, digits, spaces and - . , ( ) ' / + : @ # % & = are allowed"}, {"