|
| 1 | +const test = require('node:test'); |
| 2 | +const assert = require('node:assert/strict'); |
| 3 | + |
| 4 | +const { createChallengeSeeder } = require('../seed-first-challenge'); |
| 5 | + |
| 6 | +function fakeSpawn(result) { |
| 7 | + const calls = []; |
| 8 | + const spawn = (cmd, args, opts) => { |
| 9 | + calls.push({ cmd, args, opts }); |
| 10 | + return { status: 0, stdout: '', stderr: '', ...result }; |
| 11 | + }; |
| 12 | + return { calls, spawn }; |
| 13 | +} |
| 14 | + |
| 15 | +test('requires a token and a learning room directory', () => { |
| 16 | + assert.throws(() => createChallengeSeeder({ learningRoomDir: '/lr' }), /token/); |
| 17 | + assert.throws(() => createChallengeSeeder({ token: 't' }), /learningRoomDir/); |
| 18 | +}); |
| 19 | + |
| 20 | +test('runs the challenge progression script against the student repo', async () => { |
| 21 | + const { calls, spawn } = fakeSpawn({ stdout: 'Created Challenge 1' }); |
| 22 | + const seeder = createChallengeSeeder({ token: 'tok', learningRoomDir: '/lr', spawn }); |
| 23 | + |
| 24 | + const result = await seeder.seedFirstChallenge({ |
| 25 | + owner: 'org', |
| 26 | + repo: 'room', |
| 27 | + assignee: 'alice' |
| 28 | + }); |
| 29 | + |
| 30 | + assert.equal(calls.length, 1); |
| 31 | + const call = calls[0]; |
| 32 | + assert.equal(call.cmd, process.execPath); |
| 33 | + assert.match(call.args[0], /challenge-progression\.js$/); |
| 34 | + assert.equal(call.opts.cwd, '/lr'); |
| 35 | + assert.equal(call.opts.env.GITHUB_TOKEN, 'tok'); |
| 36 | + assert.equal(call.opts.env.GITHUB_REPOSITORY, 'org/room'); |
| 37 | + assert.equal(call.opts.env.START_CHALLENGE, '1'); |
| 38 | + assert.equal(call.opts.env.CHALLENGE_ASSIGNEE, 'alice'); |
| 39 | + assert.equal(result.output, 'Created Challenge 1'); |
| 40 | +}); |
| 41 | + |
| 42 | +test('passes an empty assignee through as an empty string', async () => { |
| 43 | + const { calls, spawn } = fakeSpawn({}); |
| 44 | + const seeder = createChallengeSeeder({ token: 'tok', learningRoomDir: '/lr', spawn }); |
| 45 | + await seeder.seedFirstChallenge({ owner: 'org', repo: 'room' }); |
| 46 | + assert.equal(calls[0].opts.env.CHALLENGE_ASSIGNEE, ''); |
| 47 | +}); |
| 48 | + |
| 49 | +test('throws with script output when the script exits non-zero', async () => { |
| 50 | + const { spawn } = fakeSpawn({ status: 1, stderr: 'FATAL: creation exploded' }); |
| 51 | + const seeder = createChallengeSeeder({ token: 'tok', learningRoomDir: '/lr', spawn }); |
| 52 | + |
| 53 | + await assert.rejects( |
| 54 | + () => seeder.seedFirstChallenge({ owner: 'org', repo: 'room', assignee: 'alice' }), |
| 55 | + /creation exploded/ |
| 56 | + ); |
| 57 | +}); |
| 58 | + |
| 59 | +test('throws when the script cannot be spawned at all', async () => { |
| 60 | + const { spawn } = fakeSpawn({ error: new Error('ENOENT node missing'), status: null }); |
| 61 | + const seeder = createChallengeSeeder({ token: 'tok', learningRoomDir: '/lr', spawn }); |
| 62 | + |
| 63 | + await assert.rejects( |
| 64 | + () => seeder.seedFirstChallenge({ owner: 'org', repo: 'room', assignee: 'alice' }), |
| 65 | + /ENOENT/ |
| 66 | + ); |
| 67 | +}); |
0 commit comments