Files
PaperClipAI/ui/src/features/connections/ConnectionSetupFlow.oauth.test.ts
T
DottaandClaude Opus 5.5 53fedd653b fix(connections): split GitHub Copilot into its own connector (#22238)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - The Connectors catalog lets people give agents tools and AI
accounts.
> - GitHub showed repository access and GitHub Copilot authentication on
one card.
> - The card had an extra "Connect Copilot" button. Saved Copilot
accounts showed in the GitHub account list.
> - These are two different products. People must find each one by its
own name.
> - This pull request makes GitHub Copilot a separate connector. GitHub
keeps repository and MCP access only.
> - The benefit is a clear catalog. Each card opens its own setup, and
saved accounts show on the correct card.

## Linked Issues or Issue Description

**What existing behavior does this improve?**

Discovery and setup of the GitHub and GitHub Copilot connectors.

**Current behavior**

The GitHub card says "Connect GitHub repository access or authenticate
GitHub Copilot agents." It shows a "Connect Copilot" button next to its
own Connect button. Saved Copilot accounts show in the GitHub account
list. Agent connection search returns Copilot as a method of GitHub. The
Copilot setup link that search gives to agents opens the GitHub tool
setup.

**Proposed behavior**

GitHub and GitHub Copilot are two cards. GitHub says "Give agents access
to GitHub repositories, issues, and pull requests." GitHub Copilot has
its own logo and opens Copilot token setup. Saved Copilot accounts,
including accounts saved before this change, show on the GitHub Copilot
card.

**Reason and benefit**

People do not have to know that Copilot is behind the GitHub card. The
catalog names the two products. The Copilot runtime keeps the same
provider identity, token storage, grants, and model discovery.

**Additional context**

This follows the same pattern as the GitHub Code Review Bot split in
#14750. Copilot setup was added in #15705.

## What Changed

- Add a `github-copilot` app definition with the existing
`copilot-token` method. Remove that method from `github`. Restore the
GitHub description and the `developer` category.
- Add the GitHub Octicons Copilot mark (MIT license, from
`primer/octicons`) as light and dark brand assets.
- Map the `github` AI provider to the `github-copilot` catalog entry in
`aiConnectionCatalogSlug`. New Copilot accounts save under a "GitHub
Copilot" application.
- Remove the "Connect Copilot" button from the GitHub card.
- Show saved AI accounts on the card for their provider. This includes
Copilot accounts that were saved under the GitHub application. The
connection page and its reconnect link use the same rule.
- Redirect old `/apps/connect?source=github&method=copilot-token` links
to `source=github-copilot`. Keep the other query values.
- Keep the "My GitHub account" identity wording on the Copilot access
step.
- In connection search and connection requests, file AI accounts under
their catalog entry. Accept the `github` provider for the
`github-copilot` service. Copilot authentication recovery cards now open
GitHub Copilot setup, not GitHub tool setup.
- Keep older Copilot accounts and requests working. An older account
saved under the GitHub application reconnects through GitHub Copilot,
and an older Copilot draft resumes through GitHub Copilot. A recovery
request saved with the `github` service name can complete. A Copilot
request declined before this change stays declined in its continuation.
Adoption accepts the `github` provider binding for a `github-copilot`
request.
- Move the rule that finds an AI account's catalog entry
(`aiConnectionSourceSlug`) into `@paperclipai/shared`, so the UI and
server use one copy.
- Use the Copilot logo for Copilot in the AI account picker and account
details.
- Update the GitHub Copilot connection doc and the GitHub connections
Storybook story. The GitHub and GitHub Copilot setup stories open
**Change** before checking "Connect GitHub as", because that heading
sits in the collapsed access section.

## Verification

- New and updated tests fail on `master` and pass with this change:
  - `pnpm exec vitest run packages/shared/src/app-definitions.test.ts`
- `pnpm exec vitest run ui/src/pages/apps/Browse.test.tsx
ui/src/pages/apps/AppsConnect.test.tsx
ui/src/pages/apps/AppDetail.test.tsx
ui/src/pages/apps/app-connect-policy.test.ts
ui/src/pages/apps/Connections.test.tsx`
- `pnpm exec vitest run
server/src/__tests__/connection-intents-service.test.ts
server/src/__tests__/ai-connections.test.ts`
- `pnpm exec vitest run
ui/src/features/connections/ConnectionSetupFlow.oauth.test.ts`
- Older-account tests cover reconnect, draft resume, completion of a
request saved with `github`, a request declined before the split, and
Copilot adoption. Each test fails without its fix.
- The play functions of all 8 stories in
`github-connections.stories.tsx` pass when rendered with
`composeStories` in jsdom.
- Brand checks: `node scripts/check-app-brand-assets.mjs`,
`ui/src/lib/app-brand-assets.test.ts`, and
`ui/src/pages/apps/AppLogo.brand-assets.test.tsx`.
- Typecheck: `@paperclipai/shared`, `@paperclipai/ui`, and
`@paperclipai/server`.
- Manual check in `pnpm dev:once` with a local company:
1. Open Connectors and search for "GitHub". Two cards show: "GitHub" and
"GitHub Copilot". GitHub has no "Connect Copilot" button.
2. Seed one GitHub tool account and one Copilot account under the GitHub
application, as older installs have them. The GitHub tool account shows
on GitHub. The Copilot account shows on GitHub Copilot.
3. Click Connect on GitHub Copilot. The "Connect GitHub Copilot" page
opens with the Copilot logo and the token field.
4. Open `/apps/connect?source=github&method=copilot-token`. The page
redirects to `source=github-copilot`.
  5. Open GitHub setup. It offers repository access only.
  6. Check the catalog in light and dark themes.

## Risks

- Low risk. There is no database migration. Existing Copilot accounts
keep their application and stored source. The UI and connection search
group them by their AI provider.
- New Copilot accounts save under a new "GitHub Copilot" application and
record `github-copilot` as their source. Code that finds GitHub
repository identities by `sourceTemplateKey === "github"` no longer sees
new Copilot accounts. These are AI accounts, so this is the intended
separation.
- A pending Copilot recovery card from before this change keeps the
`github` service slug. The server still resolves it to GitHub Copilot,
and the card can complete.
- Search for "github" now returns two results: GitHub and GitHub
Copilot.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`) through Claude Code, with tool use
(shell, file edits, test runs, and a headless browser for the manual
check).

## 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] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
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
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 08:51:36 -06:00

408 lines
14 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// @vitest-environment jsdom
import { describe, expect, it } from "vitest";
import type { ToolApplication, ToolConnection } from "@paperclipai/shared";
import { getConnectableAppDefinition } from "@paperclipai/shared";
import {
isConnectionDefinitionUnavailable,
isVercelConnectUnavailable,
isMissingOAuthStateMessage,
oauthFailureMessage,
readConnectionIntentGitHubRecovery,
readConnectionIntentOAuthFailureCode,
readConnectionIntentOAuthOutcome,
retainedResumeMatches,
retainedReconnectMatches,
requestedConnectionSetupResolution,
requestedConnectionEntry,
} from "./ConnectionSetupFlow";
const origin = "https://paperclip.test";
const interactionId = "interaction-123";
function event(data: unknown, eventOrigin = origin) {
return { origin: eventOrigin, data };
}
describe("connection intent OAuth window messages", () => {
it.each(["connected", "declined", "failed"] as const)(
"accepts a matching %s outcome",
(outcome) => {
expect(
readConnectionIntentOAuthOutcome(
event({
type: "paperclip.connection-intent.oauth",
interactionId,
outcome,
}),
origin,
interactionId,
),
).toBe(outcome);
},
);
it.each([
[
"foreign origin",
event(
{
type: "paperclip.connection-intent.oauth",
interactionId,
outcome: "connected",
},
"https://attacker.test",
),
],
[
"wrong interaction",
event({
type: "paperclip.connection-intent.oauth",
interactionId: "other",
outcome: "connected",
}),
],
[
"wrong message type",
event({ type: "other", interactionId, outcome: "connected" }),
],
[
"unknown outcome",
event({
type: "paperclip.connection-intent.oauth",
interactionId,
outcome: "authorized",
}),
],
["non-object payload", event("connected")],
])("ignores a %s message", (_label, candidate) => {
expect(
readConnectionIntentOAuthOutcome(candidate, origin, interactionId),
).toBeNull();
});
});
describe("retained reconnect definition lookup", () => {
const connection = { applicationId: "app-1" } as ToolConnection;
it("restores a hidden provider only for its exact retained application", () => {
const githubApplication = {
id: "app-1",
applicationKey: "github",
metadata: { sourceTemplateKey: "github" },
} as unknown as ToolApplication;
expect(requestedConnectionEntry({
requestedAppKey: "github",
galleryApps: [],
reconnectConnection: connection,
resumeConnection: null,
resumeApplication: null,
completedResumeConnectionId: null,
applications: [githubApplication],
})?.slug).toBe("github");
expect(requestedConnectionEntry({
requestedAppKey: "notion",
galleryApps: [],
reconnectConnection: connection,
resumeConnection: null,
resumeApplication: null,
completedResumeConnectionId: null,
applications: [githubApplication],
})).toBeNull();
});
it("ends unavailable retained reconnects instead of leaving them loading", () => {
expect(requestedConnectionSetupResolution({
reconnectConnectionId: "connection-1",
hasRequestedEntry: false,
supportedMethodCount: 0,
unsupportedOAuth: false,
vercelUnavailable: false,
definitionUnavailable: false,
})).toBe("reconnect_unavailable");
expect(requestedConnectionSetupResolution({
reconnectConnectionId: "connection-1",
hasRequestedEntry: true,
supportedMethodCount: 0,
unsupportedOAuth: false,
vercelUnavailable: false,
definitionUnavailable: false,
})).toBe("reconnect_unavailable");
expect(requestedConnectionSetupResolution({
reconnectConnectionId: null,
hasRequestedEntry: false,
supportedMethodCount: 0,
unsupportedOAuth: false,
vercelUnavailable: false,
definitionUnavailable: false,
})).toBe("fallback");
expect(requestedConnectionSetupResolution({
reconnectConnectionId: "connection-1",
hasRequestedEntry: true,
supportedMethodCount: 1,
unsupportedOAuth: false,
vercelUnavailable: false,
definitionUnavailable: false,
})).toBe("ready");
});
it("does not expose a hidden provider without an exact reconnect target", () => {
expect(requestedConnectionEntry({
requestedAppKey: "github",
galleryApps: [],
reconnectConnection: null,
resumeConnection: null,
resumeApplication: null,
completedResumeConnectionId: null,
applications: [],
})).toBeNull();
const visibleNotion = getConnectableAppDefinition("notion")!;
expect(requestedConnectionEntry({
requestedAppKey: "notion",
galleryApps: [visibleNotion],
reconnectConnection: null,
resumeConnection: null,
resumeApplication: null,
completedResumeConnectionId: null,
applications: [],
})).toBe(visibleNotion);
});
it.each([
["draft", true],
["active", true],
["disabled", false],
["archived", false],
] as const)("allows retained resume status %s only when appropriate", (status, expected) => {
const resumeConnection = {
id: "connection-1",
companyId: "company-1",
applicationId: "app-clickup",
status,
config: { sourceTemplateKey: "clickup" },
} as unknown as ToolConnection;
const resumeApplication = {
id: "app-clickup",
companyId: "company-1",
metadata: { sourceTemplateKey: "clickup" },
} as unknown as ToolApplication;
expect(retainedResumeMatches({
requestedAppKey: "clickup",
resumeConnection,
resumeApplication,
completedResumeConnectionId: "connection-1",
})).toBe(expected);
});
it("resumes an older Copilot draft saved under the GitHub application through GitHub Copilot", () => {
const github = {
id: "app-github",
companyId: "company-1",
applicationKey: "app-gallery:github",
metadata: { sourceTemplateKey: "github" },
} as unknown as ToolApplication;
const copilotDraft = {
id: "connection-1",
companyId: "company-1",
applicationId: "app-github",
status: "draft",
connectionPurpose: "ai",
config: { sourceTemplateKey: "github", ai: { provider: "github", method: "api_key" } },
} as unknown as ToolConnection;
const githubDraft = {
id: "connection-2",
companyId: "company-1",
applicationId: "app-github",
status: "draft",
config: { sourceTemplateKey: "github" },
} as unknown as ToolConnection;
const resume = (requestedAppKey: string, resumeConnection: ToolConnection) => retainedResumeMatches({
requestedAppKey,
resumeConnection,
resumeApplication: github,
completedResumeConnectionId: null,
});
expect(resume("github-copilot", copilotDraft)).toBe(true);
expect(resume("github", copilotDraft)).toBe(false);
expect(resume("github-copilot", githubDraft)).toBe(false);
expect(resume("github", githubDraft)).toBe(true);
});
it("does not apply fresh-setup availability to an exact retained reconnect", () => {
expect(isVercelConnectUnavailable({
credentialSource: "vercel_connect",
available: false,
retainedReconnectMatches: true,
})).toBe(false);
expect(isVercelConnectUnavailable({
credentialSource: "vercel_connect",
available: false,
retainedReconnectMatches: false,
})).toBe(true);
expect(isConnectionDefinitionUnavailable({
available: false,
reconnectConnectionId: "connection-1",
reconnectSourceMatches: true,
})).toBe(false);
expect(isConnectionDefinitionUnavailable({
available: false,
reconnectConnectionId: "connection-1",
reconnectSourceMatches: false,
})).toBe(true);
expect(isConnectionDefinitionUnavailable({
available: false,
reconnectConnectionId: undefined,
reconnectSourceMatches: true,
})).toBe(true);
});
});
describe("retained reconnect target matching", () => {
const connection = { applicationId: "app-1" } as ToolConnection;
const application = { id: "app-1", applicationKey: "custom-mcp" } as ToolApplication;
it("accepts a generic reconnect only for its exact retained application", () => {
expect(retainedReconnectMatches({
requestedAppKey: undefined,
byo: true,
applicationId: "app-1",
reconnectConnection: connection,
reconnectApplication: application,
})).toBe(true);
expect(retainedReconnectMatches({
requestedAppKey: undefined,
byo: true,
applicationId: "app-other",
reconnectConnection: connection,
reconnectApplication: application,
})).toBe(false);
});
it("keeps curated reconnects bound to their provider", () => {
const curated = {
id: "app-1",
applicationKey: "github",
metadata: { sourceTemplateKey: "github" },
} as unknown as ToolApplication;
expect(retainedReconnectMatches({
requestedAppKey: "github",
byo: false,
applicationId: "app-1",
reconnectConnection: connection,
reconnectApplication: curated,
})).toBe(true);
expect(retainedReconnectMatches({
requestedAppKey: "notion",
byo: false,
applicationId: "app-1",
reconnectConnection: connection,
reconnectApplication: curated,
})).toBe(false);
});
it("reconnects older Copilot accounts saved under the GitHub application through GitHub Copilot", () => {
const github = {
id: "app-1",
applicationKey: "app-gallery:github",
metadata: { sourceTemplateKey: "github" },
} as unknown as ToolApplication;
const copilot = {
applicationId: "app-1",
connectionPurpose: "ai",
config: { sourceTemplateKey: "github", ai: { provider: "github", method: "api_key" } },
} as unknown as ToolConnection;
expect(retainedReconnectMatches({
requestedAppKey: "github-copilot",
byo: false,
applicationId: undefined,
reconnectConnection: copilot,
reconnectApplication: github,
})).toBe(true);
expect(retainedReconnectMatches({
requestedAppKey: "github-copilot",
byo: false,
applicationId: undefined,
reconnectConnection: connection,
reconnectApplication: github,
})).toBe(false);
});
});
describe("connection intent OAuth failure reasons", () => {
it("explains an expired sign-in instead of a generic failure", () => {
const code = readConnectionIntentOAuthFailureCode({
type: "paperclip.connection-intent.oauth",
interactionId,
outcome: "failed",
code: "oauth_state_expired",
});
expect(code).toBe("oauth_state_expired");
expect(oauthFailureMessage(code)).toMatch(/took too long/);
});
it("explains a sign-in finished in another Paperclip session", () => {
expect(oauthFailureMessage("oauth_session_mismatch")).toMatch(/different Paperclip session/);
});
it("ignores malformed codes and never shows a raw code", () => {
expect(readConnectionIntentOAuthFailureCode({ code: "<script>" })).toBeNull();
expect(readConnectionIntentOAuthFailureCode(null)).toBeNull();
const fallback = oauthFailureMessage("oauth_callback_http_500");
expect(fallback).toBe("Authorization did not complete. Try again when you’re ready.");
expect(fallback).not.toContain("oauth_");
});
});
describe("missing sign-in state messages", () => {
const missing = { type: "paperclip.connection-intent.oauth", outcome: "failed", code: "oauth_state_missing" };
const activePopup = {} as Window;
const oldPopup = {} as Window;
const from = (data: unknown, source: Window | null, eventOrigin = origin) => ({ origin: eventOrigin, data, source });
it("accepts a missing-state failure from this setup's active sign-in window", () => {
expect(isMissingOAuthStateMessage(from(missing, activePopup), origin, activePopup)).toBe(true);
});
it("ignores a missing-state failure from an old sign-in window", () => {
expect(isMissingOAuthStateMessage(from(missing, oldPopup), origin, activePopup)).toBe(false);
expect(isMissingOAuthStateMessage(from(missing, oldPopup), origin, null)).toBe(false);
});
it("rejects other origins, other codes, and messages bound to an interaction", () => {
expect(isMissingOAuthStateMessage(from(missing, activePopup, "https://evil.test"), origin, activePopup)).toBe(false);
expect(isMissingOAuthStateMessage(from({ ...missing, code: "oauth_state_expired" }, activePopup), origin, activePopup)).toBe(false);
expect(isMissingOAuthStateMessage(from({ ...missing, interactionId }, activePopup), origin, activePopup)).toBe(false);
});
});
describe("GitHub installation recovery from the sign-in window", () => {
it("offers the install and manage links GitHub returned", () => {
expect(readConnectionIntentGitHubRecovery({
outcome: "failed",
code: "github_installation_required",
recovery: {
installationUrl: "https://github.com/apps/paperclip-for-github/installations/new",
managementUrl: "https://github.com/settings/installations",
},
})).toEqual({
installationUrl: "https://github.com/apps/paperclip-for-github/installations/new",
managementUrl: "https://github.com/settings/installations",
});
expect(oauthFailureMessage("github_installation_required")).toMatch(/Paperclip for GitHub app isn’t installed/);
});
it("drops links that are not on github.com or belong to another failure", () => {
expect(readConnectionIntentGitHubRecovery({
code: "github_installation_required",
recovery: { installationUrl: "https://evil.test/install", managementUrl: "javascript:alert(1)" },
})).toBeNull();
expect(readConnectionIntentGitHubRecovery({
code: "oauth_state_expired",
recovery: { installationUrl: "https://github.com/apps/paperclip-for-github/installations/new" },
})).toBeNull();
});
});