diff --git a/web/e2e/specs/history-debug-replay.spec.ts b/web/e2e/specs/history-debug-replay.spec.ts index 0cc7a509ab..df2601e92c 100644 --- a/web/e2e/specs/history-debug-replay.spec.ts +++ b/web/e2e/specs/history-debug-replay.spec.ts @@ -7,6 +7,10 @@ */ import { test, expect, type FrigateApp } from "../fixtures/frigate-test"; +import { + activeSessionStatus, + noSessionStatus, +} from "../fixtures/mock-data/debug-replay"; // the selection is seeded around the playback position, so land near the // live edge @@ -183,6 +187,114 @@ test.describe("Debug Replay from History @high", () => { }); }); +// both layouts render the same form, so these run on desktop and mobile +test.describe("Debug Replay form with an active session @high", () => { + test("shows a banner that stops the running session", async ({ + frigateApp, + }) => { + let active = true; + let stopCalled = false; + + await frigateApp.installDefaults(); + await frigateApp.page.route("**/api/debug_replay/status", (route) => + route.fulfill({ + json: active + ? activeSessionStatus({ sourceCamera: "front_door" }) + : noSessionStatus(), + }), + ); + await frigateApp.page.route("**/api/debug_replay/stop", (route) => { + if (route.request().method() === "POST") { + stopCalled = true; + active = false; + } + return route.fulfill({ json: { success: true } }); + }); + await openRecordingView(frigateApp); + + const form = await openDebugReplayForm(frigateApp); + const banner = form.getByRole("alert"); + const startButton = form.getByRole("button", { name: "Start Replay" }); + + await expect(banner).toContainText( + "A debug replay session is already running for Front Door", + { timeout: 5_000 }, + ); + await expect(startButton).toBeDisabled(); + + await banner.getByRole("button", { name: "Stop Replay" }).click(); + + await expect.poll(() => stopCalled, { timeout: 5_000 }).toBe(true); + await expect(banner).toBeHidden({ timeout: 5_000 }); + await expect(startButton).toBeEnabled(); + }); + + test.describe("after the session already ended", () => { + // the stop endpoint answers 400 when no session is active, which + // Chromium echoes to the console; the text carries no URL, so match the + // 400 echo itself + test.use({ + expectedErrors: [ + /^Failed to load resource: the server responded with a status of 400\b/, + ], + }); + + test("stopping clears the banner", async ({ frigateApp }) => { + let active = true; + + await frigateApp.installDefaults(); + await frigateApp.page.route("**/api/debug_replay/status", (route) => + route.fulfill({ + json: active ? activeSessionStatus() : noSessionStatus(), + }), + ); + // the server tore the session down after the form loaded its status, + // e.g. because startup failed + await frigateApp.page.route("**/api/debug_replay/stop", (route) => { + active = false; + return route.fulfill({ + status: 400, + json: { success: false, message: "No active replay session" }, + }); + }); + await openRecordingView(frigateApp); + + const form = await openDebugReplayForm(frigateApp); + const banner = form.getByRole("alert"); + + await expect(banner).toBeVisible({ timeout: 5_000 }); + await banner.getByRole("button", { name: "Stop Replay" }).click(); + + await expect(banner).toBeHidden({ timeout: 5_000 }); + await expect( + form.getByRole("button", { name: "Start Replay" }), + ).toBeEnabled(); + await expect( + frigateApp.page.getByText(/Failed to stop debug replay/), + ).toHaveCount(0); + }); + }); + + test("shows no banner when nothing is running", async ({ frigateApp }) => { + await frigateApp.installDefaults(); + await frigateApp.page.route("**/api/debug_replay/status", (route) => + route.fulfill({ json: noSessionStatus() }), + ); + await openRecordingView(frigateApp); + + const statusLoaded = frigateApp.page.waitForResponse( + "**/api/debug_replay/status", + ); + const form = await openDebugReplayForm(frigateApp); + await statusLoaded; + + await expect( + form.getByRole("button", { name: "Start Replay" }), + ).toBeEnabled(); + await expect(form.getByRole("alert")).toHaveCount(0); + }); +}); + test.describe("Debug Replay from History — mobile @high @mobile", () => { test("a reselected range lands once and stays put", async ({ frigateApp, diff --git a/web/public/locales/en/views/replay.json b/web/public/locales/en/views/replay.json index dbeaa84a07..32c6a7528b 100644 --- a/web/public/locales/en/views/replay.json +++ b/web/public/locales/en/views/replay.json @@ -18,6 +18,11 @@ "starting": "Starting replay...", "startLabel": "Start", "endLabel": "End", + "activeSession": { + "description": "A debug replay session is already running for {{camera}}. Stop it to start a new one.", + "stop": "Stop Replay", + "stopping": "Stopping..." + }, "toast": { "error": "Failed to start debug replay: {{error}}", "alreadyActive": "A replay session is already active", diff --git a/web/src/components/overlay/DebugReplayDialog.tsx b/web/src/components/overlay/DebugReplayDialog.tsx index 504c8c1b14..bf66e03059 100644 --- a/web/src/components/overlay/DebugReplayDialog.tsx +++ b/web/src/components/overlay/DebugReplayDialog.tsx @@ -20,10 +20,14 @@ import { useNavigate } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { SelectSeparator } from "../ui/select"; import ActivityIndicator from "../indicators/activity-indicator"; -import { LuBug, LuPlay, LuX } from "react-icons/lu"; +import { LuBug, LuPlay, LuTriangleAlert, LuX } from "react-icons/lu"; +import useSWR from "swr"; import { ExportMode } from "@/types/filter"; import { TimeRange } from "@/types/timeline"; +import { DebugReplayStatus } from "@/types/replay"; import { cn } from "@/lib/utils"; +import { useCameraFriendlyName } from "@/hooks/use-camera-friendly-name"; +import { Alert, AlertDescription } from "../ui/alert"; import { CustomTimeSelector } from "./CustomTimeSelector"; const REPLAY_TIME_OPTIONS = ["1", "5", "timeline", "custom"] as const; @@ -56,6 +60,44 @@ export function DebugReplayContent({ }: DebugReplayContentProps) { const { t } = useTranslation(["views/replay"]); + // Only one replay session can run at a time, so check on open rather than + // letting the user configure a range only to hit a 409 on start + const { data: replayStatus, mutate: refreshReplayStatus } = + useSWR("debug_replay/status"); + const replayActive = replayStatus?.active ?? false; + const activeSourceCamera = useCameraFriendlyName( + replayStatus?.source_camera ?? undefined, + ); + const [isStopping, setIsStopping] = useState(false); + + const handleStopActive = useCallback(() => { + setIsStopping(true); + + axios + .post("debug_replay/stop") + .catch((error) => { + // 400 means no session is active, e.g. startup failed and the + // server already cleaned it up, so there is nothing to stop + if (error.response?.status === 400) { + return; + } + + const errorMessage = + error.response?.data?.message || + error.response?.data?.detail || + "Unknown error"; + toast.error(t("dialog.toast.stopError", { error: errorMessage }), { + position: "top-center", + }); + }) + // refresh on failure too, otherwise a session that ended on its own + // leaves the banner up and Start disabled until the dialog reopens + .then(() => refreshReplayStatus()) + .finally(() => { + setIsStopping(false); + }); + }, [refreshReplayStatus, t]); + return (
{isDesktop && ( @@ -68,6 +110,34 @@ export function DebugReplayContent({ )} + {replayActive && ( + + +
+ + {t("dialog.activeSession.description", { + camera: activeSourceCamera, + })} + + +
+
+ )} + {/* Time range */}