diff --git a/apps/web/Dockerfile b/apps/web/Dockerfile index 08c5749..91bc74c 100644 --- a/apps/web/Dockerfile +++ b/apps/web/Dockerfile @@ -14,8 +14,10 @@ COPY . . # match the browser-visible URLs published by Docker Compose. ARG VITE_API_URL=http://localhost:8080 ARG VITE_WS_URL=ws://localhost:8080/ws +ARG VITE_DEMO_MODE=false ENV VITE_API_URL=${VITE_API_URL} ENV VITE_WS_URL=${VITE_WS_URL} +ENV VITE_DEMO_MODE=${VITE_DEMO_MODE} RUN npm run build # ---- runtime stage ------------------------------------------------------- diff --git a/apps/web/src/lib/env.ts b/apps/web/src/lib/env.ts index 41aa143..d96fcfe 100644 --- a/apps/web/src/lib/env.ts +++ b/apps/web/src/lib/env.ts @@ -7,6 +7,7 @@ export interface AppEnv { apiUrl: string; /** WebSocket URL for the realtime stream, e.g. ws://localhost:8080/ws. */ wsUrl: string; + demoMode: boolean; } type RawEnv = Record; @@ -37,7 +38,7 @@ export function validateEnv(source: RawEnv): AppEnv { throw new Error(`Invalid frontend environment:\n- ${errors.join("\n- ")}`); } - return { apiUrl, wsUrl }; + return { apiUrl, wsUrl, demoMode: source.VITE_DEMO_MODE === "true" }; } function isUrlWithScheme(value: string, schemes: string[]): boolean { diff --git a/apps/web/src/pages/LoginPage.test.tsx b/apps/web/src/pages/LoginPage.test.tsx index 979b3c2..92bd114 100644 --- a/apps/web/src/pages/LoginPage.test.tsx +++ b/apps/web/src/pages/LoginPage.test.tsx @@ -2,10 +2,12 @@ import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; import { render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { MemoryRouter, Route, Routes } from "react-router-dom"; -import { AuthProvider } from "@/app/AuthContext"; -import { LoginPage } from "@/pages/LoginPage"; -function renderLoginPage() { +async function renderLoginPage() { + const [{ AuthProvider }, { LoginPage }] = await Promise.all([ + import("@/app/AuthContext"), + import("@/pages/LoginPage"), + ]); return render( @@ -20,12 +22,37 @@ function renderLoginPage() { describe("LoginPage", () => { beforeEach(() => { + vi.resetModules(); + vi.stubEnv("VITE_DEMO_MODE", undefined); vi.stubGlobal("fetch", vi.fn()); }); afterEach(() => { + vi.unstubAllEnvs(); vi.unstubAllGlobals(); }); + it("shows the synthetic data banner in demo mode", async () => { + vi.stubEnv("VITE_DEMO_MODE", "true"); + + await renderLoginPage(); + + expect(screen.getByText("Synthetic data only")).toBeVisible(); + }); + + it("omits the synthetic data banner when demo mode is unset", async () => { + await renderLoginPage(); + + expect(screen.queryByText("Synthetic data only")).not.toBeInTheDocument(); + }); + + it("omits the synthetic data banner when demo mode is false", async () => { + vi.stubEnv("VITE_DEMO_MODE", "false"); + + await renderLoginPage(); + + expect(screen.queryByText("Synthetic data only")).not.toBeInTheDocument(); + }); + it("navigates to /dashboard on successful submit", async () => { (fetch as ReturnType).mockResolvedValue({ ok: true, @@ -35,7 +62,7 @@ describe("LoginPage", () => { }), }); - renderLoginPage(); + await renderLoginPage(); await userEvent.type(screen.getByLabelText(/email/i), "operator@example.com"); await userEvent.type(screen.getByLabelText(/password/i), "correct-password"); await userEvent.click(screen.getByRole("button", { name: /sign in/i })); @@ -49,7 +76,7 @@ describe("LoginPage", () => { json: async () => ({ error: { code: "INVALID_CREDENTIALS", message: "email or password is incorrect" } }), }); - renderLoginPage(); + await renderLoginPage(); await userEvent.type(screen.getByLabelText(/email/i), "operator@example.com"); await userEvent.type(screen.getByLabelText(/password/i), "wrong-password"); await userEvent.click(screen.getByRole("button", { name: /sign in/i })); diff --git a/apps/web/src/pages/LoginPage.tsx b/apps/web/src/pages/LoginPage.tsx index b819f1f..caa6982 100644 --- a/apps/web/src/pages/LoginPage.tsx +++ b/apps/web/src/pages/LoginPage.tsx @@ -2,6 +2,7 @@ import { useState, type FormEvent } from "react"; import { useNavigate } from "react-router-dom"; import { useAuth } from "@/app/AuthContext"; import { Button } from "@/components/ui/button"; +import { env } from "@/lib/env"; /** LoginPage authenticates against POST /login and stores the token in memory. */ export function LoginPage() { @@ -32,6 +33,11 @@ export function LoginPage() {

Sign in

FinWatch operator access.

+ {env.demoMode && ( +

+ Synthetic data only +

+ )}