Repository navigation
Expand file tree
/
Copy pathprovision-core.test.js
More file actions
271 lines (240 loc) · 9.61 KB
/
Copy pathprovision-core.test.js
File metadata and controls
271 lines (240 loc) · 9.61 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
const test = require('node:test');
const assert = require('node:assert/strict');
const { provisionRoster, defaultRepoName, missingWorkflows, isSecondaryRateLimit } = require('../provision-core');
const { upsertLearner, emptyRoster } = require('../roster');
const REQUIRED = ['pr-validation-bot.yml', 'student-progression.yml', 'content-validation.yml'];
function makeClient(overrides = {}) {
const state = {
repos: new Map(), // repoName -> { workflows: [...] }
calls: { create: 0, collaborator: 0, list: 0 }
};
const client = {
async repoExists({ repo }) {
return state.repos.has(repo);
},
async createFromTemplate({ repo }) {
state.calls.create += 1;
state.repos.set(repo, { workflows: [...REQUIRED] });
return { full_name: repo };
},
async ensureCollaborator() {
state.calls.collaborator += 1;
return { status: 'invited' };
},
async listWorkflowPaths({ repo }) {
state.calls.list += 1;
const r = state.repos.get(repo);
return r ? r.workflows : [];
},
async getDefaultBranchSha() {
return 'abc123';
},
...overrides
};
return { client, state };
}
const noSleep = async () => {};
const config = {
studentOwner: 'Community-Access',
templateOwner: 'Community-Access',
templateRepo: 'learning-room-template',
requiredWorkflows: REQUIRED,
delayMs: 0
};
function rosterWith(...learners) {
let r = emptyRoster();
for (const l of learners) r = upsertLearner(r, l);
return r;
}
test('defaultRepoName is deterministic and sanitized', () => {
assert.equal(defaultRepoName('Octo.Cat', 'Spring 2026'), 'learning-room-spring-2026-octo-cat');
});
test('missingWorkflows is case-insensitive', () => {
assert.deepEqual(missingWorkflows(['A.yml', 'B.yml'], ['a.yml']), ['B.yml']);
});
test('provisions a new learner end to end', async () => {
const { client, state } = makeClient();
const roster = rosterWith({ github_handle: 'alice', cohort_id: 'c1' });
const { roster: out, log, summary } = await provisionRoster({ roster, client, config, sleep: noSleep });
assert.equal(out.learners[0].provision_state, 'provisioned');
assert.equal(out.learners[0].learning_room_repo, 'Community-Access/learning-room-c1-alice');
assert.equal(state.calls.create, 1);
assert.equal(state.calls.collaborator, 1);
assert.equal(summary.created, 1);
assert.equal(log[0].result, 'created');
assert.equal(log[0].template_sha, 'abc123');
});
test('idempotent: existing complete repo is already-exists, no create', async () => {
const { client, state } = makeClient();
state.repos.set('learning-room-c1-alice', { workflows: [...REQUIRED] });
const roster = rosterWith({ github_handle: 'alice', cohort_id: 'c1' });
const { roster: out, summary } = await provisionRoster({ roster, client, config, sleep: noSleep });
assert.equal(state.calls.create, 0);
assert.equal(out.learners[0].provision_state, 'provisioned');
assert.equal(summary.already_exists, 1);
});
test('heals an incomplete repo when seedContent is available', async () => {
let seeded = false;
const { client, state } = makeClient({
async seedContent({ repo }) {
seeded = true;
state.repos.get(repo).workflows = [...REQUIRED];
}
});
state.repos.set('learning-room-c1-bob', { workflows: ['pr-validation-bot.yml'] });
const roster = rosterWith({ github_handle: 'bob', cohort_id: 'c1' });
const { roster: out, summary } = await provisionRoster({ roster, client, config, sleep: noSleep });
assert.ok(seeded);
assert.equal(out.learners[0].provision_state, 'healed');
assert.equal(summary.healed, 1);
});
test('waits for asynchronous template copy before verifying a fresh repo', async () => {
// GitHub's generate-from-template API returns before the content copy
// finishes; verification must wait instead of marking the learner failed.
const { client, state } = makeClient();
client.createFromTemplate = async ({ repo }) => {
state.calls.create += 1;
state.repos.set(repo, { workflows: [] });
return { full_name: repo };
};
let listCalls = 0;
client.listWorkflowPaths = async ({ repo }) => {
listCalls += 1;
if (listCalls >= 3) state.repos.get(repo).workflows = [...REQUIRED];
return state.repos.get(repo).workflows;
};
let slept = 0;
const countingSleep = async () => { slept += 1; };
const roster = rosterWith({ github_handle: 'dana', cohort_id: 'c1' });
const { roster: out, summary } = await provisionRoster({
roster,
client,
config,
sleep: countingSleep
});
assert.equal(out.learners[0].provision_state, 'provisioned');
assert.equal(summary.created, 1);
assert.equal(summary.error, 0);
assert.ok(slept >= 1, 'should have waited between verification attempts');
});
test('still fails when content never arrives after creation', async () => {
const { client, state } = makeClient();
client.createFromTemplate = async ({ repo }) => {
state.calls.create += 1;
state.repos.set(repo, { workflows: [] });
return { full_name: repo };
};
const roster = rosterWith({ github_handle: 'erin', cohort_id: 'c1' });
const { roster: out, log, summary } = await provisionRoster({
roster,
client,
config: { ...config, verifyRetries: 2, verifyDelayMs: 0 },
sleep: noSleep
});
assert.equal(out.learners[0].provision_state, 'failed');
assert.equal(summary.error, 1);
assert.match(log[0].error_detail, /Verification failed/);
});
test('fails loudly when repo exists but incomplete and cannot heal', async () => {
const { client, state } = makeClient();
state.repos.set('learning-room-c1-carol', { workflows: ['pr-validation-bot.yml'] });
const roster = rosterWith({ github_handle: 'carol', cohort_id: 'c1' });
const { roster: out, log, summary } = await provisionRoster({ roster, client, config, sleep: noSleep });
assert.equal(out.learners[0].provision_state, 'failed');
assert.equal(summary.error, 1);
assert.match(log[0].error_detail, /missing required workflows/);
});
test('retries on secondary rate limit then succeeds', async () => {
let attempts = 0;
const { client } = makeClient({
async repoExists() {
attempts += 1;
if (attempts === 1) {
const err = new Error('HTTP 403 secondary rate limit');
err.status = 403;
throw err;
}
return false;
}
});
const roster = rosterWith({ github_handle: 'dave', cohort_id: 'c1' });
const sleeps = [];
const { roster: out } = await provisionRoster({
roster,
client,
config,
sleep: async (ms) => sleeps.push(ms)
});
assert.equal(out.learners[0].provision_state, 'provisioned');
assert.ok(sleeps.includes(1000));
});
test('a failure does not stop the batch; others still provision', async () => {
let firstChecked = false;
const { client, state } = makeClient({
async repoExists({ repo }) {
if (repo.endsWith('-eve') && !firstChecked) {
firstChecked = true;
throw new Error('boom');
}
return state.repos.has(repo);
}
});
const roster = rosterWith(
{ github_handle: 'eve', cohort_id: 'c1' },
{ github_handle: 'frank', cohort_id: 'c1' }
);
const { roster: out, summary } = await provisionRoster({ roster, client, config, sleep: noSleep });
const eve = out.learners.find((l) => l.github_handle === 'eve');
const frank = out.learners.find((l) => l.github_handle === 'frank');
assert.equal(eve.provision_state, 'failed');
assert.equal(frank.provision_state, 'provisioned');
assert.equal(summary.error, 1);
assert.equal(summary.created, 1);
});
test('seeds the first challenge issue after workflows are verified', async () => {
const seedCalls = [];
const { client, state } = makeClient({
async seedFirstChallenge({ owner, repo, assignee }) {
seedCalls.push({ owner, repo, assignee, listCallsSoFar: state.calls.list });
}
});
const roster = rosterWith({ github_handle: 'alice', cohort_id: 'c1' });
const { roster: out, summary } = await provisionRoster({ roster, client, config, sleep: noSleep });
assert.equal(out.learners[0].provision_state, 'provisioned');
assert.equal(summary.error, 0);
assert.equal(seedCalls.length, 1);
assert.equal(seedCalls[0].owner, 'Community-Access');
assert.equal(seedCalls[0].repo, 'learning-room-c1-alice');
assert.equal(seedCalls[0].assignee, 'alice');
assert.ok(seedCalls[0].listCallsSoFar >= 1, 'seeding must happen after workflow verification');
});
test('seeds the first challenge on the already-exists path too', async () => {
const seedCalls = [];
const { client, state } = makeClient({
async seedFirstChallenge(args) {
seedCalls.push(args);
}
});
state.repos.set('learning-room-c1-alice', { workflows: [...REQUIRED] });
const roster = rosterWith({ github_handle: 'alice', cohort_id: 'c1' });
const { summary } = await provisionRoster({ roster, client, config, sleep: noSleep });
assert.equal(summary.already_exists, 1);
assert.equal(seedCalls.length, 1);
});
test('marks the learner failed when first-challenge seeding fails', async () => {
const { client } = makeClient({
async seedFirstChallenge() {
throw new Error('seed boom');
}
});
const roster = rosterWith({ github_handle: 'alice', cohort_id: 'c1' });
const { roster: out, log, summary } = await provisionRoster({ roster, client, config, sleep: noSleep });
assert.equal(out.learners[0].provision_state, 'failed');
assert.equal(summary.error, 1);
assert.match(log[0].error_detail, /seed boom/);
});
test('isSecondaryRateLimit detects status and message forms', () => {
assert.ok(isSecondaryRateLimit({ status: 429 }));
assert.ok(isSecondaryRateLimit({ message: 'oops HTTP 403 here' }));
assert.equal(isSecondaryRateLimit({ message: 'HTTP 404' }), false);
});