mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-07-23 12:31:54 +00:00
fix(ui): reset Lighthouse chat from sidebar
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
describe("Lighthouse page", () => {
|
||||
const currentDir = path.dirname(fileURLToPath(import.meta.url));
|
||||
const pagePath = path.join(currentDir, "page.tsx");
|
||||
const source = readFileSync(pagePath, "utf8");
|
||||
|
||||
it("keys the Cloud chat by the active route conversation", () => {
|
||||
// Given / When / Then
|
||||
expect(source).toContain(
|
||||
'const chatRouteKey = activeSessionId ?? initialPrompt ?? "new";',
|
||||
);
|
||||
expect(source).toContain("key={chatRouteKey}");
|
||||
});
|
||||
});
|
||||
@@ -66,11 +66,13 @@ export default async function AIChatbot({
|
||||
const initialMessages = activeSessionId
|
||||
? await getLighthouseV2Messages(activeSessionId)
|
||||
: { data: [] };
|
||||
const chatRouteKey = activeSessionId ?? initialPrompt ?? "new";
|
||||
|
||||
return (
|
||||
<div className="h-dvh min-h-0">
|
||||
<LighthouseV2NavigationModeSync />
|
||||
<LighthouseV2ChatPage
|
||||
key={chatRouteKey}
|
||||
configurations={configurations}
|
||||
modelsByProvider={modelsByProvider}
|
||||
initialSessionId={activeSessionId}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { render, screen, waitFor } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import type {
|
||||
LighthouseV2Configuration,
|
||||
@@ -105,6 +105,10 @@ describe("LighthouseV2ChatPage", () => {
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it("uses the neutral page background instead of the global app background token", () => {
|
||||
// Given / When
|
||||
const { container } = renderPage();
|
||||
@@ -148,6 +152,43 @@ describe("LighthouseV2ChatPage", () => {
|
||||
expect(createSessionMock).toHaveBeenCalledWith("Summarize findings");
|
||||
expect(pushMock).toHaveBeenCalledWith("/lighthouse?session=session-1");
|
||||
});
|
||||
|
||||
it("closes an active stream when the chat unmounts", async () => {
|
||||
// Given
|
||||
const user = userEvent.setup();
|
||||
const closeMock = vi.fn();
|
||||
const eventSourceMock = vi.fn(function MockEventSource(
|
||||
this: Record<string, unknown>,
|
||||
) {
|
||||
this.addEventListener = vi.fn();
|
||||
this.close = closeMock;
|
||||
});
|
||||
vi.stubGlobal("EventSource", eventSourceMock);
|
||||
sendMessageMock.mockResolvedValue({
|
||||
data: {
|
||||
task: {
|
||||
id: "task-1",
|
||||
name: "lighthouse-run",
|
||||
state: "executing",
|
||||
},
|
||||
streamUrl: "/api/stream",
|
||||
},
|
||||
});
|
||||
const { unmount } = renderPage({ initialSessionId: "session-1" });
|
||||
|
||||
// When
|
||||
await user.type(
|
||||
screen.getByRole("textbox", { name: "Message" }),
|
||||
["Summarize findings", "{Enter}"].join(""),
|
||||
);
|
||||
await waitFor(() =>
|
||||
expect(eventSourceMock).toHaveBeenCalledWith("/api/stream"),
|
||||
);
|
||||
unmount();
|
||||
|
||||
// Then
|
||||
expect(closeMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
function renderPage(
|
||||
|
||||
@@ -280,6 +280,10 @@ export function LighthouseV2ChatPage({
|
||||
}
|
||||
};
|
||||
|
||||
useMountEffect(() => {
|
||||
return () => closeStream();
|
||||
});
|
||||
|
||||
useMountEffect(() => {
|
||||
if (initialPrompt && !initialPromptSentRef.current) {
|
||||
initialPromptSentRef.current = true;
|
||||
|
||||
Reference in New Issue
Block a user