package main import ( "context" "errors" "testing" "time" "github.com/itworx/pulse/internal/agentsource" "github.com/itworx/pulse/internal/agentstore" "github.com/itworx/pulse/internal/datasource" ) type sourceHealthReader map[agentstore.Capability]agentstore.Snapshot func (reader sourceHealthReader) Latest(_ context.Context, capability agentstore.Capability) (agentstore.Snapshot, error) { snapshot, ok := reader[capability] if !ok { return agentstore.Snapshot{}, agentstore.ErrNoSnapshot } return snapshot, nil } func TestReadAgentSourceHealthRequiresContainersForHolisticUnraidHealth(t *testing.T) { now := time.Date(2026, 8, 21, 12, 0, 0, 0, time.UTC) reader := sourceHealthReader{} for _, capability := range requiredUnraidCapabilities { reader[capability] = agentstore.Snapshot{Capability: capability, ObservedAt: now.Add(-time.Second), ReceivedAt: now} } stale := reader[agentstore.CapabilityContainers] stale.ObservedAt = now.Add(-time.Hour) reader[agentstore.CapabilityContainers] = stale unraidHealth, storageHealth, err := readAgentSourceHealth(context.Background(), reader, now) if err != nil { t.Fatal(err) } if unraidHealth.State != datasource.HealthUnknown || unraidHealth.ReasonCode != agentsource.ReasonStale { t.Fatalf("partially stale Unraid health = %#v", unraidHealth) } if storageHealth.State != datasource.HealthHealthy { t.Fatalf("fresh storage health = %#v", storageHealth) } fresh := reader[agentstore.CapabilityContainers] fresh.ObservedAt = now.Add(-time.Second) reader[agentstore.CapabilityContainers] = fresh unraidHealth, storageHealth, err = readAgentSourceHealth(context.Background(), reader, now) if err != nil || unraidHealth.State != datasource.HealthHealthy || storageHealth.State != datasource.HealthHealthy { t.Fatalf("fully fresh health unraid=%#v storage=%#v err=%v", unraidHealth, storageHealth, err) } } func TestReadAgentSourceHealthPropagatesCancellation(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel() _, _, err := readAgentSourceHealth(ctx, sourceHealthReader{}, time.Now().UTC()) if !errors.Is(err, context.Canceled) { t.Fatalf("cancellation error = %v", err) } }