Personal Telegram developer console for GitHub
test: define Telegram approval flow contract
davoodmehraban89 committed
Oct 3, 2026 at 05:50 UTC
725cf67b64f5152e89e1fa9dcbbc270af76aacf4
1 file changed
+166
test/approval-flow.test.js
new
+166
@@ -0,0 +1,166 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import worker from '../src/index.js';
4
+
5
+const env = {
6
+ TELEGRAM_WEBHOOK_SECRET: 'right',
7
+ OWNER_TELEGRAM_ID: '123',
8
+ TELEGRAM_BOT_TOKEN: 'telegram-test-token',
9
+ GITHUB_TOKEN: 'github-test-token',
10
+ GITHUB_OWNER: 'davoodmehraban89',
11
+ APPROVAL_SIGNING_SECRET: 'approval-test-secret'
12
+};
13
+
14
+function webhook(update) {
15
+ return new Request('https://example.test/telegram/webhook', {
16
+ method: 'POST',
17
+ headers: {
18
+ 'content-type': 'application/json',
19
+ 'X-Telegram-Bot-Api-Secret-Token': 'right'
20
+ },
21
+ body: JSON.stringify(update)
22
+ });
23
+}
24
+
25
+function json(data, status = 200) {
26
+ return new Response(JSON.stringify(data), {
27
+ status,
28
+ headers: { 'content-type': 'application/json' }
29
+ });
30
+}
31
+
32
+function installMock() {
33
+ const telegramCalls = [];
34
+ const githubCalls = [];
35
+ const originalFetch = globalThis.fetch;
36
+
37
+ globalThis.fetch = async (url, options = {}) => {
38
+ const value = String(url);
39
+
40
+ if (value.startsWith('https://api.telegram.org/')) {
41
+ const method = value.split('/').at(-1);
42
+ const body = options.body ? JSON.parse(options.body) : {};
43
+ telegramCalls.push({ method, body });
44
+ return json({ ok: true, result: { message_id: body.message_id || 700, ...body } });
45
+ }
46
+
47
+ if (value === 'https://api.github.com/repositories/1001') {
48
+ githubCalls.push({ url: value, method: options.method || 'GET' });
49
+ return json({
50
+ id: 1001,
51
+ name: 'demo',
52
+ full_name: 'davoodmehraban89/demo',
53
+ private: true,
54
+ default_branch: 'main',
55
+ archived: false,
56
+ html_url: 'https://github.com/davoodmehraban89/demo'
57
+ });
58
+ }
59
+
60
+ if (value === 'https://api.github.com/repos/davoodmehraban89/demo/actions/runs/55/rerun-failed-jobs') {
61
+ githubCalls.push({ url: value, method: options.method || 'GET' });
62
+ return new Response('', { status: 201 });
63
+ }
64
+
65
+ throw new Error(`Unexpected URL: ${value}`);
66
+ };
67
+
68
+ return {
69
+ telegramCalls,
70
+ githubCalls,
71
+ restore() {
72
+ globalThis.fetch = originalFetch;
73
+ }
74
+ };
75
+}
76
+
77
+async function sendUpdate(update) {
78
+ const response = await worker.fetch(webhook(update), env);
79
+ assert.equal(response.status, 200);
80
+ return response.json();
81
+}
82
+
83
+test('WRITE callback renders owner-bound approval and performs no GitHub write before approval', async () => {
84
+ const mock = installMock();
85
+ try {
86
+ const menuMessage = { message_id: 700, chat: { id: 123 } };
87
+ const result = await sendUpdate({
88
+ callback_query: {
89
+ id: 'rerun-request',
90
+ from: { id: 123 },
91
+ data: 'rerun:1001:55',
92
+ message: menuMessage
93
+ }
94
+ });
95
+
96
+ assert.equal(result.ok, true);
97
+ assert.equal(result.approvalRequired, true);
98
+ assert.equal(mock.githubCalls.length, 0, 'GitHub write must not execute before approval');
99
+
100
+ const edit = mock.telegramCalls.find(call => call.method === 'editMessageText');
101
+ assert.ok(edit, 'approval request must edit the existing Telegram message');
102
+ assert.match(edit.body.text, /تأیید|اجرا/);
103
+
104
+ const approveButton = edit.body.reply_markup.inline_keyboard
105
+ .flat()
106
+ .find(button => String(button.callback_data || '').startsWith('approve:'));
107
+ assert.ok(approveButton, 'approval button must be present');
108
+ assert.ok(new TextEncoder().encode(approveButton.callback_data).length <= 64);
109
+ } finally {
110
+ mock.restore();
111
+ }
112
+});
113
+
114
+test('approve callback executes exactly once and replay is rejected', async () => {
115
+ const mock = installMock();
116
+ try {
117
+ const menuMessage = { message_id: 701, chat: { id: 123 } };
118
+
119
+ await sendUpdate({
120
+ callback_query: {
121
+ id: 'rerun-request',
122
+ from: { id: 123 },
123
+ data: 'rerun:1001:55',
124
+ message: menuMessage
125
+ }
126
+ });
127
+
128
+ const approvalEdit = mock.telegramCalls
129
+ .filter(call => call.method === 'editMessageText')
130
+ .at(-1);
131
+ const approveButton = approvalEdit.body.reply_markup.inline_keyboard
132
+ .flat()
133
+ .find(button => String(button.callback_data || '').startsWith('approve:'));
134
+ assert.ok(approveButton);
135
+
136
+ const first = await sendUpdate({
137
+ callback_query: {
138
+ id: 'approve-once',
139
+ from: { id: 123 },
140
+ data: approveButton.callback_data,
141
+ message: menuMessage
142
+ }
143
+ });
144
+ assert.equal(first.ok, true);
145
+
146
+ const writesAfterFirst = mock.githubCalls.filter(call => call.method === 'POST');
147
+ assert.equal(writesAfterFirst.length, 1);
148
+ assert.match(writesAfterFirst[0].url, /rerun-failed-jobs$/);
149
+
150
+ const replay = await sendUpdate({
151
+ callback_query: {
152
+ id: 'approve-replay',
153
+ from: { id: 123 },
154
+ data: approveButton.callback_data,
155
+ message: menuMessage
156
+ }
157
+ });
158
+ assert.equal(replay.ok, false);
159
+ assert.match(String(replay.error || ''), /used|replay|approval/i);
160
+
161
+ const writesAfterReplay = mock.githubCalls.filter(call => call.method === 'POST');
162
+ assert.equal(writesAfterReplay.length, 1, 'replay must not perform a second GitHub write');
163
+ } finally {
164
+ mock.restore();
165
+ }
166
+});