diff --git a/README.md b/README.md index 302250b..f53fdca 100644 --- a/README.md +++ b/README.md @@ -397,8 +397,8 @@ npm run test:app # frontend only (Vitest) CI runs on every pull request via [`.github/workflows/test.yml`](.github/workflows/test.yml). -> [!NOTE] -> A few pre-existing tests in the SEO / LLM / marketing scoring suites are inherited from the project's prior life and may fail on threshold mismatches. These are tracked and not blocking; new contributions should not introduce new failures. +> [!TIP] +> The full suite — backend (Jest) and frontend (Vitest) — runs green on `main`. New contributions should keep it that way. ## Deployment diff --git a/api/__tests__/performance.test.js b/api/__tests__/performance.test.js new file mode 100644 index 0000000..4be1488 --- /dev/null +++ b/api/__tests__/performance.test.js @@ -0,0 +1,63 @@ +// api/__tests__/performance.test.js +const { runPerformanceChecks } = require("../checks/performance"); + +// Mock fetch globally — runPerformanceChecks calls the PageSpeed Insights API +global.fetch = jest.fn(); + +// runPerformanceChecks logs to console.error on the failure paths exercised below; +// silence it so the suite output stays clean. +let errorSpy; +beforeEach(() => { + global.fetch.mockReset(); + errorSpy = jest.spyOn(console, "error").mockImplementation(() => {}); +}); +afterEach(() => { + errorSpy.mockRestore(); +}); + +describe("runPerformanceChecks", () => { + test("returns score null and available false when PageSpeed Insights is rate-limited", async () => { + global.fetch.mockResolvedValueOnce({ + ok: false, + status: 429, + json: async () => ({ error: { errors: [{ reason: "rateLimitExceeded" }] } }), + }); + + const result = await runPerformanceChecks("https://example.com"); + + // A failed PSI call must NOT be presented as a real score of 0 — + // 0 reads as "terrible site", null reads as "not measured". + expect(result.score).toBeNull(); + expect(result.available).toBe(false); + expect(result.error).toMatch(/PageSpeed Insights/); + expect(result.metrics).toEqual([]); + }); + + test("returns score null and available false on a network error", async () => { + global.fetch.mockRejectedValueOnce(new Error("ECONNREFUSED")); + + const result = await runPerformanceChecks("https://example.com"); + + expect(result.score).toBeNull(); + expect(result.available).toBe(false); + expect(result.error).toBeDefined(); + }); + + test("returns a numeric score and available true on success", async () => { + global.fetch.mockResolvedValueOnce({ + ok: true, + json: async () => ({ + lighthouseResult: { + categories: { performance: { score: 0.92 } }, + audits: {}, + }, + }), + }); + + const result = await runPerformanceChecks("https://example.com"); + + expect(result.available).toBe(true); + expect(result.score).toBe(92); + expect(result.error).toBeUndefined(); + }); +}); diff --git a/api/checks/performance.js b/api/checks/performance.js index 6f3b181..8b4d946 100644 --- a/api/checks/performance.js +++ b/api/checks/performance.js @@ -258,6 +258,7 @@ function extractMetrics(psiData) { opportunities.sort((a, b) => b.savingsMs - a.savingsMs); return { + available: true, score: perfScore, metrics, opportunities: opportunities.slice(0, 5), @@ -266,8 +267,9 @@ function extractMetrics(psiData) { } /** - * Run performance checks. Returns { score, metrics, opportunities, strategy }. - * On failure, returns a degraded result with score 0 so the scan doesn't break. + * Run performance checks. Returns { available, score, metrics, opportunities, strategy }. + * On failure (e.g. PageSpeed Insights rate limit), returns `available: false` and + * `score: null` so the result reads as "not measured" rather than a real score of 0. */ async function runPerformanceChecks(url) { try { @@ -276,7 +278,8 @@ async function runPerformanceChecks(url) { } catch (err) { console.error("Performance check failed:", err.message); return { - score: 0, + available: false, + score: null, metrics: [], opportunities: [], strategy: "mobile", diff --git a/app/src/SiteAuditApp.jsx b/app/src/SiteAuditApp.jsx index 8306bc0..8e11841 100644 --- a/app/src/SiteAuditApp.jsx +++ b/app/src/SiteAuditApp.jsx @@ -1239,9 +1239,9 @@ export default function SiteAuditApp() { { label: "SEO", score: seoScore, color: theme.accent, available: true }, { label: "AI Search", score: llmScore, color: theme.info, available: true }, { label: "Marketing", score: mktScore, color: theme.warning, available: true }, - { label: "Speed", score: perfScore, color: theme.violet, available: true }, + { label: "Speed", score: perfScore, color: theme.violet, available: hasPerfData, note: "Core Web Vitals unavailable — set a PSI_API_KEY to enable performance data" }, ].map(cat => ( -
- {cat.label}{!cat.available && " (Pro)"} + {cat.label}{!cat.available && " (n/a)"}
))}