Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ the source conversation. New outgoing turns are always converted.

## Dataset catalog

Chat drafts stay in memory while you visit Datasets and return. Text, attachments,
system prompts, the selected target, and converter setup/results return to their
original attack and conversation. A loading, missing, or read-only destination
does not consume the draft. Starting a new attack or leaving the Chat/Datasets
journey discards the handoff; refreshing the browser does not retain it.

The **Datasets** page shows summaries of datasets already loaded into the backend's
memory. When memory is empty, **Learn how to load datasets into memory** opens the
[dataset-loading guide](https://microsoft.github.io/PyRIT/latest/code/datasets/loading-datasets/).
Expand Down
680 changes: 680 additions & 0 deletions frontend/src/App.drafts.test.tsx

Large diffs are not rendered by default.

193 changes: 186 additions & 7 deletions frontend/src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT license.
*/

import { Suspense } from "react";
import { Suspense, useEffect, useLayoutEffect, useRef, useState } from "react";
import { act, render, screen, fireEvent, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { MemoryRouter, useLocation, useNavigate } from "react-router";
Expand Down Expand Up @@ -182,9 +182,11 @@ jest.mock("./components/Layout/MainLayout", () => {

jest.mock("./components/Chat/ChatWindow", () => {
const { useLocation } = jest.requireActual("react-router") as typeof import("react-router");
const { useChatDraftHandoff } = jest.requireActual("./hooks/useChatDraftHandoff") as typeof import("./hooks/useChatDraftHandoff");
const MockChatWindow = ({
onNewAttack,
activeTarget,
draftContext,
attackResultId,
conversationId,
activeConversationId,
Expand All @@ -201,6 +203,7 @@ jest.mock("./components/Chat/ChatWindow", () => {
}: {
onNewAttack: () => void;
activeTarget: unknown;
draftContext?: Pick<import("./types").ChatDraftContextState, "originatingAttackId" | "originatingConversationId">;
attackResultId: string | null;
conversationId: string | null;
activeConversationId: string | null;
Expand All @@ -216,11 +219,73 @@ jest.mock("./components/Chat/ChatWindow", () => {
isLoadingAttack?: boolean;
}) => {
const location = useLocation();
const draftHandoff = useChatDraftHandoff();
const [composerText, setComposerText] = useState(() => draftHandoff.preservedDraft?.draft?.text ?? "none");
const activeTextRef = useRef(composerText);
useLayoutEffect(() => {
activeTextRef.current = composerText;
}, [composerText]);

const restoredRef = useRef(false);
useEffect(() => {
if (!restoredRef.current && draftHandoff.preservedDraft) {
restoredRef.current = true;
setComposerText(draftHandoff.preservedDraft.draft.text);
draftHandoff.consumePreservedDraft(attackResultId, activeConversationId ?? conversationId);
}
}, [draftHandoff, attackResultId, activeConversationId, conversationId]);

const preserveDraftRef = useRef(draftHandoff.preserveDraft);
useLayoutEffect(() => {
preserveDraftRef.current = draftHandoff.preserveDraft;
}, [draftHandoff.preserveDraft]);

const routeRef = useRef({
attackResultId,
convId: activeConversationId ?? conversationId,
path: `${location.pathname}${location.search}${location.hash}`,
});
useLayoutEffect(() => {
routeRef.current = {
attackResultId,
convId: activeConversationId ?? conversationId,
path: `${location.pathname}${location.search}${location.hash}`,
};
}, [attackResultId, activeConversationId, conversationId, location.pathname, location.search, location.hash]);

useEffect(() => {
return () => {
if (activeTextRef.current && activeTextRef.current !== "none") {
preserveDraftRef.current({
originatingAttackId: routeRef.current.attackResultId,
originatingConversationId: routeRef.current.convId,
originatingPath: routeRef.current.path,
draft: { text: activeTextRef.current, attachments: [] },
});
}
};
}, []);

if (attackResultId === "ar-123" && mockCreatedAttackRender) {
throw mockCreatedAttackRender;
}
return (
<div data-testid="chat-window">
<span data-testid="draft-attack-id">{draftContext?.originatingAttackId ?? "none"}</span>
<span data-testid="draft-conversation-id">{draftContext?.originatingConversationId ?? "none"}</span>
<span data-testid="preserved-draft-text">{composerText}</span>
<button
onClick={() => setComposerText("preserved prompt draft")}
data-testid="preserve-draft"
>
Preserve Draft
</button>
<button
onClick={() => setComposerText("none")}
data-testid="clear-draft"
>
Clear Draft
</button>
<span data-testid="attack-result-id">{attackResultId ?? "none"}</span>
<span data-testid="conversation-id">{conversationId ?? "none"}</span>
<span data-testid="active-conversation-id">{activeConversationId ?? "none"}</span>
Expand Down Expand Up @@ -411,7 +476,23 @@ jest.mock("./components/Home/Home", () => {
});

jest.mock("./components/Datasets/DatasetCatalog", () => {
const MockDatasetCatalog = () => <div data-testid="dataset-catalog" />;
const { useLocation, useNavigate } = jest.requireActual<typeof import("react-router")>("react-router");
const MockDatasetCatalog = () => {
const location = useLocation();
const navigate = useNavigate();
return (
<div data-testid="dataset-catalog">
<span data-testid="dataset-location">{`${location.pathname}${location.search}`}</span>
<span data-testid="dataset-state">{JSON.stringify(location.state)}</span>
<button
onClick={() => navigate("/datasets?filter=harmful", { state: { catalogPath: "/datasets?filter=harmful", cursors: ["cur-1"] } })}
data-testid="dataset-set-filter"
>
Set Dataset Filter
</button>
</div>
);
};
MockDatasetCatalog.displayName = "MockDatasetCatalog";
return {
__esModule: true,
Expand Down Expand Up @@ -1327,17 +1408,15 @@ describe("App", () => {
fireEvent.click(screen.getByTestId("nav-history"));
fireEvent.click(screen.getByTestId("open-attack-2")); // ar-attack-2

// BEFORE getAttack resolves: ChatWindow must NOT see the stale conv id
// alongside the new attack id. While attack B loads, its data is not yet
// ready, so both the attack id and conversation id are withheld — which
// gates ChatWindow's /messages fetch and prevents the cross-attack 400.
// The loading shell must not use attack A's active conversation.
expect(screen.getByTestId("main-layout")).toHaveAttribute(
"data-current-view",
"chat"
);
expect(screen.getByTestId("attack-result-id")).toHaveTextContent("none");
expect(screen.getByTestId("active-conversation-id")).toHaveTextContent("none");
expect(screen.getByTestId("conversation-id")).toHaveTextContent("none");
expect(screen.getByTestId("draft-attack-id")).toHaveTextContent("ar-attack-2");
expect(screen.getByTestId("draft-conversation-id")).toHaveTextContent("none");

// After getAttack resolves: the conv id belonging to attack B is committed.
resolveGetAttack({
Expand All @@ -1349,6 +1428,7 @@ describe("App", () => {
expect(screen.getByTestId("active-conversation-id")).toHaveTextContent("attack-conv-2")
);
expect(screen.getByTestId("conversation-id")).toHaveTextContent("attack-conv-2");
expect(screen.getByTestId("draft-conversation-id")).toHaveTextContent("attack-conv-2");
});

it("merges default labels from backend version API", async () => {
Expand Down Expand Up @@ -2377,4 +2457,103 @@ describe("App", () => {
expect(screen.getByTestId("active-target-name")).toHaveTextContent("none");
expect(mockListTargets).not.toHaveBeenCalled();
});

it("preserves unsaved chat draft across navigation to datasets and back", async () => {
const user = userEvent.setup();
renderApp("/chat");

await waitFor(() => expect(screen.getByTestId("chat-window")).toBeInTheDocument());
expect(screen.getByTestId("preserved-draft-text")).toHaveTextContent("none");

// Simulate preserving a draft in unsaved chat
await user.click(screen.getByTestId("preserve-draft"));
expect(screen.getByTestId("preserved-draft-text")).toHaveTextContent("preserved prompt draft");

// Navigate to Datasets via sidebar button
await user.click(screen.getByTestId("nav-datasets"));
await waitFor(() => expect(screen.getByTestId("dataset-catalog")).toBeInTheDocument());

// Navigate back to Chat via sidebar button
await user.click(screen.getByTestId("nav-chat"));
await waitFor(() => expect(screen.getByTestId("chat-window")).toBeInTheDocument());

// Draft and route are preserved
expect(screen.getByTestId("preserved-draft-text")).toHaveTextContent("preserved prompt draft");
expect(screen.getByTestId("attack-result-id")).toHaveTextContent("none");
});

it("preserves attack conversation origin and restores datasets filter location when navigating between chat and datasets", async () => {
mockGetAttack.mockResolvedValue({
attack_result_id: "ar-nav-1",
conversation_id: "conv-nav-1",
labels: {},
related_conversation_ids: [],
target: null,
});

const user = userEvent.setup();
renderApp("/attacks/ar-nav-1/conversations/conv-nav-1");

await waitFor(() => expect(screen.getByTestId("chat-window")).toBeInTheDocument());
expect(screen.getByTestId("route-location")).toHaveTextContent("/attacks/ar-nav-1/conversations/conv-nav-1");

// Preserve draft from persisted attack conversation
await user.click(screen.getByTestId("preserve-draft"));
expect(screen.getByTestId("preserved-draft-text")).toHaveTextContent("preserved prompt draft");

// Navigate to Datasets
await user.click(screen.getByTestId("nav-datasets"));
await waitFor(() => expect(screen.getByTestId("dataset-catalog")).toBeInTheDocument());

// Apply a dataset filter (modifies datasets location)
await user.click(screen.getByTestId("dataset-set-filter"));
await waitFor(() =>
expect(screen.getByTestId("dataset-location")).toHaveTextContent("/datasets?filter=harmful")
);

// Navigate back to Chat - should return to originating attack conversation
await user.click(screen.getByTestId("nav-chat"));
await waitFor(() => expect(screen.getByTestId("chat-window")).toBeInTheDocument());
expect(screen.getByTestId("route-location")).toHaveTextContent("/attacks/ar-nav-1/conversations/conv-nav-1");

// Navigate back to Datasets - should restore exact filtered location and state
await user.click(screen.getByTestId("nav-datasets"));
await waitFor(() => expect(screen.getByTestId("dataset-catalog")).toBeInTheDocument());
expect(screen.getByTestId("dataset-location")).toHaveTextContent("/datasets?filter=harmful");
expect(screen.getByTestId("dataset-state")).toHaveTextContent('{"catalogPath":"/datasets?filter=harmful","cursors":["cur-1"]}');
});

it("clearing draft text stays cleared after another round trip to datasets", async () => {
const user = userEvent.setup();
renderApp("/chat");

await waitFor(() => expect(screen.getByTestId("chat-window")).toBeInTheDocument());
expect(screen.getByTestId("preserved-draft-text")).toHaveTextContent("none");

// 1. User types prompt and preserves draft
await user.click(screen.getByTestId("preserve-draft"));
expect(screen.getByTestId("preserved-draft-text")).toHaveTextContent("preserved prompt draft");

// 2. User visits Datasets
await user.click(screen.getByTestId("nav-datasets"));
await waitFor(() => expect(screen.getByTestId("dataset-catalog")).toBeInTheDocument());

// 3. User returns to Chat: draft is restored and consumed
await user.click(screen.getByTestId("nav-chat"));
await waitFor(() => expect(screen.getByTestId("chat-window")).toBeInTheDocument());
expect(screen.getByTestId("preserved-draft-text")).toHaveTextContent("preserved prompt draft");

// 4. User clears all prompt text
await user.click(screen.getByTestId("clear-draft"));
expect(screen.getByTestId("preserved-draft-text")).toHaveTextContent("none");

// 5. User visits Datasets again
await user.click(screen.getByTestId("nav-datasets"));
await waitFor(() => expect(screen.getByTestId("dataset-catalog")).toBeInTheDocument());

// 6. User returns to Chat again: cleared prompt must NOT resurrect the old deleted text
await user.click(screen.getByTestId("nav-chat"));
await waitFor(() => expect(screen.getByTestId("chat-window")).toBeInTheDocument());
expect(screen.getByTestId("preserved-draft-text")).toHaveTextContent("none");
});
});
Loading
Loading