Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
63 changes: 63 additions & 0 deletions api/__tests__/performance.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
9 changes: 6 additions & 3 deletions api/checks/performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 {
Expand All @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions app/src/SiteAuditApp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 => (
<div key={cat.label} style={{
<div key={cat.label} title={cat.available ? undefined : cat.note} style={{
flex: 1, padding: "14px 16px", borderRadius: 12,
background: cat.available ? `${cat.color}08` : "rgba(255,255,255,0.01)",
border: `1px solid ${cat.available ? `${cat.color}18` : theme.cardBorder}`,
Expand All @@ -1260,7 +1260,7 @@ export default function SiteAuditApp() {
fontSize: 10, color: theme.textMuted, marginTop: 5,
fontFamily: theme.fontMono, letterSpacing: "0.04em", textTransform: "uppercase",
}}>
{cat.label}{!cat.available && " (Pro)"}
{cat.label}{!cat.available && " (n/a)"}
</div>
</div>
))}
Expand Down
Loading