@cursor/july 0.1.20 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (212) hide show
  1. package/dist/bin/agent-serve.js +0 -0
  2. package/dist/channels/github/instrument.d.ts +20 -0
  3. package/dist/channels/github/instrument.d.ts.map +1 -0
  4. package/dist/channels/slack/api.d.ts +2 -0
  5. package/dist/channels/slack/api.d.ts.map +1 -1
  6. package/dist/channels/slack/api.js +3 -0
  7. package/dist/channels/slack/bot-mentions.d.ts +106 -0
  8. package/dist/channels/slack/bot-mentions.d.ts.map +1 -0
  9. package/dist/channels/slack/bot-mentions.js +243 -0
  10. package/dist/channels/slack/dispatch.d.ts +6 -0
  11. package/dist/channels/slack/dispatch.d.ts.map +1 -1
  12. package/dist/channels/slack/dispatch.js +24 -3
  13. package/dist/channels/slack/inbound.d.ts +10 -1
  14. package/dist/channels/slack/inbound.d.ts.map +1 -1
  15. package/dist/channels/slack/inbound.js +12 -4
  16. package/dist/channels/slack/index.d.ts +1 -0
  17. package/dist/channels/slack/index.d.ts.map +1 -1
  18. package/dist/channels/slack/index.js +1 -0
  19. package/dist/channels/slack/slack-channel.d.ts.map +1 -1
  20. package/dist/channels/slack/slack-channel.js +61 -22
  21. package/dist/channels/slack/types.d.ts +58 -0
  22. package/dist/channels/slack/types.d.ts.map +1 -1
  23. package/dist/docs/404.html +1 -1
  24. package/dist/docs/ab.html +2 -2
  25. package/dist/docs/assets/{app.T-5ImnM3.js → app.jDxLzWv4.js} +1 -1
  26. package/dist/docs/assets/chunks/@localSearchIndexroot.DoJHJjqF.js +1 -0
  27. package/dist/docs/assets/chunks/{VPLocalSearchBox.OMGMWH6H.js → VPLocalSearchBox.Y6bDR1-a.js} +1 -1
  28. package/dist/docs/assets/chunks/{theme.DLg4yB4D.js → theme.CLazCWlJ.js} +2 -2
  29. package/dist/docs/building-with-agents.html +2 -2
  30. package/dist/docs/concepts.html +2 -2
  31. package/dist/docs/deployment.html +2 -2
  32. package/dist/docs/evals.html +2 -2
  33. package/dist/docs/example-agents/approval-buddy.html +2 -2
  34. package/dist/docs/example-agents/benny.html +2 -2
  35. package/dist/docs/example-agents/bugbot.html +2 -2
  36. package/dist/docs/example-agents/codebase-wiki.html +2 -2
  37. package/dist/docs/example-agents/codeowners-review.html +2 -2
  38. package/dist/docs/example-agents/concierge.html +2 -2
  39. package/dist/docs/example-agents/fsd.html +2 -2
  40. package/dist/docs/example-agents/index.html +2 -2
  41. package/dist/docs/example-agents/knowledge-base.html +2 -2
  42. package/dist/docs/example-agents/oncall.html +2 -2
  43. package/dist/docs/example-agents/security-reviewer.html +2 -2
  44. package/dist/docs/example-agents/slack-agent.html +2 -2
  45. package/dist/docs/example-agents/weather-agent.html +2 -2
  46. package/dist/docs/guides/agent-to-agent.html +2 -2
  47. package/dist/docs/guides/cloud-runtime.html +2 -2
  48. package/dist/docs/guides/github.html +2 -2
  49. package/dist/docs/guides/human-in-the-loop.html +2 -2
  50. package/dist/docs/guides/mcp-oauth.html +2 -2
  51. package/dist/docs/guides/slack.html +2 -2
  52. package/dist/docs/guides/webhooks.html +2 -2
  53. package/dist/docs/hillclimbing.html +2 -2
  54. package/dist/docs/index.html +2 -2
  55. package/dist/docs/quickstart.html +2 -2
  56. package/dist/docs/reference/agent-config.html +2 -2
  57. package/dist/docs/reference/channels.html +2 -2
  58. package/dist/docs/reference/cli.html +2 -2
  59. package/dist/docs/reference/connections.html +2 -2
  60. package/dist/docs/reference/hooks.html +2 -2
  61. package/dist/docs/reference/http-api.html +2 -2
  62. package/dist/docs/reference/instructions.html +2 -2
  63. package/dist/docs/reference/playground.html +2 -2
  64. package/dist/docs/reference/project-layout.html +2 -2
  65. package/dist/docs/reference/prompt.html +2 -2
  66. package/dist/docs/reference/schedules.html +2 -2
  67. package/dist/docs/reference/sessions.html +2 -2
  68. package/dist/docs/reference/skills.html +2 -2
  69. package/dist/docs/reference/subagents.html +2 -2
  70. package/dist/docs/reference/tools.html +2 -2
  71. package/dist/docs/scaffolding-agents.html +2 -2
  72. package/dist/docs/storage.html +2 -2
  73. package/dist/docs/troubleshooting.html +2 -2
  74. package/dist/internal/json-dir-store.d.ts +32 -0
  75. package/dist/internal/json-dir-store.d.ts.map +1 -0
  76. package/dist/internal/json-dir-store.js +100 -0
  77. package/dist/internal/resolved-connections.d.ts +1 -1
  78. package/dist/internal/resolved-connections.d.ts.map +1 -1
  79. package/dist/internal/resolved-connections.js +10 -0
  80. package/dist/internal/session-engine.d.ts +4 -1
  81. package/dist/internal/session-engine.d.ts.map +1 -1
  82. package/dist/internal/session-engine.js +13 -2
  83. package/dist/playground/assets/index-CjOQ4hN9.css +1 -0
  84. package/dist/playground/assets/index-dshZQJCp.js +85 -0
  85. package/dist/playground/index.html +2 -2
  86. package/dist/types.d.ts +7 -0
  87. package/dist/types.d.ts.map +1 -1
  88. package/dist/types.js +9 -0
  89. package/package.json +24 -24
  90. package/src/bin/agent-serve.version.test.ts +62 -0
  91. package/src/channels/github/api.test.ts +64 -0
  92. package/src/channels/github/auth.test.ts +105 -0
  93. package/src/channels/github/cursor-account.test.ts +204 -0
  94. package/src/channels/github/forward.test.ts +457 -0
  95. package/src/channels/github/github.test.ts +937 -0
  96. package/src/channels/github/replay.test.ts +179 -0
  97. package/src/channels/slack/api.post-message.test.ts +148 -0
  98. package/src/channels/slack/api.ts +5 -0
  99. package/src/channels/slack/approvals.test.ts +328 -0
  100. package/src/channels/slack/block-actions.test.ts +452 -0
  101. package/src/channels/slack/bot-mentions.test.ts +217 -0
  102. package/src/channels/slack/bot-mentions.ts +315 -0
  103. package/src/channels/slack/channel-watch.test.ts +363 -0
  104. package/src/channels/slack/cursor-account.test.ts +253 -0
  105. package/src/channels/slack/defaults.final-post.test.ts +182 -0
  106. package/src/channels/slack/dispatch.test.ts +795 -0
  107. package/src/channels/slack/dispatch.ts +27 -1
  108. package/src/channels/slack/eval-directive.test.ts +273 -0
  109. package/src/channels/slack/inbound.ts +25 -4
  110. package/src/channels/slack/index.ts +1 -0
  111. package/src/channels/slack/message-body.test.ts +54 -0
  112. package/src/channels/slack/nudge-store.test.ts +143 -0
  113. package/src/channels/slack/slack-channel.ts +66 -8
  114. package/src/channels/slack/slack.test.ts +391 -0
  115. package/src/channels/slack/stop.test.ts +23 -0
  116. package/src/channels/slack/thread-context.test.ts +202 -0
  117. package/src/channels/slack/types.ts +60 -0
  118. package/src/evals/assertions.test.ts +580 -0
  119. package/src/evals/expect.test.ts +144 -0
  120. package/src/evals/judge.test.ts +181 -0
  121. package/src/evals/loaders.test.ts +132 -0
  122. package/src/evals/matchers.test.ts +95 -0
  123. package/src/evals/reporters.test.ts +303 -0
  124. package/src/evals/run-facts.test.ts +259 -0
  125. package/src/internal/ab-snapshot.test.ts +325 -0
  126. package/src/internal/approval-gate.test.ts +49 -0
  127. package/src/internal/approvals.integration.test.ts +383 -0
  128. package/src/internal/authored-loaders.test.ts +31 -0
  129. package/src/internal/builtin-tools/reminders.test.ts +201 -0
  130. package/src/internal/channel-route-schema.test.ts +294 -0
  131. package/src/internal/chat-attach.test.ts +262 -0
  132. package/src/internal/cli-deploy.test.ts +1991 -0
  133. package/src/internal/cli-mcp.test.ts +789 -0
  134. package/src/internal/cli-skills.test.ts +133 -0
  135. package/src/internal/cli-slack.test.ts +1647 -0
  136. package/src/internal/cloud-merge.test.ts +74 -0
  137. package/src/internal/cron.test.ts +22 -0
  138. package/src/internal/cursor/account-mcp.test.ts +807 -0
  139. package/src/internal/cursor/backend-client.test.ts +591 -0
  140. package/src/internal/cursor/credentials.test.ts +351 -0
  141. package/src/internal/cursor/github-credentials.test.ts +136 -0
  142. package/src/internal/cursor-account-mcp-auth.test.ts +310 -0
  143. package/src/internal/cursor-account.integration.test.ts +441 -0
  144. package/src/internal/cursor-event-relay.test.ts +746 -0
  145. package/src/internal/cursor-github-credentials.integration.test.ts +271 -0
  146. package/src/internal/cursor-slack-relay.test.ts +525 -0
  147. package/src/internal/deploy-source.test.ts +111 -0
  148. package/src/internal/discovery.builtin-tools.test.ts +94 -0
  149. package/src/internal/discovery.concurrency.test.ts +60 -0
  150. package/src/internal/discovery.cursor-account.test.ts +133 -0
  151. package/src/internal/discovery.cwd.test.ts +83 -0
  152. package/src/internal/discovery.hosting.test.ts +80 -0
  153. package/src/internal/discovery.identity.test.ts +44 -0
  154. package/src/internal/docs-site.test.ts +66 -0
  155. package/src/internal/duration.test.ts +29 -0
  156. package/src/internal/eval-judge-model.test.ts +187 -0
  157. package/src/internal/eval-run-store.cancel.test.ts +142 -0
  158. package/src/internal/eval-run-store.storage.test.ts +211 -0
  159. package/src/internal/eval-runner.http.test.ts +403 -0
  160. package/src/internal/eval-runner.run.test.ts +928 -0
  161. package/src/internal/evals-client.test.ts +307 -0
  162. package/src/internal/event-mapper.test.ts +243 -0
  163. package/src/internal/github-fanout.test.ts +213 -0
  164. package/src/internal/handleAgentServeTrigger.test.ts +179 -0
  165. package/src/internal/host-kv.test.ts +82 -0
  166. package/src/internal/host-platforms.test.ts +126 -0
  167. package/src/internal/http-channel.test.ts +402 -0
  168. package/src/internal/init-project.test.ts +269 -0
  169. package/src/internal/install-cursor-skills.test.ts +262 -0
  170. package/src/internal/local-env.test.ts +100 -0
  171. package/src/internal/log-ring.test.ts +31 -0
  172. package/src/internal/logs-client.test.ts +350 -0
  173. package/src/internal/mcp-endpoint.test.ts +436 -0
  174. package/src/internal/mcp-host.test.ts +298 -0
  175. package/src/internal/mcp-oauth.test.ts +148 -0
  176. package/src/internal/net.test.ts +17 -0
  177. package/src/internal/peer-connections.test.ts +128 -0
  178. package/src/internal/peer-mcp.integration.test.ts +289 -0
  179. package/src/internal/playground/toolchain.test.ts +53 -0
  180. package/src/internal/playground-cli.test.ts +187 -0
  181. package/src/internal/playground-proxy.test.ts +376 -0
  182. package/src/internal/prompt-context.integration.test.ts +232 -0
  183. package/src/internal/prompt-context.test.ts +127 -0
  184. package/src/internal/reminder-runner.test.ts +390 -0
  185. package/src/internal/reminder-store.test.ts +53 -0
  186. package/src/internal/request-headers.test.ts +27 -0
  187. package/src/internal/resolve-prod-target.test.ts +787 -0
  188. package/src/internal/resolved-connections.test.ts +295 -0
  189. package/src/internal/resolved-connections.ts +18 -1
  190. package/src/internal/router.test.ts +57 -0
  191. package/src/internal/sdk-runner.test.ts +290 -0
  192. package/src/internal/session-engine.coalesce.test.ts +169 -0
  193. package/src/internal/session-engine.concurrency.test.ts +250 -0
  194. package/src/internal/session-engine.host-oauth-mcp.test.ts +110 -0
  195. package/src/internal/session-engine.interrupt.test.ts +577 -0
  196. package/src/internal/session-engine.storage.test.ts +547 -0
  197. package/src/internal/session-engine.ts +15 -1
  198. package/src/internal/session-urls.test.ts +28 -0
  199. package/src/internal/sessions-client.test.ts +518 -0
  200. package/src/internal/storage-coordinator.test.ts +517 -0
  201. package/src/internal/tool-call.test.ts +458 -0
  202. package/src/internal/tool-result.test.ts +52 -0
  203. package/src/internal/trajectory.approvals.test.ts +83 -0
  204. package/src/internal/trajectory.subagents.test.ts +198 -0
  205. package/src/internal/turn-governor.test.ts +137 -0
  206. package/src/internal/update-check.test.ts +485 -0
  207. package/src/internal/workspace.test.ts +81 -0
  208. package/src/storage-backends/cursor-hosted.test.ts +121 -0
  209. package/src/types.ts +12 -0
  210. package/dist/docs/assets/chunks/@localSearchIndexroot.CvejISXF.js +0 -1
  211. package/dist/playground/assets/index-CidizGZv.css +0 -1
  212. package/dist/playground/assets/index-CuiAKZaP.js +0 -85
@@ -0,0 +1,204 @@
1
+ import { afterEach, describe, expect, it } from "vitest";
2
+ import { createGitHubOctokit } from "./api.js";
3
+ import { resolveCredentialValue } from "./auth.js";
4
+ import {
5
+ getGithubCursorAccountRuntime,
6
+ resolveCursorGithubPermissions,
7
+ resolveCursorGithubProxyUrl,
8
+ } from "./cursor-account.js";
9
+ import { getGitHubChannelOptions, githubChannel } from "./github-channel.js";
10
+
11
+ describe("githubChannel({ cursorAccount })", () => {
12
+ it("binds outbound API auth without rewriting authored cursorAccount options", async () => {
13
+ const definition = githubChannel({
14
+ cursorAccount: { repos: ["Acme/Widgets"], permissions: "read" },
15
+ });
16
+ const runtime = getGithubCursorAccountRuntime(definition);
17
+
18
+ expect(runtime?.options.cursorAccount).toEqual({
19
+ repos: ["Acme/Widgets"],
20
+ permissions: "read",
21
+ });
22
+ expect(runtime?.scope).toEqual({
23
+ owner: "acme",
24
+ repos: ["acme/widgets"],
25
+ permissions: "read",
26
+ });
27
+ expect(() => runtime?.credentials.getToken()).toThrow(/has not been bound/);
28
+ runtime?.credentials.bind(async () => "ghs_cursor_account");
29
+ await expect(
30
+ resolveCredentialValue(
31
+ getGitHubChannelOptions(definition)?.credentials?.token
32
+ )
33
+ ).resolves.toBe("ghs_cursor_account");
34
+ });
35
+
36
+ it("defaults permissions to pr-write on the private scope", () => {
37
+ const definition = githubChannel({
38
+ cursorAccount: { repos: ["acme/widgets"] },
39
+ });
40
+ expect(getGithubCursorAccountRuntime(definition)?.scope).toEqual({
41
+ owner: "acme",
42
+ repos: ["acme/widgets"],
43
+ permissions: "pr-write",
44
+ });
45
+ });
46
+
47
+ it("allows a webhook secret but rejects separate outbound credentials", () => {
48
+ expect(() =>
49
+ githubChannel({
50
+ cursorAccount: true,
51
+ credentials: { webhookSecret: "signing-secret" },
52
+ })
53
+ ).not.toThrow();
54
+ expect(() =>
55
+ githubChannel({
56
+ cursorAccount: true,
57
+ credentials: { token: "ghp_dedicated" },
58
+ })
59
+ ).toThrow(/cannot be combined/);
60
+ expect(() =>
61
+ githubChannel({
62
+ cursorAccount: true,
63
+ credentials: { appId: 1, privateKey: "pem" },
64
+ })
65
+ ).toThrow(/cannot be combined/);
66
+ });
67
+
68
+ it("rejects an empty inline repository scope", () => {
69
+ expect(() => githubChannel({ cursorAccount: { repos: [] } })).toThrow(
70
+ /require at least one repository/
71
+ );
72
+ });
73
+
74
+ it.each([
75
+ [["acme/widgets", "other/tools"], /must share one owner/],
76
+ [["https://github.com/acme/widgets"], /expected owner\/repo/],
77
+ [
78
+ Array.from({ length: 21 }, (_, index) => `acme/repo-${index}`),
79
+ /at most 20 repositories/,
80
+ ],
81
+ ])("rejects an unsupported repository scope", (repos, error) => {
82
+ expect(() => githubChannel({ cursorAccount: { repos } })).toThrow(error);
83
+ });
84
+
85
+ it("treats cursorAccount false as disabled", () => {
86
+ expect(
87
+ getGithubCursorAccountRuntime(githubChannel({ cursorAccount: false }))
88
+ ).toBeUndefined();
89
+ });
90
+
91
+ it("accepts contents-write permissions on the private scope", () => {
92
+ const definition = githubChannel({
93
+ cursorAccount: {
94
+ repos: ["acme/widgets"],
95
+ permissions: "contents-write",
96
+ },
97
+ });
98
+ expect(getGithubCursorAccountRuntime(definition)?.scope?.permissions).toBe(
99
+ "contents-write"
100
+ );
101
+ });
102
+ });
103
+
104
+ describe("resolveCursorGithubPermissions", () => {
105
+ it("escalates to the most privileged requested tier", () => {
106
+ expect(resolveCursorGithubPermissions(["read", "pr-write"])).toBe(
107
+ "pr-write"
108
+ );
109
+ expect(
110
+ resolveCursorGithubPermissions(["read", "contents-write", "pr-write"])
111
+ ).toBe("contents-write");
112
+ });
113
+ });
114
+
115
+ describe("cursor-account GitHub proxy mode", () => {
116
+ afterEach(() => {
117
+ delete process.env.AGENT_SERVE_GITHUB_PROXY_URL;
118
+ });
119
+
120
+ function captureFetch(): {
121
+ fetchImpl: typeof fetch;
122
+ requests: { url: string; headers: Headers }[];
123
+ } {
124
+ const requests: { url: string; headers: Headers }[] = [];
125
+ const fetchImpl: typeof fetch = async (input, init) => {
126
+ requests.push({
127
+ url: input instanceof Request ? input.url : String(input),
128
+ headers:
129
+ input instanceof Request ? input.headers : new Headers(init?.headers),
130
+ });
131
+ return Response.json({ id: 1 });
132
+ };
133
+ return { fetchImpl, requests };
134
+ }
135
+
136
+ it("resolves the proxy URL from the backend base URL or env override", () => {
137
+ expect(resolveCursorGithubProxyUrl("https://api.cursor.com")).toBe(
138
+ "https://api.cursor.com/v0/github-proxy"
139
+ );
140
+ process.env.AGENT_SERVE_GITHUB_PROXY_URL =
141
+ "https://backend.local:8000/v0/github-proxy/";
142
+ expect(resolveCursorGithubProxyUrl("https://api.cursor.com")).toBe(
143
+ "https://backend.local:8000/v0/github-proxy"
144
+ );
145
+ });
146
+
147
+ it("routes channel Octokit calls to the bound proxy with scope headers", async () => {
148
+ const definition = githubChannel({
149
+ cursorAccount: { repos: ["acme/widgets"], permissions: "pr-write" },
150
+ });
151
+ const runtime = getGithubCursorAccountRuntime(definition);
152
+ runtime?.credentials.bind(async () => "crsr_api_key");
153
+ runtime?.credentials.bindProxyTransport({
154
+ apiBaseUrl: "https://api.cursor.com/v0/github-proxy",
155
+ headers: {
156
+ "x-cursor-github-repos": "acme/widgets",
157
+ "x-cursor-github-permissions": "pr-write",
158
+ },
159
+ });
160
+
161
+ const { fetchImpl, requests } = captureFetch();
162
+ const octokit = await createGitHubOctokit({
163
+ api: { fetch: fetchImpl },
164
+ credentials: getGitHubChannelOptions(definition)?.credentials,
165
+ });
166
+ await octokit.request("GET /repos/{owner}/{repo}", {
167
+ owner: "acme",
168
+ repo: "widgets",
169
+ });
170
+
171
+ expect(requests).toHaveLength(1);
172
+ const { url, headers } = requests[0]!;
173
+ expect(url).toBe(
174
+ "https://api.cursor.com/v0/github-proxy/repos/acme/widgets"
175
+ );
176
+ expect(headers.get("authorization")).toBe("Bearer crsr_api_key");
177
+ expect(headers.get("x-cursor-github-repos")).toBe("acme/widgets");
178
+ expect(headers.get("x-cursor-github-permissions")).toBe("pr-write");
179
+ });
180
+
181
+ it("keeps raw-token mode when no proxy transport is bound", async () => {
182
+ const definition = githubChannel({
183
+ cursorAccount: { repos: ["acme/widgets"] },
184
+ });
185
+ getGithubCursorAccountRuntime(definition)?.credentials.bind(
186
+ async () => "ghs_raw"
187
+ );
188
+
189
+ const { fetchImpl, requests } = captureFetch();
190
+ const octokit = await createGitHubOctokit({
191
+ api: { fetch: fetchImpl },
192
+ credentials: getGitHubChannelOptions(definition)?.credentials,
193
+ });
194
+ await octokit.request("GET /repos/{owner}/{repo}", {
195
+ owner: "acme",
196
+ repo: "widgets",
197
+ });
198
+
199
+ const { url, headers } = requests[0]!;
200
+ expect(url).toBe("https://api.github.com/repos/acme/widgets");
201
+ expect(headers.get("authorization")).toBe("Bearer ghs_raw");
202
+ expect(headers.get("x-cursor-github-repos")).toBeNull();
203
+ });
204
+ });
@@ -0,0 +1,457 @@
1
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
2
+ import { setDevMode } from "../../internal/dev-mode.js";
3
+ import type { AgentProject, DiscoveredChannel } from "../../types.js";
4
+ import { deriveGitHubWebhookEvents, GITHUB_CHANNEL_KIND } from "./events.js";
5
+ import {
6
+ type AgentMountLike,
7
+ buildGitHubForwardArgs,
8
+ channelForwardUrl,
9
+ detectGitHubEnvTokenOverride,
10
+ findGitHubChannelTargets,
11
+ GITHUB_WEBHOOK_EXTENSION_PIN,
12
+ ghWebhookExtensionInstallArgs,
13
+ ghWebhookExtensionPinSatisfied,
14
+ parseGhWebhookExtensionInstalled,
15
+ parseGhWebhookExtensionVersion,
16
+ parseRepoFromRemoteUrl,
17
+ resolveGitHubForwardTargets,
18
+ unionForwardEvents,
19
+ } from "./forward.js";
20
+ import { githubChannel } from "./github-channel.js";
21
+
22
+ describe("deriveGitHubWebhookEvents", () => {
23
+ it("returns the base set for a hookless channel", () => {
24
+ expect(deriveGitHubWebhookEvents({})).toEqual([
25
+ "pull_request",
26
+ "issue_comment",
27
+ "pull_request_review_comment",
28
+ ]);
29
+ });
30
+
31
+ it("adds an event per declared typed hook", () => {
32
+ const events = deriveGitHubWebhookEvents({
33
+ onCheckSuite: () => null,
34
+ onStatus: () => null,
35
+ });
36
+ expect(events).toContain("check_suite");
37
+ expect(events).toContain("status");
38
+ expect(events).not.toContain("check_run");
39
+ });
40
+
41
+ it("adds the review catch-all events when onEvent is declared", () => {
42
+ const events = deriveGitHubWebhookEvents({ onEvent: () => null });
43
+ expect(events).toContain("pull_request_review");
44
+ expect(events).toContain("pull_request_review_thread");
45
+ });
46
+
47
+ it("treats webhookEvents as authoritative (deduped)", () => {
48
+ expect(
49
+ deriveGitHubWebhookEvents({
50
+ webhookEvents: ["pull_request", "pull_request", " status "],
51
+ onCheckRun: () => null,
52
+ })
53
+ ).toEqual(["pull_request", "status"]);
54
+ });
55
+ });
56
+
57
+ describe("githubChannel meta", () => {
58
+ it("publishes the github kind and derived events", () => {
59
+ const channel = githubChannel({ onCheckRun: () => null });
60
+ expect(channel.meta?.kind).toBe(GITHUB_CHANNEL_KIND);
61
+ expect(channel.meta?.githubWebhookEvents).toContain("check_run");
62
+ });
63
+
64
+ it("honors an explicit webhookEvents pin", () => {
65
+ const channel = githubChannel({ webhookEvents: ["pull_request"] });
66
+ expect(channel.meta?.githubWebhookEvents).toEqual(["pull_request"]);
67
+ });
68
+ });
69
+
70
+ describe("githubChannel dev auth default", () => {
71
+ const savedWebhookSecret = process.env.GITHUB_WEBHOOK_SECRET;
72
+ beforeEach(() => {
73
+ delete process.env.GITHUB_WEBHOOK_SECRET;
74
+ });
75
+ afterEach(() => {
76
+ setDevMode(false);
77
+ if (savedWebhookSecret === undefined) {
78
+ delete process.env.GITHUB_WEBHOOK_SECRET;
79
+ } else {
80
+ process.env.GITHUB_WEBHOOK_SECRET = savedWebhookSecret;
81
+ }
82
+ });
83
+ const forwarded = new Request("http://example.com/hook");
84
+ const nonLoopback = { requestIp: "203.0.113.4" };
85
+ const loopback = { requestIp: "127.0.0.1" };
86
+
87
+ it("admits loopback deliveries in dev without a secret", async () => {
88
+ setDevMode(true);
89
+ const policy = githubChannel().auth?.[0];
90
+ expect(
91
+ await policy?.(new Request("http://127.0.0.1/hook"), loopback)
92
+ ).not.toBeNull();
93
+ });
94
+
95
+ it("admits loopback deliveries carrying gh forwarding headers in dev", async () => {
96
+ // `gh webhook forward` relays GitHub's edge X-Forwarded-For / X-Real-Ip;
97
+ // dev auth must admit them from a loopback peer (localDev/localDevStrict
98
+ // would reject on those headers and break forwarding).
99
+ setDevMode(true);
100
+ const policy = githubChannel().auth?.[0];
101
+ const ghForwarded = new Request("http://127.0.0.1/hook", {
102
+ headers: {
103
+ "x-github-event": "pull_request",
104
+ "x-forwarded-for": "140.82.115.1",
105
+ "x-real-ip": "140.82.115.1",
106
+ },
107
+ });
108
+ expect(await policy?.(ghForwarded, loopback)).not.toBeNull();
109
+ });
110
+
111
+ it("rejects loopback tunneled traffic with forwarding headers but no GitHub delivery headers in dev", async () => {
112
+ setDevMode(true);
113
+ const policy = githubChannel().auth?.[0];
114
+ const tunneled = new Request("http://127.0.0.1/hook", {
115
+ headers: {
116
+ "x-forwarded-for": "203.0.113.4",
117
+ "x-real-ip": "203.0.113.4",
118
+ },
119
+ });
120
+ expect(await policy?.(tunneled, loopback)).toBeNull();
121
+ });
122
+
123
+ it("rejects non-loopback deliveries in dev without a secret", async () => {
124
+ setDevMode(true);
125
+ const policy = githubChannel().auth?.[0];
126
+ expect(await policy?.(forwarded, nonLoopback)).toBeNull();
127
+ });
128
+
129
+ it("stays loopback-strict outside dev without a secret", async () => {
130
+ setDevMode(false);
131
+ const policy = githubChannel().auth?.[0];
132
+ expect(await policy?.(forwarded, nonLoopback)).toBeNull();
133
+ });
134
+
135
+ it("re-evaluates isDevMode per request on a cached channel definition", async () => {
136
+ // Channel modules call githubChannel() once at import; flipping --dev
137
+ // later must still change auth behavior for the same policy closure.
138
+ setDevMode(false);
139
+ const policy = githubChannel().auth?.[0];
140
+ const ghForwarded = new Request("http://127.0.0.1/hook", {
141
+ headers: {
142
+ "x-github-event": "pull_request",
143
+ "x-forwarded-for": "140.82.115.1",
144
+ "x-real-ip": "140.82.115.1",
145
+ },
146
+ });
147
+ // Outside --dev, localDevStrict rejects proxy-forwarded headers.
148
+ expect(await policy?.(ghForwarded, loopback)).toBeNull();
149
+ setDevMode(true);
150
+ // In --dev, loopbackGitHubDev admits gh webhook forward deliveries.
151
+ expect(await policy?.(ghForwarded, loopback)).not.toBeNull();
152
+ });
153
+ });
154
+
155
+ describe("findGitHubChannelTargets", () => {
156
+ it("collects github channels across mounts and ignores others", () => {
157
+ const mounts: AgentMountLike[] = [
158
+ mount("fsd", [
159
+ channel("github", githubChannel({ onCheckSuite: () => null })),
160
+ channel("drive", { __agentServe: "channel", routes: [] }),
161
+ ]),
162
+ mount("reviewer", [
163
+ channel(
164
+ "github",
165
+ githubChannel({ webhookEvents: ["pull_request"], path: "/hook" })
166
+ ),
167
+ ]),
168
+ ];
169
+
170
+ const targets = findGitHubChannelTargets(mounts);
171
+ expect(targets).toHaveLength(2);
172
+ expect(targets[0]).toMatchObject({
173
+ slug: "fsd",
174
+ channelId: "github",
175
+ routePath: "/",
176
+ });
177
+ expect(targets[0]?.events).toContain("check_suite");
178
+ expect(targets[1]).toMatchObject({
179
+ slug: "reviewer",
180
+ channelId: "github",
181
+ routePath: "/hook",
182
+ events: ["pull_request"],
183
+ });
184
+ });
185
+ });
186
+
187
+ describe("channelForwardUrl", () => {
188
+ it("omits the slug prefix and the default path", () => {
189
+ expect(
190
+ channelForwardUrl({
191
+ host: "127.0.0.1",
192
+ port: 3000,
193
+ slug: "",
194
+ channelId: "github",
195
+ routePath: "/",
196
+ })
197
+ ).toBe("http://127.0.0.1:3000/v1/channels/github");
198
+ });
199
+
200
+ it("includes the slug and a custom path", () => {
201
+ expect(
202
+ channelForwardUrl({
203
+ host: "localhost",
204
+ port: 4000,
205
+ slug: "fsd",
206
+ channelId: "github",
207
+ routePath: "/hook",
208
+ })
209
+ ).toBe("http://localhost:4000/fsd/v1/channels/github/hook");
210
+ });
211
+ });
212
+
213
+ describe("resolveGitHubForwardTargets", () => {
214
+ const target = {
215
+ slug: "fsd",
216
+ channelId: "github",
217
+ routePath: "/",
218
+ events: ["pull_request", "check_run"],
219
+ };
220
+
221
+ it("derives the url and events from a single target", () => {
222
+ const result = resolveGitHubForwardTargets({
223
+ targets: [target],
224
+ host: "127.0.0.1",
225
+ port: 3000,
226
+ });
227
+ expect(result).toEqual({
228
+ ok: true,
229
+ targets: [
230
+ {
231
+ slug: "fsd",
232
+ channelId: "github",
233
+ url: "http://127.0.0.1:3000/fsd/v1/channels/github",
234
+ events: ["pull_request", "check_run"],
235
+ },
236
+ ],
237
+ });
238
+ });
239
+
240
+ it("returns every matching channel for fan-out", () => {
241
+ const reviewer = {
242
+ slug: "reviewer",
243
+ channelId: "github",
244
+ routePath: "/",
245
+ events: ["pull_request"],
246
+ };
247
+ const result = resolveGitHubForwardTargets({
248
+ targets: [target, reviewer],
249
+ host: "127.0.0.1",
250
+ port: 3000,
251
+ });
252
+ expect(result.ok && result.targets.map((t) => t.slug)).toEqual([
253
+ "fsd",
254
+ "reviewer",
255
+ ]);
256
+ expect(result.ok && unionForwardEvents(result.targets)).toEqual([
257
+ "pull_request",
258
+ "check_run",
259
+ ]);
260
+ });
261
+
262
+ it("applies an explicit event override to all targets", () => {
263
+ const result = resolveGitHubForwardTargets({
264
+ targets: [target, { ...target, slug: "reviewer" }],
265
+ host: "127.0.0.1",
266
+ port: 3000,
267
+ events: ["issues"],
268
+ });
269
+ expect(result.ok && result.targets.map((t) => t.events)).toEqual([
270
+ ["issues"],
271
+ ["issues"],
272
+ ]);
273
+ });
274
+
275
+ it("narrows by slug and channel", () => {
276
+ const other = { ...target, slug: "reviewer", channelId: "gh2" };
277
+ const result = resolveGitHubForwardTargets({
278
+ targets: [target, other],
279
+ slug: "reviewer",
280
+ channel: "gh2",
281
+ host: "127.0.0.1",
282
+ port: 3000,
283
+ });
284
+ expect(result.ok && result.targets).toHaveLength(1);
285
+ expect(result.ok && result.targets[0]?.channelId).toBe("gh2");
286
+ });
287
+
288
+ it("uses an explicit url with a single target's events", () => {
289
+ const result = resolveGitHubForwardTargets({
290
+ targets: [target],
291
+ host: "127.0.0.1",
292
+ port: 3000,
293
+ url: "https://example.ngrok.app/hook",
294
+ });
295
+ expect(result.ok && result.targets[0]?.url).toBe(
296
+ "https://example.ngrok.app/hook"
297
+ );
298
+ expect(result.ok && result.targets[0]?.events).toEqual([
299
+ "pull_request",
300
+ "check_run",
301
+ ]);
302
+ });
303
+
304
+ it("rejects --url when several channels match", () => {
305
+ const result = resolveGitHubForwardTargets({
306
+ targets: [target, { ...target, slug: "reviewer" }],
307
+ host: "127.0.0.1",
308
+ port: 3000,
309
+ url: "https://example.ngrok.app/hook",
310
+ });
311
+ expect(result.ok).toBe(false);
312
+ });
313
+
314
+ it("requires --events for --url when no single target matches", () => {
315
+ const result = resolveGitHubForwardTargets({
316
+ targets: [],
317
+ host: "127.0.0.1",
318
+ port: 3000,
319
+ url: "https://example.ngrok.app/hook",
320
+ });
321
+ expect(result.ok).toBe(false);
322
+ });
323
+
324
+ it("errors when no github channel exists", () => {
325
+ const result = resolveGitHubForwardTargets({
326
+ targets: [],
327
+ host: "127.0.0.1",
328
+ port: 3000,
329
+ });
330
+ expect(result.ok).toBe(false);
331
+ expect(!result.ok && result.error).toContain("no GitHub channel found");
332
+ });
333
+ });
334
+
335
+ describe("buildGitHubForwardArgs", () => {
336
+ it("builds repo + events + url args", () => {
337
+ expect(
338
+ buildGitHubForwardArgs({
339
+ url: "http://127.0.0.1:3000/v1/channels/github",
340
+ events: ["pull_request", "issue_comment"],
341
+ repo: "acme/widgets",
342
+ })
343
+ ).toEqual([
344
+ "webhook",
345
+ "forward",
346
+ "--url=http://127.0.0.1:3000/v1/channels/github",
347
+ "--events=pull_request,issue_comment",
348
+ "--repo=acme/widgets",
349
+ ]);
350
+ });
351
+
352
+ it("supports org + secret and dedupes events", () => {
353
+ const args = buildGitHubForwardArgs({
354
+ url: "http://127.0.0.1:3000/v1/channels/github",
355
+ events: ["pull_request", "pull_request"],
356
+ org: "octo-org",
357
+ secret: "s3cr3t",
358
+ });
359
+ expect(args).toContain("--org=octo-org");
360
+ expect(args).toContain("--secret=s3cr3t");
361
+ expect(args).toContain("--events=pull_request");
362
+ });
363
+ });
364
+
365
+ describe("parseGhWebhookExtensionInstalled", () => {
366
+ it("detects the extension in gh extension list output", () => {
367
+ expect(
368
+ parseGhWebhookExtensionInstalled("gh webhook\tcli/gh-webhook\tv1.0.0\n")
369
+ ).toBe(true);
370
+ });
371
+
372
+ it("returns false when absent", () => {
373
+ expect(parseGhWebhookExtensionInstalled("gh dash\tdlvhdr/gh-dash\n")).toBe(
374
+ false
375
+ );
376
+ });
377
+ });
378
+
379
+ describe("gh webhook extension pin", () => {
380
+ const listLine = `gh webhook\tcli/gh-webhook\t${GITHUB_WEBHOOK_EXTENSION_PIN}\n`;
381
+
382
+ it("parses the installed version", () => {
383
+ expect(parseGhWebhookExtensionVersion(listLine)).toBe(
384
+ GITHUB_WEBHOOK_EXTENSION_PIN
385
+ );
386
+ });
387
+
388
+ it("reports pin satisfaction", () => {
389
+ expect(ghWebhookExtensionPinSatisfied(listLine)).toBe(true);
390
+ expect(
391
+ ghWebhookExtensionPinSatisfied("gh webhook\tcli/gh-webhook\tv0.1.0\n")
392
+ ).toBe(false);
393
+ });
394
+
395
+ it("builds pinned install argv", () => {
396
+ expect(ghWebhookExtensionInstallArgs()).toEqual([
397
+ "extension",
398
+ "install",
399
+ "cli/gh-webhook",
400
+ "--pin",
401
+ GITHUB_WEBHOOK_EXTENSION_PIN,
402
+ ]);
403
+ expect(ghWebhookExtensionInstallArgs({ force: true })).toContain("--force");
404
+ });
405
+ });
406
+
407
+ describe("detectGitHubEnvTokenOverride", () => {
408
+ it("flags GITHUB_TOKEN, then GH_TOKEN", () => {
409
+ expect(detectGitHubEnvTokenOverride({ GITHUB_TOKEN: "x" })).toBe(
410
+ "GITHUB_TOKEN"
411
+ );
412
+ expect(detectGitHubEnvTokenOverride({ GH_TOKEN: "x" })).toBe("GH_TOKEN");
413
+ });
414
+
415
+ it("ignores empty/absent tokens", () => {
416
+ expect(detectGitHubEnvTokenOverride({})).toBeUndefined();
417
+ expect(
418
+ detectGitHubEnvTokenOverride({ GITHUB_TOKEN: "", GH_TOKEN: "" })
419
+ ).toBeUndefined();
420
+ });
421
+ });
422
+
423
+ describe("parseRepoFromRemoteUrl", () => {
424
+ it("parses https, ssh, and git@ remotes", () => {
425
+ expect(parseRepoFromRemoteUrl("https://github.com/acme/widgets.git")).toBe(
426
+ "acme/widgets"
427
+ );
428
+ expect(parseRepoFromRemoteUrl("git@github.com:acme/widgets.git")).toBe(
429
+ "acme/widgets"
430
+ );
431
+ expect(parseRepoFromRemoteUrl("ssh://git@github.com/acme/widgets")).toBe(
432
+ "acme/widgets"
433
+ );
434
+ expect(parseRepoFromRemoteUrl("https://github.com/acme/widgets/\n")).toBe(
435
+ "acme/widgets"
436
+ );
437
+ });
438
+
439
+ it("returns undefined for unrecognized input", () => {
440
+ expect(parseRepoFromRemoteUrl("")).toBeUndefined();
441
+ expect(parseRepoFromRemoteUrl("not-a-url")).toBeUndefined();
442
+ });
443
+ });
444
+
445
+ function mount(slug: string, channels: DiscoveredChannel[]): AgentMountLike {
446
+ return {
447
+ slug,
448
+ project: { channels } as unknown as AgentProject,
449
+ };
450
+ }
451
+
452
+ // Discovery erases the channel's state generic (`mod.default as
453
+ // ChannelDefinition`); mirror that here so githubChannel()'s typed state
454
+ // assigns to DiscoveredChannel.
455
+ function channel(id: string, definition: unknown): DiscoveredChannel {
456
+ return { id, definition: definition as DiscoveredChannel["definition"] };
457
+ }