package server import ( "context" "encoding/json" "errors" "testing" "pageswiki/internal/seheweit" "pageswiki/internal/store" ) func TestSeheweitAPIContractAndReadOnlyCollection(t *testing.T) { s, token, _ := configFixture(t) seedMonitorJobs(t, s) s.schedulerRunning.Store(2) w := configRequest(s, token, "GET", "/_/api/v1/queue/resources", nil) var snapshot seheweit.Snapshot if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &snapshot) != nil { t.Fatalf("snapshot %d %s", w.Code, w.Body.String()) } if snapshot.Queue.Stats.Queued != 1 || snapshot.Queue.Stats.Running != 1 || snapshot.Queue.ActiveWorkers != 2 || snapshot.IO.ProcessID == "" { t.Fatalf("application connection %+v", snapshot) } if w.Header().Get("Cache-Control") != "no-store" { t.Fatal("live response cached") } for _, tc := range []struct { query string hours int }{{"", 24}, {"?hours=invalid", 24}, {"?hours=1", 1}, {"?hours=168", 168}} { w = configRequest(s, token, "GET", "/_/api/v1/queue/resources/history"+tc.query, nil) var result struct { Samples []json.RawMessage Hours int RetentionDays int `json:"retention_days"` } if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &result) != nil || result.Hours != tc.hours || result.RetentionDays != 7 || result.Samples == nil || len(result.Samples) != 0 { t.Fatalf("history default/empty shape %s", w.Body.String()) } } // Neither current-value nor history GET creates persisted samples. var count int if err := s.store.DB.QueryRow(`SELECT COUNT(*) FROM resource_samples`).Scan(&count); err != nil || count != 0 { t.Fatalf("request persisted sample: %d %v", count, err) } ctx, cancel := context.WithCancel(t.Context()) cancel() err := s.registeredEventHandlers().Run(ctx, &store.EventJob{UUID: "cancelled-monitor", Type: store.ResourceEventType}) if !errors.Is(err, context.Canceled) { t.Fatalf("cancelled job %v", err) } if err := s.store.DB.QueryRow(`SELECT COUNT(*) FROM resource_samples`).Scan(&count); err != nil || count != 0 { t.Fatal("cancelled job persisted sample") } }