|
| 1 | +// `tgcli doctor` lists synced chats missing from the dialog list that Telegram gave no |
| 2 | +// answer about. The archive is real, in a temporary store; Telegram is stubbed out. |
| 3 | + |
| 4 | +import fs from 'fs'; |
| 5 | +import os from 'os'; |
| 6 | +import path from 'path'; |
| 7 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 8 | + |
| 9 | +import MessageSyncService from '../message-sync-service.js'; |
| 10 | + |
| 11 | +vi.mock('../core/services.js', () => ({ |
| 12 | + createTelegramClient: vi.fn(() => ({ |
| 13 | + telegramClient: { |
| 14 | + isAuthorized: vi.fn().mockResolvedValue(true), |
| 15 | + destroy: vi.fn().mockResolvedValue(undefined), |
| 16 | + }, |
| 17 | + })), |
| 18 | + createMessageSyncService: vi.fn((telegramClient, { storeDir }) => ({ |
| 19 | + messageSyncService: new MessageSyncService(telegramClient, { dbPath: path.join(storeDir, 'messages.db') }), |
| 20 | + })), |
| 21 | + resolveValidatedConfig: vi.fn(), |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock('../core/update-check.js', async (importOriginal) => ({ |
| 25 | + ...(await importOriginal()), |
| 26 | + getUpdateStatus: () => ({ disabledReason: 'CI' }), |
| 27 | +})); |
| 28 | + |
| 29 | +const { buildProgram } = await import('../cli.js'); |
| 30 | + |
| 31 | +let storeDir; |
| 32 | +let stdout; |
| 33 | + |
| 34 | +beforeEach(() => { |
| 35 | + storeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tgcli-doctor-unanswered-')); |
| 36 | + vi.stubEnv('TGCLI_STORE', storeDir); |
| 37 | + stdout = []; |
| 38 | + vi.spyOn(console, 'log').mockImplementation((...args) => { stdout.push(args.join(' ')); }); |
| 39 | + vi.spyOn(process.stdout, 'write').mockImplementation((chunk) => { stdout.push(String(chunk)); return true; }); |
| 40 | + vi.spyOn(process.stderr, 'write').mockImplementation(() => true); |
| 41 | +}); |
| 42 | + |
| 43 | +afterEach(() => { |
| 44 | + vi.restoreAllMocks(); |
| 45 | + vi.unstubAllEnvs(); |
| 46 | + process.exitCode = undefined; |
| 47 | + fs.rmSync(storeDir, { recursive: true, force: true }); |
| 48 | +}); |
| 49 | + |
| 50 | +function seed(statements) { |
| 51 | + const service = new MessageSyncService(null, { dbPath: path.join(storeDir, 'messages.db') }); |
| 52 | + for (const [sql, ...params] of statements) { |
| 53 | + service.db.prepare(sql).run(...params); |
| 54 | + } |
| 55 | + service.close(); |
| 56 | +} |
| 57 | + |
| 58 | +async function doctor(...flags) { |
| 59 | + stdout.length = 0; |
| 60 | + const program = buildProgram(); |
| 61 | + program.exitOverride(); |
| 62 | + await program.parseAsync(['node', 'cli.js', ...flags, 'doctor']); |
| 63 | + expect(process.exitCode).toBeUndefined(); |
| 64 | + return stdout.join('\n'); |
| 65 | +} |
| 66 | + |
| 67 | +const chat = (id, { membership = 'unknown', inDialogs = 0, sync = 1, title = null } = {}) => [ |
| 68 | + 'INSERT INTO channels (channel_id, peer_title, sync_enabled, in_dialogs, membership, membership_checked_at) VALUES (?, ?, ?, ?, ?, ?)', |
| 69 | + id, title, sync, inDialogs, membership, Date.parse('2026-11-06T12:00:00Z'), |
| 70 | +]; |
| 71 | + |
| 72 | +describe('doctor unanswered chats', () => { |
| 73 | + it('says nothing when Telegram answered about every missing chat', async () => { |
| 74 | + seed([chat('-1000000000801', { membership: 'member' }), chat('-1000000000802', { membership: 'left', sync: 0 })]); |
| 75 | + |
| 76 | + expect(await doctor()).not.toContain('UNANSWERED'); |
| 77 | + expect(JSON.parse(await doctor('--json')).unansweredChats).toEqual([]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('lists them, ten at most, with the command that turns them off', async () => { |
| 81 | + seed([ |
| 82 | + ...Array.from({ length: 12 }, (_, index) => chat(String(-1000000000810 - index), { title: index === 0 ? 'Closed group' : null })), |
| 83 | + chat('-1000000000899', { inDialogs: 1, membership: null }), |
| 84 | + chat('-1000000000898', { sync: 0 }), |
| 85 | + ]); |
| 86 | + |
| 87 | + const lines = (await doctor()).split('\n'); |
| 88 | + |
| 89 | + expect(lines.find((line) => line.startsWith('UNANSWERED:'))).toContain('UNANSWERED: 12 synced chat(s) are missing from your dialog list'); |
| 90 | + expect(lines.find((line) => line.startsWith('UNANSWERED:'))).toContain('`tgcli channels unwatch --missing`'); |
| 91 | + expect(lines.filter((line) => /^ -100/.test(line))).toHaveLength(10); |
| 92 | + expect(lines).toContain(' -1000000000810 Closed group'); |
| 93 | + expect(lines).toContain(' … and 2 more; `tgcli --json doctor` lists them all.'); |
| 94 | + |
| 95 | + const { unansweredChats } = JSON.parse(await doctor('--json')); |
| 96 | + expect(unansweredChats).toHaveLength(12); |
| 97 | + expect(unansweredChats[0]).toEqual({ channelId: '-1000000000810', title: 'Closed group', checkedAt: '2026-11-06T12:00:00.000Z' }); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('turning off the unanswered chats', () => { |
| 102 | + it('turns off only those, as if the user had, and leaves them off when they come back', async () => { |
| 103 | + const service = new MessageSyncService(null, { dbPath: path.join(storeDir, 'messages.db') }); |
| 104 | + for (const [sql, ...params] of [ |
| 105 | + chat('-1000000000820'), |
| 106 | + chat('-1000000000821'), |
| 107 | + chat('-1000000000822', { membership: 'member' }), |
| 108 | + chat('-1000000000823', { inDialogs: 1, membership: null }), |
| 109 | + ]) { |
| 110 | + service.db.prepare(sql).run(...params); |
| 111 | + } |
| 112 | + |
| 113 | + expect(service.unwatchUnansweredChats()).toEqual(['-1000000000820', '-1000000000821']); |
| 114 | + const rows = Object.fromEntries(service.db.prepare('SELECT channel_id, sync_enabled, sync_off_reason FROM channels').all() |
| 115 | + .map((row) => [row.channel_id, [row.sync_enabled, row.sync_off_reason]])); |
| 116 | + expect(rows).toEqual({ |
| 117 | + '-1000000000820': [0, null], |
| 118 | + '-1000000000821': [0, null], |
| 119 | + '-1000000000822': [1, null], |
| 120 | + '-1000000000823': [1, null], |
| 121 | + }); |
| 122 | + expect(service.listUnansweredChats()).toEqual([]); |
| 123 | + service.close(); |
| 124 | + }); |
| 125 | +}); |
0 commit comments