-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscenario-harness.test.js
More file actions
1078 lines (946 loc) · 37.6 KB
/
Copy pathscenario-harness.test.js
File metadata and controls
1078 lines (946 loc) · 37.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @jest-environment node
*/
import { jest } from '@jest/globals';
import { createScenarioHarness } from '../../test-setup/scenario-harness.js';
import { t } from '../../i18n/index.js';
process.env.SUPPRESS_JEST_WARNINGS = '1';
const tr = (key, params = {}) => t('en', key, params, { fallbackLanguage: 'en' });
describe('Scenario Harness Integration', () => {
beforeEach(() => {
jest.useFakeTimers();
jest.setSystemTime(new Date('2025-01-10T09:00:00Z'));
});
afterEach(() => {
jest.useRealTimers();
});
it('lists rides as a paginated Rich Message with ascending global numbering', async () => {
const harness = await createScenarioHarness();
const chat = { id: 42, type: 'private' };
const user = { id: 42, first_name: 'List', username: 'listuser' };
for (let index = 1; index <= 6; index++) {
await harness.dispatchMessage({
text: `/newride\ntitle: Ride ${index}\nwhen: tomorrow 1${index}:00`,
chat,
from: user
});
}
await harness.dispatchMessage({ text: '/listrides', chat, from: user });
const firstPage = harness.outbox.replies.at(-1);
expect(firstPage.richMessage).toEqual(expect.objectContaining({
html: expect.stringContaining('<ol start="1">')
}));
expect(firstPage.text).toContain('<h3>The Rides You Created</h3>\n<hr/>');
expect(firstPage.text).not.toContain('<p><br></p>');
expect(firstPage.text).toContain('<li value="1"><b>🚲 Ride 6</b>');
expect(firstPage.text).toContain('<ul><li>📅 ');
expect(firstPage.text).toContain('</ul><br></li>');
expect(firstPage.text).toContain('</ul></li></ol>');
expect(firstPage.options.reply_markup.inline_keyboard.at(-1)).toEqual([
expect.objectContaining({ text: '✖️ Close', callback_data: 'list:close' })
]);
await harness.dispatchCallback({
data: 'list:2',
chat,
from: user,
message: {
message_id: firstPage.messageId,
chat,
from: { id: 0, is_bot: true, username: 'testbot' }
}
});
const secondPage = harness.outbox.edits.at(-1);
expect(secondPage.richMessage).toEqual(expect.objectContaining({
html: expect.stringContaining('<ol start="6">')
}));
expect(secondPage.text).toContain('<li value="6"><b>🚲 Ride 1</b>');
expect(secondPage.text).toContain('<hr/>\n<footer>Page 2/2</footer>');
await harness.dispatchCallback({
data: 'list:close',
chat,
from: user,
message: {
message_id: firstPage.messageId,
chat,
from: { id: 0, is_bot: true, username: 'testbot' }
}
});
expect(harness.outbox.deletes).toContainEqual({
chatId: chat.id,
messageId: firstPage.messageId
});
});
it('shows current planned status only when the list is requested or paged', async () => {
const harness = await createScenarioHarness();
const chat = { id: 43, type: 'private' };
const user = { id: 43, first_name: 'Plan', username: 'planner' };
await harness.dispatchMessage({
text: '/newride\ntitle: Planned Ride\nwhen: tomorrow 10:00',
chat,
from: user
});
const [ride] = harness.listRides();
await harness.dispatchMessage({ text: '/planned', chat, from: user });
const joinedList = harness.outbox.replies.at(-1);
expect(joinedList.text).toContain('Planned Ride');
expect(joinedList.text).toContain('🙋 Joined');
await harness.dispatchCallback({
data: `thinking:${ride.id}`,
chat,
from: user,
message: {
message_id: ride.messages[0].messageId,
text: 'Planned Ride',
chat,
from: { id: 0, is_bot: true, username: 'testbot' }
}
});
expect(harness.outbox.edits.some(edit => edit.messageId === joinedList.messageId)).toBe(false);
await harness.dispatchMessage({ text: '/planned', chat, from: user });
expect(harness.outbox.replies.at(-1).text).toContain('🤔 Thinking');
await harness.dispatchCallback({
data: `skip:${ride.id}`,
chat,
from: user,
message: {
message_id: ride.messages[0].messageId,
text: 'Planned Ride',
chat,
from: { id: 0, is_bot: true, username: 'testbot' }
}
});
await harness.dispatchMessage({ text: '/planned', chat, from: user });
expect(harness.outbox.replies.at(-1).text).toContain('You have no planned rides.');
});
it('sends /help as two Rich Messages with creation and management sections', async () => {
const harness = await createScenarioHarness();
await harness.dispatchMessage({
text: '/help',
chat: { id: 42, type: 'private' },
from: { id: 42, first_name: 'Help', username: 'helpuser' }
});
expect(harness.outbox.replies).toHaveLength(2);
expect(harness.outbox.replies[0].richMessage?.html).toContain('<h2>Ride Announcement Bot Help</h2>');
expect(harness.outbox.replies[0].richMessage?.html).toContain('</h3>\n<p>');
expect(harness.outbox.replies[0].richMessage?.html).not.toContain('<br><br>');
expect(harness.outbox.replies[0].richMessage?.html).toContain('/newride');
expect(harness.outbox.replies[1].richMessage?.html).toContain('<h2>Managing Rides</h2>');
expect(harness.outbox.replies[1].richMessage?.html).not.toContain('<br><br>');
expect(harness.outbox.replies[1].richMessage?.html).toContain('/settings');
expect(harness.outbox.replies[1].richMessage?.html).toContain('<hr/>');
expect(harness.outbox.replies.every(reply => reply.options.parse_mode == null)).toBe(true);
});
it('sends a persistent /start keyboard and handles its localized text actions', async () => {
const harness = await createScenarioHarness();
await harness.dispatchMessage({
text: '/start',
chat: { id: 42, type: 'private' },
from: { id: 42, first_name: 'Start', username: 'startuser' }
});
expect(harness.outbox.replies).toHaveLength(1);
const reply = harness.outbox.replies[0];
expect(reply.richMessage?.html).toContain(
'<img src="https://static.ridebot.valera.ws/ridebot/ride-announcement-teaser.jpg"/>'
);
expect(reply.richMessage?.html).toContain('<h2>Welcome to Ride Announcement Bot!</h2>');
expect(reply.richMessage?.html).toContain('<h3>Key Features:</h3>');
expect(reply.richMessage?.html).toContain('<h3>Quick Start:</h3>');
expect(reply.options.reply_markup.keyboard).toEqual([
[
{ text: '☰ Buttons' },
{ text: '➕ Create with wizard' },
{ text: '⚙️ Settings' },
{ text: '❓ Help' }
]
]);
expect(reply.options.reply_markup).toEqual(expect.objectContaining({
is_persistent: true,
resize_keyboard: true
}));
await harness.dispatchMessage({ text: '☰ Buttons' });
const expandedMenu = harness.outbox.replies.at(-1);
expect(expandedMenu.text).toBe('Choose an action:');
expect(expandedMenu.options.reply_markup.inline_keyboard).toEqual([
[
{ text: '➕ Create with wizard', callback_data: 'main:newride' },
{ text: '🤖 Create with AI', callback_data: 'main:airide' },
{ text: '📋 Created rides', callback_data: 'main:listrides' }
],
[
{ text: '🗓 Planned rides', callback_data: 'main:planned' },
{ text: '⚙️ Settings', callback_data: 'main:settings' },
{ text: '❓ Help', callback_data: 'main:help' }
],
[
{ text: '✖️ Close', callback_data: 'main:close' }
]
]);
await harness.dispatchCallback({ data: 'main:listrides' });
expect(harness.outbox.replies.at(-1).text).toContain('You have not created any rides yet.');
await harness.dispatchCallback({ data: 'main:planned' });
expect(harness.outbox.replies.at(-1).text).toContain('You have no planned rides.');
await harness.dispatchMessage({ text: '⚙️ Settings' });
expect(harness.outbox.replies.at(-1).richMessage?.html).toContain(
'<h3>Default settings for new rides</h3>'
);
const repliesBeforeHelp = harness.outbox.replies.length;
await harness.dispatchCallback({ data: 'main:help' });
expect(harness.outbox.replies).toHaveLength(repliesBeforeHelp + 2);
await harness.dispatchCallback({ data: 'main:newride' });
expect(harness.outbox.replies.at(-1).text).toContain('Please enter the ride title');
expect(harness.outbox.replies.every(reply =>
reply.options?.reply_markup?.remove_keyboard !== true
)).toBe(true);
await harness.dispatchCallback({ data: 'wizard:cancel' });
expect(harness.outbox.replies.at(-1).options.reply_markup).toBeUndefined();
await harness.dispatchCallback({ data: 'main:airide' });
expect(harness.outbox.replies.at(-1).text).toContain('Describe the ride');
await harness.dispatchCallback({ data: 'airide:cancel:200:100' });
await harness.dispatchMessage({ text: '☰ Buttons' });
const closeableMenu = harness.outbox.replies.at(-1);
await harness.dispatchCallback({
data: 'main:close',
message: {
message_id: closeableMenu.messageId,
chat: { id: 100, type: 'private' },
from: { id: 0, is_bot: true, username: 'testbot' }
}
});
expect(harness.outbox.deletes).toContainEqual({
chatId: 100,
messageId: closeableMenu.messageId
});
});
it('opens compact calendar options privately from an announcement link', async () => {
const harness = await createScenarioHarness();
const user = { id: 42, first_name: 'Calendar', username: 'calendaruser' };
await harness.dispatchMessage({
text: '/newride\ntitle: Calendar Ride\nwhen: tomorrow 10:00',
chat: { id: 42, type: 'private' },
from: user
});
const ride = harness.listRides()[0];
const announcement = harness.outbox.replies[0];
expect(announcement.text).toContain(
`href="https://t.me/testbot?start=calendar_${ride.id}"`
);
expect(announcement.options.reply_markup.inline_keyboard.flat()).not.toEqual(
expect.arrayContaining([
expect.objectContaining({ callback_data: `calendar:menu:${ride.id}` })
])
);
await harness.dispatchMessage({
text: `/start calendar_${ride.id}`,
chat: { id: 42, type: 'private' },
from: user
});
const privateMenu = harness.outbox.replies.find(reply =>
reply.chatId === 42 &&
reply.options?.reply_markup?.inline_keyboard?.flat().some(button =>
button.callback_data === `calendar:ics:${ride.id}`
)
);
expect(privateMenu).toBeDefined();
expect(privateMenu.options.reply_markup.inline_keyboard[0]).toHaveLength(3);
expect(privateMenu.options.reply_markup.inline_keyboard[1]).toEqual([
expect.objectContaining({ callback_data: 'calendar:close' })
]);
expect(harness.outbox.documents).toHaveLength(0);
await harness.dispatchCallback({
data: `calendar:ics:${ride.id}`,
chat: { id: 42, type: 'private' },
from: user
});
expect(harness.outbox.documents).toEqual([
expect.objectContaining({
chatId: 42,
document: expect.objectContaining({ filename: `ride-${ride.id}.ics` })
})
]);
});
it('creates a ride from inline /newride params and posts the ride message', async () => {
const harness = await createScenarioHarness();
await harness.dispatchMessage({
text: '/newride\ntitle: Sunrise Ride\nwhen: tomorrow 11:00\nmeet: River Park',
chat: { id: 501, type: 'private' },
from: { id: 42, first_name: 'Alex', last_name: 'Rider', username: 'alex' },
});
const rides = harness.listRides();
expect(rides).toHaveLength(1);
const [ride] = rides;
expect(ride.title).toBe('Sunrise Ride');
expect(ride.createdBy).toBe(42);
expect(ride.meetingPoint).toBe('River Park');
expect(ride.messages).toHaveLength(1);
expect(ride.participation.joined).toEqual([
expect.objectContaining({
userId: 42,
username: 'alex',
firstName: 'Alex',
lastName: 'Rider',
}),
]);
expect(harness.outbox.replies).toHaveLength(1);
expect(harness.outbox.replies[0].text).toContain('Sunrise Ride');
expect(harness.outbox.replies[0].richMessage?.html).toContain('<h3>🚲 Sunrise Ride</h3>');
expect(harness.outbox.replies[0].richMessage?.html).toContain('<tg-time ');
expect(harness.outbox.replies[0].options.parse_mode).toBeUndefined();
expect(harness.outbox.replies[0].options.reply_markup.inline_keyboard[0]).toEqual(
expect.arrayContaining([
expect.objectContaining({ callback_data: `join:${ride.id}` }),
expect.objectContaining({ callback_data: `thinking:${ride.id}` }),
expect.objectContaining({ callback_data: `skip:${ride.id}` }),
])
);
expect(harness.outbox.replies[0].options.reply_markup.inline_keyboard[1]).toEqual(
expect.arrayContaining([
expect.objectContaining({ callback_data: `rideowner:update:${ride.id}` }),
expect.objectContaining({ callback_data: `rideowner:duplicate:${ride.id}` }),
expect.objectContaining({ callback_data: `rideowner:delete:${ride.id}` }),
])
);
expect(harness.outbox.replies[0].options.reply_markup.inline_keyboard[2]).toEqual(
expect.arrayContaining([
expect.objectContaining({ callback_data: `rideowner:cancel:${ride.id}` }),
expect.objectContaining({ callback_data: `rideowner:participants:${ride.id}` }),
expect.objectContaining({ callback_data: `rideowner:settings:${ride.id}` }),
])
);
});
it('creates and renders independent average and cruising speeds', async () => {
const harness = await createScenarioHarness();
await harness.dispatchMessage({
text: '/newride\ntitle: Two Speeds\nwhen: tomorrow 11:00\nspeed: 24\ncruisingSpeed: 28-30',
chat: { id: 501, type: 'private' },
from: { id: 42, first_name: 'Alex', username: 'alex' }
});
const [ride] = harness.listRides();
expect(ride).toMatchObject({
speedMin: 24,
speedMax: 24,
cruisingSpeedMin: 28,
cruisingSpeedMax: 30
});
const message = harness.outbox.replies[0].text;
expect(message).toContain('⚡ Average moving speed: ~24 km/h');
expect(message).toContain('🛣️ Cruising speed: 28-30 km/h');
expect(message.indexOf('Average moving speed')).toBeLessThan(message.indexOf('Cruising speed'));
});
it('keeps repeated /shareride announcements bounded and synchronizes retained messages', async () => {
const originalLimit = process.env.MAX_RIDE_MESSAGES_PER_CHAT_THREAD;
process.env.MAX_RIDE_MESSAGES_PER_CHAT_THREAD = '2';
try {
const harness = await createScenarioHarness();
const owner = { id: 42, first_name: 'Alex', last_name: 'Rider', username: 'alex' };
const guest = { id: 43, first_name: 'Sam', last_name: 'Guest', username: 'sam' };
const chat = { id: 501, type: 'private' };
await harness.dispatchMessage({
text: '/newride\ntitle: Repeat Ride\nwhen: tomorrow 11:00',
chat,
from: owner,
});
const [createdRide] = harness.listRides();
const oldestMessageId = createdRide.messages[0].messageId;
await harness.dispatchMessage({ text: `/shareride ${createdRide.id}`, chat, from: owner });
expect(harness.getRide(createdRide.id).messages).toHaveLength(2);
expect(harness.outbox.deletes).toEqual([]);
await harness.dispatchMessage({ text: `/shareride ${createdRide.id}`, chat, from: owner });
const retainedRide = harness.getRide(createdRide.id);
const retainedMessageIds = retainedRide.messages.map(message => message.messageId);
expect(harness.outbox.deletes).toEqual([{ chatId: chat.id, messageId: oldestMessageId }]);
expect(retainedRide.messages).toHaveLength(2);
expect(retainedMessageIds).not.toContain(oldestMessageId);
await harness.dispatchCallback({
data: `join:${createdRide.id}`,
chat,
from: guest,
message: {
message_id: retainedMessageIds[0],
text: 'Repeat Ride',
chat,
from: { id: 0, is_bot: true, username: 'testbot' },
},
});
const synchronizedMessageIds = harness.outbox.edits
.filter(edit => edit.via === 'api.editMessageText')
.map(edit => edit.messageId);
expect(synchronizedMessageIds).toEqual(expect.arrayContaining(retainedMessageIds));
} finally {
if (originalLimit === undefined) {
delete process.env.MAX_RIDE_MESSAGES_PER_CHAT_THREAD;
} else {
process.env.MAX_RIDE_MESSAGES_PER_CHAT_THREAD = originalLimit;
}
}
});
it('starts the update wizard from creator owner button callback', async () => {
const harness = await createScenarioHarness();
const owner = { id: 42, first_name: 'Alex', last_name: 'Rider', username: 'alex' };
const chat = { id: 501, type: 'private' };
await harness.dispatchMessage({
text: '/newride\ntitle: Sunrise Ride\nwhen: tomorrow 11:00',
chat,
from: owner,
});
const [ride] = harness.listRides();
const trackedMessage = ride.messages[0];
await harness.dispatchCallback({
data: `rideowner:update:${ride.id}`,
chat,
from: owner,
message: {
message_id: trackedMessage.messageId,
text: harness.outbox.replies[0].text,
chat,
from: { id: 0, is_bot: true, username: 'testbot' },
},
});
expect(harness.outbox.replies).toEqual(
expect.arrayContaining([
expect.objectContaining({
chatId: chat.id,
text: expect.stringContaining('Sunrise Ride'),
options: expect.objectContaining({ parse_mode: 'HTML' }),
}),
])
);
expect(harness.outbox.callbackAnswers).toContainEqual({ text: null });
});
it('publishes a creator ride from the numbered recent-destinations menu', async () => {
const harness = await createScenarioHarness();
const owner = { id: 42, first_name: 'Alex', last_name: 'Rider', username: 'alex' };
const privateChat = { id: 42, type: 'private' };
const forumChat = {
id: -1001234567890,
type: 'supergroup',
title: 'Cyclists Forum',
username: 'cyclists_forum'
};
await harness.dispatchMessage({
text: '/newride\ntitle: Publish Me\nwhen: tomorrow 11:00',
chat: privateChat,
from: owner,
});
const [ride] = harness.listRides();
const privateRideMessage = harness.outbox.replies[0];
await harness.dispatchMessage({
text: `/shareride ${ride.id}`,
chat: forumChat,
from: owner,
messageThreadId: 77,
});
expect(harness.getRide(ride.id).messages).toEqual(expect.arrayContaining([
expect.objectContaining({
chatId: forumChat.id,
messageThreadId: 77,
chatTitle: forumChat.title,
publishedBy: owner.id
})
]));
await harness.dispatchCallback({
data: `rideowner:publish:${ride.id}`,
chat: privateChat,
from: owner,
message: {
message_id: privateRideMessage.messageId,
text: privateRideMessage.text,
chat: privateChat,
from: { id: 0, is_bot: true, username: 'testbot' },
},
});
const destinationMenu = harness.outbox.replies.at(-1);
expect(destinationMenu.text).toContain(
'✅ <a href="https://t.me/cyclists_forum/77">Cyclists Forum / Thread #77</a>'
);
expect(destinationMenu.options.reply_markup.inline_keyboard).toEqual([
[expect.objectContaining({
text: '1',
callback_data: `ridepublish:${ride.id}:${forumChat.id}:77`
})],
[expect.objectContaining({ callback_data: 'ridepublish:close' })]
]);
await harness.dispatchCallback({
data: `ridepublish:${ride.id}:${forumChat.id}:77`,
chat: privateChat,
from: owner,
message: {
message_id: destinationMenu.messageId,
text: destinationMenu.text,
chat: privateChat,
from: { id: 0, is_bot: true, username: 'testbot' },
},
});
const forumAnnouncements = harness.outbox.replies.filter(reply => reply.chatId === forumChat.id);
expect(forumAnnouncements).toHaveLength(2);
expect(forumAnnouncements.at(-1).options.message_thread_id).toBe(77);
expect(harness.outbox.deletes).not.toContainEqual({
chatId: privateChat.id,
messageId: destinationMenu.messageId
});
expect(harness.outbox.edits.at(-1)).toEqual(expect.objectContaining({
chatId: privateChat.id,
messageId: destinationMenu.messageId,
text: expect.stringContaining('>[2]</a>')
}));
});
it('shows participants list and swaps cancel button to resume after owner callback cancellation', async () => {
const harness = await createScenarioHarness();
const owner = { id: 88, first_name: 'Ira', last_name: 'Owner', username: 'ira' };
const guest = { id: 99, first_name: 'Sam', last_name: 'Guest', username: 'sam' };
const chat = { id: 1201, type: 'private' };
await harness.dispatchMessage({
text: '/newride\ntitle: Owner Ride\nwhen: tomorrow 08:00',
chat,
from: owner,
});
const [ride] = harness.listRides();
const trackedMessage = ride.messages[0];
await harness.dispatchCallback({
data: `join:${ride.id}`,
chat,
from: guest,
message: {
message_id: trackedMessage.messageId,
text: harness.outbox.replies[0].text,
chat,
from: { id: 0, is_bot: true, username: 'testbot' },
},
});
await harness.dispatchCallback({
data: `rideowner:participants:${ride.id}`,
chat,
from: owner,
message: {
message_id: trackedMessage.messageId,
text: harness.outbox.replies[0].text,
chat,
from: { id: 0, is_bot: true, username: 'testbot' },
},
});
expect(harness.outbox.replies).toEqual(
expect.arrayContaining([
expect.objectContaining({
chatId: chat.id,
text: expect.stringContaining('Sam Guest (@sam)'),
}),
])
);
await harness.dispatchCallback({
data: `rideowner:cancel:${ride.id}`,
chat,
from: owner,
message: {
message_id: trackedMessage.messageId,
text: harness.outbox.replies[0].text,
chat,
from: { id: 0, is_bot: true, username: 'testbot' },
},
});
const latestEdit = harness.outbox.edits[harness.outbox.edits.length - 1];
expect(latestEdit.options.reply_markup.inline_keyboard[0]).toEqual(
expect.arrayContaining([
expect.objectContaining({ callback_data: `rideowner:update:${ride.id}` }),
expect.objectContaining({ callback_data: `rideowner:duplicate:${ride.id}` }),
expect.objectContaining({ callback_data: `rideowner:delete:${ride.id}` }),
])
);
expect(latestEdit.options.reply_markup.inline_keyboard[1]).toEqual(
expect.arrayContaining([
expect.objectContaining({ callback_data: `rideowner:resume:${ride.id}` }),
expect.objectContaining({ callback_data: `rideowner:participants:${ride.id}` }),
expect.objectContaining({ callback_data: `rideowner:settings:${ride.id}` }),
])
);
});
it('handles a join callback through real bot wiring and updates the stored ride message', async () => {
const harness = await createScenarioHarness();
await harness.dispatchMessage({
text: '/newride\ntitle: Coffee Ride\nwhen: tomorrow 08:30',
chat: { id: 777, type: 'private' },
from: { id: 10, first_name: 'Mila', last_name: 'Owner', username: 'mila' },
});
const [ride] = harness.listRides();
const trackedMessage = ride.messages[0];
await harness.dispatchCallback({
data: `join:${ride.id}`,
chat: { id: 777, type: 'private' },
from: { id: 99, first_name: 'Sam', last_name: 'Guest', username: 'sam' },
message: {
message_id: trackedMessage.messageId,
text: harness.outbox.replies[0].text,
chat: { id: trackedMessage.chatId, type: 'private' },
from: { id: 0, is_bot: true, username: 'testbot' },
},
});
const updatedRide = harness.getRide(ride.id);
expect(updatedRide.participation.joined).toHaveLength(2);
expect(updatedRide.participation.joined).toEqual(
expect.arrayContaining([
expect.objectContaining({
userId: 10,
username: 'mila',
firstName: 'Mila',
lastName: 'Owner',
}),
expect.objectContaining({
userId: 99,
username: 'sam',
firstName: 'Sam',
lastName: 'Guest',
}),
])
);
expect(harness.outbox.edits).toEqual(
expect.arrayContaining([
expect.objectContaining({
via: 'api.editMessageText',
chatId: trackedMessage.chatId,
messageId: trackedMessage.messageId,
text: expect.stringContaining('Sam'),
}),
])
);
expect(harness.outbox.callbackAnswers).toContainEqual({
text: 'You have joined the ride!',
});
});
it('supports create, update, cancel, and resume through real command wiring', async () => {
const harness = await createScenarioHarness();
const owner = { id: 55, first_name: 'Nina', last_name: 'Owner', username: 'nina' };
const chat = { id: 808, type: 'private' };
await harness.dispatchMessage({
text: '/newride\ntitle: Tempo Ride\nwhen: tomorrow 07:30\nmeet: City Hall',
chat,
from: owner,
});
let [ride] = harness.listRides();
const trackedMessage = ride.messages[0];
await harness.dispatchMessage({
text: `/updateride #${ride.id}\ntitle: Tempo Ride Updated\nmeet: North Gate`,
chat,
from: owner,
});
ride = harness.getRide(ride.id);
expect(ride.title).toBe('Tempo Ride Updated');
expect(ride.meetingPoint).toBe('North Gate');
expect(harness.outbox.edits).toEqual(
expect.arrayContaining([
expect.objectContaining({
via: 'api.editMessageText',
chatId: trackedMessage.chatId,
messageId: trackedMessage.messageId,
text: expect.stringContaining('Tempo Ride Updated'),
}),
])
);
expect(harness.outbox.replies).toContainEqual(
expect.objectContaining({
chatId: chat.id,
text: tr('commands.common.rideActionUpdatedMessages', {
action: tr('commands.common.actions.updated'),
count: 1,
}),
})
);
await harness.dispatchMessage({
text: `/cancelride #${ride.id}`,
chat,
from: owner,
});
ride = harness.getRide(ride.id);
expect(ride.cancelled).toBe(true);
expect(harness.outbox.replies).toContainEqual(
expect.objectContaining({
chatId: chat.id,
text: tr('commands.common.rideActionUpdatedMessages', {
action: tr('commands.common.actions.cancelled'),
count: 1,
}),
})
);
await harness.dispatchMessage({
text: `/resumeride #${ride.id}`,
chat,
from: owner,
});
ride = harness.getRide(ride.id);
expect(ride.cancelled).toBe(false);
expect(harness.outbox.replies).toContainEqual(
expect.objectContaining({
chatId: chat.id,
text: tr('commands.common.rideActionUpdatedMessages', {
action: tr('commands.common.actions.resumed'),
count: 1,
}),
})
);
});
it('blocks a non-owner from updating another user ride', async () => {
const harness = await createScenarioHarness();
const owner = { id: 11, first_name: 'Owner', last_name: 'One', username: 'owner1' };
const otherUser = { id: 22, first_name: 'Other', last_name: 'User', username: 'other' };
const chat = { id: 909, type: 'private' };
await harness.dispatchMessage({
text: '/newride\ntitle: Private Ride\nwhen: tomorrow 09:15',
chat,
from: owner,
});
const [ride] = harness.listRides();
await harness.dispatchMessage({
text: `/updateride #${ride.id}\ntitle: Hijacked Ride`,
chat,
from: otherUser,
});
const unchangedRide = harness.getRide(ride.id);
expect(unchangedRide.title).toBe('Private Ride');
expect(harness.outbox.replies).toContainEqual(
expect.objectContaining({
chatId: chat.id,
text: tr('commands.update.onlyCreator'),
})
);
});
it('deletes a ride after confirmation callback and removes tracked messages', async () => {
const harness = await createScenarioHarness();
const owner = { id: 77, first_name: 'Dana', last_name: 'Owner', username: 'dana' };
const chat = { id: 1001, type: 'private' };
await harness.dispatchMessage({
text: '/newride\ntitle: Delete Me\nwhen: tomorrow 06:45',
chat,
from: owner,
});
const [ride] = harness.listRides();
const trackedMessage = ride.messages[0];
await harness.dispatchMessage({
text: `/deleteride #${ride.id}`,
chat,
from: owner,
});
const confirmationReply = harness.outbox.replies[harness.outbox.replies.length - 1];
expect(confirmationReply.options.reply_markup.inline_keyboard[0]).toEqual(
expect.arrayContaining([
expect.objectContaining({ callback_data: `delete:confirm:${ride.id}:message` }),
expect.objectContaining({ callback_data: `delete:cancel:${ride.id}:message` }),
])
);
await harness.dispatchCallback({
data: `delete:confirm:${ride.id}:message`,
chat,
from: owner,
message: {
message_id: confirmationReply.messageId,
text: confirmationReply.text,
chat,
from: { id: 0, is_bot: true, username: 'testbot' },
},
});
expect(harness.getRide(ride.id)).toBeNull();
expect(harness.outbox.deletes).toContainEqual({
chatId: trackedMessage.chatId,
messageId: trackedMessage.messageId,
});
expect(harness.outbox.deletes).toContainEqual({
chatId: chat.id,
messageId: confirmationReply.messageId,
});
expect(harness.outbox.replies).toEqual(
expect.arrayContaining([
expect.objectContaining({
chatId: chat.id,
text: expect.stringContaining(tr('commands.delete.success')),
}),
])
);
expect(harness.outbox.callbackAnswers).toContainEqual({
text: null,
});
});
it('returns a user-facing error when joining a non-existent ride', async () => {
const harness = await createScenarioHarness();
const chat = { id: 1200, type: 'private' };
const user = { id: 333, first_name: 'Kai', last_name: 'User', username: 'kai' };
await harness.dispatchCallback({
data: 'join:missingRideId',
chat,
from: user,
message: {
message_id: 9999,
text: 'missing ride message',
chat,
from: { id: 0, is_bot: true, username: 'testbot' },
},
});
expect(harness.outbox.callbackAnswers).toContainEqual({
text: tr('commands.participation.rideNotFound'),
});
});
it('applies user defaults to new rides, allows ride-level override, and gates participation notifications', async () => {
const harness = await createScenarioHarness();
const owner = { id: 42, first_name: 'Alex', last_name: 'Rider', username: 'alex' };
const guestOne = { id: 77, first_name: 'Sam', last_name: 'Guest', username: 'sam' };
const guestTwo = { id: 78, first_name: 'Mia', last_name: 'Guest', username: 'mia' };
const chat = { id: 501, type: 'private' };
await harness.dispatchMessage({
text: '/settings',
chat,
from: owner,
});
expect(harness.storage.users.has(owner.id)).toBe(false);
const userSettingsMessage = harness.outbox.replies[harness.outbox.replies.length - 1];
expect(userSettingsMessage.richMessage?.html).toContain('<table bordered striped compact>');
expect(userSettingsMessage.options.reply_markup.inline_keyboard.at(-1)[0].callback_data).toBe('settings:close');
await harness.dispatchCallback({
data: 'settings:user:bool:np:off',
chat,
from: owner,
message: {
message_id: userSettingsMessage.messageId,
text: userSettingsMessage.text,
chat,
from: { id: 0, is_bot: true, username: 'testbot' },
},
});
expect(harness.storage.users.get(owner.id).settings.rideDefaults.notifyParticipation).toBe(false);
await harness.dispatchMessage({
text: '/newride\ntitle: Quiet Ride\nwhen: tomorrow 11:00',
chat,
from: owner,
});
const [ride] = harness.listRides();
expect(ride.settings.notifyParticipation).toBe(false);
const trackedMessage = ride.messages[0];
const trackedReply = harness.outbox.replies.find((msg) => msg.messageId === trackedMessage.messageId);
const repliesBeforeFirstJoin = harness.outbox.replies.length;
await harness.dispatchCallback({
data: `join:${ride.id}`,
chat,
from: guestOne,
message: {
message_id: trackedMessage.messageId,
text: trackedReply?.text || '',
chat,
from: { id: 0, is_bot: true, username: 'testbot' },
},
});
await jest.advanceTimersByTimeAsync(20_000);
expect(harness.outbox.replies).toHaveLength(repliesBeforeFirstJoin);
await harness.dispatchCallback({
data: `rideowner:settings:${ride.id}`,
chat,
from: owner,
message: {
message_id: trackedMessage.messageId,
text: trackedReply?.text || '',
chat,
from: { id: 0, is_bot: true, username: 'testbot' },
},
});
const rideSettingsMessage = harness.outbox.replies[harness.outbox.replies.length - 1];
expect(rideSettingsMessage.richMessage?.html).toContain('<table bordered striped compact>');
expect(rideSettingsMessage.richMessage?.html).toContain(tr('commands.settings.allowRepostsLabel'));
expect(rideSettingsMessage.richMessage?.html).not.toContain(tr('commands.settings.notificationPreferencesTitle'));
expect(rideSettingsMessage.richMessage?.html).not.toContain('<tg-button');
await harness.dispatchCallback({
data: `settings:ride:bool:np:on:${ride.id}`,
chat,
from: owner,
message: {
message_id: rideSettingsMessage.messageId,
text: rideSettingsMessage.text,
chat,
from: { id: 0, is_bot: true, username: 'testbot' },
},
});
expect(harness.getRide(ride.id).settings.notifyParticipation).toBe(true);
await harness.dispatchCallback({
data: `join:${ride.id}`,
chat,
from: guestTwo,
message: {
message_id: trackedMessage.messageId,
text: trackedReply?.text || '',
chat,
from: { id: 0, is_bot: true, username: 'testbot' },
},
});
await jest.advanceTimersByTimeAsync(20_000);
expect(harness.outbox.replies).toEqual(
expect.arrayContaining([
expect.objectContaining({
chatId: owner.id,
text: expect.stringContaining('joined your ride'),
}),
])
);
});
it('closes a settings message without changing settings', async () => {
const harness = await createScenarioHarness();