Skip to content

Open source alternative to Semrush and Ahrefs

8.9 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { LighthouseResult } from "@/server/lib/audit/types";

const {
  fetchLighthouseResultMock,
  selectLighthouseSampleMock,
  storeLighthouseResultMock,
  pgStepMock,
  getPagesForAuditMock,
  insertLighthouseResultsMock,
  updateAuditProgressMock,
} = vi.hoisted(() => ({
  fetchLighthouseResultMock: vi.fn(),
  selectLighthouseSampleMock: vi.fn(),
  storeLighthouseResultMock: vi.fn(),
  pgStepMock: vi.fn(),
  getPagesForAuditMock: vi.fn(),
  insertLighthouseResultsMock: vi.fn(),
  updateAuditProgressMock: vi.fn(),
}));

// The real module reaches cloudflare:workers through r2.ts at import time.
vi.mock("cloudflare:workers", () => ({ env: {} }));
// failedLighthouseFetch stays real — the failure-path test asserts on the
// rows it builds.
vi.mock("@/server/lib/audit/lighthouse", async (importOriginal) => {
  const actual = await importOriginal<Record<string, unknown>>();
  return {
    ...actual,
    fetchLighthouseResult: fetchLighthouseResultMock,
    selectLighthouseSample: selectLighthouseSampleMock,
    storeLighthouseResult: storeLighthouseResultMock,
  };
});
vi.mock("@/server/features/audit/repositories/AuditRepository", () => ({
  AuditRepository: {
    getPagesForAudit: getPagesForAuditMock,
    insertLighthouseResults: insertLighthouseResultsMock,
    updateAuditProgress: updateAuditProgressMock,
    hasPagesForAudit: vi.fn().mockResolvedValue(true),
    insertIssues: vi.fn(),
    countPagesByFetchClass: vi.fn().mockResolvedValue(0),
    completeAudit: vi.fn(),
  },
}));
vi.mock("@/server/features/audit/AuditScratchpad", () => ({
  getAuditScratchpad: vi.fn(),
}));
vi.mock("@/server/lib/audit/progress-kv", () => ({
  AuditProgressKV: { clear: vi.fn() },
}));
vi.mock("@/server/lib/audit/discovery", () => ({
  discoverUrls: vi.fn(),
  parseRobotsTxt: vi.fn(),
}));
vi.mock("@/server/lib/audit/issues/multipage", () => ({
  runMultipageChecks: vi.fn(),
}));
vi.mock("@/server/lib/posthog", () => ({ captureServerEvent: vi.fn() }));
vi.mock("@/server/workflows/siteAuditWorkflowCrawl", () => ({
  runCrawlPhase: vi.fn(),
}));
vi.mock("@/server/workflows/pgStep", () => ({ pgStep: pgStepMock }));

import {
  runAuditPhases,
  runLighthousePhase,
} from "@/server/workflows/siteAuditWorkflowPhases";
import { AuditRepository } from "@/server/features/audit/repositories/AuditRepository";
import { getAuditScratchpad } from "@/server/features/audit/AuditScratchpad";
import { runCrawlPhase } from "@/server/workflows/siteAuditWorkflowCrawl";
import { runMultipageChecks } from "@/server/lib/audit/issues/multipage";

const PHASE_PARAMS = {
  auditId: "audit-1",
  workflowInstanceId: "workflow-1",
  billingCustomer: {
    userId: "user-1",
    userEmail: "[email protected]",
    organizationId: "org-1",
  },
  projectId: "project-1",
  startUrl: "https://example.com/",
  config: { maxPages: 50, lighthouseStrategy: "auto" as const },
};

describe("runLighthousePhase", () => {
  beforeEach(() => {
    getPagesForAuditMock.mockResolvedValue([
      {
        id: "page-1",
        url: "https://example.com/",
        statusCode: 200,
      },
    ]);
    selectLighthouseSampleMock.mockReturnValue(["https://example.com/"]);
    fetchLighthouseResultMock.mockImplementation(
      async (_url: string, pageId: string, strategy: "mobile" | "desktop") => ({
        result: { pageId, strategy },
        payloadJson: "{}",
      }),
    );
    storeLighthouseResultMock.mockImplementation(
      async ({ fetched }: { fetched: { result: unknown } }) => fetched.result,
    );
    insertLighthouseResultsMock.mockResolvedValue(undefined);
    updateAuditProgressMock.mockResolvedValue(undefined);
  });

  it("does not replay paid calls when persistence retries", async () => {
    let persistenceAttempts = 0;
    let fetchRetryLimit: number | undefined;
    let persistenceRetryLimit: number | undefined;
    pgStepMock.mockImplementation(
      async (
        _step: unknown,
        name: string,
        config: { retries?: { limit?: number } },
        callback: () => Promise<unknown>,
      ) => {
        if (name === "lighthouse-fetch-1") {
          fetchRetryLimit = config.retries?.limit;
        }
        if (name === "lighthouse-persist-chunk-1") {
          persistenceRetryLimit = config.retries?.limit;
          persistenceAttempts += 1;
          try {
            return await callback();
          } catch (error) {
            if ((config.retries?.limit ?? 0) < 1) throw error;
            persistenceAttempts += 1;
            return callback();
          }
        }
        return callback();
      },
    );

    updateAuditProgressMock
      .mockResolvedValueOnce(undefined)
      .mockRejectedValueOnce(new Error("progress unavailable"))
      .mockResolvedValueOnce(undefined);

    // pgStep is mocked above, so the opaque WorkflowStep object is never read.
    // oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion
    await runLighthousePhase({} as never, PHASE_PARAMS);

    expect(fetchLighthouseResultMock).toHaveBeenCalledTimes(2);
    expect(storeLighthouseResultMock).toHaveBeenCalledTimes(4);
    expect(insertLighthouseResultsMock).toHaveBeenCalledTimes(2);
    expect(persistenceAttempts).toBe(2);
    expect(fetchRetryLimit).toBe(0);
    expect(persistenceRetryLimit).toBe(3);
    expect(updateAuditProgressMock).toHaveBeenLastCalledWith(
      "audit-1",
      "workflow-1",
      { lighthouseCompleted: 2, lighthouseFailed: 0 },
    );
  });

  it("persists sibling results when one fetch step fails", async () => {
    getPagesForAuditMock.mockResolvedValue([
      { id: "page-1", url: "https://example.com/", statusCode: 200 },
      { id: "page-2", url: "https://example.com/about", statusCode: 200 },
    ]);
    selectLighthouseSampleMock.mockReturnValue([
      "https://example.com/",
      "https://example.com/about",
    ]);
    pgStepMock.mockImplementation(
      async (
        _step: unknown,
        name: string,
        _config: unknown,
        callback: () => Promise<unknown>,
      ) => {
        if (name === "lighthouse-fetch-2") {
          throw new Error("step timed out");
        }
        return callback();
      },
    );

    // pgStep is mocked above, so the opaque WorkflowStep object is never read.
    // oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion
    await runLighthousePhase({} as never, PHASE_PARAMS);

    // Only the surviving URL's pair was fetched; the failed step's checks
    // still land as errorMessage rows in the same insert.
    expect(fetchLighthouseResultMock).toHaveBeenCalledTimes(2);
    expect(insertLighthouseResultsMock).toHaveBeenCalledTimes(1);
    // vi.fn() mock calls are untyped; the mocked storeLighthouseResult above
    // passes fetch results through as rows.
    // oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion
    const inserted = insertLighthouseResultsMock.mock
      .calls[0]?.[1] as LighthouseResult[];
    expect(inserted).toHaveLength(4);
    expect(
      inserted.filter((row) => row.errorMessage === "step timed out"),
    ).toHaveLength(2);
    expect(updateAuditProgressMock).toHaveBeenLastCalledWith(
      "audit-1",
      "workflow-1",
      { lighthouseCompleted: 2, lighthouseFailed: 2 },
    );
  });
});

describe("app-shell crawl coverage", () => {
  it.each([false, true])(
    "allows orphan checks only without unread app shells (shells=%s)",
    async (hasShells) => {
      vi.mocked(AuditRepository.hasPagesForAudit).mockResolvedValue(true);
      vi.mocked(runCrawlPhase).mockResolvedValue({
        pagesCrawled: 2,
        completed: true,
      });
      vi.mocked(runMultipageChecks).mockResolvedValue({
        issues: [],
        hasUnreadShells: hasShells,
      });
      const finalize = vi
        .fn()
        .mockResolvedValue({ brokenLinks: [], orphanPages: [] });
      // Only these two scratchpad methods are reached after mocked discovery/crawl.
      // oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion
      vi.mocked(getAuditScratchpad).mockReturnValue({
        runFinalizeChecks: finalize,
        destroy: vi.fn(),
      } as unknown as ReturnType<typeof getAuditScratchpad>);
      pgStepMock.mockImplementation(
        async (
          _step: unknown,
          name: string,
          _config: unknown,
          callback: () => Promise<unknown>,
        ) => {
          if (name === "discover-urls-v2")
            return { robotsText: "", seededCount: 2 };
          return callback();
        },
      );
      // pgStep is mocked, so no WorkflowStep implementation is needed.
      // oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion
      await runAuditPhases({} as never, {
        ...PHASE_PARAMS,
        config: { maxPages: 50, lighthouseStrategy: "none" },
        renderUsage: { cloudflareAttempts: 0, contextCredits: 0 },
      });
      expect(finalize).toHaveBeenCalledWith({
        startUrl: "https://example.com/",
        crawlCompleted: !hasShells,
      });
    },
  );
});