import { describe, it, expect, vi } from 'vitest'; import { Api, TelegramClient, sessions, errors } from 'teleproto'; import { StringSession as GramSession } from 'gramjs-reference/sessions/index.js'; import { AuthKey as GramKey } from 'gramjs-reference/crypto/AuthKey.js'; import { Logger } from 'teleproto/extensions/Logger.js'; import bigInt from 'big-integer'; import { translateTelegramError } from '../../src/lib/errors.js'; import { createRequestClient } from '../../src/lib/daemon/request-client.js'; describe('Teleproto offline compatibility against pinned GramJS reference',()=>{ it.each([[2,'149.154.167.51',443],[4,'2001:db8::1',80]] as const)('round-trips a synthetic GramJS session for DC %s',async(dc,ip,port)=>{ const old=new GramSession('');old.setDC(dc,ip,port);const key=new GramKey();await key.setKey(Buffer.alloc(256,7));old.setAuthKey(key); const serialized=old.save();const next=new sessions.StringSession(serialized);await next.load(); expect(next.dcId).toBe(dc);expect(next.serverAddress).toBe(ip);expect(next.port).toBe(port);expect(next.save()).toBe(serialized); const back=new GramSession(next.save());await back.load();expect(back.authKey?.getKey()).toEqual(Buffer.alloc(256,7)); }); it('serializes layer-232 poll and forum requests with actual TL constructors',()=>{ const poll=new Api.Poll({id:bigInt.zero,hash:bigInt.zero,question:new Api.TextWithEntities({text:'Question',entities:[]}),answers:[new Api.InputPollAnswer({text:new Api.TextWithEntities({text:'Answer',entities:[]})})],quiz:true}); const media=new Api.InputMediaPoll({poll,correctAnswers:[0]});expect(media.getBytes().length).toBeGreaterThan(20); const topics=new Api.messages.GetForumTopics({peer:new Api.InputPeerSelf(),offsetDate:0,offsetId:0,offsetTopic:0,limit:10});expect(topics.getBytes().length).toBeGreaterThan(12); const personal=new Api.photos.UploadContactProfilePhoto({userId:new Api.InputUserSelf(),save:true,suggest:false,file:new Api.InputFile({id:bigInt(1),parts:1,name:'fixture.jpg',md5Checksum:''})});expect(personal.getBytes().length).toBeGreaterThan(20); }); it('preserves FloodWait mapping with actual Teleproto errors',()=>{ const error=new errors.FloodWaitError({capture:30,request:new Api.help.GetConfig()});expect(translateTelegramError(error).code).toBe('FLOOD'); }); it('uses SDK reconnect catch-up without tying it to a cancelled command',async()=>{ const client=new TelegramClient(new sessions.StringSession(''),1,'synthetic',{baseLogger:new Logger('none'),catchUp:true}); const getMe=vi.spyOn(client,'getMe').mockResolvedValue(new Api.User({id:bigInt(1)})); const catchUp=vi.spyOn((client as any).updateManager,'catchUp').mockResolvedValue(undefined); const start=vi.spyOn((client as any)._updateLoop,'start').mockImplementation(()=>{}); try {const controller=new AbortController();const view=createRequestClient(client,controller.signal);const recovering=(view.client as any)._handleReconnect();controller.abort();await recovering;await view.settled();expect(getMe).toHaveBeenCalledOnce();expect(catchUp).toHaveBeenCalledOnce();expect(start).toHaveBeenCalledOnce();} finally{await client.destroy();} }); });