package server import ( "context" "os" "path/filepath" "testing" "time" "git.a-wan.net/alan/pipelines/internal/buildinfo" "git.a-wan.net/alan/pipelines/internal/protocol" ) type fakeStatsSampler struct{ snapshot protocol.ServerStats } func (f fakeStatsSampler) Snapshot(context.Context) protocol.ServerStats { return f.snapshot } func TestHandleServerStatsUsesSamplerAndEchoesID(t *testing.T) { want := protocol.ServerStats{SampledAt: "2026-01-02T03:04:05Z"} var got protocol.Message handleServerStats(context.Background(), protocol.Message{Type: protocol.ServerStatsRequest, ID: "abc"}, nil, fakeStatsSampler{snapshot: want}, time.Time{}, buildinfo.Info{}, func(m protocol.Message) error { got = m; return nil }) if got.Type != protocol.ServerStatsResult || got.ID != "abc" || got.ServerStats == nil || got.ServerStats.SampledAt != want.SampledAt { t.Fatalf("unexpected reply: %+v", got) } } func TestParseNVIDIA(t *testing.T) { devices := parseNVIDIA("GeForce RTX, 42, 8192, 1024, 55\nmalformed") if len(devices) != 1 || devices[0].Name != "GeForce RTX" || devices[0].UtilizationPercent == nil || *devices[0].UtilizationPercent != 42 { t.Fatalf("unexpected device: %+v", devices) } if devices[0].MemoryTotalBytes == nil || *devices[0].MemoryTotalBytes != uint64(8192)*1024*1024 { t.Fatalf("memory total=%v, want %d", devices[0].MemoryTotalBytes, uint64(8192)*1024*1024) } if devices[0].MemoryUsedBytes == nil || *devices[0].MemoryUsedBytes != uint64(1024)*1024*1024 { t.Fatalf("memory used=%v, want %d", devices[0].MemoryUsedBytes, uint64(1024)*1024*1024) } } func TestBoundedSizeSkipsSymlinks(t *testing.T) { dir := t.TempDir() if err := os.WriteFile(filepath.Join(dir, "a"), []byte("1234"), 0600); err != nil { t.Fatal(err) } if err := os.Symlink(filepath.Join(dir, "a"), filepath.Join(dir, "link")); err != nil { t.Fatal(err) } got, err := boundedSize(context.Background(), dir, nil) if err != nil { t.Fatal(err) } if got != 4 { t.Fatalf("size=%d, want 4", got) } } func TestBoundedSizeReportsIncompleteWalk(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel() if _, err := boundedSize(ctx, t.TempDir(), nil); err != context.Canceled { t.Fatalf("cancelled size walk error = %v", err) } if _, err := boundedSize(context.Background(), filepath.Join(t.TempDir(), "missing"), nil); err == nil { t.Fatal("missing root must report a partial/unavailable size") } }