import { screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, it, vi } from "vitest";
import { KnowledgeView } from "@/components/knowledge/knowledge-view";
import { MemoryView } from "@/components/knowledge/memory-view";
import { setAccessToken } from "@/lib/session";
import { mockFetch } from "@/test/fixtures";
import { renderUi } from "@/test/render";
vi.mock("next/navigation", () => ({
useRouter: () => ({ push: vi.fn(), replace: vi.fn() }),
usePathname: () => "/knowledge",
}));
afterEach(() => {
vi.unstubAllGlobals();
setAccessToken(null);
});
const DOC = {
id: "d1",
title: "Returns Policy",
source_type: "markdown",
source_uri: null,
size_bytes: 900,
page_count: null,
chunk_count: 3,
status: "ready",
flagged_chunks: 1,
created_by: "user:1",
created_at: "2026-12-05T10:00:00Z",
};
const HIT = {
chunk_id: "c1aaaaaa-0000-0000-0000-000000000000",
doc_id: "d1",
title: "Returns Policy",
page: null,
ordinal: 1,
text: "If your item arrives damaged or defective, contact us within 7 days.",
score: 0.61,
flags: [],
source_uri: null,
citation: "Returns Policy (chunk c1aaaaaa)",
};
describe("KnowledgeView", () => {
it("lists documents, flags injected passages and shows search hits with citations", async () => {
const { calls } = mockFetch({
"GET /knowledge/docs": () => [DOC],
"GET /knowledge/search": () => ({ untrusted: true, hits: [HIT] }),
"POST /knowledge/docs": () => DOC,
});
renderUi();
const docs = await screen.findByTestId("knowledge-docs");
expect(within(docs).getByText("Returns Policy")).toBeInTheDocument();
expect(within(docs).getByText("1 flagged")).toBeInTheDocument();
await userEvent.type(screen.getByLabelText("Search"), "return policy damaged");
await userEvent.click(screen.getByTestId("knowledge-search"));
const hits = await screen.findByTestId("knowledge-hits");
expect(within(hits).getByTestId("citation")).toHaveTextContent("Returns Policy (chunk c1aaaaaa)");
expect(within(hits).getByText("Untrusted text")).toBeInTheDocument();
await waitFor(() => expect(screen.getByTestId("knowledge-upload")).toBeInTheDocument());
await userEvent.type(screen.getByLabelText("Or paste text"), "Exchanges within 14 days.");
await userEvent.click(screen.getByTestId("knowledge-add"));
await waitFor(() =>
expect(calls.some((c) => c.method === "POST" && c.path === "/knowledge/docs")).toBe(true),
);
const post = calls.find((c) => c.method === "POST" && c.path === "/knowledge/docs");
expect(post?.body).toMatchObject({ text: "Exchanges within 14 days." });
});
});
describe("MemoryView", () => {
it("shows remembered items and hides tampered content", async () => {
mockFetch({
"GET /memory/items": () => [
{
id: "m1",
namespace: ["tenant", "t1", "cases", "support"],
key: "run:1",
kind: "case",
value: { category: "inquiry", resolution: "answer drafted" },
integrity_ok: true,
size_bytes: 80,
written_by: "agent:1",
created_at: "2026-12-05T10:00:00Z",
updated_at: "2026-12-05T10:00:00Z",
expires_at: "2027-06-03T10:00:00Z",
},
{
id: "m2",
namespace: ["tenant", "t1", "customer", "c9"],
key: "pref",
kind: "customer",
value: null,
integrity_ok: false,
size_bytes: 40,
written_by: "user:1",
created_at: "2026-12-05T10:00:00Z",
updated_at: "2026-12-05T10:00:00Z",
expires_at: "2027-06-03T10:00:00Z",
},
],
});
renderUi();
const table = await screen.findByTestId("memory-table");
expect(within(table).getByText(/resolution: answer drafted/)).toBeInTheDocument();
expect(within(table).getByText(/Integrity check failed/)).toBeInTheDocument();
expect(within(table).getByText("Resolved cases")).toBeInTheDocument();
});
});