72 lines
4.7 KiB
TypeScript
72 lines
4.7 KiB
TypeScript
import { describe, it, expect, vi, afterEach } from 'vitest';
|
|
import { Api } from 'teleproto';
|
|
import { scanDialogs } from '../../src/commands/bridge/dialog-scan.js';
|
|
import { runOrganize } from '../../src/commands/bridge/organize.js';
|
|
import { dialogPage } from './dialog-scan-fixture.js';
|
|
|
|
afterEach(() => vi.useRealTimers());
|
|
describe('resumable dialog metadata', () => {
|
|
it('continues with server offsets, includes pins/archive, reuses collected pages', async () => {
|
|
const first = dialogPage(1, 100, true); first.dialogs[0].pinned = true; first.dialogs[0].folderId = 1;
|
|
const c = { invoke: vi.fn().mockResolvedValueOnce(first).mockResolvedValueOnce(dialogPage(101, 2)) };
|
|
const a = await scanDialogs(c as any, 'main', 100);
|
|
const b = await scanDialogs(c as any, 'main', 200, a.cursor);
|
|
expect(b.rows).toHaveLength(102); expect(b.complete).toBe(true);
|
|
expect(b.rows[0]).toMatchObject({ pinned: true, archived: true });
|
|
expect(c.invoke.mock.calls[1][0]).toMatchObject({ offsetId: 100, offsetDate: 1699999900, excludePinned: true });
|
|
expect(c.invoke.mock.calls[1][0].offsetPeer).toBeInstanceOf(Api.InputPeerUser);
|
|
await scanDialogs(c as any, 'main', 200, a.cursor);
|
|
expect(c.invoke).toHaveBeenCalledTimes(2);
|
|
expect(JSON.stringify(b)).not.toMatch(/987654321|PRIVATE BODY|accessHash/);
|
|
});
|
|
it('preserves progress at FloodWait and enforces cooldown even with refresh', async () => {
|
|
vi.useFakeTimers();
|
|
const c = { invoke: vi.fn().mockResolvedValueOnce(dialogPage(1, 100, true))
|
|
.mockRejectedValueOnce(Object.assign(new Error('FLOOD_WAIT_27'), { seconds: 27 }))
|
|
.mockResolvedValueOnce(dialogPage(101, 1)) };
|
|
const a = await scanDialogs(c as any, 'main', 200);
|
|
expect(a.scanned).toBe(100); expect(a.retry_after_seconds).toBe(27); expect(a.complete).toBe(false);
|
|
const waiting = await scanDialogs(c as any, 'main', 200, a.cursor);
|
|
expect(waiting.scanned).toBe(100);
|
|
await scanDialogs(c as any, 'main', 200, undefined, true);
|
|
expect(c.invoke).toHaveBeenCalledTimes(2);
|
|
vi.advanceTimersByTime(27000);
|
|
const b = await scanDialogs(c as any, 'main', 200, a.cursor);
|
|
expect(b.complete).toBe(true); expect(b.scanned).toBe(101);
|
|
expect(c.invoke.mock.calls[2][0].offsetId).toBe(100);
|
|
});
|
|
it('bounds each call and rejects cross-account/session and expired cursors', async () => {
|
|
vi.useFakeTimers(); let start = 1;
|
|
const c = { invoke: vi.fn(async () => { const r = dialogPage(start, 100, true); start += 100; return r; }) };
|
|
const a = await scanDialogs(c as any, 'main', 10000);
|
|
expect(a.scanned).toBe(500); expect(a.complete).toBe(false); expect(c.invoke).toHaveBeenCalledTimes(5);
|
|
await expect(scanDialogs(c as any, 'reserve', 100, a.cursor)).rejects.toThrow('expired');
|
|
await expect(scanDialogs({ invoke: c.invoke } as any, 'main', 100, a.cursor)).rejects.toThrow('expired');
|
|
vi.advanceTimersByTime(31 * 60000);
|
|
await expect(scanDialogs(c as any, 'main', 100, a.cursor)).rejects.toThrow('expired');
|
|
});
|
|
it('shares state across request views of one session; deduplicates moving rows', async () => {
|
|
const session = {}, invoke = vi.fn().mockResolvedValueOnce(dialogPage(1, 100, true)).mockResolvedValueOnce(dialogPage(100, 2));
|
|
const a = await scanDialogs({ session, invoke } as any, 'main', 100);
|
|
const b = await scanDialogs({ session, invoke } as any, 'main', 200, a.cursor);
|
|
expect(b.scanned).toBe(101);
|
|
});
|
|
it('does not retry unknown failures and rejects stalled cursors', async () => {
|
|
const c = { invoke: vi.fn().mockRejectedValue(new Error('TIMEOUT')) };
|
|
await expect(scanDialogs(c as any, 'main', 100)).rejects.toThrow('TIMEOUT');
|
|
expect(c.invoke).toHaveBeenCalledOnce();
|
|
c.invoke.mockResolvedValue(dialogPage(1, 100, true));
|
|
await expect(scanDialogs(c as any, 'main', 200)).rejects.toThrow('did not advance');
|
|
});
|
|
it('legacy offset pages and audits share one snapshot; inherited mute is resolved', async () => {
|
|
const c = { invoke: vi.fn().mockResolvedValueOnce(dialogPage(1, 100, true)).mockResolvedValueOnce(dialogPage(101, 2)),
|
|
getDialogFilters: vi.fn(async () => ({ filters: [], tagsEnabled: false })),
|
|
getNotifySettings: vi.fn(async () => ({ muteUntil: 2147483647 })) };
|
|
const first: any = await runOrganize(c as any, { action: 'dialogs', account: 'main', limit: 100 });
|
|
const second: any = await runOrganize(c as any, { action: 'dialogs', account: 'main', limit: 100, offset: 100 });
|
|
expect(second.chats).toHaveLength(2); expect(second.has_more).toBe(false);
|
|
const audit: any = await runOrganize(c as any, { action: 'audit', account: 'main', scan_limit: 200, cursor: first.cursor });
|
|
expect(audit.complete).toBe(true); expect(audit.chats[0].muted).toBe(true);
|
|
expect(c.invoke).toHaveBeenCalledTimes(2);
|
|
});
|
|
});
|