import { describe, it, expect, vi } from 'vitest'; import { Api } from 'teleproto'; import bigInt from 'big-integer'; import { runBridge } from '../../src/commands/bridge/index.js'; import { mkdtemp, readFile, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; const user = new Api.User({id:bigInt('9007199254740993'),accessHash:bigInt(2),firstName:'Fixture'}); const client = () => ({getEntity:vi.fn(async()=>user),getDialogs:vi.fn(async()=>[{entity:user}]),sendMessage:vi.fn(async()=>({id:7,entities:[new Api.MessageEntityBold({offset:0,length:4})]})),deleteMessages:vi.fn(async()=>[1])}); describe('legacy bridge contracts using real Teleproto objects',()=>{ it.each(['main','reserve'])('keeps HTML DTO and parse mode for %s',async account=>{ const c=client();expect(await runBridge(c as any,'html',{account,chat:'fixture',text:'Test'})).toEqual({message_id:7,format:'html',entities:['MessageEntityBold']}); expect(c.sendMessage).toHaveBeenCalledOnce();expect(c.sendMessage.mock.calls[0][1]).toMatchObject({parseMode:'html'}); }); it('validates schedules before invoking Telegram and preserves server scheduling',async()=>{ const c=client();await expect(runBridge(c as any,'schedule',{account:'main',chat:'me',text:'fixture',when:'invalid'})).rejects.toThrow();expect(c.getEntity).not.toHaveBeenCalled(); const when=new Date(Date.now()+60000).toISOString();const result:any=await runBridge(c as any,'schedule',{account:'main',chat:'me',text:'fixture',when}); expect(result).toMatchObject({id:7,scheduled_for:when,account:'main'});expect(c.sendMessage.mock.calls[0][1].schedule).toBe(Math.floor(Date.parse(when)/1000)); }); it.each([true,false])('passes explicit delete revoke=%s',async revoke=>{ const c=client();await runBridge(c as any,'delete',{account:'main',chat:'fixture',ids:'1,2',revoke});expect(c.deleteMessages).toHaveBeenCalledWith(user,[1,2],{revoke}); }); it('rejects absent revoke, invalid accounts and arbitrary operations without requests',async()=>{ const c=client();for(const [op,p] of [['delete',{account:'main',chat:'me',ids:'1'}],['html',{account:'typo'}],['invoke',{account:'main'}]] as const)await expect(runBridge(c as any,op,p)).rejects.toThrow();expect(c.getEntity).not.toHaveBeenCalled(); }); it('never repeats a failed send or a missing send result',async()=>{ for(const missing of [false,true]){ const c=client();if(missing)c.sendMessage.mockResolvedValue(undefined as any);else c.sendMessage.mockRejectedValue(new Error('TIMEOUT')); await expect(runBridge(c as any,'html',{account:'main',chat:'me',text:'fixture'})).rejects.toThrow();expect(c.sendMessage).toHaveBeenCalledOnce(); } }); it('rejects ambiguous bare IDs rather than delivering to another peer kind',async()=>{ const c=client();c.getDialogs.mockResolvedValue([{entity:user},{entity:user}]);await expect(runBridge(c as any,'send-dialog',{account:'main',chat:String(user.id),text:'fixture'})).rejects.toThrow('ambiguous');expect(c.sendMessage).not.toHaveBeenCalled(); }); it('preserves DM array fields and large IDs',async()=>{ const c=client();c.getDialogs.mockResolvedValue([{entity:user,message:{date:100,message:'fixture'},unreadCount:2}] as any); expect(await runBridge(c as any,'recent-dms',{account:'main',since:0})).toEqual([{user_id:'9007199254740993',username:null,first_name:'Fixture',last_name:null,last_msg_date:100,last_msg_preview:'fixture',unread_count:2,is_self:false}]); }); it('writes downloaded bytes and preserves the media DTO',async()=>{ const dir=await mkdtemp(join(tmpdir(),'tg-bridge-')); try {const path=join(dir,'media');const c={...client(),getMessages:vi.fn(async()=>[{media:{}}]),downloadMedia:vi.fn(async()=>Buffer.from('fixture'))}; expect(await runBridge(c as any,'download-media',{account:'reserve',chat:'fixture',id:4,path})).toEqual({id:4,size:7,path});expect(await readFile(path,'utf8')).toBe('fixture'); }finally{await rm(dir,{recursive:true,force:true});} }); it('refuses protected story media before download',async()=>{ const story=new Api.StoryItem({id:1,date:100,expireDate:200,media:new Api.MessageMediaPhoto({photo:new Api.PhotoEmpty({id:bigInt(1)})}),noforwards:true}); const c={getInputEntity:vi.fn(async()=>new Api.InputPeerSelf()),invoke:vi.fn(async()=>({stories:[story]})),downloadMedia:vi.fn()}; await expect(runBridge(c as any,'stories',{account:'main',action:'download',chat:'fixture',story_id:1})).rejects.toThrow('MEDIA_PROTECTED');expect(c.downloadMedia).not.toHaveBeenCalled(); }); });