aboutsummaryrefslogtreecommitdiff
path: root/test.py
blob: f58a018c26157ba4dd938b95f843fd00f19ced09 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
#!/usr/bin/env python3

from datetime import datetime, date, timedelta
import json
import os
from shutil import copyfile
from string import Template
import subprocess
import unittest

alice, bob, charlie = "alice", "bob", "charlie"
num = {alice: "+49123456", bob: "+49654321", charlie: "+49615243"}
os.environ["GSB_GROUP_ID"] = "test"
os.environ["GSB_STATE_FILE"] = "test/state.json"
os.environ["GSB_SEND_CMD"] = "cat"
os.environ["GSB_SEND_GROUP"] = "cat"
os.environ["GSB_SEND_USER"] = "cat"
os.environ["GSB_MODULES"] = "geldschiebing.py"

now = datetime.now().date()

msg_template = Template("""
{"envelope":
    {"source":"$sender",
    "sourceDevice":2,
    "relay":null,
    "timestamp":1544101248419,
    "isReceipt":false,
    "dataMessage":
        {"timestamp":1544101248419,
        "message":"$msg",
        "expiresInSeconds":0,
        "attachments":[],
        "groupInfo":
            {"groupId":"test",
            "members":null,
            "name":null,
            "type":"DELIVER"}
        },
    "syncMessage":null,
    "callMessage":null}
}""".replace("\n", ""))

scheduled_state_template = Template("""
{"balance": {"alice": {"bob": -100, "charlie": -100},
            "bob": {"alice": 100, "charlie": 0},
            "charlie": {"alice": 100, "bob": 0}},
"name2num": {"alice": "+49123456", "bob": "+49654321", "charlie": "+49615243"},
"num2name": {"+49123456": "alice", "+49654321": "bob", "+49615243": "charlie"},
"cars": {},
"scheduled_cmds": {"stuff": {"schedule": "$schedule", "last_time": "$last_time",
                    "sender": "+49123456", "cmd": ["split", "3", "bob", "charlie"]}},
"changes": {"alice": [], "bob": [], "charlie": []}}""")


def _run_bot(sender, cmd):
    msg = msg_template.substitute(sender=sender, msg=cmd).replace("\n",
                                                                  "\\n") + "\n"
    res = subprocess.run(
        ["python3", "./geldschieberbot.py", "--no-quote"],
        text=True,
        capture_output=True,
        check=False,
        # res = subprocess.run(["python3", "./bot.py"], text=True, capture_output=True,
        input=msg)

    if res.returncode != 0:
        print(res.stdout)
        print(res.stderr)

    return res


def run_bot(test, sender, cmd):
    res = _run_bot(sender, cmd)
    test.assertEqual(res.returncode, 0)
    test.assertEqual(res.stderr, "")
    return res


def save_state(dest):
    copyfile(os.environ["GSB_STATE_FILE"], dest)


def reset_state(state=None):
    if state:
        copyfile(state, os.environ["GSB_STATE_FILE"])
    else:
        state = os.environ["GSB_STATE_FILE"]
        if os.path.isfile(state):
            os.remove(state)


def reset_state_string(string):
    with open(os.environ["GSB_STATE_FILE"], "w", encoding='utf-8') as f:
        json.dump(json.loads(string), f)


def compare_state(comp_state):
    with open(comp_state, "r", encoding='utf-8') as csf, \
         open(os.environ["GSB_STATE_FILE"], "r", encoding='utf-8') as sf:
        cs = csf.read()
        s = sf.read()
    return cs == s


# pragma pylint: disable=missing-function-docstring,missing-class-docstring,invalid-name
class TestRegCmd(unittest.TestCase):

    def setUp(self):
        reset_state()

    def test_correct_reg(self):
        res = run_bot(self, num[alice], "!reg " + alice)
        self.assertEqual(res.stdout, f'Happy geldschiebing {alice}!')

    def test_double_reg(self):
        res = run_bot(self, num[alice], "!reg " + alice)
        self.assertEqual(res.stdout, f'Happy geldschiebing {alice}!')
        res = run_bot(self, num[alice], "!reg " + alice)
        self.assertEqual(res.stdout, 'ERROR: ' + alice + ' already registered')

    def test_invalid_reg(self):
        res = run_bot(self, num[alice], "!reg nase 03")
        self.assertEqual(res.stdout, 'ERROR: not in form "!reg name"')

    def test_numerical_reg(self):
        res = run_bot(self, num[alice], "!reg 9")
        self.assertEqual(res.stdout,
                         'ERROR: pure numerical names are not allowed')

        res = run_bot(self, num[alice], "!reg 9.7")
        self.assertEqual(res.stdout,
                         'ERROR: pure numerical names are not allowed')

    def test_additional_reg(self):
        res = run_bot(self, num[alice], "!reg " + alice)
        self.assertEqual(res.stdout, f'Happy geldschiebing {alice}!')
        res = run_bot(self, num[bob], "!reg " + bob)
        self.assertEqual(res.stdout, f'Happy geldschiebing {bob}!')
        res = run_bot(self, num[charlie], "!reg " + charlie)
        self.assertEqual(res.stdout, f'Happy geldschiebing {charlie}!')

        self.assertTrue(compare_state("test/state_3users.json"))


class TestTransactionCmd(unittest.TestCase):

    @classmethod
    def tearDownClass(cls):
        reset_state()

    def setUp(self):
        reset_state("test/state_3users.json")

    def test_unknown_recipient(self):
        res = run_bot(self, num[alice], "!schieb 10 horst")
        self.assertEqual(res.stdout, 'ERROR: recipient not known')

        res = run_bot(self, num[alice], "!schieb horst 10")
        self.assertEqual(res.stdout, 'ERROR: recipient not known')

    def test_correct_schieb(self):
        res = run_bot(self, num[alice], "!schieb 10 " + bob)
        self.assertEqual(res.stdout, f'New Balance: {alice} <- 10.00 {bob}\n')

        res = run_bot(self, num[bob], "!schieb 10 " + alice)
        self.assertEqual(res.stdout, f'New Balance: {bob} <- 0.00 {alice}\n')

    def test_correct_gib(self):
        res = run_bot(self, num[alice], "!gib 10 " + bob)
        self.assertEqual(res.stdout, f'New Balance: {alice} <- 10.00 {bob}\n')

        res = run_bot(self, num[bob], "!gib 10 " + alice)
        self.assertEqual(res.stdout, f'New Balance: {bob} <- 0.00 {alice}\n')

    def test_correct_amount(self):
        res = run_bot(self, num[bob], "!schieb 1.1 " + alice)
        self.assertEqual(res.stdout, f'New Balance: {bob} <- 1.10 {alice}\n')

        res = run_bot(self, num[bob], "!gib 1,1 " + alice)
        self.assertEqual(res.stdout, f'New Balance: {bob} <- 2.20 {alice}\n')

    def test_invalid_amount(self):
        res = run_bot(self, num[bob], "!schieb 1b1 " + alice)
        self.assertEqual(res.stdout, 'ERROR: amount must be a positive number')

        res = run_bot(self, num[bob], "!schieb ä€ " + alice)
        self.assertEqual(res.stdout, 'ERROR: amount must be a positive number')

    def test_correct_schieb_name_before_amount(self):
        res = run_bot(self, num[alice], "!schieb " + bob + " 10")
        self.assertEqual(res.stdout, f'New Balance: {alice} <- 10.00 {bob}\n')

        res = run_bot(self, num[bob], "!schieb " + alice + " 10")
        self.assertEqual(res.stdout, f'New Balance: {bob} <- 0.00 {alice}\n')

    def test_correct_gib_name_before_amount(self):
        res = run_bot(self, num[alice], "!gib " + charlie + " 10")
        self.assertEqual(res.stdout,
                         f'New Balance: {alice} <- 10.00 {charlie}\n')

        res = run_bot(self, num[charlie], "!gib " + alice + " 10")
        self.assertEqual(res.stdout,
                         f'New Balance: {charlie} <- 0.00 {alice}\n')

    def test_correct_nimm(self):
        res = run_bot(self, num[alice], "!nimm 10 " + bob)
        self.assertEqual(res.stdout, f'New Balance: {alice} -> 10.00 {bob}\n')

        res = run_bot(self, num[bob], "!nimm 10 " + alice)
        self.assertEqual(res.stdout, f'New Balance: {bob} <- 0.00 {alice}\n')

    def test_correct_zieh_name_before_amount(self):
        res = run_bot(self, num[alice], "!zieh " + charlie + " 10")
        self.assertEqual(res.stdout,
                         f'New Balance: {alice} -> 10.00 {charlie}\n')

        res = run_bot(self, num[charlie], "!zieh " + alice + " 10")
        self.assertEqual(res.stdout,
                         f'New Balance: {charlie} <- 0.00 {alice}\n')

    def test_transactions_complex(self):
        res = run_bot(self, num[alice], "!schieb " + charlie + " 1,1")
        self.assertEqual(res.stdout,
                         f'New Balance: {alice} <- 1.10 {charlie}\n')

        res = run_bot(self, num[alice], "!zieh " + charlie + " 2.1")
        self.assertEqual(res.stdout,
                         f'New Balance: {alice} -> 1.00 {charlie}\n')

        res = run_bot(self, num[charlie], "!schieb " + bob + " 42")
        self.assertEqual(res.stdout,
                         f'New Balance: {charlie} <- 42.00 {bob}\n')

        res = run_bot(self, num[alice], "!zieh " + bob + " 0.01")
        self.assertEqual(res.stdout, f'New Balance: {alice} -> 0.01 {bob}\n')

        compare_state("test/state_transactions1.json")

    def test_transactions_with_myself(self):
        res = run_bot(self, num[alice], f"!schieb {alice} 1,1")
        self.assertEqual(
            res.stdout,
            'ERROR: you can not transfer money to or from yourself')

        res = run_bot(self, num[alice], f"!zieh {alice} 2.1")
        self.assertEqual(
            res.stdout,
            'ERROR: you can not transfer money to or from yourself')

    def test_transactions_with_multiple_recipients(self):
        for cmds in [('schieb', 'zieh'), ('gib', 'nimm')]:
            res = run_bot(self, num[charlie], f'!{cmds[0]} 5 {alice} {bob}')
            self.assertEqual(res.stdout, \
"""-> alice 5.00
-> bob 5.00
New Balance:
charlie:
\t<- alice 5.00
\t<- bob 5.00
\tBalance: 10.00""")

            res = run_bot(self, num[charlie], f'!{cmds[1]} 5 {alice} {bob}')
            self.assertEqual(res.stdout, \
"""<- alice 5.00
<- bob 5.00
New Balance:
charlie:
\tAll fine :)""")

    def test_invalid_transactions_with_multiple_recipients(self):
        res = run_bot(self, num[charlie], f'!schieb {alice} 5 {bob}')
        self.assertEqual(res.stdout, 'ERROR: amount must be a positive number')

        res = run_bot(self, num[charlie], f'!zieh 5 5 {bob}')
        self.assertEqual(res.stdout, 'ERROR: recipient "5" not known')


class TestSumCmd(unittest.TestCase):

    def test_summary_single_user(self):
        reset_state("test/state_transactions1.json")
        res = run_bot(self, num[alice], "!sum " + alice)
        self.assertEqual(
            res.stdout,
            'Summary:\nalice:\n\t-> bob 0.01\n\t-> charlie 1.00\n\tBalance: -1.01\n'
        )

    def test_summary_invalide_single_user(self):
        reset_state()
        res = run_bot(self, num[alice], "!sum " + alice)
        self.assertEqual(res.stdout, 'ERROR: name "alice" not registered')

    def test_summary_double_user(self):
        reset_state("test/state_transactions1.json")
        res = run_bot(self, num[alice], f"!sum {alice} {bob}")
        summary = \
"""Summary:
alice:
\t-> bob 0.01
\t-> charlie 1.00
\tBalance: -1.01
bob:
\t<- alice 0.01
\t-> charlie 42.00
\tBalance: -41.99
"""
        self.assertEqual(res.stdout, summary)

    def test_summary(self):
        reset_state("test/state_transactions1.json")

        self.assertEqual(
            run_bot(self, num[alice], "!sum").stdout,
            'Summary:\nalice:\n\t-> bob 0.01\n\t-> charlie 1.00\n\tBalance: -1.01'
        )
        self.assertEqual(
            run_bot(self, num[alice], "!summary").stdout,
            'Summary:\nalice:\n\t-> bob 0.01\n\t-> charlie 1.00\n\tBalance: -1.01'
        )
        self.assertEqual(
            run_bot(self, num[alice], "!balance").stdout,
            'Summary:\nalice:\n\t-> bob 0.01\n\t-> charlie 1.00\n\tBalance: -1.01'
        )

    def test_full_summary(self):
        reset_state("test/state_transactions1.json")
        res = run_bot(self, num[alice], "!full-sum")
        summary = \
"""Summary:
alice:
\t-> bob 0.01
\t-> charlie 1.00
\tBalance: -1.01
bob:
\t<- alice 0.01
\t-> charlie 42.00
\tBalance: -41.99
charlie:
\t<- alice 1.00
\t<- bob 42.00
\tBalance: 43.00"""
        self.assertEqual(res.stdout, summary)

    def test_full_summary_with_args(self):
        reset_state("test/state_transactions1.json")
        res = run_bot(self, num[alice], "!full-sum foo")
        self.assertEqual(res.stdout, 'ERROR: full-sum takes no arguments')


class TestMisc(unittest.TestCase):

    def test_unknown_command(self):
        res = run_bot(self, num[alice], "!foo")
        self.assertEqual(
            res.stdout,
            "ERROR: unknown cmd. Enter !help for a list of commands.")

    def test_no_command(self):
        res = run_bot(self, num[alice], "Hi, how are you?")
        self.assertEqual(res.stdout, "")


class TestListCmd(unittest.TestCase):

    def setUp(self):
        reset_state("test/state_3users.json")

    def test_ls(self):
        res = run_bot(self, num[alice], "!ls")
        msg = f"alice: {num[alice]}\nbob: {num[bob]}\ncharlie: {num[charlie]}\n"
        self.assertEqual(res.stdout, msg)

    def test_list(self):
        res = run_bot(self, num[bob], "!list")
        msg = f"alice: {num[alice]}\nbob: {num[bob]}\ncharlie: {num[charlie]}\n"
        self.assertEqual(res.stdout, msg)


class TestSplitCmd(unittest.TestCase):

    def setUp(self):
        reset_state("test/state_3users.json")

    def test_split_unregistered(self):
        res = run_bot(self, "+4971576357", "!split")
        self.assertEqual(res.stdout, 'ERROR: you must register first')

    def test_split_invalid_args(self):
        res = run_bot(self, num[alice], "!split")
        self.assertEqual(res.stdout,
                         'ERROR: not in form "!split amount [name]+"')

        res = run_bot(self, num[alice], "!split 10")
        self.assertEqual(res.stdout,
                         'ERROR: not in form "!split amount [name]+"')

    def test_split_one_unknown_user(self):
        res = run_bot(self, num[alice], "!split 30 " + "foo" + " " + charlie)
        msg = \
"""Split 30.00 between 3 -> 10.00 each
foo not known. Please take care manually
New Balance:
alice:
\t<- charlie 10.00
\tBalance: 10.00"""
        self.assertEqual(res.stdout, msg)

    def test_split_with_sender_in_user_list(self):
        res = run_bot(self, num[alice], f"!split 30 {charlie} {alice}")
        msg = \
"""Split 30.00 between 3 -> 10.00 each
alice, you will be charged multiple times. This may not be what you want
New Balance:
alice:
\t<- charlie 10.00
\tBalance: 10.00"""
        self.assertEqual(res.stdout, msg)

    def test_split_with_double_user(self):
        res = run_bot(self, num[alice], f"!split 30 {charlie} {charlie}")
        msg = \
"""Split 30.00 between 3 -> 10.00 each
New Balance:
alice:
\t<- charlie 20.00
\tBalance: 20.00"""
        self.assertEqual(res.stdout, msg)

    def test_split(self):
        res = run_bot(self, num[alice], "!split 30 " + bob + " " + charlie)
        msg = \
"""Split 30.00 between 3 -> 10.00 each
New Balance:
alice:
\t<- bob 10.00
\t<- charlie 10.00
\tBalance: 20.00"""
        self.assertEqual(res.stdout, msg)

    def test_split_single_user_amount_swap(self):
        res = run_bot(self, num[alice], f"!split {bob} 10")
        msg = \
"""Split 10.00 between 2 -> 5.00 each
New Balance:
alice:
\t<- bob 5.00
\tBalance: 5.00"""
        self.assertEqual(res.stdout, msg)

    def test_split_whitespace(self):
        res = run_bot(self, num[alice],
                      "!split 30 " + bob + " " + charlie + " ")
        msg = \
"""Split 30.00 between 3 -> 10.00 each
New Balance:
alice:
\t<- bob 10.00
\t<- charlie 10.00
\tBalance: 20.00"""
        self.assertEqual(res.stdout, msg)


class TestAliasAdd(unittest.TestCase):

    def setUp(self):
        reset_state("test/state_3users.json")

    def test_add_success(self):
        res = run_bot(self, num[alice], f"!alias foo {alice} {bob}")
        self.assertEqual(res.stdout, 'New alias "foo" registered')

        res = run_bot(self, num[alice], f"!alias bar {charlie}")
        self.assertEqual(res.stdout, 'New alias "bar" registered')

    def test_add_invalid_aliases(self):
        res = run_bot(self, num[alice], f"!alias foo {alice} {bob}")
        self.assertEqual(res.stdout, 'New alias "foo" registered')
        res = run_bot(self, num[alice], f"!alias foo {alice} {bob}")
        self.assertEqual(res.stdout,
                         'ERROR: Alias "foo" is already registered')

        res = run_bot(self, num[alice], f"!alias {alice} {alice} {bob}")
        self.assertEqual(res.stdout,
                         f'ERROR: A user "{alice}" is already registered')

        res = run_bot(self, num[alice], f"!alias 13.23 {charlie}")
        self.assertEqual(res.stdout,
                         'ERROR: Pure numerical aliases are not allowed')

        res = run_bot(self, num[alice], "!alias bar ingo")
        self.assertEqual(res.stdout, 'ERROR: User ingo is not registered')

        res = run_bot(self, num[alice], f"!alias all {alice}")
        self.assertEqual(res.stdout,
                         'ERROR: Alias "all" is already registered')


class TestAlias(unittest.TestCase):

    def setUp(self):
        reset_state("test/state_3users.json")

    def test_list_aliases_empty_state(self):
        res = run_bot(self, num[alice], "!alias")
        self.assertEqual(res.stdout, f"""Aliases:
\tall: {alice} {bob} {charlie}""")

    def test_list_aliases(self):
        run_bot(self, num[alice], f"!alias alob {alice} {bob}")
        res = run_bot(self, num[alice], "!alias")
        self.assertEqual(
            res.stdout, f"""Aliases:
\tall: {alice} {bob} {charlie}
\talob: {alice} {bob}""")

    def test_add_invalid_aliases(self):
        run_bot(self, num[alice], f"!alias foo {alice} {bob}")
        res = run_bot(self, num[alice], f"!alias foo {alice} {bob}")
        self.assertEqual(res.stdout,
                         'ERROR: Alias "foo" is already registered')

        res = run_bot(self, num[alice], f"!alias {alice} {alice} {bob}")
        self.assertEqual(res.stdout,
                         f'ERROR: A user "{alice}" is already registered')

        res = run_bot(self, num[alice], f"!alias 13.23 {charlie}")
        self.assertEqual(res.stdout,
                         'ERROR: Pure numerical aliases are not allowed')

        res = run_bot(self, num[alice], "!alias bar ingo")
        self.assertEqual(res.stdout, 'ERROR: User ingo is not registered')

        res = run_bot(self, num[alice], f"!alias all {alice}")
        self.assertEqual(res.stdout,
                         'ERROR: Alias "all" is already registered')


class TestAliasUsage(unittest.TestCase):

    def setUp(self):
        reset_state("test/state_3users_1alias.json")

    def test_schieb_alias(self):
        expected = \
"""-> alice 5.00
-> bob 5.00
New Balance:
charlie:
\t<- alice 5.00
\t<- bob 5.00
\tBalance: 10.00"""

        for cmd in ['gib', 'schieb']:
            for args in ['5 alob', 'alob 5']:
                res = run_bot(self, num[charlie], f'!{cmd} {args}')
                self.assertEqual(res.stdout, expected)
                self.setUp()

    def test_zieh_alias(self):
        expected = \
"""<- alice 5.00
<- bob 5.00
New Balance:
charlie:
\t-> alice 5.00
\t-> bob 5.00
\tBalance: -10.00"""

        for cmd in ['nimm', 'zieh']:
            for args in ['5 alob', 'alob 5']:
                res = run_bot(self, num[charlie], f'!{cmd} {args}')
                self.assertEqual(res.stdout, expected)
                self.setUp()

    def test_split_all(self):
        res = run_bot(self, num[alice], "!split 9 all")
        self.assertEqual(res.stdout, \
"""Split 9.00 between 3 -> 3.00 each
New Balance:
alice:
\t<- bob 3.00
\t<- charlie 3.00
\tBalance: 6.00""")

    def test_split_alias(self):
        res = run_bot(self, num[charlie], "!split 9 alob")
        self.assertEqual(res.stdout, \
"""Split 9.00 between 3 -> 3.00 each
New Balance:
charlie:
\t<- alice 3.00
\t<- bob 3.00
\tBalance: 6.00""")

    def test_tanken_all(self):
        res = run_bot(self, num[alice], "!tanken 10\n10 all")
        self.assertEqual(res.stdout, \
"""alice: 10km = fuel: 3.33, service charge: 0.00
bob: 10km = fuel: 3.33, service charge: 0.00
charlie: 10km = fuel: 3.33, service charge: 0.00
New Balance:
alice:
\t<- bob 3.33
\t<- charlie 3.33
\tBalance: 6.66""")

    def test_tanken_alias(self):
        res = run_bot(self, num[charlie], "!tanken 8\n10 alob")
        self.assertEqual(res.stdout, \
"""alice: 10km = fuel: 4.00, service charge: 0.00
bob: 10km = fuel: 4.00, service charge: 0.00
New Balance:
charlie:
\t<- alice 4.00
\t<- bob 4.00
\tBalance: 8.00""")


class TestCarsAdd(unittest.TestCase):

    def setUp(self):
        reset_state("test/state_3users.json")

    def test_add_success(self):
        i = "!cars add foo 0.04"
        res = run_bot(self, num[alice], i)
        o = 'added "foo" as an available car'
        self.assertEqual(res.stdout, o)

        i = "!cars new bar 0.02"
        res = run_bot(self, num[alice], i)
        o = 'added "bar" as an available car'
        self.assertEqual(res.stdout, o)

        save_state("test/state_2cars.json")

    def test_add_invalid_service_charge(self):
        i = "!cars add foo 0.04hut"
        res = run_bot(self, num[alice], i)
        o = "ERROR: service-charge must be a positive number"
        self.assertEqual(res.stdout, o)

        i = "!cars new bar -5"
        res = run_bot(self, num[alice], i)
        o = "ERROR: service-charge must be a positive number"
        self.assertEqual(res.stdout, o)

    def test_add_name_conflict(self):
        i = "!cars add foo 0.04"
        res = run_bot(self, num[alice], i)
        o = 'added "foo" as an available car'
        self.assertEqual(res.stdout, o)

        i = "!cars new alice 0.02"
        res = run_bot(self, num[alice], i)
        o = 'ERROR: A user named "alice" already exists. Please use a different name for this car'
        self.assertEqual(res.stdout, o)


class TestCarsTransactions(unittest.TestCase):

    def setUp(self):
        reset_state("test/state_2cars.json")

    def test_schieb_car(self):
        i = "!schieb foo 20"
        res = run_bot(self, num[alice], i)
        o = "New Balance: alice <- 20.00 foo\n"
        self.assertEqual(res.stdout, o)


class TestCarsSum(unittest.TestCase):

    def setUp(self):
        reset_state("test/state_2cars.json")

    def test_sum_car(self):
        res = run_bot(self, num[alice], "!sum foo")
        o =\
"""Summary:
foo:
\tAll fine :)
"""
        self.assertEqual(res.stdout, o)


class TestCarsRemove(unittest.TestCase):

    def setUp(self):
        reset_state("test/state_2cars.json")

    def test_schieb_car(self):
        i = "!cars remove foo"
        res = run_bot(self, num[alice], i)
        o = 'removed "foo" from the available cars'
        self.assertEqual(res.stdout, o)

        i = "!sum foo"
        res = run_bot(self, num[alice], i)
        o = 'ERROR: name "foo" not registered'
        self.assertEqual(res.stdout, o)


class TestCarPayCmd(unittest.TestCase):

    def setUp(self):
        reset_state("test/state_2cars.json")

    def test_alice_pays_exact(self):
        run_bot(self, num[bob], "!zieh foo 20")
        run_bot(self, num[charlie], "!zieh foo 10")

        i = "!cars pay foo 30"
        res = run_bot(self, num[alice], i)
        o = \
"""alice payed 30.00
Transferring 100.00% of everybody's charges
Transfer 20.00 from bob to alice
Transfer 10.00 from charlie to alice
New Balances:
alice:
\t<- bob 20.00
\t<- charlie 10.00
\tBalance: 30.00
foo:
\tAll fine :)"""
        self.assertEqual(res.stdout, o)

    def test_alice_pays_more(self):
        run_bot(self, num[bob], "!zieh foo 20")
        run_bot(self, num[charlie], "!zieh foo 10")

        i = "!cars pay foo 40"
        res = run_bot(self, num[alice], i)
        o = \
"""alice payed 40.00
Transferring 100.00% of everybody's charges
Transfer 20.00 from bob to alice
Transfer 10.00 from charlie to alice
New Balances:
alice:
\t<- bob 20.00
\t<- charlie 10.00
\tBalance: 30.00
\tCars:
\t<- foo 10.00
\tLiability: 10.00
foo:
\t-> alice 10.00
\tBalance: -10.00"""
        self.assertEqual(res.stdout, o)

    def test_alice_pays_half(self):
        run_bot(self, num[bob], "!zieh foo 20")
        run_bot(self, num[charlie], "!zieh foo 10")

        i = "!cars pay foo 15"
        res = run_bot(self, num[alice], i)
        o = \
"""alice payed 15.00
Transferring 50.00% of everybody's charges
Transfer 10.00 from bob to alice
Transfer 5.00 from charlie to alice
New Balances:
alice:
\t<- bob 10.00
\t<- charlie 5.00
\tBalance: 15.00
foo:
\t<- bob 10.00
\t<- charlie 5.00
\tBalance: 15.00"""
        self.assertEqual(res.stdout, o)


class TestCarsListCmd(unittest.TestCase):

    def setUp(self):
        reset_state("test/state_2cars.json")

    def test_no_cars(self):
        reset_state()
        i = "!cars"
        res = run_bot(self, num[alice], i)
        o = "No cars registered yet."
        self.assertEqual(res.stdout, o)

    def test_implicit_call(self):
        i = "!cars"
        res = run_bot(self, num[alice], i)
        o = \
"""foo - service charge 4ct/km
foo:
\tAll fine :)
bar - service charge 2ct/km
bar:
\tAll fine :)"""
        self.assertEqual(res.stdout, o)

    def test_list_all(self):
        i = "!cars ls"
        res = run_bot(self, num[alice], i)
        o = \
"""foo - service charge 4ct/km
foo:
\tAll fine :)
bar - service charge 2ct/km
bar:
\tAll fine :)"""
        self.assertEqual(res.stdout, o)

        i = "!cars list"
        res = run_bot(self, num[alice], i)
        o = \
"""foo - service charge 4ct/km
foo:
\tAll fine :)
bar - service charge 2ct/km
bar:
\tAll fine :)"""
        self.assertEqual(res.stdout, o)

    def test_list_explicit_car(self):
        i = "!cars ls foo"
        res = run_bot(self, num[alice], i)
        o = \
"""foo - service charge 4ct/km
foo:
\tAll fine :)"""
        self.assertEqual(res.stdout, o)

    def test_list_invalid_explicit_car(self):
        i = "!cars ls alice"
        res = run_bot(self, num[alice], i)
        o = 'ERROR: "alice" is no available car\n'
        self.assertEqual(res.stdout, o)


class TestTankenCmd(unittest.TestCase):

    def setUp(self):
        reset_state("test/state_3users.json")

    def test_tanken_3users(self):
        i = \
"""!tanken 10 alice
20 alice bob
10 charlie"""
        res = run_bot(self, num[alice], i)
        o = \
"""alice: 20km = fuel: 3.33, service charge: 0.00
bob: 20km = fuel: 3.33, service charge: 0.00
charlie: 10km = fuel: 3.33, service charge: 0.00
New Balance:
alice:
\t<- bob 3.33
\t<- charlie 3.33
\tBalance: 6.66"""
        self.assertEqual(res.stdout, o)

    def test_tanken_unknown_user(self):
        i = \
"""!tanken 10 alice
20 alice dieter
10 charlie"""
        res = run_bot(self, num[alice], i)
        o = \
"""alice: 20km = fuel: 3.33, service charge: 0.00
dieter: 20km = fuel: 3.33, service charge: 0.00
dieter not known. Please collect fuel cost manually
charlie: 10km = fuel: 3.33, service charge: 0.00
New Balance:
alice:
\t<- charlie 3.33
\tBalance: 3.33"""
        self.assertEqual(res.stdout, o)

    def test_tanken_3users_with_car(self):
        i = "!cars add foo 0.04"
        res = run_bot(self, num[alice], i)
        o = 'added "foo" as an available car'
        self.assertEqual(res.stdout, o)

        i = \
"""!tanken 10 alice foo
20 alice bob
10 charlie"""
        res = run_bot(self, num[alice], i)
        o = \
"""alice: 20km = fuel: 3.33, service charge: 0.40
bob: 20km = fuel: 3.33, service charge: 0.40
charlie: 10km = fuel: 3.33, service charge: 0.40
New Balance:
alice:
\t<- bob 3.33
\t<- charlie 3.33
\tBalance: 6.66
\tCars:
\t-> foo 0.40
\tLiability: -0.40
Car foo:
\t<- alice 0.40
\t<- bob 0.40
\t<- charlie 0.40
\tBalance: 1.20"""
        self.assertEqual(res.stdout, o)

    def test_tanken_unknown_user_with_car(self):
        i = "!cars add foo 0.04"
        res = run_bot(self, num[alice], i)
        o = 'added "foo" as an available car'
        self.assertEqual(res.stdout, o)

        i = \
"""!tanken 10 alice foo
20 alice bob
10 dieter"""
        res = run_bot(self, num[alice], i)
        o = \
"""alice: 20km = fuel: 3.33, service charge: 0.40
bob: 20km = fuel: 3.33, service charge: 0.40
dieter: 10km = fuel: 3.33, service charge: 0.40
dieter not known. alice held accountable for service charge. Please collect fuel cost manually
New Balance:
alice:
\t<- bob 3.33
\tBalance: 3.33
\tCars:
\t-> foo 0.80
\tLiability: -0.80
Car foo:
\t<- alice 0.80
\t<- bob 0.40
\tBalance: 1.20"""
        self.assertEqual(res.stdout, o)


class TestTransferCmd(unittest.TestCase):

    def setUp(self):
        reset_state("test/state_3users.json")

    def test_transfer(self):
        i = '!transfer 5 bob charlie'
        res = run_bot(self, num[alice], i)
        o = \
"""New Balance: alice -> 5.00 bob
New Balance: alice <- 5.00 charlie
New Balance: bob -> 5.00 charlie
"""
        self.assertEqual(res.stdout, o)

    def test_transfer_credit(self):
        res = run_bot(self, num[alice], "!schieb bob 5")

        i = '!transfer 5 bob charlie'
        res = run_bot(self, num[alice], i)
        o = \
"""New Balance: alice <- 0.00 bob
New Balance: alice <- 5.00 charlie
New Balance: bob -> 5.00 charlie
"""
        self.assertEqual(res.stdout, o)

    def test_transfer_change_dept(self):
        res = run_bot(self, num[alice], "!schieb bob 5")

        i = '!transfer 5 charlie alice'
        res = run_bot(self, num[bob], i)
        o = \
"""New Balance: bob -> 5.00 charlie
New Balance: bob <- 0.00 alice
New Balance: charlie -> 5.00 alice
"""
        self.assertEqual(res.stdout, o)


#TODO: tanken, transfer, cars pay
class TestListChangesCmd(unittest.TestCase):

    def setUp(self):
        reset_state("test/state_3users.json")

    def test_list_changes_unregistered(self):
        res = run_bot(self, "+4971576357", "!list-changes")
        self.assertEqual(res.stdout, 'ERROR: you must register first')

    def test_list_changes_single_change(self):
        run_bot(self, num[alice], f"!schieb {bob} 10")
        res = run_bot(self, num[alice], "!list-changes")
        msg = \
"""Changes from alice 1-1
Change 1:
\t!schieb bob 10
\talice <- 10.00 bob
"""
        self.assertEqual(res.stdout, msg)

    def test_list_changes_single_change_invalid_amount(self):
        run_bot(self, num[alice], f"!schieb {bob} 10")
        res = run_bot(self, num[alice], "!list-changes foo")
        msg = \
"""ERROR: the amount of changes to list must be a number"""
        self.assertEqual(res.stdout, msg)

    def test_list_changes_single_change_invalid_offset(self):
        run_bot(self, num[alice], f"!schieb {bob} 10")
        res = run_bot(self, num[alice], "!list-changes 1 foo")
        msg = \
"""ERROR: the first change to list must be a number"""
        self.assertEqual(res.stdout, msg)

        res = run_bot(self, num[alice], "!list-changes 1 20")
        msg = \
"""ERROR: the first change to list is bigger than there are changes"""
        self.assertEqual(res.stdout, msg)

    def test_list_changes_two_changes(self):
        run_bot(self, num[alice], f"!schieb {bob} 10")
        run_bot(self, num[alice], f"!zieh {charlie} 5")
        res = run_bot(self, num[alice], "!list-changes")
        msg = \
"""Changes from alice 1-2
Change 1:
\t!schieb bob 10
\talice <- 10.00 bob
Change 2:
\t!zieh charlie 5
\talice -> 5.00 charlie
"""
        self.assertEqual(res.stdout, msg)

    def test_list_changes_one_of_two(self):
        run_bot(self, num[alice], f"!schieb {bob} 10")
        run_bot(self, num[alice], f"!zieh {charlie} 5")
        res = run_bot(self, num[alice], "!list-changes 1")
        msg = \
"""Changes from alice 2-2
Change 2:
\t!zieh charlie 5
\talice -> 5.00 charlie
"""
        self.assertEqual(res.stdout, msg)

    def test_list_changes_only_second(self):
        run_bot(self, num[alice], f"!schieb {bob} 10")
        run_bot(self, num[alice], f"!zieh {charlie} 5")
        run_bot(self, num[alice], f"!split 9 {bob} {charlie}")
        res = run_bot(self, num[alice], "!list-changes 1 2")
        msg = \
"""Changes from alice 2-2
Change 2:
\t!zieh charlie 5
\talice -> 5.00 charlie
"""
        self.assertEqual(res.stdout, msg)


class TestFuckCmd(unittest.TestCase):

    def setUp(self):
        reset_state("test/state_3users.json")

    def test_fuck_unregistered(self):
        res = run_bot(self, "+4971576357", "!fuck")
        self.assertEqual(res.stdout, 'ERROR: you must register first')

    def test_fuck_nothing(self):
        res = run_bot(self, num[alice], "!fuck")
        self.assertEqual(res.stdout, 'Nothing to rewind')

    def test_fuck_invalid_change(self):
        run_bot(self, num[alice], f"!schieb {bob} 10")
        res = run_bot(self, num[alice], "!fuck foo")
        msg = "ERROR: change to rewind must be a number"
        self.assertEqual(res.stdout, msg)

        res = run_bot(self, num[alice], "!fuck 20")
        msg = "ERROR: change to rewind is bigger than there are changes"
        self.assertEqual(res.stdout, msg)

    def test_fuck_transaction(self):
        for cmd in ["fuck", "undo", "rewind"]:
            run_bot(self, num[alice], f"!schieb {bob} 10")
            res = run_bot(self, num[alice], f"!{cmd}")
            msg = \
"""alice: sorry I fucked up!
Rewinding:
!schieb bob 10
alice <- 10.00 bob
"""
            self.assertEqual(res.stdout, msg)
            compare_state("test/state_3users.json")

    def test_fuck_transfer(self):
        run_bot(self, num[alice], f"!transfer 5 {bob} {charlie}")
        res = run_bot(self, num[alice], "!fuck")
        msg = \
"""alice: sorry I fucked up!
Rewinding:
!transfer 5 bob charlie
alice -> 5.00 bob
alice <- 5.00 charlie
charlie <- 5.00 bob
"""
        self.assertEqual(res.stdout, msg)
        compare_state("test/state_3users.json")

    def test_rewind_first(self):
        run_bot(self, num[alice], f"!split 3 {bob} {charlie}")
        run_bot(self, num[alice], f"!schieb {bob} 10")
        run_bot(self, num[alice], f"!nimm {charlie} 10")
        res = run_bot(self, num[alice], "!fuck 1")
        msg = ""\
"""alice: sorry I fucked up!
Rewinding:
!split 3 bob charlie
alice <- 1.00 bob
alice <- 1.00 charlie
"""
        self.assertEqual(res.stdout, msg)

    def test_rewind_second(self):
        run_bot(self, num[alice], f"!split 10 {bob} {charlie}")
        run_bot(self, num[alice], f"!schieb {bob} 10")
        run_bot(self, num[alice], f"!nimm {charlie} 10")
        res = run_bot(self, num[alice], "!fuck 2")
        msg = ""\
"""alice: sorry I fucked up!
Rewinding:
!schieb bob 10
alice <- 10.00 bob
"""
        self.assertEqual(res.stdout, msg)

    def test_fuck_split(self):
        run_bot(self, num[alice], "!split 3 bob charlie")
        res = run_bot(self, num[alice], "!fuck")
        msg = \
"""alice: sorry I fucked up!
Rewinding:
!split 3 bob charlie
alice <- 1.00 bob
alice <- 1.00 charlie
"""
        self.assertEqual(res.stdout, msg)
        compare_state("test/state_3users.json")


class TestScheduleCmd(unittest.TestCase):

    def setUp(self):
        reset_state("test/state_3users.json")

    def test_weekly(self):
        res = run_bot(self, num[alice], "!weekly stuff split 3 bob charlie")
        msg = \
"""Recorded the weekly command "split 3 bob charlie" as "stuff"
Running weekly command stuff for alice initially
Split 3.00 between 3 -> 1.00 each
New Balance:
alice:
\t<- bob 1.00
\t<- charlie 1.00
\tBalance: 2.00"""
        self.assertEqual(res.stdout, msg)

        save_state("test/state.json_schedule_weekly")

        res = run_bot(self, num[alice], "!fuck")
        msg = \
"""alice: sorry I fucked up!
Rewinding:
split 3 bob charlie
alice <- 1.00 bob
alice <- 1.00 charlie
Cancelled the weekly cmd "stuff\""""
        self.assertEqual(res.stdout, msg)

        # Last exec onw week ago
        reset_state_string(
            scheduled_state_template.substitute(last_time=now - timedelta(7),
                                                schedule="weekly"))

        res = run_bot(self, num[alice], "!fuck")
        # There is no new line because the real bot uses two Messages
        msg = \
f"""Nothing to rewindRunning weekly command stuff for alice triggered on {now.isoformat()}
Split 3.00 between 3 -> 1.00 each
New Balance:
alice:
\t<- bob 2.00
\t<- charlie 2.00
\tBalance: 4.00"""
        self.assertEqual(res.stdout, msg)

        compare_state("test/state.json_schedule_weekly")

        res = run_bot(self, num[alice], "!fuck")
        self.assertEqual(res.stdout, 'Nothing to rewind')

        # Last exec two week ago
        reset_state_string(
            scheduled_state_template.substitute(last_time=now - timedelta(14),
                                                schedule="weekly"))

        res = run_bot(self, num[alice], "!fuck")
        # There is no new line because the real bot uses two Messages
        last_week = now - timedelta(7)
        msg = \
f"""Nothing to rewindRunning weekly command stuff for alice triggered on {last_week.isoformat()}
Split 3.00 between 3 -> 1.00 each
New Balance:
alice:
\t<- bob 2.00
\t<- charlie 2.00
\tBalance: 4.00Running weekly command stuff for alice triggered on {now.isoformat()}
Split 3.00 between 3 -> 1.00 each
New Balance:
alice:
\t<- bob 3.00
\t<- charlie 3.00
\tBalance: 6.00"""
        self.assertEqual(res.stdout, msg)

        os.remove("test/state.json_schedule_weekly")

    def test_monthly(self):
        self.maxDiff = None
        res = run_bot(self, num[alice], "!monthly stuff split 3 bob charlie")
        msg = \
"""Recorded the monthly command "split 3 bob charlie" as "stuff"
Running monthly command stuff for alice initially
Split 3.00 between 3 -> 1.00 each
New Balance:
alice:
\t<- bob 1.00
\t<- charlie 1.00
\tBalance: 2.00"""
        self.assertEqual(res.stdout, msg)

        save_state("test/state.json_schedule_monthly")

        res = run_bot(self, num[alice], "!fuck")
        msg = \
"""alice: sorry I fucked up!
Rewinding:
split 3 bob charlie
alice <- 1.00 bob
alice <- 1.00 charlie
Cancelled the monthly cmd "stuff\""""
        self.assertEqual(res.stdout, msg)

        # Last exec one month ago
        if now.month > 1:
            one_month_ago = date(now.year, now.month - 1, now.day)
        else:
            one_month_ago = date(now.year - 1, 12, now.day)
        reset_state_string(
            scheduled_state_template.substitute(last_time=one_month_ago,
                                                schedule="monthly"))

        res = run_bot(self, num[alice], "!fuck")
        # There is no new line because the real bot uses two Messages
        msg = \
f"""Nothing to rewindRunning monthly command stuff for alice triggered on {now.isoformat()}
Split 3.00 between 3 -> 1.00 each
New Balance:
alice:
\t<- bob 2.00
\t<- charlie 2.00
\tBalance: 4.00"""
        self.assertEqual(res.stdout, msg)

        compare_state("test/state.json_schedule_monthly")

        res = run_bot(self, num[alice], "!fuck")
        self.assertEqual(res.stdout, 'Nothing to rewind')

        # Last exec two month ago
        if now.month > 2:
            two_months_ago = date(now.year, now.month - 2, now.day)
        else:
            two_months_ago = date(now.year - 1, 12 - now.month + 1, now.day)

        reset_state_string(
            scheduled_state_template.substitute(last_time=two_months_ago,
                                                schedule="monthly"))

        res = run_bot(self, num[alice], "!fuck")
        # There is no new line because the real bot uses to Messages
        msg = \
f"""Nothing to rewindRunning monthly command stuff for alice triggered on {one_month_ago.isoformat()}
Split 3.00 between 3 -> 1.00 each
New Balance:
alice:
\t<- bob 2.00
\t<- charlie 2.00
\tBalance: 4.00Running monthly command stuff for alice triggered on {now.isoformat()}
Split 3.00 between 3 -> 1.00 each
New Balance:
alice:
\t<- bob 3.00
\t<- charlie 3.00
\tBalance: 6.00"""
        self.assertEqual(res.stdout, msg)

        os.remove("test/state.json_schedule_monthly")

    def test_monthly_car(self):
        run_bot(self, num[alice], "!cars add fiat 0.5")
        res = run_bot(self, num[alice],
                      "!monthly versicherung cars pay fiat 3")
        msg = \
"""Recorded the monthly command "cars pay fiat 3" as "versicherung"
Running monthly command versicherung for alice initially
alice payed 3.00
Transferring 100.00% of everybody's charges
New Balances:
alice:
\tAll fine :)
\tCars:
\t<- fiat 3.00
\tLiability: 3.00
fiat:
\t-> alice 3.00
\tBalance: -3.00"""
        self.assertEqual(res.stdout, msg)


class TestThanks(unittest.TestCase):

    def test_thanks(self):
        res = run_bot(self, num[alice], "!thanks")
        self.assertEqual(
            res.stdout,
            f"You are welcome. It is a pleasure to work with you, {alice}.")

    def test_thanks_nick(self):
        res = run_bot(self, num[alice], "!thanks pussy")
        self.assertEqual(
            res.stdout,
            f"You are welcome. It is a pleasure to work with you, {alice}.\nBut don't call me pussy."
        )


if __name__ == '__main__':
    unittest.main()