From e340fbbadecd41e1b2d05ad9978dde44b604ca68 Mon Sep 17 00:00:00 2001 From: "pulse-triage[bot]" <249995291+pulse-triage[bot]@users.noreply.github.com> Date: Tue, 10 Nov 2026 06:26:34 +0000 Subject: [PATCH] Replace resource hot-path source pins with native cache checks Containing CI correctly rejected stale string assertions that required the removed full-fleet copy and repeated summary work. Exercise list and stats HTTP responses and generation-summary reuse in the existing native cache test instead; retain page/live-health/concurrency and PMG ownership coverage. Lower only the retired source-read baseline entry. Contract-Neutral: Test-only replacement of obsolete implementation-shape pins with executable endpoint/cache assertions; runtime source, wire contracts, rendered state and performance budgets are unchanged. Change-source: pulse-maintainer --- .../scripts/source-text-test-baseline.json | 1 - .../resourcesHotPath.guardrails.test.ts | 22 ---------- .../resources_shared_cache_test.go | 42 +++++++++++++++++++ 3 files changed, 42 insertions(+), 23 deletions(-) delete mode 100644 frontend-modern/src/api/__tests__/resourcesHotPath.guardrails.test.ts diff --git a/frontend-modern/scripts/source-text-test-baseline.json b/frontend-modern/scripts/source-text-test-baseline.json index be721b4474..47627d1495 100644 --- a/frontend-modern/scripts/source-text-test-baseline.json +++ b/frontend-modern/scripts/source-text-test-baseline.json @@ -4,7 +4,6 @@ "cmd/pulse-mcp/main_test.go": 3, "frontend-modern/src/__tests__/App.architecture.test.ts": 9, "frontend-modern/src/api/__tests__/recoveryTransport.test.ts": 1, - "frontend-modern/src/api/__tests__/resourcesHotPath.guardrails.test.ts": 1, "frontend-modern/src/components/__tests__/GitHubStarBanner.test.tsx": 1, "frontend-modern/src/components/__tests__/whatsNewModel.test.ts": 3, "frontend-modern/src/components/AI/__tests__/FindingsPanel.test.ts": 3, diff --git a/frontend-modern/src/api/__tests__/resourcesHotPath.guardrails.test.ts b/frontend-modern/src/api/__tests__/resourcesHotPath.guardrails.test.ts deleted file mode 100644 index df19e8c38f..0000000000 --- a/frontend-modern/src/api/__tests__/resourcesHotPath.guardrails.test.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { readFileSync } from 'node:fs'; -import { resolve } from 'node:path'; -import { describe, expect, it } from 'vitest'; - -const resourcesHandlerSource = readFileSync( - resolve(process.cwd(), '../internal/api/resourceapi/resources.go'), - 'utf8', -); - -describe('resource API hot-path guardrails', () => { - it('reuses the per-generation presentation snapshot across list and stats requests', () => { - expect( - resourcesHandlerSource.match(/sharedPresentationResources\(orgID\)/g) ?? [], - ).toHaveLength(2); - expect(resourcesHandlerSource).toContain('allResources := flatCopyResources(sharedResources)'); - expect( - resourcesHandlerSource.match(/computeResourceContractStats\(allResources\)/g) ?? [], - ).toHaveLength(2); - expect(resourcesHandlerSource).not.toContain('allResources := registry.List()'); - expect(resourcesHandlerSource).not.toContain('computeResourceContractByType(registry.List())'); - }); -}); diff --git a/internal/api/resourceapi/resources_shared_cache_test.go b/internal/api/resourceapi/resources_shared_cache_test.go index 8ca7ddb560..37c2da76c2 100644 --- a/internal/api/resourceapi/resources_shared_cache_test.go +++ b/internal/api/resourceapi/resources_shared_cache_test.go @@ -2,8 +2,10 @@ package resourceapi import ( "context" + "encoding/json" "net/http" "net/http/httptest" + "reflect" "testing" "time" @@ -37,6 +39,7 @@ func TestPruneResourceForListResponseDoesNotMutateSharedPMG(t *testing.T) { func TestSharedResourceListsCachedPerGenerationAndImmuneToRequestDecoration(t *testing.T) { now := time.Date(2026, 8, 5, 12, 0, 0, 0, time.UTC) h := NewQueryService(&config.Config{DataPath: t.TempDir()}) + defer h.CloseStores() h.SetStateProvider(resourceUnifiedSeedProvider{ snapshot: models.StateSnapshot{LastUpdate: now}, resources: []unifiedresources.Resource{ @@ -88,6 +91,45 @@ func TestSharedResourceListsCachedPerGenerationAndImmuneToRequestDecoration(t *t if rec.Code != http.StatusOK { t.Fatalf("status = %d, body=%s", rec.Code, rec.Body.String()) } + var listResponse ResourcesResponse + if err := json.Unmarshal(rec.Body.Bytes(), &listResponse); err != nil { + t.Fatal(err) + } + cachedStats := h.registryCache[cacheKey(orgID)].contractStats + if cachedStats == nil { + t.Fatal("list request did not retain its generation-owned structural summary") + } + + // Exercise both consumers, not their source spelling: stats and a later + // list must reuse this generation's summary and return the same counts. + for _, path := range []string{"/api/resources/stats", "/api/resources?limit=1"} { + req := httptest.NewRequest(http.MethodGet, path, nil) + req = req.WithContext(context.WithValue(req.Context(), apicontext.OrgIDContextKey, orgID)) + rec := httptest.NewRecorder() + var stats unifiedresources.ResourceStats + if path == "/api/resources/stats" { + h.HandleStats(rec, req) + if err := json.Unmarshal(rec.Body.Bytes(), &stats); err != nil { + t.Fatal(err) + } + } else { + h.HandleListResources(rec, req) + var page ResourcesResponse + if err := json.Unmarshal(rec.Body.Bytes(), &page); err != nil { + t.Fatal(err) + } + stats = page.Aggregations + if len(page.Data) != 1 || page.Meta.Total != listResponse.Meta.Total { + t.Fatal("paginated response lost its row limit or full-fleet total") + } + } + if rec.Code != http.StatusOK || !reflect.DeepEqual(stats, listResponse.Aggregations) { + t.Fatalf("list and stats disagree for %s: status=%d", path, rec.Code) + } + if h.registryCache[cacheKey(orgID)].contractStats != cachedStats { + t.Fatalf("%s replaced the unchanged generation's summary", path) + } + } after, _, err := h.sharedPresentationResources(orgID) if err != nil {