Intelligence, Advanced Charting & Trading Platform
test(ops): prove Bale test dry-run guard, single-send, secrecy and opt-in chat discovery
davoodmehraban89 committed
Oct 15, 2026 at 19:30 UTC
a7d1b7e8668d4729e643436a295d84dd9009920c
1 file changed
+55
scripts/diagnostics/bale-one-message-test.test.mjs
new
+55
@@ -0,0 +1,55 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import {singleTest,tokenOK,chatOK,safeSummary} from './bale-one-message-test.mjs';
4
+const token='123456:abcDEFghiJKLmnoPQRstuVW';
5
+test('dry run does not inspect secrets or use network',async()=>{
6
+ let called=false;
7
+ const r=await singleTest({transport:()=>{called=true;throw Error('unwanted network')},env:{}});
8
+ assert.equal(r.status,'NOT_SENT');assert.equal(called,false);
9
+});
10
+test('requires token, chat and exact consent before sending',async()=>{
11
+ await assert.rejects(()=>singleTest({mode:'--send',env:{}}),/BALE_BOT_TOKEN/);
12
+ await assert.rejects(()=>singleTest({mode:'--send',env:{BALE_BOT_TOKEN:token}}),/CONSENT/);
13
+ await assert.rejects(()=>singleTest({mode:'--send',env:{BALE_BOT_TOKEN:token,BALE_TEST_CONSENT:'I_OWN_THIS_CHAT'}}),/CHAT_ID/);
14
+ assert.equal(chatOK('0'),false);
15
+ assert.equal(tokenOK('bad/foo?'),false);
16
+});
17
+test('one send only, correct endpoint and sanitized accepted response',async()=>{
18
+ let sent=0;
19
+ const r=await singleTest({
20
+ mode:'--send',env:{BALE_BOT_TOKEN:token,BALE_CHAT_ID:'456789',BALE_TEST_CONSENT:'I_OWN_THIS_CHAT'},
21
+ transport:async(uri,opt)=>{
22
+ sent++;
23
+ assert.ok(uri.startsWith('https://tapi.bale.ai/bot'));
24
+ assert.equal(opt.method,'POST');
25
+ assert.ok(!uri.includes('getUpdates'));
26
+ const body=JSON.parse(opt.body);
27
+ assert.equal(body.chat_id,'456789');
28
+ assert.ok(body.text.includes('آزمایشی'));
29
+ return {status:200,json:async()=>({ok:true,result:{chat:{id:456789},message_id:101}})};
30
+ }
31
+ });
32
+ assert.equal(sent,1);assert.equal(r.status,'Bale_API_ACCEPTED_NOT_READ_CONFIRMED');assert.equal(r.messageId,101);
33
+ assert.ok(!JSON.stringify(r).includes(token));
34
+});
35
+test('redacts full error payload and refuses mismatched chat',()=>{
36
+ assert.deepEqual(safeSummary({ok:false,description:'SECRET TOKEN',error_code:403},403,'13'),
37
+ {status:'FAILED',httpStatus:403,reason:'bale_api_error_redacted'});
38
+ assert.equal(safeSummary({ok:true,result:{chat:{id:9},message_id:10}},200,'13').status,'FAILED');
39
+});
40
+test('discover only matches consent nonce and ignores other chats',async()=>{
41
+ let count=0;
42
+ const r=await singleTest({mode:'--discover',
43
+ env:{BALE_BOT_TOKEN:token,BALE_DISCOVERY_NONCE:'abc123TEST'},
44
+ transport:async(uri,opt)=>{
45
+ count++;
46
+ assert.ok(uri.endsWith('/getUpdates'));
47
+ return {status:200,json:async()=>({ok:true,result:[
48
+ {message:{chat:{id:1},text:'/start wrong'}},
49
+ {message:{chat:{id:2},text:'/start abc123TEST'}},
50
+ {message:{chat:{id:3},text:'/start something'}}
51
+ ]})};
52
+ }
53
+ });
54
+ assert.equal(count,1);assert.deepEqual(r,{status:'CHAT_FOUND',chatId:'2'});
55
+});