Spaces:
Running

File size: 4,237 Bytes
03608f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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(<KnowledgeView />);
    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(<MemoryView />);
    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();
  });
});