import { expect, it, vi } from "vitest"; import type { ChatCatalogContext } from "../chat-catalog-contract.js"; const port = vi.hoisted(() => ({ calls: [] as Array>, close: vi.fn(async () => {}), probe: vi.fn(async () => 200), missing: false, reply: "Morgan's sample-page test succeeded." })); vi.mock("./external-provider.js", () => ({ startNotesProvider: async () => ({ url: "http://127.0.0.1:492/", token: "synthetic-private-app492-credential", calls: port.calls, close: port.close }), connectNotes: async () => ({ connectionId: "connection", read: ["read"], write: ["write"] }) })); vi.mock("./identity-support.js", () => ({ identityWorker: async () => ({ id: "morgan" }), waitForReply: vi.fn(async () => {}), cleanupIdentityProbes: vi.fn(async () => {}), probeAgentPermission: port.probe })); vi.mock("./external-support.js", async original => ({ ...await original(), observeExternal: async (ctx: ChatCatalogContext, state: Record) => ({ scenarioId: "app-permissions.test-as-agent", origin: "https://fixture.test", companyId: "company", leadId: "ceo", state, tasks: [], projects: [], goals: [], comments: [], documents: [], revisions: [], activity: [], humanQueries: [], links: [], reply: port.reply, runs: state.sourceIssueId ? [{ id: "native", companyId: "company", agentId: "ceo", runtimeMode: "native", status: "succeeded", contextSnapshot: { issueId: "source", commentId: "human" } }] : [], phaseReplies: [], connections: [], profiles: [], secrets: [], endpoints: [], interactions: [], }) })); import { appTestExternalCases } from "./external-app-test.js"; const definition = appTestExternalCases[0]!; function fixture() { port.calls.splice(0); port.close.mockClear(); port.probe.mockClear(); port.missing = false; port.reply = "Morgan's sample-page test succeeded."; const metadata = { source: "test", headerSummary: { value: "synthetic-private-metadata-canary" }, execution: { request: { endpoint: "http://127.0.0.1:492/", mcpMethod: "tools/call", upstreamToolName: "launch_read", requestId: "paperclip-tool-owned" }, unknownToken: "synthetic-private-metadata-canary" } }; const get = vi.fn(async (path: string) => { if (path === "/api/auth/get-session") return { user: { id: "person" } }; if (path.includes("/profiles/effective/agents/")) return { agentId: path.split("/").at(-1), allowedTools: port.missing ? [] : [{ id: "read" }], profiles: [], entries: [], bindings: [], installedConnections: [{ id: "connection" }] }; if (path.endsWith("/runs/native/decisions")) return { runId: "native", decisions: [{ invocation: { id: "invocation", companyId: "company", actorType: "agent", actorId: "ceo", agentId: "morgan", issueId: "source", runId: "native", connectionId: "connection", catalogEntryId: "read", toolName: "qualified-read", status: "succeeded", policyDecision: "allow", errorCode: null, resultHash: "a".repeat(64), resultSizeBytes: 100, startedAt: "2026-11-03T00:00:01Z", completedAt: "2026-11-03T00:00:02Z" }, auditEvents: [{ id: "event", invocationId: "invocation", eventType: "call_completed", toolName: "qualified-read", metadata }] }] }; if (path.includes("/comments?")) return [{ id: "human", authorAgentId: null, authorUserId: "person", createdByRunId: null, body: "Check whether Morgan can read the sample page with this connection.", createdAt: "2026-11-03T00:00:00Z" }]; if (path.includes("/heartbeat-runs?")) return []; if (path === "/api/tool-connections/connection/test-agents/morgan/access") return { access: { connectionId: "connection", toolCount: 2, allowedCount: 1, askFirstCount: 0, offCount: 1, tools: [{ toolName: "launch_read", gatewayToolName: "qualified-read", risk: "read", decision: "allowed", reasonCode: "allow_profile", matchedPolicyIds: [] }, { toolName: "launch_update", gatewayToolName: "qualified-write", risk: "destructive", decision: "off", reasonCode: "deny_default", matchedPolicyIds: [] }] } }; throw Error("Unexpected owned read"); }); const api = { get, putSensitive: vi.fn(async () => {}), post: vi.fn(async () => {}), postSensitive: vi.fn(async () => { throw Error("No pre-query test is permitted"); }), patch: vi.fn(async () => {}), delete: vi.fn(async () => {}) }; const ctx = { input: { api, fixtures: { company: { id: "company", issuePrefix: "UNIT" }, agent: { id: "ceo" } } }, scenario: { query: "Check whether Morgan can read the sample page with this connection." }, issue: () => ({ id: "source" }), refreshIssue: vi.fn(async () => {}) } as unknown as ChatCatalogContext; return { ctx, api, metadata }; } it("actual setup preserves real read grants and never probes or pre-completes the requested test", async () => { const f = fixture(); let state: Record = {}; try { state = await definition.setup(f.ctx); expect(port.probe).not.toHaveBeenCalled(); expect(f.api.postSensitive).not.toHaveBeenCalled(); expect(state).toMatchObject({ providerCallsBeforeQuery: 0, initialCeoScope: { agentId: "ceo", allowed: ["read"] }, initialMorganScope: { agentId: "morgan", allowed: ["read"] } }); expect(f.api.post).toHaveBeenCalledTimes(1); expect(f.api.putSensitive).toHaveBeenCalledTimes(1); } finally { await definition.cleanup!(f.ctx, state); } expect(port.close).toHaveBeenCalledTimes(1); }); it("refuses a denied initial grant instead of supplying the outcome or granting more access", async () => { const f = fixture(); port.missing = true; await expect(definition.setup(f.ctx)).rejects.toThrow("scoped access must already allow"); expect(f.api.postSensitive).not.toHaveBeenCalled(); expect(f.api.post).not.toHaveBeenCalled(); expect(port.close).toHaveBeenCalledTimes(1); }); it("actual observer reads the canonical run's saved decision and retains only closed test provenance", async () => { const f = fixture(); let state: Record = {}; try { state = await definition.setup(f.ctx); port.calls.push({ method: "tools/call", name: "launch_read", authorized: true, arguments: { hidden: "synthetic-private-metadata-canary" }, responseHttpStatus: 200, receivedAt: 1, respondedAt: 2 }); await definition.observe(f.ctx, state); expect(f.api.get).toHaveBeenCalledWith("/api/companies/company/tools/runs/native/decisions"); expect(state).toMatchObject({ decisions: [{ runId: "native", invocation: { agentId: "morgan", actorId: "ceo" }, events: [{ source: "test", endpointMatches: true, method: "tools/call", upstreamToolName: "launch_read", requestIdSha256: expect.stringMatching(/^[a-f0-9]{64}$/) }] }], credentialDisclosed: false, morganRuntimeRunCount: 0 }); expect(JSON.stringify(state)).not.toContain("synthetic-private-metadata-canary"); expect(JSON.stringify(state)).not.toContain("synthetic-private-app492-credential"); expect(port.probe).not.toHaveBeenCalled(); expect(f.api.postSensitive).not.toHaveBeenCalled(); } finally { await definition.cleanup!(f.ctx, state); } }); it("actual observer leaves unknown audit source and request identity ineligible", async () => { const f = fixture(); let state: Record = {}; try { state = await definition.setup(f.ctx); f.metadata.source = "unrelated"; f.metadata.execution.request.requestId = "unknown request with spaces"; await definition.observe(f.ctx, state); expect(state).toMatchObject({ decisions: [{ events: [{ source: "other", requestIdSha256: null }] }] }); } finally { await definition.cleanup!(f.ctx, state); } }); it("actual observer flags an encoded credential disclosure without retaining its value", async () => { const f = fixture(); let state: Record = {}; try { state = await definition.setup(f.ctx); port.reply += Buffer.from("synthetic-private-app492-credential").toString("base64"); await definition.observe(f.ctx, state); expect(state.credentialDisclosed).toBe(true); expect(JSON.stringify(state)).not.toContain("synthetic-private-app492-credential"); } finally { await definition.cleanup!(f.ctx, state); } });