Personal Telegram developer console for GitHub

test(github): define safe PR merge contract

davoodmehraban89 committed Oct 5, 2026 at 04:40 UTC aa30f8dcd26a55ebbf69c3f5ff343968a7fddc5b
1 file changed +49
test/github-pr-merge.test.js new
+49
@@ -0,0 +1,49 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { GitHubService } from '../src/github.js';
4 +import { githubPluginManifest } from '../src/plugins/github/manifest.js';
5 +
6 +test('PR merge is a DESTRUCTIVE GitHub action', () => {
7 + assert.deepEqual(githubPluginManifest.actions['github.pull_request.merge'], { risk: 'DESTRUCTIVE' });
8 +});
9 +
10 +test('mergePullRequest binds mutation to the expected PR head SHA', async () => {
11 + const calls = [];
12 + const fetchImpl = async (url, options = {}) => {
13 + calls.push({ url: String(url), options });
14 + return new Response(JSON.stringify({ merged: true, sha: 'merge-sha', message: 'Pull Request successfully merged' }), {
15 + status: 200,
16 + headers: { 'content-type': 'application/json' }
17 + });
18 + };
19 + const github = new GitHubService({ token: 'token', fetchImpl });
20 + const result = await github.mergePullRequest('owner/repo', 42, {
21 + expectedHeadSha: 'abc123',
22 + mergeMethod: 'squash'
23 + });
24 + assert.equal(result.merged, true);
25 + assert.equal(calls.length, 1);
26 + assert.match(calls[0].url, /repos\/owner\/repo\/pulls\/42\/merge$/);
27 + assert.equal(calls[0].options.method, 'PUT');
28 + const body = JSON.parse(calls[0].options.body);
29 + assert.equal(body.sha, 'abc123');
30 + assert.equal(body.merge_method, 'squash');
31 +});
32 +
33 +test('mergePullRequest rejects missing exact-head binding before provider mutation', async () => {
34 + let called = false;
35 + const github = new GitHubService({ token: 'token', fetchImpl: async () => { called = true; throw new Error('must not call'); } });
36 + await assert.rejects(
37 + github.mergePullRequest('owner/repo', 42, { mergeMethod: 'squash' }),
38 + /head sha|required|expected/i
39 + );
40 + assert.equal(called, false);
41 +});
42 +
43 +test('mergePullRequest only accepts supported merge methods', async () => {
44 + const github = new GitHubService({ token: 'token', fetchImpl: async () => { throw new Error('must not call'); } });
45 + await assert.rejects(
46 + github.mergePullRequest('owner/repo', 42, { expectedHeadSha: 'abc123', mergeMethod: 'octopus' }),
47 + /merge method|unsupported/i
48 + );
49 +});