import { screen } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import LandingPage from "@/app/page";
import { renderWithQueryClient } from "./test-utils";
function mockFetchImplementation(isLocalhost: boolean) {
return vi.fn().mockImplementation((url: string) => {
const path = url.replace("http://localhost:8000", "");
if (path.startsWith("/api/v1/system/status")) {
return Promise.resolve({
ok: true,
status: 200,
json: async () => ({
app_env: "development",
auth_mode: "local",
llm_provider: "mock",
search_provider: "mock",
sms_enabled: false,
sms_provider: "twilio",
ninjapear_configured: false,
ninjapear_credit_balance: null,
ninjapear_estimated_credits_per_company: null,
is_localhost: isLocalhost,
components: [],
}),
});
}
return Promise.resolve({ ok: false, status: 404, json: async () => ({ detail: "not found" }) });
});
}
describe("LandingPage", () => {
beforeEach(() => {
vi.stubGlobal("fetch", mockFetchImplementation(true));
});
it("renders the value proposition and primary calls to action", async () => {
renderWithQueryClient();
expect(
screen.getByRole("heading", { name: /know what your competitors are doing/i }),
).toBeInTheDocument();
expect(
await screen.findByRole("link", { name: /open dashboard \(local mode\)/i }),
).toHaveAttribute("href", "/dashboard");
expect(screen.getByRole("link", { name: /sign in/i })).toHaveAttribute("href", "/login");
expect(screen.getByRole("link", { name: /create account/i })).toHaveAttribute(
"href",
"/register",
);
});
it("discloses the public-information-only collection policy", () => {
renderWithQueryClient();
expect(
screen.getByRole("heading", { name: /only publicly available information/i }),
).toBeInTheDocument();
});
it("drops the local-mode wording when the request isn't from localhost", async () => {
vi.stubGlobal("fetch", mockFetchImplementation(false));
renderWithQueryClient();
expect(await screen.findByRole("link", { name: /^open dashboard$/i })).toHaveAttribute(
"href",
"/dashboard",
);
expect(screen.queryByText(/local mode/i)).not.toBeInTheDocument();
});
it("hides the Git Repository link when no NEXT_PUBLIC_GIT_REPO_URL is configured", () => {
renderWithQueryClient();
expect(screen.queryByRole("link", { name: /git repository/i })).not.toBeInTheDocument();
});
it("shows a Git Repository link opening in a new tab when configured", () => {
vi.stubEnv("NEXT_PUBLIC_GIT_REPO_URL", "https://git.ciagent.org/admin/ci-agent");
renderWithQueryClient();
const link = screen.getByRole("link", { name: /git repository/i });
expect(link).toHaveAttribute("href", "https://git.ciagent.org/admin/ci-agent");
expect(link).toHaveAttribute("target", "_blank");
expect(link).toHaveAttribute("rel", "noopener noreferrer");
});
});