Repository navigation
Expand file tree
/
Copy pathgithub-actions-client.test.ts
More file actions
227 lines (213 loc) · 8.75 KB
/
Copy pathgithub-actions-client.test.ts
File metadata and controls
227 lines (213 loc) · 8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { describe, expect, it, vi } from "vitest";
import {
GitHubActionsClient,
type DelegateLifecycleEvent,
type DelegateRequest,
} from "../samples/github-actions-client.js";
const request: DelegateRequest = {
task: "Review retry handling",
repository: "octo/example",
ref: "main",
agentType: "researcher",
expectedOutput: "A concise report",
};
const invocation = {
sessionId: "session-1",
toolCallId: "tool-call-1",
};
function jsonResponse(value: unknown, status = 200): Response {
return new Response(JSON.stringify(value), {
status,
headers: { "Content-Type": "application/json" },
});
}
function createClient(fetchImplementation: typeof fetch, events: DelegateLifecycleEvent[] = []) {
return new GitHubActionsClient({
token: () => "test-token",
targets: [
{
repository: "octo/example",
workflow: "worker.yml",
allowedRefs: ["main"],
allowedAgentTypes: ["researcher", "editor"],
},
],
fetch: fetchImplementation,
pollIntervalMs: 1,
sleep: async () => {},
onLifecycle: (event) => events.push(event),
});
}
describe("GitHubActionsClient", () => {
it("dispatches once, correlates the run, and returns outputs", async () => {
const events: DelegateLifecycleEvent[] = [];
let runLookup = 0;
const fetchImplementation = vi.fn<typeof fetch>(async (input, init) => {
const url = String(input);
if (url.includes("/actions/workflows/worker.yml/runs")) {
runLookup += 1;
if (runLookup === 1) {
return jsonResponse({ workflow_runs: [] });
}
return jsonResponse({
workflow_runs: [
{
id: 42,
status: runLookup < 4 ? "in_progress" : "completed",
conclusion: runLookup < 4 ? null : "success",
html_url: "https://github.com/octo/example/actions/runs/42",
display_title: "SDK delegate 76364575d6bbe9ed561b09ed",
head_sha: "abc123",
},
],
});
}
if (url.endsWith("/actions/workflows/worker.yml/dispatches")) {
expect(init?.method).toBe("POST");
expect(init?.headers).toMatchObject({
Authorization: ["Bearer", "test-token"].join(" "),
});
const body = JSON.parse(String(init?.body)) as {
inputs: Record<string, string>;
};
expect(body.inputs.output_branch).toBe("agent/76364575d6bbe9ed561b09ed");
return new Response(null, { status: 204 });
}
if (url.endsWith("/actions/runs/42/artifacts")) {
return jsonResponse({
artifacts: [
{
id: 7,
name: "agent-result",
archive_download_url:
"https://api.github.com/repos/octo/example/actions/artifacts/7/zip",
expired: false,
},
],
});
}
if (url.includes("/pulls?")) {
return jsonResponse([
{
number: 9,
html_url: "https://github.com/octo/example/pull/9",
state: "open",
},
]);
}
throw new Error(`Unexpected request: ${init?.method ?? "GET"} ${url}`);
});
const result = await createClient(fetchImplementation, events).execute(request, invocation);
expect(result).toMatchObject({
runId: 42,
conclusion: "success",
outputBranch: "agent/76364575d6bbe9ed561b09ed",
artifacts: [{ id: 7, name: "agent-result" }],
pullRequests: [{ number: 9, state: "open" }],
});
expect(events.map((event) => event.state)).toEqual(["queued", "running", "completed"]);
expect(
fetchImplementation.mock.calls.filter(([url]) => String(url).endsWith("/dispatches"))
).toHaveLength(1);
});
it("reuses a correlated run instead of dispatching it again", async () => {
const fetchImplementation = vi.fn<typeof fetch>(async (input) => {
const url = String(input);
if (url.includes("/actions/workflows/worker.yml/runs")) {
return jsonResponse({
workflow_runs: [
{
id: 42,
status: "completed",
conclusion: "failure",
html_url: "https://github.com/octo/example/actions/runs/42",
display_title: "SDK delegate 76364575d6bbe9ed561b09ed",
head_sha: "abc123",
},
],
});
}
if (url.endsWith("/actions/runs/42/artifacts")) {
return jsonResponse({ artifacts: [] });
}
if (url.includes("/pulls?")) {
return jsonResponse([]);
}
throw new Error(`Unexpected request: ${url}`);
});
const result = await createClient(fetchImplementation).execute(request, invocation);
expect(result.conclusion).toBe("failure");
expect(
fetchImplementation.mock.calls.some(([url]) => String(url).endsWith("/dispatches"))
).toBe(false);
});
it("cancels the correlated run when the invocation is aborted", async () => {
const controller = new AbortController();
let runLookup = 0;
let cancellationRequested = false;
const fetchImplementation = vi.fn<typeof fetch>(async (input, init) => {
const url = String(input);
if (url.includes("/actions/workflows/worker.yml/runs")) {
runLookup += 1;
if (runLookup === 1) {
return jsonResponse({ workflow_runs: [] });
}
return jsonResponse({
workflow_runs: [
{
id: 42,
status: "in_progress",
conclusion: null,
html_url: "https://github.com/octo/example/actions/runs/42",
display_title: "SDK delegate 76364575d6bbe9ed561b09ed",
head_sha: "abc123",
},
],
});
}
if (url.endsWith("/actions/workflows/worker.yml/dispatches")) {
return new Response(null, { status: 204 });
}
if (url.endsWith("/actions/runs/42/cancel")) {
cancellationRequested = init?.method === "POST";
return new Response(null, { status: 202 });
}
throw new Error(`Unexpected request: ${url}`);
});
const client = new GitHubActionsClient({
token: () => "test-token",
targets: [
{
repository: "octo/example",
workflow: "worker.yml",
allowedRefs: ["main"],
allowedAgentTypes: ["researcher"],
},
],
fetch: fetchImplementation,
pollIntervalMs: 1,
sleep: async () => {
controller.abort();
throw Object.assign(new Error("aborted"), { name: "AbortError" });
},
});
await expect(
client.execute(request, { ...invocation, signal: controller.signal })
).rejects.toMatchObject({ name: "AbortError" });
expect(cancellationRequested).toBe(true);
});
it("rejects repositories, refs, and agent types outside the allowlist", async () => {
const fetchImplementation = vi.fn<typeof fetch>();
const client = createClient(fetchImplementation);
await expect(
client.dispatch({ ...request, repository: "other/example" }, invocation)
).rejects.toThrow("not allowlisted");
await expect(
client.dispatch({ ...request, ref: "feature/untrusted" }, invocation)
).rejects.toThrow("not allowlisted");
await expect(
client.dispatch({ ...request, agentType: "admin" }, invocation)
).rejects.toThrow("not allowlisted");
expect(fetchImplementation).not.toHaveBeenCalled();
});
});