47 lines
2.4 KiB
JavaScript
47 lines
2.4 KiB
JavaScript
import { test } from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import { sendTelegram } from '../server/telegram.mjs';
|
||
|
||
test('brief plain-text mode reaches Telegram request without HTML parsing or content changes',async()=>{
|
||
const oldFetch=globalThis.fetch,oldToken=process.env.TELEGRAM_BOT_TOKEN;
|
||
process.env.TELEGRAM_BOT_TOKEN='synthetic-test-token';
|
||
const payloads=[];
|
||
globalThis.fetch=async(url,options)=>{payloads.push(JSON.parse(options.body));return {ok:true,json:async()=>({ok:true})};};
|
||
try {
|
||
const text='Laboratuvar <test> & ürün 🧪';
|
||
assert.equal(await sendTelegram('test-chat',text,{parseMode:null}),true);
|
||
assert.equal(payloads[0].text,text);
|
||
assert.equal(Object.hasOwn(payloads[0],'parse_mode'),false);
|
||
await sendTelegram('test-chat','<b>Başlık</b>');
|
||
assert.equal(payloads[1].parse_mode,'HTML');
|
||
} finally {
|
||
globalThis.fetch=oldFetch;
|
||
if(oldToken===undefined)delete process.env.TELEGRAM_BOT_TOKEN;
|
||
else process.env.TELEGRAM_BOT_TOKEN=oldToken;
|
||
}
|
||
});
|
||
|
||
test('Telegram receipts preserve chat and message ids and reject incomplete or mismatched acceptance',async()=>{
|
||
const oldFetch=globalThis.fetch,oldToken=process.env.TELEGRAM_BOT_TOKEN;
|
||
process.env.TELEGRAM_BOT_TOKEN='synthetic-test-token';
|
||
let body={ok:true,result:{message_id:42,chat:{id:-100123}}},httpOk=true;
|
||
globalThis.fetch=async()=>({ok:httpOk,status:httpOk?200:502,json:async()=>body});
|
||
try {
|
||
const options={parseMode:null,returnReceipt:true};
|
||
assert.deepEqual(await sendTelegram('-100123','Test',options),{id:'-100123:42',chatId:'-100123',messageId:42});
|
||
for(const result of [undefined,{}, {message_id:0,chat:{id:-100123}}, {message_id:42,chat:{id:999}},
|
||
{message_id:'42',chat:{id:-100123}}, {message_id:42,chat:{id:0}}, {message_id:Number.MAX_SAFE_INTEGER+1,chat:{id:-100123}}]) {
|
||
body={ok:true,result};
|
||
await assert.rejects(sendTelegram('-100123','Test',options),/belirsiz/);
|
||
}
|
||
body={ok:true,result:{message_id:42,chat:{id:-100123}}};httpOk=false;
|
||
await assert.rejects(sendTelegram('-100123','Test',options),/HTTP 502/);
|
||
httpOk=true;body={ok:'true',result:{message_id:42,chat:{id:-100123}}};
|
||
await assert.rejects(sendTelegram('-100123','Test',options),/HTTP 200/);
|
||
} finally {
|
||
globalThis.fetch=oldFetch;
|
||
if(oldToken===undefined)delete process.env.TELEGRAM_BOT_TOKEN;
|
||
else process.env.TELEGRAM_BOT_TOKEN=oldToken;
|
||
}
|
||
});
|