Personal Telegram developer console for GitHub
test: define GitHub Actions service contract
davoodmehraban89 committed
Oct 3, 2026 at 05:36 UTC
d0acefc181cd892828ee08491d070d4a726881f1
1 file changed
+165
-27
test/github.test.js
+165
-27
@@ -2,57 +2,195 @@ import test from 'node:test';
2
import assert from 'node:assert/strict';
3
import { GitHubService } from '../src/github.js';
4
5
+async function withFetch(mock, fn) {
6
+ const originalFetch = globalThis.fetch;
7
+ globalThis.fetch = mock;
8
+ try {
9
+ return await fn();
10
+ } finally {
11
+ globalThis.fetch = originalFetch;
12
+ }
13
+}
14
+
15
+function json(data, status = 200) {
16
+ return new Response(JSON.stringify(data), {
17
+ status,
18
+ headers: { 'content-type': 'application/json' }
19
+ });
20
+}
21
+
22
+function repositoryContext(id = 123) {
23
+ return {
24
+ id,
25
+ name: 'nexa-dev-bot',
26
+ full_name: 'davoodmehraban89/nexa-dev-bot',
27
+ default_branch: 'main',
28
+ private: true
29
+ };
30
+}
31
+
32
test('GitHubService fails closed when GITHUB_TOKEN is missing', async () => {
33
const service = new GitHubService('', 'davoodmehraban89');
34
await assert.rejects(() => service.listRepositories(), /GITHUB_TOKEN/i);
35
});
36
37
test('GitHubService surfaces GitHub API failures instead of returning sample data', async () => {
11
- const originalFetch = globalThis.fetch;
12
- globalThis.fetch = async () => new Response(JSON.stringify({ message: 'Bad credentials' }), {
13
- status: 401,
14
- headers: { 'content-type': 'application/json' },
15
- });
16
-
17
- try {
18
- const service = new GitHubService('bad-token', 'davoodmehraban89');
19
- await assert.rejects(() => service.listPullRequests('Factor-easy'), /401|Bad credentials/i);
20
- } finally {
21
- globalThis.fetch = originalFetch;
22
- }
38
+ await withFetch(
39
+ async () => json({ message: 'Bad credentials' }, 401),
40
+ async () => {
41
+ const service = new GitHubService('bad-token', 'davoodmehraban89');
42
+ await assert.rejects(() => service.listPullRequests('Factor-easy'), /401|Bad credentials/i);
43
+ }
44
+ );
45
});
46
47
test('GitHubService repository listing uses the authenticated user endpoint', async () => {
26
- const originalFetch = globalThis.fetch;
48
let requestedUrl = '';
28
- globalThis.fetch = async (url) => {
49
+ await withFetch(async (url) => {
50
requestedUrl = String(url);
51
if (requestedUrl.startsWith('https://api.github.com/user/repos?')) {
31
- return new Response(JSON.stringify([
52
+ return json([
53
{
54
name: 'nexa-dev-bot',
55
full_name: 'davoodmehraban89/nexa-dev-bot',
56
private: true,
57
stargazers_count: 0,
58
forks_count: 0,
38
- description: 'Personal Telegram developer console for GitHub',
39
- },
40
- ]), { status: 200, headers: { 'content-type': 'application/json' } });
59
+ description: 'Personal Telegram developer console for GitHub'
60
+ }
61
+ ]);
62
}
42
- return new Response(JSON.stringify({ message: 'Not Found' }), {
43
- status: 404,
44
- headers: { 'content-type': 'application/json' },
45
- });
46
- };
47
-
48
- try {
63
+ return json({ message: 'Not Found' }, 404);
64
+ }, async () => {
65
const service = new GitHubService('token', 'davoodmehraban89');
66
const repos = await service.listRepositories();
67
assert.match(requestedUrl, /\/user\/repos\?/);
68
assert.equal(repos.length, 1);
69
assert.equal(repos[0].name, 'nexa-dev-bot');
70
assert.equal(repos[0].private, true);
55
- } finally {
56
- globalThis.fetch = originalFetch;
71
+ });
72
+});
73
+
74
+test('GitHubService lists and maps workflow runs for a numeric repository id', async () => {
75
+ const calls = [];
76
+ await withFetch(async (url, options = {}) => {
77
+ const value = String(url);
78
+ calls.push({ url: value, method: options.method || 'GET' });
79
+ if (value === 'https://api.github.com/repositories/123') return json(repositoryContext());
80
+ if (value === 'https://api.github.com/repos/davoodmehraban89/nexa-dev-bot/actions/runs?per_page=10') {
81
+ return json({ workflow_runs: [{
82
+ id: 77,
83
+ name: 'CI',
84
+ status: 'completed',
85
+ conclusion: 'failure',
86
+ event: 'push',
87
+ head_branch: 'main',
88
+ head_sha: 'abcdef0123456789',
89
+ run_number: 12,
90
+ html_url: 'https://github.com/x/actions/runs/77',
91
+ created_at: '2026-10-03T00:00:00Z',
92
+ updated_at: '2026-10-03T00:01:00Z'
93
+ }] });
94
+ }
95
+ return json({ message: 'Not Found' }, 404);
96
+ }, async () => {
97
+ const service = new GitHubService('token');
98
+ const runs = await service.listWorkflowRuns(123);
99
+ assert.equal(runs.length, 1);
100
+ assert.deepEqual(runs[0], {
101
+ id: 77,
102
+ name: 'CI',
103
+ status: 'completed',
104
+ conclusion: 'failure',
105
+ event: 'push',
106
+ branch: 'main',
107
+ sha: 'abcdef0',
108
+ runNumber: 12,
109
+ url: 'https://github.com/x/actions/runs/77',
110
+ createdAt: '2026-10-03T00:00:00Z',
111
+ updatedAt: '2026-10-03T00:01:00Z'
112
+ });
113
+ assert.equal(calls.at(-1).method, 'GET');
114
+ });
115
+});
116
+
117
+test('GitHubService gets workflow run and lists jobs with bounded step details', async () => {
118
+ await withFetch(async (url) => {
119
+ const value = String(url);
120
+ if (value === 'https://api.github.com/repositories/123') return json(repositoryContext());
121
+ if (value.endsWith('/actions/runs/77')) {
122
+ return json({
123
+ id: 77,
124
+ name: 'CI',
125
+ status: 'completed',
126
+ conclusion: 'failure',
127
+ event: 'push',
128
+ head_branch: 'main',
129
+ head_sha: 'abcdef0123456789',
130
+ run_number: 12,
131
+ html_url: 'https://github.com/x/actions/runs/77',
132
+ created_at: '2026-10-03T00:00:00Z',
133
+ updated_at: '2026-10-03T00:01:00Z'
134
+ });
135
+ }
136
+ if (value.endsWith('/actions/runs/77/jobs?per_page=20')) {
137
+ return json({ jobs: [{
138
+ id: 88,
139
+ name: 'test',
140
+ status: 'completed',
141
+ conclusion: 'failure',
142
+ started_at: '2026-10-03T00:00:10Z',
143
+ completed_at: '2026-10-03T00:00:50Z',
144
+ html_url: 'https://github.com/x/actions/runs/77/job/88',
145
+ steps: [
146
+ { number: 1, name: 'checkout', status: 'completed', conclusion: 'success' },
147
+ { number: 2, name: 'test', status: 'completed', conclusion: 'failure' }
148
+ ]
149
+ }] });
150
+ }
151
+ return json({ message: 'Not Found' }, 404);
152
+ }, async () => {
153
+ const service = new GitHubService('token');
154
+ const run = await service.getWorkflowRun(123, 77);
155
+ assert.equal(run.id, 77);
156
+ assert.equal(run.sha, 'abcdef0');
157
+ const jobs = await service.listWorkflowJobs(123, 77);
158
+ assert.equal(jobs.length, 1);
159
+ assert.equal(jobs[0].id, 88);
160
+ assert.equal(jobs[0].steps[1].conclusion, 'failure');
161
+ });
162
+});
163
+
164
+test('GitHubService uses POST endpoints for rerun failed and cancel and accepts empty success bodies', async () => {
165
+ const calls = [];
166
+ await withFetch(async (url, options = {}) => {
167
+ const value = String(url);
168
+ calls.push({ url: value, method: options.method || 'GET', headers: options.headers });
169
+ if (value === 'https://api.github.com/repositories/123') return json(repositoryContext());
170
+ if (value.endsWith('/actions/runs/77/rerun-failed-jobs')) return new Response(null, { status: 201 });
171
+ if (value.endsWith('/actions/runs/77/cancel')) return new Response(null, { status: 202 });
172
+ return json({ message: 'Not Found' }, 404);
173
+ }, async () => {
174
+ const service = new GitHubService('token');
175
+ assert.deepEqual(await service.rerunFailedWorkflowRun(123, 77), { accepted: true, status: 201 });
176
+ assert.deepEqual(await service.cancelWorkflowRun(123, 77), { accepted: true, status: 202 });
177
+ assert.equal(calls.find(call => call.url.endsWith('/rerun-failed-jobs')).method, 'POST');
178
+ assert.equal(calls.find(call => call.url.endsWith('/cancel')).method, 'POST');
179
+ });
180
+});
181
+
182
+test('GitHubService write actions surface provider permission/state errors', async () => {
183
+ for (const [status, message] of [[403, 'Resource not accessible by personal access token'], [409, 'Conflict'], [422, 'Validation Failed']]) {
184
+ await withFetch(async (url) => {
185
+ const value = String(url);
186
+ if (value === 'https://api.github.com/repositories/123') return json(repositoryContext());
187
+ return json({ message }, status);
188
+ }, async () => {
189
+ const service = new GitHubService('token');
190
+ await assert.rejects(
191
+ () => service.rerunFailedWorkflowRun(123, 77),
192
+ new RegExp(`${status}|${message}`, 'i')
193
+ );
194
+ });
195
}
196
});