|
| 1 | +// Regression tests for Challenge 7 merge conflict seeding. The previous |
| 2 | +// implementation replaced a `[TODO]` placeholder in docs/welcome.md on main, |
| 3 | +// but that placeholder is always resolved back in Challenge 1, long before |
| 4 | +// Challenge 7 opens - so the seed silently skipped every time and no student |
| 5 | +// ever saw a real conflict (Community-Access/support#61, 2026-07-05). The |
| 6 | +// fix opens a dedicated branch and commits a different edit to the same line |
| 7 | +// on that branch and on main, then opens a PR between them, so a genuine |
| 8 | +// conflict exists regardless of what any earlier challenge did. |
| 9 | +const test = require('node:test'); |
| 10 | +const assert = require('node:assert/strict'); |
| 11 | +const path = require('node:path'); |
| 12 | + |
| 13 | +process.env.GITHUB_TOKEN = 'test-token'; |
| 14 | +process.env.GITHUB_REPOSITORY = 'org/room'; |
| 15 | +process.env.START_CHALLENGE = ''; |
| 16 | +process.env.GITHUB_EVENT_NAME = ''; |
| 17 | +process.env.CLOSED_ISSUE_TITLE = ''; |
| 18 | + |
| 19 | +// The script resolves its issue-template directory from the working |
| 20 | +// directory, so requiring it must happen from inside learning-room/. |
| 21 | +process.chdir(path.join(__dirname, '..', '..', '..', 'learning-room')); |
| 22 | + |
| 23 | +const { ensureMergeConflictPractice } = require('../../../learning-room/.github/scripts/challenge-progression'); |
| 24 | + |
| 25 | +const WELCOME_CONTENT = 'Welcome to the Learning Room!\n'; |
| 26 | +const WELCOME_SHA = 'welcome-sha'; |
| 27 | + |
| 28 | +function fakeRequest(overrides = {}) { |
| 29 | + const calls = []; |
| 30 | + const request = async (route, options = {}) => { |
| 31 | + const method = options.method || 'GET'; |
| 32 | + calls.push({ method, route, body: options.body }); |
| 33 | + for (const [pattern, handler] of Object.entries(overrides)) { |
| 34 | + const [overrideMethod, routePrefix] = pattern.split(' '); |
| 35 | + if (method === overrideMethod && route.startsWith(routePrefix)) { |
| 36 | + return handler({ route, options }); |
| 37 | + } |
| 38 | + } |
| 39 | + if (method === 'GET' && route.startsWith('/repos/org/room/pulls?')) { |
| 40 | + return []; |
| 41 | + } |
| 42 | + if (method === 'GET' && route.startsWith('/repos/org/room/git/ref/heads/main')) { |
| 43 | + return { object: { sha: 'main-sha' } }; |
| 44 | + } |
| 45 | + if (method === 'GET' && route.startsWith('/repos/org/room/contents/docs/welcome.md')) { |
| 46 | + return { content: Buffer.from(WELCOME_CONTENT).toString('base64'), sha: WELCOME_SHA }; |
| 47 | + } |
| 48 | + if (method === 'POST' && route === '/repos/org/room/pulls') { |
| 49 | + return { number: 99, html_url: 'https://example.test/pull/99' }; |
| 50 | + } |
| 51 | + return null; |
| 52 | + }; |
| 53 | + return { calls, request }; |
| 54 | +} |
| 55 | + |
| 56 | +test('opens a branch, two conflicting edits, a PR, and comments the issue', async () => { |
| 57 | + const { calls, request } = fakeRequest(); |
| 58 | + |
| 59 | + await ensureMergeConflictPractice(11, request); |
| 60 | + |
| 61 | + const refPosts = calls.filter((c) => c.method === 'POST' && c.route === '/repos/org/room/git/refs'); |
| 62 | + assert.equal(refPosts.length, 1); |
| 63 | + assert.equal(refPosts[0].body.ref, 'refs/heads/challenge-7/conflict-practice'); |
| 64 | + assert.equal(refPosts[0].body.sha, 'main-sha'); |
| 65 | + |
| 66 | + const filePuts = calls.filter((c) => c.method === 'PUT'); |
| 67 | + assert.equal(filePuts.length, 2); |
| 68 | + assert.equal(filePuts[0].body.branch, 'challenge-7/conflict-practice'); |
| 69 | + assert.equal(filePuts[1].body.branch, 'main'); |
| 70 | + const branchText = Buffer.from(filePuts[0].body.content, 'base64').toString('utf8'); |
| 71 | + const mainText = Buffer.from(filePuts[1].body.content, 'base64').toString('utf8'); |
| 72 | + assert.notEqual(branchText, mainText); |
| 73 | + assert.match(branchText, /practice branch/); |
| 74 | + assert.match(mainText, /directly on main/); |
| 75 | + |
| 76 | + const prPosts = calls.filter((c) => c.method === 'POST' && c.route === '/repos/org/room/pulls'); |
| 77 | + assert.equal(prPosts.length, 1); |
| 78 | + assert.equal(prPosts[0].body.head, 'challenge-7/conflict-practice'); |
| 79 | + assert.equal(prPosts[0].body.base, 'main'); |
| 80 | + assert.match(prPosts[0].body.body, /#11/); |
| 81 | + |
| 82 | + const issueComments = calls.filter((c) => c.method === 'POST' && c.route === '/repos/org/room/issues/11/comments'); |
| 83 | + assert.equal(issueComments.length, 1); |
| 84 | + assert.match(issueComments[0].body.body, /#99/); |
| 85 | +}); |
| 86 | + |
| 87 | +test('is idempotent: does nothing when a practice PR already exists in any state', async () => { |
| 88 | + const { calls, request } = fakeRequest({ |
| 89 | + 'GET /repos/org/room/pulls?': () => [{ number: 5, state: 'closed' }] |
| 90 | + }); |
| 91 | + |
| 92 | + await ensureMergeConflictPractice(11, request); |
| 93 | + |
| 94 | + const writes = calls.filter((c) => c.method === 'POST' || c.method === 'PUT'); |
| 95 | + assert.deepEqual(writes, [ |
| 96 | + { method: 'POST', route: '/repos/org/room/issues/11/comments', body: { body: 'Your merge conflict practice PR is ready: #5. Open that PR (not any other pull request you may already have open) and follow Step 1 below.' } } |
| 97 | + ]); |
| 98 | +}); |
| 99 | + |
| 100 | +test('is idempotent: skips when the conflict anchor is already on main', async () => { |
| 101 | + const { calls, request } = fakeRequest({ |
| 102 | + 'GET /repos/org/room/contents/docs/welcome.md': () => ({ |
| 103 | + content: Buffer.from(`${WELCOME_CONTENT}\n<!-- challenge-7-conflict-anchor -->\n`).toString('base64'), |
| 104 | + sha: WELCOME_SHA |
| 105 | + }) |
| 106 | + }); |
| 107 | + |
| 108 | + await ensureMergeConflictPractice(11, request); |
| 109 | + |
| 110 | + const writes = calls.filter((c) => c.method === 'POST' || c.method === 'PUT'); |
| 111 | + assert.deepEqual(writes, []); |
| 112 | +}); |
| 113 | + |
| 114 | +test('falls back to looking up the issue when no issue number is passed', async () => { |
| 115 | + const { calls, request } = fakeRequest({ |
| 116 | + 'GET /repos/org/room/issues?': () => [ |
| 117 | + { number: 21, title: 'Challenge 7: Survive a Merge Conflict (@student)' } |
| 118 | + ] |
| 119 | + }); |
| 120 | + |
| 121 | + await ensureMergeConflictPractice(null, request); |
| 122 | + |
| 123 | + const issueComments = calls.filter((c) => c.method === 'POST' && c.route === '/repos/org/room/issues/21/comments'); |
| 124 | + assert.equal(issueComments.length, 1); |
| 125 | +}); |
| 126 | + |
| 127 | +test('never throws: progression survives a seeding failure', async () => { |
| 128 | + const request = async () => { |
| 129 | + throw new Error('boom: no network'); |
| 130 | + }; |
| 131 | + |
| 132 | + await assert.doesNotReject(ensureMergeConflictPractice(11, request)); |
| 133 | +}); |
0 commit comments