import { describe, it, expect, vi } from 'vitest'; import { Api } from 'teleproto'; import bigInt from 'big-integer'; import { dialogPage } from './dialog-scan-fixture.js'; import { runOrganize } from '../../src/commands/bridge/organize.js'; import { executeDaemonCommand } from '../../src/lib/daemon/execute.js'; const user = new Api.InputPeerUser({ userId: bigInt('9007199254740993'), accessHash: bigInt('123456789') }); const folder = () => new Api.DialogFilter({ id: 2, title: new Api.TextWithEntities({ text: 'Work', entities: [] }), contacts: true, color: 3, pinnedPeers: [user], includePeers: [], excludePeers: [] }); const client = () => ({ getDialogFilters: vi.fn(async () => ({ filters: [new Api.DialogFilterDefault(), folder()], tagsEnabled: true })), getInputEntity: vi.fn(async () => user), updateDialogFilter: vi.fn(async () => true), updateDialogFiltersOrder: vi.fn(async () => true), invoke: vi.fn(async () => dialogPage(1, 0)), editPeerFolders: vi.fn(), getNotifySettings: vi.fn(async () => ({ muteUntil: 0 })), updateNotifySettings: vi.fn(async () => true), markAsRead: vi.fn(), saveDraft: vi.fn(async () => true), getDialogs: vi.fn(async () => []) }); describe('organization operations', () => { it('returns serializable folder metadata without access hashes, preserving large peer IDs', async () => { const out: any = await runOrganize(client() as any, { action: 'folders', account: 'main' }); expect(out.folders[1]).toMatchObject({ pinned: ['9007199254740993'], title: 'Work', color: 3 }); expect(JSON.stringify(out)).not.toContain('123456789'); expect(out.revision).toMatch(/^[a-f0-9]{64}$/); }); it('preview never mutates; patches preserve rules/pins/color and reject stale apply', async () => { const c = client(); const p = { action: 'folder-put', account: 'main', id: 2, patch: { title: 'New' } }; const preview: any = await runOrganize(c as any, p); expect(preview.after).toMatchObject({ title: 'New', color: 3, rules: { contacts: true }, pinned: ['9007199254740993'] }); expect(c.updateDialogFilter).not.toHaveBeenCalled(); await expect(runOrganize(c as any, { ...p, dry_run: false })).rejects.toThrow('requires'); await expect(runOrganize(c as any, { ...p, dry_run: false, revision: '0'.repeat(64) })).rejects.toThrow('CONFLICT'); expect(c.updateDialogFilter).not.toHaveBeenCalled(); await runOrganize(c as any, { ...p, dry_run: false, revision: preview.revision }); expect(c.updateDialogFilter).toHaveBeenCalledOnce(); expect(c.updateDialogFilter.mock.calls[0][1]).toBeInstanceOf(Api.DialogFilter); }); it('validates unknown fields, unsafe operations and bounds before requests', async () => { const c = client(); for (const p of [{ action: 'invoke', account: 'main' }, { action: 'folders', account: 'other' }, { action: 'dialogs', account: 'main', limit: 10000 }, { action: 'folder-put', account: 'main', id: 2, patch: { accessHash: 'x' } }]) { await expect(runOrganize(c as any, p)).rejects.toThrow(); } expect(c.getDialogs).not.toHaveBeenCalled(); expect(c.getDialogFilters).not.toHaveBeenCalled(); }); it('rejects missing/duplicate reorder entries and create collisions', async () => { const c = client(); for (const order of [[2], [0, 2, 2]]) await expect(runOrganize(c as any, { action: 'folder-order', account: 'main', order })).rejects.toThrow(); await expect(runOrganize(c as any, { action: 'folder-put', account: 'main', id: 2, create: true, patch: { title: 'New' } })).rejects.toThrow('exists'); expect(c.updateDialogFiltersOrder).not.toHaveBeenCalled(); }); it('refuses edits/deletion of shared folders', async () => { const c = client(); c.getDialogFilters.mockResolvedValue({ filters: [new Api.DialogFilterChatlist({ id: 2, title: new Api.TextWithEntities({ text: 'Shared', entities: [] }), pinnedPeers: [], includePeers: [] })], tagsEnabled: false } as any); for (const p of [{ action: 'folder-delete' }, { action: 'folder-put', patch: { title: 'New' } }]) await expect(runOrganize(c as any, { ...p, id: 2, account: 'main' })).rejects.toThrow(); expect(c.updateDialogFilter).not.toHaveBeenCalled(); }); it('defaults every state change to preview and never retries failures', async () => { const c = client(); for (const p of [{ action: 'archive', archived: true }, { action: 'read', max_id: 3 }, { action: 'dialog-pin', pinned: true }, { action: 'unread', unread: true }, { action: 'draft-save', text: 'hello' }]) expect(await runOrganize(c as any, { ...p, account: 'main', chat: 'fixture' })).toMatchObject({ dry_run: true }); expect(c.invoke).not.toHaveBeenCalled(); expect(c.markAsRead).not.toHaveBeenCalled(); expect(c.saveDraft).not.toHaveBeenCalled(); c.editPeerFolders.mockRejectedValue(new Error('TIMEOUT')); await expect(runOrganize(c as any, { action: 'archive', account: 'main', chat: 'fixture', archived: true, dry_run: false })).rejects.toThrow('TIMEOUT'); expect(c.editPeerFolders).toHaveBeenCalledOnce(); }); it('audit respects explicit inclusion and flags partial scans without reading message bodies', async () => { const c = client(); c.invoke.mockResolvedValue(dialogPage(1, 2)); const result: any = await runOrganize(c as any, { action: 'audit', account: 'main', scan_limit: 1 }); expect(result.complete).toBe(false); expect(result.chats[0]).toMatchObject({ folders: [2], muted: false }); expect(c.markAsRead).not.toHaveBeenCalled(); }); it('works through the daemon command boundary', async () => { const c = client(); const r = await executeDaemonCommand(c as any, 'fixture_main', { argv: ['bridge', 'call', 'organize', JSON.stringify({ action: 'folders', account: 'main' })] }, new AbortController().signal); expect(r.exitCode).toBe(0); expect(r.output.ok).toBe(true); }); });