@@ -5,7 +5,7 @@ import fs from 'node:fs';
55import os from 'node:os' ;
66import path from 'node:path' ;
77
8- import { Long , tl } from '@mtcute/core' ;
8+ import { Long , MtPeerNotFoundError , tl } from '@mtcute/core' ;
99import { iterHistory } from '@mtcute/core/methods.js' ;
1010import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
1111
@@ -208,6 +208,49 @@ describe('backfill repair', () => {
208208 expect ( repairRow ( service ) ) . toMatchObject ( { dry_run : 0 , added_count : 2 } ) ;
209209 } ) ;
210210
211+ // A dry run takes the chat's row: over a repair under way it would drop the rest of it.
212+ it ( 'refuses a dry run over a repair still under way, and takes one after it' , async ( ) => {
213+ const service = setUp ( range ( 10 , 259 ) , [ 10 ] ) ;
214+ service . addRepairs ( { chats : [ CHANNEL_ID ] } ) ;
215+ await service . _repairNextPage ( service . _getNextRepair ( ) ) ;
216+
217+ expect ( ( ) => service . addRepairs ( { chats : [ CHANNEL_ID ] , dryRun : true } ) ) . toThrow ( / r e p a i r i s u n d e r w a y i n 1 o f t h e s e c h a t / ) ;
218+ expect ( ( ) => service . addRepairs ( { all : true , dryRun : true } ) ) . toThrow ( / u n d e r w a y / ) ;
219+ expect ( repairRow ( service ) ) . toMatchObject ( { status : 'in_progress' , dry_run : 0 , cursor_message_id : 160 } ) ;
220+
221+ await service . processQueue ( ) ;
222+ expect ( service . addRepairs ( { chats : [ CHANNEL_ID ] , dryRun : true } ) ) . toEqual ( [ CHANNEL_ID ] ) ;
223+ } ) ;
224+
225+ it ( 'counts in a dry run only this chat\'s rows as present' , async ( ) => {
226+ const service = setUp ( range ( 10 , 20 ) , [ 10 ] ) ;
227+ service . db . prepare ( 'INSERT INTO channels (channel_id, sync_enabled) VALUES (?, 1)' ) . run ( OTHER_ID ) ;
228+ service . insertMessagesTx ( range ( 11 , 20 ) . map ( ( id ) => service . _buildMessageRecord ( OTHER_ID , { id, date : BASE_DATE + id } ) ) ) ;
229+
230+ service . addRepairs ( { chats : [ CHANNEL_ID ] , dryRun : true } ) ;
231+ await service . processQueue ( ) ;
232+
233+ expect ( repairRow ( service ) ) . toMatchObject ( { dry_run : 1 , added_count : 10 } ) ;
234+ } ) ;
235+
236+ it ( 'adds what it needs to an archive made before dry runs and dialog membership' , async ( ) => {
237+ const first = setUp ( [ 10 , 11 ] , [ 10 ] ) ;
238+ const dbPath = first . db . name ;
239+ first . db . exec ( 'ALTER TABLE archive_repairs DROP COLUMN dry_run' ) ;
240+ first . db . exec ( 'ALTER TABLE channels DROP COLUMN in_dialogs' ) ;
241+ first . close ( ) ;
242+
243+ const service = makeService ( ) ;
244+ const columns = ( table ) => service . db . prepare ( `PRAGMA table_info(${ table } )` ) . all ( ) . map ( ( col ) => col . name ) ;
245+ expect ( service . db . name ) . toBe ( dbPath ) ;
246+ expect ( columns ( 'archive_repairs' ) ) . toContain ( 'dry_run' ) ;
247+ expect ( columns ( 'channels' ) ) . toContain ( 'in_dialogs' ) ;
248+
249+ service . addRepairs ( { chats : [ CHANNEL_ID ] , dryRun : true } ) ;
250+ await service . processQueue ( ) ;
251+ expect ( repairRow ( service ) ) . toMatchObject ( { status : 'idle' , dry_run : 1 , added_count : 1 } ) ;
252+ } ) ;
253+
211254 it ( 'stops at the date bound' , async ( ) => {
212255 const service = setUp ( range ( 100 , 200 ) , [ 100 , 101 , 200 ] ) ;
213256
@@ -374,6 +417,37 @@ describe('backfill repair', () => {
374417 expect ( repairRow ( service ) . status ) . toBe ( 'skipped' ) ;
375418 } ) ;
376419
420+ it . each ( [
421+ [ 'CHANNEL_INVALID' , ( ) => tl . RpcError . fromTl ( { _ : 'rpc_error' , errorCode : 400 , errorMessage : 'CHANNEL_INVALID' } ) ] ,
422+ [ 'a peer missing from the cache' , ( ) => new MtPeerNotFoundError ( 'Peer -1000000000500 is not found in local cache' ) ] ,
423+ ] ) ( 'skips a chat on %s' , async ( _ , makeError ) => {
424+ const service = setUp ( range ( 10 , 30 ) , [ 10 ] ) ;
425+ answer = ( ) => {
426+ throw makeError ( ) ;
427+ } ;
428+
429+ service . addRepairs ( { chats : [ CHANNEL_ID ] } ) ;
430+ await service . processQueue ( ) ;
431+
432+ expect ( repairRow ( service ) . status ) . toBe ( 'skipped' ) ;
433+ } ) ;
434+
435+ // These break every request, not this chat: skipping would give up on each chat in turn.
436+ it . each ( [
437+ [ 406 , 'AUTH_KEY_DUPLICATED' ] ,
438+ [ 420 , 'FROZEN_METHOD_INVALID' ] ,
439+ ] ) ( 'keeps %i %s a failure that is retried' , async ( errorCode , errorMessage ) => {
440+ const service = setUp ( range ( 10 , 30 ) , [ 10 ] ) ;
441+ answer = ( ) => {
442+ throw tl . RpcError . fromTl ( { _ : 'rpc_error' , errorCode, errorMessage } ) ;
443+ } ;
444+
445+ service . addRepairs ( { chats : [ CHANNEL_ID ] } ) ;
446+ await service . processQueue ( ) ;
447+
448+ expect ( repairRow ( service ) . status ) . toBe ( 'error' ) ;
449+ } ) ;
450+
377451 it ( 'stops for a chat whose sync was turned off, and stays stopped while it is off' , async ( ) => {
378452 const service = setUp ( range ( 10 , 30 ) , [ 10 ] ) ;
379453 service . addRepairs ( { chats : [ CHANNEL_ID ] } ) ;
0 commit comments