Personal Telegram developer console for GitHub
test: surface approved provider write failures
davoodmehraban89 committed
Oct 3, 2026 at 08:43 UTC
075f0349f67c7cd258de5cccad978a00fa637237
1 file changed
+133
test/approval-provider-error-ui.test.js
new
+133
@@ -0,0 +1,133 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import worker from '../src/index.js';
4
+
5
+function makeReplayNamespace() {
6
+ const used = new Set();
7
+ return {
8
+ idFromName(name) { return String(name); },
9
+ get(id) {
10
+ return {
11
+ async fetch(request) {
12
+ const body = await request.json();
13
+ const key = String(id);
14
+ if (used.has(key)) return Response.json({ accepted: false, reason: 'already_used' }, { status: 409 });
15
+ used.add(key);
16
+ return Response.json({ accepted: true, expiresAt: body.expiresAt });
17
+ }
18
+ };
19
+ }
20
+ };
21
+}
22
+
23
+const env = {
24
+ TELEGRAM_WEBHOOK_SECRET: 'right',
25
+ OWNER_TELEGRAM_ID: '123',
26
+ TELEGRAM_BOT_TOKEN: 'telegram-test-token',
27
+ GITHUB_TOKEN: 'github-test-token',
28
+ GITHUB_OWNER: 'davoodmehraban89',
29
+ APPROVAL_SIGNING_SECRET: 'approval-test-secret',
30
+ APPROVAL_REPLAY_GUARD: makeReplayNamespace()
31
+};
32
+
33
+function webhook(update) {
34
+ return new Request('https://example.test/telegram/webhook', {
35
+ method: 'POST',
36
+ headers: {
37
+ 'content-type': 'application/json',
38
+ 'X-Telegram-Bot-Api-Secret-Token': 'right'
39
+ },
40
+ body: JSON.stringify(update)
41
+ });
42
+}
43
+
44
+function json(data, status = 200) {
45
+ return new Response(JSON.stringify(data), {
46
+ status,
47
+ headers: { 'content-type': 'application/json' }
48
+ });
49
+}
50
+
51
+function installMock() {
52
+ const telegramCalls = [];
53
+ const githubCalls = [];
54
+ const originalFetch = globalThis.fetch;
55
+
56
+ globalThis.fetch = async (url, options = {}) => {
57
+ const value = String(url);
58
+ if (value.startsWith('https://api.telegram.org/')) {
59
+ const method = value.split('/').at(-1);
60
+ const body = options.body ? JSON.parse(options.body) : {};
61
+ telegramCalls.push({ method, body });
62
+ return json({ ok: true, result: { message_id: body.message_id || 950, ...body } });
63
+ }
64
+ if (value === 'https://api.github.com/repositories/1001') {
65
+ githubCalls.push({ url: value, method: options.method || 'GET' });
66
+ return json({ id: 1001, name: 'demo', full_name: 'davoodmehraban89/demo' });
67
+ }
68
+ if (value === 'https://api.github.com/repos/davoodmehraban89/demo/actions/runs/55/rerun-failed-jobs') {
69
+ githubCalls.push({ url: value, method: options.method || 'GET' });
70
+ return json({ message: 'Resource not accessible by personal access token' }, 403);
71
+ }
72
+ throw new Error(`Unexpected URL: ${value}`);
73
+ };
74
+
75
+ return { telegramCalls, githubCalls, restore() { globalThis.fetch = originalFetch; } };
76
+}
77
+
78
+async function sendUpdate(update) {
79
+ const response = await worker.fetch(webhook(update), env);
80
+ assert.equal(response.status, 200);
81
+ return response.json();
82
+}
83
+
84
+test('approved GitHub WRITE provider failure visibly updates Telegram and does not claim success', async () => {
85
+ const mock = installMock();
86
+ try {
87
+ const menuMessage = { message_id: 950, chat: { id: 123 } };
88
+ const issued = await sendUpdate({
89
+ callback_query: {
90
+ id: 'issue-provider-failure-approval',
91
+ from: { id: 123 },
92
+ data: 'rerun:1001:55',
93
+ message: menuMessage
94
+ }
95
+ });
96
+ assert.equal(issued.approvalRequired, true);
97
+
98
+ const approvalEdit = mock.telegramCalls.filter(call => call.method === 'editMessageText').at(-1);
99
+ const approveButton = approvalEdit.body.reply_markup.inline_keyboard
100
+ .flat()
101
+ .find(button => String(button.callback_data || '').startsWith('approve:'));
102
+ assert.ok(approveButton);
103
+
104
+ const result = await sendUpdate({
105
+ callback_query: {
106
+ id: 'approve-provider-failure',
107
+ from: { id: 123 },
108
+ data: approveButton.callback_data,
109
+ message: {
110
+ ...menuMessage,
111
+ text: approvalEdit.body.text,
112
+ reply_markup: approvalEdit.body.reply_markup
113
+ }
114
+ }
115
+ });
116
+
117
+ assert.equal(result.ok, false);
118
+ const posts = mock.githubCalls.filter(call => call.method === 'POST');
119
+ assert.equal(posts.length, 1, 'approved provider write should be attempted exactly once');
120
+
121
+ const edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
122
+ assert.equal(edits.length, 2, 'provider failure must visibly replace the approval message');
123
+ const feedback = edits.at(-1);
124
+ assert.match(feedback.body.text, /اجرا نشد|GitHub|403|دسترسی/);
125
+ assert.doesNotMatch(feedback.body.text, /موفق|success/i);
126
+ assert.ok(
127
+ feedback.body.reply_markup.inline_keyboard.flat().some(button => button.callback_data === 'run:1001:55'),
128
+ 'provider failure must provide a safe route back to the run'
129
+ );
130
+ } finally {
131
+ mock.restore();
132
+ }
133
+});
\ No newline at end of file