The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | EnvironmentId, |
| 3 | IssueTrackerKind, |
| 4 | ProjectId, |
| 5 | ProviderInstanceId, |
| 6 | ThreadId, |
| 7 | type IssueDetail, |
| 8 | type OrchestrationProjectShell, |
| 9 | type ThreadIssueLink, |
| 10 | SourceControlProviderKind, |
| 11 | } from "@t3tools/contracts"; |
| 12 | import { describe, expect, it } from "@effect/vitest"; |
| 13 | import { |
| 14 | IssueTrackerError, |
| 15 | type StandaloneIssueTrackerApi, |
| 16 | } from "@t3tools/source-control-core/server/IssueTracker"; |
| 17 | import * as Effect from "effect/Effect"; |
| 18 | import * as Layer from "effect/Layer"; |
| 19 | import * as Option from "effect/Option"; |
| 20 | import * as Ref from "effect/Ref"; |
| 21 | import * as Stream from "effect/Stream"; |
| 22 | import type { Tool } from "effect/ai"; |
| 23 | |
| 24 | import * as IssueService from "../../../issue/IssueService.ts"; |
| 25 | import * as IssueTrackerRegistry from "../../../issue/IssueTrackerRegistry.ts"; |
| 26 | import * as Orchestrator from "../../../orchestration-v2/Orchestrator.ts"; |
| 27 | import * as ThreadIssueService from "../../../orchestration-v2/ThreadIssueService.ts"; |
| 28 | import { v2PullRequestThread } from "../../../orchestration-v2/testkit/pullRequestFixtures.ts"; |
| 29 | import * as IssueTrackerBindings from "../../../persistence/IssueTrackerBindings.ts"; |
| 30 | import * as ProjectService from "../../../project/ProjectService.ts"; |
| 31 | import * as RepositoryIdentityResolver from "../../../project/RepositoryIdentityResolver.ts"; |
| 32 | import * as McpInvocationContext from "../../McpInvocationContext.ts"; |
| 33 | import * as McpToolAccess from "../../McpToolAccess.ts"; |
| 34 | import * as McpToolAccessTestkit from "../../McpToolAccess.testkit.ts"; |
| 35 | import * as IssuesHandlers from "./handlers.ts"; |
| 36 | import { IssuesToolkit } from "./tools.ts"; |
| 37 | |
| 38 | const PROJECT_ID = ProjectId.make("project-1"); |
| 39 | const THREAD_ID = ThreadId.make("thread-1"); |
| 40 | const GITHUB = IssueTrackerKind.make("github"); |
| 41 | |
| 42 | const invocation = ( |
| 43 | capabilities: ReadonlyArray<McpInvocationContext.McpCapability>, |
| 44 | ): McpInvocationContext.McpInvocationScope => ({ |
| 45 | environmentId: EnvironmentId.make("environment-1"), |
| 46 | requestNamespace: "provider-session-1", |
| 47 | thread: { |
| 48 | threadId: THREAD_ID, |
| 49 | providerSessionId: "provider-session-1", |
| 50 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 51 | }, |
| 52 | client: undefined, |
| 53 | capabilities: new Set(capabilities), |
| 54 | issuedAt: 1, |
| 55 | }); |
| 56 | |
| 57 | const project: OrchestrationProjectShell = { |
| 58 | id: PROJECT_ID, |
| 59 | title: "Project", |
| 60 | workspaceRoot: "/workspace/project", |
| 61 | defaultModelSelection: null, |
| 62 | scripts: [], |
| 63 | repositoryIdentity: { |
| 64 | canonicalKey: "github.com/t3tools/t3code", |
| 65 | locator: { |
| 66 | source: "git-remote", |
| 67 | remoteName: "origin", |
| 68 | remoteUrl: "git@github.com:T3Tools/T3Code.git", |
| 69 | }, |
| 70 | provider: SourceControlProviderKind.make("github"), |
| 71 | displayName: "T3Tools/T3Code", |
| 72 | owner: "T3Tools", |
| 73 | name: "T3Code", |
| 74 | }, |
| 75 | createdAt: "2026-08-01T00:00:00.000Z", |
| 76 | updatedAt: "2026-08-01T00:00:00.000Z", |
| 77 | }; |
| 78 | |
| 79 | function makeThread(issues: ReadonlyArray<ThreadIssueLink>) { |
| 80 | return { |
| 81 | ...v2PullRequestThread({ |
| 82 | id: THREAD_ID, |
| 83 | projectId: PROJECT_ID, |
| 84 | title: "Thread", |
| 85 | modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" }, |
| 86 | runtimeMode: "full-access", |
| 87 | interactionMode: "default", |
| 88 | branch: null, |
| 89 | worktreePath: null, |
| 90 | pullRequests: [], |
| 91 | createdAt: "2026-08-01T00:00:00.000Z", |
| 92 | updatedAt: "2026-08-20T00:00:00.000Z", |
| 93 | archivedAt: null, |
| 94 | settledOverride: null, |
| 95 | settledAt: null, |
| 96 | latestUserMessageAt: "2026-08-20T00:00:00.000Z", |
| 97 | }), |
| 98 | issues, |
| 99 | }; |
| 100 | } |
| 101 | |
| 102 | const detail: IssueDetail = { |
| 103 | tracker: GITHUB, |
| 104 | host: "github.com", |
| 105 | scope: "t3tools/t3code", |
| 106 | key: "7", |
| 107 | displayKey: "#7", |
| 108 | title: "Seven", |
| 109 | url: "https://github.com/T3Tools/T3Code/issues/7", |
| 110 | author: null, |
| 111 | state: { category: "open", name: "open" }, |
| 112 | createdAt: "2026-08-01T00:00:00.000Z", |
| 113 | updatedAt: "2026-08-02T00:00:00.000Z", |
| 114 | closedAt: null, |
| 115 | assignees: [], |
| 116 | labels: [], |
| 117 | commentCount: 0, |
| 118 | projectId: PROJECT_ID, |
| 119 | projectTitle: "Project", |
| 120 | writable: true, |
| 121 | capabilities: { |
| 122 | create: true, |
| 123 | edit: true, |
| 124 | comment: true, |
| 125 | labels: true, |
| 126 | assignees: true, |
| 127 | reactions: false, |
| 128 | templates: false, |
| 129 | linkedPullRequests: true, |
| 130 | events: true, |
| 131 | search: true, |
| 132 | }, |
| 133 | states: [], |
| 134 | viewerPermissions: { setState: true, comment: true, edit: true, labels: true, assignees: true }, |
| 135 | viewer: null, |
| 136 | body: "", |
| 137 | comments: [], |
| 138 | commentsCursor: null, |
| 139 | events: [], |
| 140 | linkedPullRequests: [], |
| 141 | }; |
| 142 | |
| 143 | const projectLayer = Layer.mock(ProjectService.ProjectService)({ |
| 144 | getShell: (id) => Effect.succeed(id === PROJECT_ID ? Option.some(project) : Option.none()), |
| 145 | listShells: () => Effect.succeed([project]), |
| 146 | }); |
| 147 | |
| 148 | const makeHarness = Effect.fn("makeIssuesToolkitHarness")(function* ( |
| 149 | issues: ReadonlyArray<ThreadIssueLink> = [], |
| 150 | issueLayer?: Layer.Layer<IssueService.IssueService>, |
| 151 | ) { |
| 152 | const links = yield* Ref.make<ReadonlyArray<ThreadIssueService.LinkThreadIssueInput>>([]); |
| 153 | const layerDependencies = Layer.mergeAll( |
| 154 | projectLayer, |
| 155 | Layer.mock(Orchestrator.OrchestratorV2)({ |
| 156 | getThreadShell: (id) => Effect.succeed(id === THREAD_ID ? makeThread(issues) : null), |
| 157 | }), |
| 158 | issueLayer ?? |
| 159 | Layer.mock(IssueService.IssueService)({ |
| 160 | detail: () => Effect.succeed(detail), |
| 161 | projectScope: (projectId) => |
| 162 | Effect.succeed({ |
| 163 | projectId, |
| 164 | tracker: GITHUB, |
| 165 | host: "github.com", |
| 166 | scope: "t3tools/t3code", |
| 167 | }), |
| 168 | resolveUrl: (url) => |
| 169 | Effect.succeed({ |
| 170 | projectId: PROJECT_ID, |
| 171 | tracker: GITHUB, |
| 172 | host: "github.com", |
| 173 | scope: "t3tools/t3code", |
| 174 | key: url.split("/").at(-1)!, |
| 175 | writable: true, |
| 176 | }), |
| 177 | }), |
| 178 | Layer.mock(ThreadIssueService.ThreadIssueService)({ |
| 179 | link: (input) => |
| 180 | Ref.update(links, (recorded) => [...recorded, input]).pipe( |
| 181 | Effect.as({ alreadyLinked: false }), |
| 182 | ), |
| 183 | unlink: () => Effect.succeed({ wasLinked: true }), |
| 184 | }), |
| 185 | McpToolAccessTestkit.liveThreadsLayer, |
| 186 | ); |
| 187 | const toolkit = yield* IssuesToolkit.pipe( |
| 188 | Effect.provide( |
| 189 | McpToolAccess.HandlersLayer.layer(IssuesHandlers.layer).pipe( |
| 190 | Layer.provide(layerDependencies), |
| 191 | ), |
| 192 | ), |
| 193 | ); |
| 194 | const call = <Name extends keyof typeof IssuesToolkit.tools>( |
| 195 | name: Name, |
| 196 | params: Parameters<typeof toolkit.handle<Name>>[1], |
| 197 | capabilities: ReadonlyArray<McpInvocationContext.McpCapability> = ["issues"], |
| 198 | ) => |
| 199 | toolkit.handle(name, params).pipe( |
| 200 | Stream.unwrap, |
| 201 | Stream.runCollect, |
| 202 | Effect.map( |
| 203 | (chunk) => chunk.at(-1)!.result as Tool.Success<(typeof IssuesToolkit.tools)[Name]>, |
| 204 | ), |
| 205 | Effect.provideService(McpInvocationContext.McpInvocationContext, invocation(capabilities)), |
| 206 | Effect.provide(layerDependencies), |
| 207 | ); |
| 208 | return { links, call }; |
| 209 | }); |
| 210 | |
| 211 | describe("issue toolkit handlers", () => { |
| 212 | it.effect("returns a Linear issue by URL with one issue read left before the rate limit", () => |
| 213 | Effect.gen(function* () { |
| 214 | const tracker = IssueTrackerKind.make("linear"); |
| 215 | const host = "linear.app"; |
| 216 | const url = "https://linear.app/acme/issue/ENG-7/seven"; |
| 217 | const readsLeft = yield* Ref.make(1); |
| 218 | const linear: StandaloneIssueTrackerApi = { |
| 219 | kind: tracker, |
| 220 | capabilities: detail.capabilities, |
| 221 | standalone: { |
| 222 | tracker, |
| 223 | displayName: "Linear", |
| 224 | host, |
| 225 | scopeNoun: "team", |
| 226 | credential: { label: "Personal API key", helpUrl: null }, |
| 227 | }, |
| 228 | parseIssueUrl: (link) => |
| 229 | link.href === url ? { host, workspace: "acme", scope: "ENG", key: "ENG-7" } : null, |
| 230 | probeCredential: () => Effect.succeed({ account: "user", scopes: [] }), |
| 231 | getViewer: () => Effect.succeed("user"), |
| 232 | listIssues: () => Effect.succeed({ items: [], nextCursor: null }), |
| 233 | getComments: () => Effect.succeed({ comments: [], nextCursor: null }), |
| 234 | getIssue: ({ key }) => |
| 235 | Ref.getAndUpdate(readsLeft, (left) => left - 1).pipe( |
| 236 | Effect.flatMap((left) => |
| 237 | left > 0 |
| 238 | ? Effect.succeed({ |
| 239 | ...detail, |
| 240 | reactions: [], |
| 241 | key, |
| 242 | url, |
| 243 | displayKey: key, |
| 244 | body: "Repro steps", |
| 245 | }) |
| 246 | : Effect.fail( |
| 247 | new IssueTrackerError({ |
| 248 | tracker, |
| 249 | operation: "getIssue", |
| 250 | reason: "rate-limited", |
| 251 | detail: "Linear's rate limit was reached.", |
| 252 | }), |
| 253 | ), |
| 254 | ), |
| 255 | ), |
| 256 | }; |
| 257 | const issueLayer = IssueService.layer.pipe( |
| 258 | Layer.provide( |
| 259 | Layer.mergeAll( |
| 260 | projectLayer, |
| 261 | Layer.mock(RepositoryIdentityResolver.RepositoryIdentityResolver)({}), |
| 262 | Layer.mock(IssueTrackerBindings.IssueTrackerBindingsRepository)({ |
| 263 | list: Effect.succeed(new Map([[PROJECT_ID, { tracker, scope: "ENG" }]])), |
| 264 | }), |
| 265 | Layer.succeed( |
| 266 | IssueTrackerRegistry.IssueTrackerRegistry, |
| 267 | IssueTrackerRegistry.fromTrackers([linear]), |
| 268 | ), |
| 269 | ), |
| 270 | ), |
| 271 | ); |
| 272 | const harness = yield* makeHarness([], issueLayer); |
| 273 | const result = yield* harness.call("read_issue", { url }).pipe(Effect.result); |
| 274 | expect(result).toMatchObject({ |
| 275 | _tag: "Success", |
| 276 | success: { url, displayKey: "ENG-7", title: "Seven", body: "Repro steps" }, |
| 277 | }); |
| 278 | }), |
| 279 | ); |
| 280 | |
| 281 | it.effect("refuses a credential without the issues capability", () => |
| 282 | Effect.gen(function* () { |
| 283 | const harness = yield* makeHarness(); |
| 284 | const error = yield* harness |
| 285 | .call("list_thread_issues", {}, ["pull-requests"]) |
| 286 | .pipe(Effect.flip); |
| 287 | expect(error).toMatchObject({ _tag: "McpCapabilityUnavailableError", capability: "issues" }); |
| 288 | // Naming the project or the issue outright must not skip the check. |
| 289 | const refused = { _tag: "McpCapabilityUnavailableError", capability: "issues" }; |
| 290 | expect( |
| 291 | yield* harness |
| 292 | .call("list_issues", { projectId: PROJECT_ID }, ["pull-requests"]) |
| 293 | .pipe(Effect.flip), |
| 294 | ).toMatchObject(refused); |
| 295 | expect( |
| 296 | yield* harness |
| 297 | .call("read_issue", { projectId: PROJECT_ID, key: "7" }, ["pull-requests"]) |
| 298 | .pipe(Effect.flip), |
| 299 | ).toMatchObject(refused); |
| 300 | expect( |
| 301 | yield* harness |
| 302 | .call("read_issue", { projectId: PROJECT_ID, url: detail.url }, ["pull-requests"]) |
| 303 | .pipe(Effect.flip), |
| 304 | ).toMatchObject(refused); |
| 305 | }), |
| 306 | ); |
| 307 | |
| 308 | it.effect("links by URL with source agent on the token's thread", () => |
| 309 | Effect.gen(function* () { |
| 310 | const harness = yield* makeHarness(); |
| 311 | const result = yield* harness.call("link_issue", { |
| 312 | url: "https://github.com/T3Tools/T3Code/issues/7", |
| 313 | }); |
| 314 | expect(result).toEqual({ |
| 315 | projectId: PROJECT_ID, |
| 316 | tracker: "github", |
| 317 | host: "github.com", |
| 318 | scope: "t3tools/t3code", |
| 319 | key: "7", |
| 320 | url: "https://github.com/T3Tools/T3Code/issues/7", |
| 321 | alreadyLinked: false, |
| 322 | }); |
| 323 | expect(yield* Ref.get(harness.links)).toEqual([ |
| 324 | { |
| 325 | threadId: THREAD_ID, |
| 326 | ref: { |
| 327 | projectId: PROJECT_ID, |
| 328 | tracker: "github", |
| 329 | host: "github.com", |
| 330 | scope: "t3tools/t3code", |
| 331 | key: "7", |
| 332 | }, |
| 333 | url: "https://github.com/T3Tools/T3Code/issues/7", |
| 334 | displayKey: detail.displayKey, |
| 335 | title: detail.title, |
| 336 | source: "agent", |
| 337 | }, |
| 338 | ]); |
| 339 | }), |
| 340 | ); |
| 341 | |
| 342 | it.effect("links by key through the thread's project and reads the URL from the tracker", () => |
| 343 | Effect.gen(function* () { |
| 344 | const harness = yield* makeHarness(); |
| 345 | const result = yield* harness.call("link_issue", { key: "7" }); |
| 346 | expect(result.url).toBe(detail.url); |
| 347 | expect((yield* Ref.get(harness.links))[0]?.url).toBe(detail.url); |
| 348 | // Named by key and never read: the identity has no URL rather than an empty one, so it can |
| 349 | // be handed to another issue tool as it is. |
| 350 | const unlinked = yield* harness.call("unlink_issue", { key: "7" }); |
| 351 | expect(unlinked).toMatchObject({ key: "7", wasLinked: false }); |
| 352 | expect(unlinked).not.toHaveProperty("url"); |
| 353 | }), |
| 354 | ); |
| 355 | |
| 356 | it.effect("lists linked issues from the thread shell as they were named when linked", () => |
| 357 | Effect.gen(function* () { |
| 358 | const harness = yield* makeHarness([ |
| 359 | { |
| 360 | tracker: GITHUB, |
| 361 | host: "github.com", |
| 362 | scope: "t3tools/t3code", |
| 363 | key: "7", |
| 364 | projectId: PROJECT_ID, |
| 365 | url: detail.url, |
| 366 | source: "manual", |
| 367 | linkedAt: "2026-08-10T00:00:00.000Z", |
| 368 | displayKey: "#7", |
| 369 | title: "Seven", |
| 370 | }, |
| 371 | ]); |
| 372 | const result = yield* harness.call("list_thread_issues", {}); |
| 373 | expect(result.issues).toMatchObject([{ key: "7", title: "Seven", displayKey: "#7" }]); |
| 374 | const unlinked = yield* harness.call("unlink_issue", { url: detail.url }); |
| 375 | expect(unlinked.wasLinked).toBe(true); |
| 376 | }), |
| 377 | ); |
| 378 | }); |