Files
PaperClipAI/ui/src/pages/AgentToolsTab.test.tsx
T
DottaandPaperclip f712cedbc2 feat(mcp) [split 7/8]: activate Apps and gateway UI (#9562)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Governed MCP access spans contracts, runtime enforcement, adapters,
UI surfaces, and operator verification
> - The parity reference PR #9534 is too large for effective automated
or human review
> - The feature therefore needs a linear stack whose individual diffs
stay below the 100-file review limit
> - This pull request is split 7/8 and focuses on Apps/Gateways UI and
feature-flagged activation
> - The benefit is a standalone, testable review boundary while
preserving byte-for-byte parity at the top of the stack

## Linked Issues or Issue Description

- Related parity reference: #9534
- Problem: The standalone UI foundation needs feature-flagged routes,
navigation, app detail flows, gateways, Storybook scenarios, and QA
configuration.
- Proposed solution: Adds Apps/Gateways pages and components,
navigation/route activation, remaining page integrations, Storybook
stories, and QA Vite configuration.
- Alternatives considered: keeping #9534 as one 403-file review, or
rewriting the feature to manufacture seams; both were rejected in favor
of path extraction plus compile-driven boundary moves.
- Roadmap alignment: this advances the existing governed MCP/tool-access
work already represented by #9534; it does not introduce a separate
roadmap initiative.
- Stack position: base branch is
`pap10341-split/06-ui-tools-foundation`.
- Merge policy: merge bottom-up, in order, only after the complete
eight-PR stack has been reviewed and the top-of-stack parity gate
remains empty.
- Requested review: UXDesigner sanity pass on Apps/Gateways flows and
flag-off behavior; Greptile on every PR.

## What Changed

- Adds Apps/Gateways pages and components, navigation/route activation,
remaining page integrations, Storybook stories, and QA Vite
configuration.
- Keeps this PR below 100 changed files and independently typecheckable.
- Preserves the final tree from #9534 when combined with the other seven
stack levels.

## Verification

- `pnpm typecheck`
- `pnpm check:token-gates` — all gates clean
- Focused UI Vitest run with `NODE_ENV=test` — 17 files, 147 tests
passed

## Risks

- Navigation or flag regressions could expose incomplete experiences;
activation remains controlled by existing experimental settings.
- Stack risk: merging out of order can expose incomplete layers;
mitigate by following the documented bottom-up merge policy.
- Parity risk: later edits to an intermediate branch can drift from
#9534; mitigate by re-running the empty top-of-stack diff before merge.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- OpenAI Codex, exact model ID `gpt-5.4`; runtime-managed context
window; medium reasoning with repository, shell, Git, GitHub CLI, and
code-execution tools enabled.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] Internal references are omitted except the execution-plan link
explicitly required for this coordinated split stack
- [x] My branch name describes the change and contains no internal
Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge


## Stack Coordination

- Internal execution plan:
[PAP-13874](/PAP/issues/PAP-13874#document-plan)
- Parity reference: #9534
- Stack: #9556 → #9557 → #9558 → #9559 → #9560 → #9561 → #9562 → #9563
- Merge bottom-up only after full-stack review and an empty parity diff
at #9563.


## UI Evidence

QA captured these from the live Garden MCP split stack at 1440px and
verified clean rendering:

![Apps
connections](https://raw.githubusercontent.com/paperclipai/paperclip/42512d030c96f678c379fbd97d8b7c1737d654fc/doc/screenshots/garden-mcp-split-stack/apps-list.png)

![Gateway
detail](https://raw.githubusercontent.com/paperclipai/paperclip/42512d030c96f678c379fbd97d8b7c1737d654fc/doc/screenshots/garden-mcp-split-stack/gateway-detail.png)

![BYO connect
wizard](https://raw.githubusercontent.com/paperclipai/paperclip/42512d030c96f678c379fbd97d8b7c1737d654fc/doc/screenshots/garden-mcp-split-stack/byo-connect.png)

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-07-14 15:40:08 -05:00

310 lines
10 KiB
TypeScript

// @vitest-environment jsdom
import { flushSync } from "react-dom";
import { createRoot } from "react-dom/client";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import type {
ToolCatalogEntry,
ToolPolicy,
ToolProfileEffectiveSummary,
} from "@paperclipai/shared";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createElement } from "react";
const mockToolsApi = vi.hoisted(() => ({
getEffectiveProfilesForAgent: vi.fn(),
listConnections: vi.fn(),
listPolicies: vi.fn(),
listCatalog: vi.fn(),
listAudit: vi.fn(),
putConnectionInstalls: vi.fn(),
}));
vi.mock("../api/tools", () => ({ toolsApi: mockToolsApi }));
// Render the company-aware Link as a plain anchor so we don't need a Router.
vi.mock("@/lib/router", () => ({
Link: ({ to, children, ...rest }: { to: string; children: unknown }) =>
createElement("a", { href: to, ...rest }, children as never),
}));
(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
async function act(callback: () => void | Promise<void>) {
let result: void | Promise<void> = undefined;
flushSync(() => {
result = callback();
});
await result;
}
async function flushReact(cycles = 4) {
// Multiple cycles let dependent query waves settle (connections → per-connection catalog).
for (let i = 0; i < cycles; i += 1) {
await act(async () => {
await Promise.resolve();
await new Promise((resolve) => window.setTimeout(resolve, 0));
});
}
}
function makeCatalogEntry(overrides: Partial<ToolCatalogEntry>): ToolCatalogEntry {
return {
id: "cat-1",
companyId: "company-1",
applicationId: "app-1",
connectionId: "conn-1",
entryKind: "tool",
toolName: "github.read_repo",
title: "Read repo",
description: null,
inputSchema: null,
outputSchema: null,
annotations: null,
riskLevel: "read",
isReadOnly: true,
isWrite: false,
isDestructive: false,
status: "active",
addedAt: new Date("2026-06-01T00:00:00Z"),
version: null,
schemaHash: null,
firstSeenAt: new Date("2026-06-01T00:00:00Z"),
lastSeenAt: new Date("2026-06-01T00:00:00Z"),
reviewedAt: null,
reviewedByAgentId: null,
reviewedByUserId: null,
createdAt: new Date("2026-06-01T00:00:00Z"),
updatedAt: new Date("2026-06-01T00:00:00Z"),
...overrides,
};
}
function makePolicy(overrides: Partial<ToolPolicy>): ToolPolicy {
return {
id: "pol-1",
companyId: "company-1",
name: "Require approval for writes",
description: "All write tools need board approval",
policyType: "require_approval",
priority: 100,
enabled: true,
selectors: {},
conditions: null,
config: null,
createdByAgentId: null,
createdByUserId: null,
createdAt: new Date("2026-06-01T00:00:00Z"),
updatedAt: new Date("2026-06-01T00:00:00Z"),
...overrides,
};
}
describe("AgentToolsTab", () => {
let container: HTMLDivElement;
let root: ReturnType<typeof createRoot>;
beforeEach(() => {
container = document.createElement("div");
document.body.appendChild(container);
mockToolsApi.getEffectiveProfilesForAgent.mockReset();
mockToolsApi.listConnections.mockReset();
mockToolsApi.listPolicies.mockReset();
mockToolsApi.listCatalog.mockReset();
mockToolsApi.putConnectionInstalls.mockReset();
mockToolsApi.putConnectionInstalls.mockResolvedValue({ connectionId: "conn-1", installs: [] });
});
afterEach(async () => {
await act(async () => {
root?.unmount();
});
container.remove();
vi.clearAllMocks();
});
async function renderTab() {
const { AgentToolsTab } = await import("./AgentToolsTab");
const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
const agent = { id: "agent-1", name: "Coder" } as never;
await act(async () => {
root = createRoot(container);
root.render(
createElement(
QueryClientProvider,
{ client },
createElement(AgentToolsTab, { agent, companyId: "company-1" }),
),
);
});
await flushReact();
}
it("preserves checkbox changes made after the last saved install state", async () => {
const { mergeInstallDraft } = await import("./AgentToolsTab");
expect(
mergeInstallDraft(
{ "conn-1": true, "conn-2": false },
{ "conn-1": true, "conn-2": true },
{ "conn-1": false, "conn-2": false },
),
).toEqual({
draft: { "conn-1": true, "conn-2": true },
hasPendingChanges: true,
});
});
it("renders effective access, access profiles, governing policy, and unavailable tools", async () => {
const allowed = makeCatalogEntry({ id: "cat-allow", toolName: "github.read_repo" });
const denied = makeCatalogEntry({
id: "cat-deny",
toolName: "github.delete_repo",
riskLevel: "critical",
isReadOnly: false,
isWrite: true,
isDestructive: true,
});
mockToolsApi.getEffectiveProfilesForAgent.mockResolvedValue({
agentId: "agent-1",
profiles: [
{
id: "prof-1",
companyId: "company-1",
profileKey: "github-safe",
name: "GitHub safe",
description: null,
status: "active",
defaultAction: "deny",
newToolsReviewedAt: null,
metadata: null,
createdAt: new Date(),
updatedAt: new Date(),
entries: [],
bindings: [],
summary: {
accessMode: "selected",
allowedToolCount: 1,
allowedApplicationCount: 1,
excludedToolCount: 0,
totalToolCount: 1,
assignmentCount: 1,
appliesToAgentCount: 1,
isCompanyDefault: true,
},
},
],
entries: [],
bindings: [
{
id: "bind-1",
companyId: "company-1",
profileId: "prof-1",
targetType: "agent",
targetId: "agent-1",
priority: 100,
metadata: null,
createdByAgentId: null,
createdByUserId: null,
createdAt: new Date(),
updatedAt: new Date(),
},
],
allowedTools: [allowed],
allowedToolNames: ["github.read_repo"],
installedConnections: [],
} satisfies ToolProfileEffectiveSummary);
mockToolsApi.listConnections.mockResolvedValue({
connections: [{ id: "conn-1", name: "Production GitHub" }],
});
mockToolsApi.listPolicies.mockResolvedValue({
policies: [
makePolicy({ id: "pol-1", name: "Require approval for writes" }),
makePolicy({
id: "pol-2",
name: "Block other agent",
enabled: true,
selectors: { agentId: "someone-else" },
}),
],
});
mockToolsApi.listCatalog.mockResolvedValue({ catalog: [allowed, denied] });
await renderTab();
const text = container.textContent ?? "";
expect(text).toContain("Effective access");
expect(text).toContain("github.read_repo");
expect(text).toContain("Production GitHub");
expect(text).toContain("GitHub safe");
expect(text).toContain("Access profiles");
expect(text).toContain("Company default");
expect(container.querySelector('a[href="/apps/advanced/profiles/prof-1"]')?.textContent).toBe("GitHub safe");
expect(container.querySelector('a[href="/apps/advanced/profiles?check=1"]')?.textContent).toBe("Check access");
// Governing policy #1 is the company-wide require_approval rule.
expect(text).toContain("#1 Require approval for writes");
// The policy that targets a different agent must NOT appear.
expect(text).not.toContain("Block other agent");
// Unavailable tool surfaced from the full tool list minus allowed.
expect(text).toContain("github.delete_repo");
});
it("shows the empty allow-list message when no profile applies", async () => {
mockToolsApi.getEffectiveProfilesForAgent.mockResolvedValue({
agentId: "agent-1",
profiles: [],
entries: [],
bindings: [],
allowedTools: [],
allowedToolNames: [],
installedConnections: [],
} satisfies ToolProfileEffectiveSummary);
mockToolsApi.listConnections.mockResolvedValue({ connections: [] });
mockToolsApi.listPolicies.mockResolvedValue({ policies: [] });
await renderTab();
const text = container.textContent ?? "";
expect(text).toContain("No tools are allowed for this agent");
expect(text).toContain("No active profile applies");
});
it("autosaves installed apps for the current agent", async () => {
const allowed = makeCatalogEntry({ id: "cat-allow", toolName: "github.read_repo" });
mockToolsApi.getEffectiveProfilesForAgent.mockResolvedValue({
agentId: "agent-1",
profiles: [],
entries: [],
bindings: [],
allowedTools: [allowed],
allowedToolNames: ["github.read_repo"],
installedConnections: [],
} satisfies ToolProfileEffectiveSummary);
mockToolsApi.listConnections.mockResolvedValue({
connections: [{ id: "conn-1", companyId: "company-1", name: "Production GitHub", installs: [] }],
});
mockToolsApi.listPolicies.mockResolvedValue({ policies: [] });
mockToolsApi.listCatalog.mockResolvedValue({ catalog: [allowed] });
await renderTab();
expect(container.textContent).toContain("Installed apps");
expect(container.textContent).toContain("Permitted only");
expect(container.textContent).toContain("Permitted but not installed — tools will not appear in runs.");
expect(container.querySelector('a[href="/apps/conn-1/permissions"]')?.textContent).toBe("Open permissions");
const installCheckbox = container.querySelector<HTMLElement>('[aria-label="Install Production GitHub on Coder"]');
expect(installCheckbox).toBeTruthy();
await act(async () => {
installCheckbox!.dispatchEvent(new MouseEvent("click", { bubbles: true }));
await new Promise((resolve) => window.setTimeout(resolve, 300));
});
await flushReact();
expect(mockToolsApi.putConnectionInstalls).toHaveBeenCalledWith("conn-1", [
{ targetType: "agent", targetId: "agent-1" },
]);
});
});