File: undo.t

package info (click to toggle)
timew 1.9.1%2Bds.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,436 kB
  • sloc: cpp: 30,358; python: 6,829; sh: 706; makefile: 15
file content (726 lines) | stat: -rwxr-xr-x 31,472 bytes parent folder | download
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
#!/usr/bin/env python3

###############################################################################
#
# Copyright 2018 - 2021, 2024 - 2025, Gothenburg Bit Factory.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# https://opensource.org/license/mit
#
###############################################################################

import os
import unittest
from datetime import datetime, timezone, timedelta

import sys

# Ensure python finds the local simpletap module
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from basetest import Timew, TestCase


class TestUndo(TestCase):
    def setUp(self):
        """Executed before each test in the class"""
        self.t = Timew()

    def test_undo_annotate(self):
        """Test undo of command 'annotate'"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)
        two_hours_before_utc = now_utc - timedelta(hours=2)

        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} foo".format(two_hours_before_utc, one_hour_before_utc))
        self.t("annotate @1 \"lorem ipsum\"")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval before, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedAnnotation="lorem ipsum")

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval afterwards, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedAnnotation="")

    def test_undo_annotate_with_embedded_quotes(self):
        """Test undo of command 'annotate' with embedded quotes"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)
        two_hours_before_utc = now_utc - timedelta(hours=2)

        self.t("start {:%Y%m%dT%H%M%SZ} foo".format(two_hours_before_utc))
        self.t("start {:%Y%m%dT%H%M%SZ} bar".format(one_hour_before_utc))
        self.t("annotate 'foo \"bar\"'")

        j = self.t.export()
        self.assertEqual(len(j), 2, msg="Expected 2 intervals before, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["foo"])
        self.assertOpenInterval(j[1],
                                expectedStart=one_hour_before_utc,
                                expectedTags=["bar"],
                                expectedAnnotation="foo \"bar\"")

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 2, msg="Expected 2 intervals afterwards, got {}".format(len(j)))
        self.assertOpenInterval(j[1],
                                expectedStart=one_hour_before_utc,
                                expectedTags=["bar"],
                                expectedAnnotation="")

    def test_undo_cancel(self):
        """Test undo of command 'cancel'"""
        one_hour_before_utc = datetime.now(timezone.utc) - timedelta(hours=1)

        self.t("start {:%Y%m%dT%H%M%SZ} foo".format(one_hour_before_utc))
        self.t("cancel")

        j = self.t.export()
        self.assertEqual(len(j), 0, msg="Expected 0 interval before, got {}".format(len(j)))

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval afterwards, got {}".format(len(j)))
        self.assertOpenInterval(j[0],
                                expectedStart=one_hour_before_utc,
                                expectedTags=["foo"])

    def test_undo_config_add_name(self):
        """Test undo of command 'config' (add name)"""
        self.t("config")
        initial = [x for x in self.t.timewrc_content if x != '\n']

        self.t("config foo bar :yes")

        before = [x for x in self.t.timewrc_content if x != '\n']

        self.t("undo")

        after = [x for x in self.t.timewrc_content if x != '\n']

        self.assertNotEqual(before, after)
        self.assertEqual(initial, after)

    def test_undo_config_remove_name(self):
        """Test undo of command 'config' (remove name)"""
        self.t("config foo bar :yes")
        self.t("config foo :yes")

        before = self.t.timewrc_content[:]

        self.t("undo")

        after = self.t.timewrc_content[:]

        self.assertNotEqual(before, after)

    def test_undo_config_set_value(self):
        """Test undo of command 'config' (set value)"""
        self.t("config foo bar :yes")
        self.t("config foo baz :yes")

        before = self.t.timewrc_content[:]

        self.t("undo")

        after = self.t.timewrc_content[:]

        self.assertNotEqual(before, after)

    def test_undo_config_remove_value(self):
        """Test undo of command 'config' (remove value)"""
        self.t("config foo bar :yes")
        self.t("config foo '' :yes")

        before = self.t.timewrc_content[:]

        self.t("undo")

        after = self.t.timewrc_content[:]

        self.assertNotEqual(before, after)

    def test_undo_continue(self):
        """Test undo of command 'continue'"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)
        two_hours_before_utc = now_utc - timedelta(hours=2)

        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} foo".format(two_hours_before_utc, one_hour_before_utc))
        self.t("continue @1 {:%Y%m%dT%H%M%SZ}".format(now_utc))

        j = self.t.export()
        self.assertEqual(len(j), 2, msg="Expected 2 interval before, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["foo"])
        self.assertOpenInterval(j[1],
                                expectedStart=now_utc,
                                expectedTags=["foo"])

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval afterwards, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["foo"])

    def test_undo_delete(self):
        """Test undo of command 'delete'"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)
        two_hours_before_utc = now_utc - timedelta(hours=2)

        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} foo".format(two_hours_before_utc, one_hour_before_utc))
        self.t("delete @1")

        j = self.t.export()
        self.assertEqual(len(j), 0, msg="Expected 0 intervals before, got {}".format(len(j)))

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval afterwards, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["foo"])

    # def test_undo_fill(self):
    # """Test undo of command 'fill 'Not yet implemented
    #   self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} foo bar")
    #   self.t("untag @1 bar")
    #
    #   j = self.t.export()
    #   self.assertEqual(len(j), 1, msg="Expected 1 interval before, got {}".format(len(j)))
    #   self.assertClosedInterval(j[0],
    #                             expectedStart="20180101T060000",
    #                             expectedEnd="20180101T070000",
    #                             expectedTags=["foo"])
    #
    #   self.t("undo")
    #
    #   j = self.t.export()
    #   self.assertEqual(len(j), 1, msg="Expected 1 interval afterwards, got {}".format(len(j)))
    #   self.assertClosedInterval(j[0],
    #                             expectedStart="20180101T060000",
    #                             expectedEnd="20180101T070000",
    #                             expectedTags=["foo bar"])

    def test_undo_join(self):
        """Test undo of command 'join'"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)
        two_hours_before_utc = now_utc - timedelta(hours=2)
        three_hours_before_utc = now_utc - timedelta(hours=3)
        four_hours_before_utc = now_utc - timedelta(hours=4)

        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} bar".format(four_hours_before_utc, three_hours_before_utc))
        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} foo".format(two_hours_before_utc, one_hour_before_utc))
        self.t("join @1 @2")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval before, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=four_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["bar", "foo"],
                                  description="joined interval")

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 2, msg="Expected 2 interval afterwards, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=four_hours_before_utc,
                                  expectedEnd=three_hours_before_utc,
                                  expectedTags=["bar"],
                                  description="first interval")
        self.assertClosedInterval(j[1],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["foo"],
                                  description="second interval")

    def test_undo_lengthen(self):
        """Test undo of command 'lengthen'"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)
        two_hours_before_utc = now_utc - timedelta(hours=2)
        three_hours_before_utc = now_utc - timedelta(hours=3)

        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} foo".format(three_hours_before_utc, two_hours_before_utc))
        self.t("lengthen @1 1h")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval before, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=three_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["foo"])

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval afterwards, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=three_hours_before_utc,
                                  expectedEnd=two_hours_before_utc,
                                  expectedTags=["foo"])

    def test_undo_move(self):
        """Test undo of command 'move'"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)
        two_hours_before_utc = now_utc - timedelta(hours=2)
        three_hours_before_utc = now_utc - timedelta(hours=3)

        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} foo".format(two_hours_before_utc, one_hour_before_utc))
        self.t("move @1 {:%Y%m%dT%H%M%SZ}".format(three_hours_before_utc))

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval before, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=three_hours_before_utc,
                                  expectedEnd=two_hours_before_utc,
                                  expectedTags=["foo"])

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval afterwards, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["foo"])

    def test_undo_resize(self):
        """Test undo of command 'resize'"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)
        two_hours_before_utc = now_utc - timedelta(hours=2)
        three_hours_before_utc = now_utc - timedelta(hours=3)

        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} foo".format(three_hours_before_utc, one_hour_before_utc))
        self.t("resize @1 1h")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval before, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=three_hours_before_utc,
                                  expectedEnd=two_hours_before_utc,
                                  expectedTags=["foo"])

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval afterwards, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=three_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["foo"])

    def test_undo_shorten(self):
        """Test undo of command 'shorten'"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)
        two_hours_before_utc = now_utc - timedelta(hours=2)
        three_hours_before_utc = now_utc - timedelta(hours=3)

        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} foo".format(three_hours_before_utc, one_hour_before_utc))
        self.t("shorten @1 1h")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval before, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=three_hours_before_utc,
                                  expectedEnd=two_hours_before_utc,
                                  expectedTags=["foo"])

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval afterwards, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=three_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["foo"])

    def test_undo_split(self):
        """Test undo of command 'split'"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)
        two_hours_before_utc = now_utc - timedelta(hours=2)
        three_hours_before_utc = now_utc - timedelta(hours=3)

        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} foo".format(three_hours_before_utc, one_hour_before_utc))
        self.t("split @1")

        j = self.t.export()
        self.assertEqual(len(j), 2, msg="Expected 2 intervals before, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=three_hours_before_utc,
                                  expectedEnd=two_hours_before_utc,
                                  expectedTags=["foo"])
        self.assertClosedInterval(j[1],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["foo"])

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval afterwards, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=three_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["foo"])

    def test_undo_start(self):
        """Test undo of command 'start'"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)

        self.t("start {:%Y%m%dT%H%M%SZ} foo".format(one_hour_before_utc))

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval before, got {}".format(len(j)))
        self.assertOpenInterval(j[0],
                                expectedStart=one_hour_before_utc,
                                expectedTags=["foo"])

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 0, msg="Expected 0 interval afterwards, got {}".format(len(j)))

    def test_undo_consecutive_start(self):
        """Test undo of consecutive commands 'start'"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)
        two_hours_before_utc = now_utc - timedelta(hours=2)

        self.t("start {:%Y%m%dT%H%M%SZ} foo".format(two_hours_before_utc))
        self.t("start {:%Y%m%dT%H%M%SZ} bar".format(one_hour_before_utc))

        j = self.t.export()
        self.assertEqual(len(j), 2, msg="Expected 2 intervals before, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["foo"])
        self.assertOpenInterval(j[1],
                                expectedStart=one_hour_before_utc,
                                expectedTags=["bar"])

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval afterwards, got {}".format(len(j)))
        self.assertOpenInterval(j[0],
                                expectedStart=two_hours_before_utc,
                                expectedTags=["foo"])

    def test_undo_start_with_embedded_quotes_in_tag(self):
        """Test undo of 'start' with embedded quotes in tag"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)
        two_hours_before_utc = now_utc - timedelta(hours=2)

        self.t("start {:%Y%m%dT%H%M%SZ} foo".format(two_hours_before_utc))
        self.t("start {:%Y%m%dT%H%M%SZ} 'test \"this quoted\" string'".format(one_hour_before_utc))

        j = self.t.export()
        self.assertEqual(len(j), 2, msg="Expected 2 intervals before, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["foo"])
        self.assertOpenInterval(j[1],
                                expectedStart=one_hour_before_utc,
                                expectedTags=["test \"this quoted\" string"])

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval afterwards, got {}".format(len(j)))
        self.assertOpenInterval(j[0],
                                expectedStart=two_hours_before_utc,
                                expectedTags=["foo"])

    def test_undo_start_with_tag_enclosed_in_backslashes(self):
        """Test undo of 'start' with tag enclosed in backslashes"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)

        self.t("start {:%Y%m%dT%H%M%SZ} '\\foo\\'".format(one_hour_before_utc))

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 intervals before, got {}".format(len(j)))
        self.assertOpenInterval(j[0],
                                  expectedStart=one_hour_before_utc,
                                  expectedTags=["\\foo\\"])

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 0, msg="Expected 0 interval afterwards, got {}".format(len(j)))

    def test_undo_stop(self):
        """Test undo of command 'stop'"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)

        self.t("start {:%Y%m%dT%H%M%SZ} foo".format(one_hour_before_utc))
        self.t("stop {:%Y%m%dT%H%M%SZ}".format(now_utc))

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval before, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=one_hour_before_utc,
                                  expectedEnd=now_utc,
                                  expectedTags=["foo"])

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval afterwards, got {}".format(len(j)))
        self.assertOpenInterval(j[0],
                                expectedStart=one_hour_before_utc,
                                expectedTags=["foo"])

    def test_undo_tag(self):
        """Test undo of command 'tag'"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)
        two_hours_before_utc = now_utc - timedelta(hours=2)

        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} foo".format(two_hours_before_utc, one_hour_before_utc))
        self.t("tag @1 bar")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval before, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["bar", "foo"])

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval afterwards, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["foo"])

    def test_undo_track(self):
        """Test undo of command 'track'"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)
        two_hours_before_utc = now_utc - timedelta(hours=2)
        three_hours_before_utc = now_utc - timedelta(hours=3)
        four_hours_before_utc = now_utc - timedelta(hours=4)

        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} foo".format(four_hours_before_utc, three_hours_before_utc))
        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} bar".format(two_hours_before_utc, one_hour_before_utc))

        j = self.t.export()
        self.assertEqual(len(j), 2, msg="Expected 2 intervals before, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=four_hours_before_utc,
                                  expectedEnd=three_hours_before_utc,
                                  expectedTags=["foo"])
        self.assertClosedInterval(j[1],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["bar"])

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval afterwards, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=four_hours_before_utc,
                                  expectedEnd=three_hours_before_utc,
                                  expectedTags=["foo"])

    def test_undo_track_with_adjust_hint(self):
        """Test undo of command 'track' with adjust hint"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)
        two_hours_before_utc = now_utc - timedelta(hours=2)
        three_hours_before_utc = now_utc - timedelta(hours=3)
        four_hours_before_utc = now_utc - timedelta(hours=4)
        six_hours_before_utc = now_utc - timedelta(hours=6)

        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} foo".format(four_hours_before_utc, three_hours_before_utc))
        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} bar".format(two_hours_before_utc, one_hour_before_utc))

        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} xyz :adjust".format(six_hours_before_utc, now_utc))

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 2 intervals before, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=six_hours_before_utc,
                                  expectedEnd=now_utc,
                                  expectedTags=["xyz"])

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 2, msg="Expected 2 interval afterwards, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=four_hours_before_utc,
                                  expectedEnd=three_hours_before_utc,
                                  expectedTags=["foo"])
        self.assertClosedInterval(j[1],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["bar"])

    def test_undo_untag(self):
        """Test undo of command 'untag'"""
        now_utc = datetime.now(timezone.utc)
        one_hour_before_utc = now_utc - timedelta(hours=1)
        two_hours_before_utc = now_utc - timedelta(hours=2)

        self.t("track {:%Y%m%dT%H%M%SZ} - {:%Y%m%dT%H%M%SZ} foo bar".format(two_hours_before_utc, one_hour_before_utc))
        self.t("untag @1 bar")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval before, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["foo"])

        self.t("undo")

        j = self.t.export()
        self.assertEqual(len(j), 1, msg="Expected 1 interval afterwards, got {}".format(len(j)))
        self.assertClosedInterval(j[0],
                                  expectedStart=two_hours_before_utc,
                                  expectedEnd=one_hour_before_utc,
                                  expectedTags=["bar", "foo"])

    def test_undo_journal_size_equals_one(self):
        """Test undo only stores the last entry when journal.size == 1"""

        self.t("config journal.size 1 :yes")
        self.t("start 16h ago proja")
        self.t("start 15h ago projb")
        before = self.t.export()
        self.t("start 14h ago projc")
        self.t("undo")
        # since we set the journal size to 1, these undos should not have any
        # effect since we have already undone the one entry we saved
        self.t("undo")
        self.t("undo")
        self.assertEqual(before, self.t.export())

    def test_undo_journal_size_equals_two(self):
        """Test undo only stores two entries when journal.size == 2"""
        
        self.t("config journal.size 2 :yes")
        self.t("start 16h ago proja")
        before_b = self.t.export()
        self.t("start 15h ago projb")
        before_c = self.t.export()
        self.t("start 14h ago projc")
        self.t("undo")
        self.assertEqual(before_c, self.t.export())
        self.t("undo")
        self.t("undo") # This undo should not have any effect
        self.assertEqual(before_b, self.t.export())

    def test_undo_journal_size_equals_zero(self):
        """Test undo does not save any entries when journal.size == 0"""

        self.t("config journal.size 0 :yes")
        before_a = self.t.export()
        self.t("start 16h ago proja")
        before_b = self.t.export()
        self.t("start 15h ago projb")
        before_c = self.t.export()
        self.t("start 14h ago projc")
        end = self.t.export()

        # Check that any undo commands do not change the results of the export
        self.t("undo")
        self.assertEqual(end, self.t.export())
        self.t("undo")
        self.assertEqual(end, self.t.export())
        self.t("undo")
        self.assertEqual(end, self.t.export())

    def test_undo_journal_size_unlimited(self):
        """Test undo handles more than two entries when journal.size == -1 """

        # This should error since 0 should be the default value
        self.t.runError("config journal.size -1 :yes")
        before_a = self.t.export()
        self.t("start 16h ago proja")
        before_b = self.t.export()
        self.t("start 15h ago projb")
        before_c = self.t.export()
        self.t("start 14h ago projc")
        self.t("undo")
        self.assertEqual(before_c, self.t.export())
        self.t("undo")
        self.assertEqual(before_b, self.t.export())
        self.t("undo")
        self.assertEqual(before_a, self.t.export())

    def test_undo_process_commands_when_disabled(self):
        """Test that disabling the journal clears it."""

        self.t("start 16h ago proja")
        self.t("start 15h ago projb")
        self.t("start 14h ago projc")
        after_c = self.t.export ();
        self.t("config journal.size 0 :yes")
        after_config = self.t("config")
        code, out, error = self.t("undo") # This change will not change the config or the entries
        self.assertIn("Nothing", out)
        self.assertEqual(after_config, self.t("config"))
        self.assertEqual(after_c, self.t.export())

if __name__ == "__main__":
    from simpletap import TAPTestRunner

    unittest.main(testRunner=TAPTestRunner())