|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const assert = require('node:assert/strict'); |
| 4 | +const test = require('node:test'); |
| 5 | + |
| 6 | +const { |
| 7 | + retireSupersededLadonReviews, |
| 8 | + supersededChangeRequests, |
| 9 | +} = require('./retire-superseded-ladon-reviews.cjs'); |
| 10 | + |
| 11 | +const BOT = 'aao-secretariat[bot]'; |
| 12 | +const HEAD = 'new-head'; |
| 13 | + |
| 14 | +function review(id, state, commitId = HEAD, login = BOT) { |
| 15 | + return { id, state, commit_id: commitId, user: { login } }; |
| 16 | +} |
| 17 | + |
| 18 | +test('selects only the bot change requests superseded by its final head approval', () => { |
| 19 | + const reviews = [ |
| 20 | + review(10, 'CHANGES_REQUESTED', 'old-head'), |
| 21 | + review(11, 'CHANGES_REQUESTED'), |
| 22 | + review(12, 'CHANGES_REQUESTED', 'old-head', 'human-reviewer'), |
| 23 | + review(13, 'DISMISSED', 'old-head'), |
| 24 | + review(14, 'APPROVED'), |
| 25 | + ]; |
| 26 | + |
| 27 | + assert.deepEqual( |
| 28 | + supersededChangeRequests(reviews, 'AAO-SECRETARIAT[BOT]', HEAD).map( |
| 29 | + ({ id }) => id, |
| 30 | + ), |
| 31 | + [10, 11], |
| 32 | + ); |
| 33 | +}); |
| 34 | + |
| 35 | +test('keeps change requests when the latest bot review is not an approval', () => { |
| 36 | + const reviews = [ |
| 37 | + review(20, 'CHANGES_REQUESTED', 'old-head'), |
| 38 | + review(21, 'APPROVED'), |
| 39 | + review(22, 'COMMENTED'), |
| 40 | + ]; |
| 41 | + |
| 42 | + assert.deepEqual(supersededChangeRequests(reviews, BOT, HEAD), []); |
| 43 | +}); |
| 44 | + |
| 45 | +test('keeps change requests when the latest approval targets an older head', () => { |
| 46 | + const reviews = [ |
| 47 | + review(30, 'CHANGES_REQUESTED', 'older-head'), |
| 48 | + review(31, 'APPROVED', 'previous-head'), |
| 49 | + ]; |
| 50 | + |
| 51 | + assert.deepEqual(supersededChangeRequests(reviews, BOT, HEAD), []); |
| 52 | +}); |
| 53 | + |
| 54 | +test('dismisses every selected review through the pull request API', async () => { |
| 55 | + const dismissed = []; |
| 56 | + const listReviews = Symbol('listReviews'); |
| 57 | + const github = { |
| 58 | + paginate: async (method, params) => { |
| 59 | + assert.equal(method, listReviews); |
| 60 | + assert.deepEqual(params, { |
| 61 | + owner: 'adcontextprotocol', |
| 62 | + repo: 'adcp-client-python', |
| 63 | + pull_number: 1134, |
| 64 | + per_page: 100, |
| 65 | + }); |
| 66 | + return [ |
| 67 | + review(40, 'CHANGES_REQUESTED', 'old-head'), |
| 68 | + review(41, 'APPROVED'), |
| 69 | + ]; |
| 70 | + }, |
| 71 | + rest: { |
| 72 | + pulls: { |
| 73 | + listReviews, |
| 74 | + dismissReview: async (params) => dismissed.push(params), |
| 75 | + }, |
| 76 | + }, |
| 77 | + }; |
| 78 | + |
| 79 | + const ids = await retireSupersededLadonReviews({ |
| 80 | + github, |
| 81 | + owner: 'adcontextprotocol', |
| 82 | + repo: 'adcp-client-python', |
| 83 | + pullNumber: 1134, |
| 84 | + botLogin: BOT, |
| 85 | + headSha: HEAD, |
| 86 | + }); |
| 87 | + |
| 88 | + assert.deepEqual(ids, [40]); |
| 89 | + assert.deepEqual(dismissed, [ |
| 90 | + { |
| 91 | + owner: 'adcontextprotocol', |
| 92 | + repo: 'adcp-client-python', |
| 93 | + pull_number: 1134, |
| 94 | + review_id: 40, |
| 95 | + message: `Superseded by Ladon approval of ${HEAD}.`, |
| 96 | + event: 'DISMISS', |
| 97 | + }, |
| 98 | + ]); |
| 99 | +}); |
0 commit comments