@@ -451,4 +451,124 @@ void main() {
451
451
});
452
452
});
453
453
});
454
+
455
+ group ('updateMessageFlags' , () {
456
+ Future <UpdateMessageFlagsResult > checkUpdateMessageFlags (
457
+ FakeApiConnection connection, {
458
+ required List <int > messages,
459
+ required UpdateMessageFlagsOp op,
460
+ required MessageFlag flag,
461
+ required Map <String , String > expected,
462
+ }) async {
463
+ final result = await updateMessageFlags (connection,
464
+ messages: messages, op: op, flag: flag);
465
+ check (connection.lastRequest).isNotNull ().isA< http.Request > ()
466
+ ..method.equals ('POST' )
467
+ ..url.path.equals ('/api/v1/messages/flags' )
468
+ ..bodyFields.deepEquals (expected);
469
+ return result;
470
+ }
471
+
472
+ test ('smoke' , () {
473
+ return FakeApiConnection .with_ ((connection) async {
474
+ connection.prepare (json:
475
+ UpdateMessageFlagsResult (messages: [1 , 2 ]).toJson ());
476
+ await checkUpdateMessageFlags (connection,
477
+ messages: [1 , 2 , 3 ],
478
+ op: UpdateMessageFlagsOp .add, flag: MessageFlag .read,
479
+ expected: {
480
+ 'messages' : jsonEncode ([1 , 2 , 3 ]),
481
+ 'op' : 'add' ,
482
+ 'flag' : 'read' ,
483
+ });
484
+ });
485
+ });
486
+ });
487
+
488
+ group ('updateMessageFlagsForNarrow' , () {
489
+ Future <UpdateMessageFlagsForNarrowResult > checkUpdateMessageFlagsForNarrow (
490
+ FakeApiConnection connection, {
491
+ required Anchor anchor,
492
+ required int numBefore,
493
+ required int numAfter,
494
+ required ApiNarrow narrow,
495
+ required UpdateMessageFlagsOp op,
496
+ required MessageFlag flag,
497
+ required Map <String , String > expected,
498
+ }) async {
499
+ final result = await updateMessageFlagsForNarrow (connection,
500
+ anchor: anchor, numBefore: numBefore, numAfter: numAfter,
501
+ narrow: narrow, op: op, flag: flag);
502
+ check (connection.lastRequest).isNotNull ().isA< http.Request > ()
503
+ ..method.equals ('POST' )
504
+ ..url.path.equals ('/api/v1/messages/flags/narrow' )
505
+ ..bodyFields.deepEquals (expected);
506
+ return result;
507
+ }
508
+
509
+ mkResult ({required bool foundOldest}) =>
510
+ UpdateMessageFlagsForNarrowResult (
511
+ processedCount: 11 , updatedCount: 3 ,
512
+ firstProcessedId: null , lastProcessedId: null ,
513
+ foundOldest: foundOldest, foundNewest: true );
514
+
515
+ test ('smoke' , () {
516
+ return FakeApiConnection .with_ ((connection) async {
517
+ connection.prepare (json: mkResult (foundOldest: true ).toJson ());
518
+ await checkUpdateMessageFlagsForNarrow (connection,
519
+ anchor: AnchorCode .oldest,
520
+ numBefore: 0 , numAfter: 20 ,
521
+ narrow: const AllMessagesNarrow ().apiEncode (),
522
+ op: UpdateMessageFlagsOp .add, flag: MessageFlag .read,
523
+ expected: {
524
+ 'anchor' : 'oldest' ,
525
+ 'num_before' : '0' ,
526
+ 'num_after' : '20' ,
527
+ 'narrow' : jsonEncode ([]),
528
+ 'op' : 'add' ,
529
+ 'flag' : 'read' ,
530
+ });
531
+ });
532
+ });
533
+
534
+ test ('narrow uses resolveLegacyElements to encode' , () {
535
+ return FakeApiConnection .with_ (zulipFeatureLevel: 176 , (connection) async {
536
+ connection.prepare (json: mkResult (foundOldest: true ).toJson ());
537
+ await checkUpdateMessageFlagsForNarrow (connection,
538
+ anchor: AnchorCode .oldest,
539
+ numBefore: 0 , numAfter: 20 ,
540
+ narrow: [ApiNarrowDm ([123 , 234 ])],
541
+ op: UpdateMessageFlagsOp .add, flag: MessageFlag .read,
542
+ expected: {
543
+ 'anchor' : 'oldest' ,
544
+ 'num_before' : '0' ,
545
+ 'num_after' : '20' ,
546
+ 'narrow' : jsonEncode ([
547
+ {'operator' : 'pm-with' , 'operand' : [123 , 234 ]},
548
+ ]),
549
+ 'op' : 'add' ,
550
+ 'flag' : 'read' ,
551
+ });
552
+ });
553
+ });
554
+
555
+ test ('numeric anchor' , () {
556
+ return FakeApiConnection .with_ ((connection) async {
557
+ connection.prepare (json: mkResult (foundOldest: false ).toJson ());
558
+ await checkUpdateMessageFlagsForNarrow (connection,
559
+ anchor: const NumericAnchor (42 ),
560
+ numBefore: 0 , numAfter: 20 ,
561
+ narrow: const AllMessagesNarrow ().apiEncode (),
562
+ op: UpdateMessageFlagsOp .add, flag: MessageFlag .read,
563
+ expected: {
564
+ 'anchor' : '42' ,
565
+ 'num_before' : '0' ,
566
+ 'num_after' : '20' ,
567
+ 'narrow' : jsonEncode ([]),
568
+ 'op' : 'add' ,
569
+ 'flag' : 'read' ,
570
+ });
571
+ });
572
+ });
573
+ });
454
574
}
0 commit comments