|
| 1 | +import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import { configure, fireEvent, render, screen, waitFor } from "@testing-library/react"; |
| 3 | + |
| 4 | +import GraphPage from "@/app/admin/graph/page"; |
| 5 | +import DashboardPage from "@/app/admin/dashboard/page"; |
| 6 | +import AgentsPage from "@/app/panel/agents/page"; |
| 7 | + |
| 8 | +configure({ testIdAttribute: "data-test" }); |
| 9 | + |
| 10 | +afterEach(() => { |
| 11 | + vi.unstubAllGlobals(); |
| 12 | + vi.restoreAllMocks(); |
| 13 | +}); |
| 14 | + |
| 15 | +// ── Finding #2: graph NL→Cypher sent the wrong request key ─────────────────── |
| 16 | +// Frontend POSTed { question } but the backend NLQueryRequest requires `intent`, |
| 17 | +// so every "Cypher üret" click returned HTTP 422 and the feature never worked. |
| 18 | +describe("Graph NL→Cypher — request key matches backend contract", () => { |
| 19 | + it("POSTs { intent } (not { question }) to /v1/graph/nl-query", async () => { |
| 20 | + const cap: { body?: string } = {}; |
| 21 | + vi.stubGlobal( |
| 22 | + "fetch", |
| 23 | + vi.fn((url: string | URL | Request, init?: RequestInit) => { |
| 24 | + const u = String(url); |
| 25 | + if (u.includes("/v1/graph/nl-query")) { |
| 26 | + cap.body = String(init?.body ?? ""); |
| 27 | + return Promise.resolve( |
| 28 | + new Response(JSON.stringify({ cypher: "MATCH (n) RETURN n LIMIT 1" }), { |
| 29 | + status: 200, |
| 30 | + }), |
| 31 | + ); |
| 32 | + } |
| 33 | + // schema fetch on mount |
| 34 | + return Promise.resolve( |
| 35 | + new Response(JSON.stringify({ node_labels: [], relationship_types: [] }), { |
| 36 | + status: 200, |
| 37 | + }), |
| 38 | + ); |
| 39 | + }), |
| 40 | + ); |
| 41 | + |
| 42 | + render(<GraphPage />); |
| 43 | + fireEvent.change(await screen.findByTestId("graph-nl-input"), { |
| 44 | + target: { value: "Acme çalışanları" }, |
| 45 | + }); |
| 46 | + fireEvent.click(screen.getByTestId("graph-nl-run")); |
| 47 | + |
| 48 | + await waitFor(() => expect(cap.body).toBeTruthy()); |
| 49 | + const parsed = JSON.parse(cap.body!); |
| 50 | + expect(parsed.intent).toBe("Acme çalışanları"); |
| 51 | + expect(parsed.question).toBeUndefined(); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +// ── Finding #3: tamper warning was dead code ───────────────────────────────── |
| 56 | +// Backend emits audit_chain_integrity as the string "ok" | "tampered"; the panel |
| 57 | +// tested `=== false`, which a string can never satisfy, so the warning never fired. |
| 58 | +describe("Dashboard — audit chain tamper warning fires on a string status", () => { |
| 59 | + function mockDashboard(integrity: "ok" | "tampered") { |
| 60 | + vi.stubGlobal( |
| 61 | + "fetch", |
| 62 | + vi.fn(() => |
| 63 | + Promise.resolve( |
| 64 | + new Response( |
| 65 | + JSON.stringify({ vault: { total_entries: 5, audit_chain_integrity: integrity } }), |
| 66 | + { status: 200 }, |
| 67 | + ), |
| 68 | + ), |
| 69 | + ), |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + it("shows the tamper warning when integrity is 'tampered'", async () => { |
| 74 | + mockDashboard("tampered"); |
| 75 | + render(<DashboardPage />); |
| 76 | + expect(await screen.findByText(/Zincir bütünlüğü bozuk/i)).toBeTruthy(); |
| 77 | + }); |
| 78 | + |
| 79 | + it("does NOT show the tamper warning when integrity is 'ok'", async () => { |
| 80 | + mockDashboard("ok"); |
| 81 | + render(<DashboardPage />); |
| 82 | + // Let the fetch resolve + state settle. |
| 83 | + await screen.findByText(/Vault audit/i); |
| 84 | + await new Promise((r) => setTimeout(r, 20)); |
| 85 | + expect(screen.queryByText(/Zincir bütünlüğü bozuk/i)).toBeNull(); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +// ── Finding #1: fabricated "100%" stat ─────────────────────────────────────── |
| 90 | +// "Structured Output: 100%" sat in the same styled stat grid as real, fetched |
| 91 | +// counts, reading as a measured per-tenant compliance rate. It is a design |
| 92 | +// invariant, not a measurement — reframed so it no longer masquerades as live. |
| 93 | +describe("Agent Registry — structured-output stat is honest, not a fake metric", () => { |
| 94 | + beforeEach(() => { |
| 95 | + vi.stubGlobal( |
| 96 | + "fetch", |
| 97 | + vi.fn(() => |
| 98 | + Promise.resolve( |
| 99 | + new Response( |
| 100 | + JSON.stringify({ total: 12, approval_gated: 4, categories: [] }), |
| 101 | + { status: 200 }, |
| 102 | + ), |
| 103 | + ), |
| 104 | + ), |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + it("does not render a fabricated 100% measurement", async () => { |
| 109 | + render(<AgentsPage />); |
| 110 | + await screen.findByText(/Structured Output/i); |
| 111 | + expect(screen.queryByText("100%")).toBeNull(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments