summaryrefslogtreecommitdiff
path: root/internal/handlers/chains_timeline_test.go
blob: d4bc369a32f3b36ba1a34c2ef7632b290a87aa15 (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
package handlers

import (
	"context"
	"testing"
	"time"

	"task-dashboard/internal/config"
	"task-dashboard/internal/models"
)

// Chain steps carry no due date and are kept out of the undated list, so they
// stay off the day's timeline entirely until deliberately committed. This is the
// whole point of the pull model: the day contains what was chosen plus real
// deadlines, so finishing at zero is reachable.
func TestBuildTimeline_UnlockedChainTaskAbsentUntilCommitted(t *testing.T) {
	s, cleanup := setupTestDB(t)
	defer cleanup()

	chain, err := s.CreateChain("Ham Radio Track", []models.ChainTaskInput{
		{Content: "Study Technician"}, {Content: "Pass exam"}, {Content: "Study General"},
	})
	if err != nil {
		t.Fatal(err)
	}
	tasks, err := s.GetChainTasks(chain.ID)
	if err != nil {
		t.Fatal(err)
	}

	now := time.Now()
	items, err := BuildTimeline(context.Background(), s, now.Add(-time.Hour), now.Add(24*time.Hour))
	if err != nil {
		t.Fatalf("BuildTimeline: %v", err)
	}
	for _, item := range items {
		if item.ID == tasks[0].ID {
			t.Error("uncommitted chain step appeared on the timeline; chain work should only arrive by being committed")
		}
	}
}

func TestBuildTimeline_CommittedChainTaskAppearsWithBadge(t *testing.T) {
	s, cleanup := setupTestDB(t)
	defer cleanup()

	chain, err := s.CreateChain("Ham Radio Track", []models.ChainTaskInput{
		{Content: "Study Technician"}, {Content: "Pass exam"}, {Content: "Study General"},
	})
	if err != nil {
		t.Fatal(err)
	}
	tasks, err := s.GetChainTasks(chain.ID)
	if err != nil {
		t.Fatal(err)
	}
	if err := s.SetTaskCommitted(tasks[0].ID, config.Now()); err != nil {
		t.Fatal(err)
	}

	now := time.Now()
	items, err := BuildTimeline(context.Background(), s, now.Add(-time.Hour), now.Add(24*time.Hour))
	if err != nil {
		t.Fatalf("BuildTimeline: %v", err)
	}

	var found bool
	for _, item := range items {
		if item.ID == tasks[0].ID {
			found = true
			if item.ChainPosition != 1 || item.ChainTotal != 3 {
				t.Errorf("ChainPosition/ChainTotal = %d/%d, want 1/3", item.ChainPosition, item.ChainTotal)
			}
		}
		if item.ID == tasks[1].ID || item.ID == tasks[2].ID {
			t.Errorf("locked chain task %q should not appear in the timeline", item.ID)
		}
	}
	if !found {
		t.Fatal("expected the committed chain step to appear on the timeline")
	}
}

// A task that is both committed and due today must appear once, not twice.
func TestBuildTimeline_CommittedAndDueTaskNotDuplicated(t *testing.T) {
	s, cleanup := setupTestDB(t)
	defer cleanup()

	due := config.Now()
	if err := s.CreateNativeTask(models.Task{ID: "dual", Content: "Bills", DueDate: &due}); err != nil {
		t.Fatal(err)
	}
	if err := s.SetTaskCommitted("dual", config.Now()); err != nil {
		t.Fatal(err)
	}

	now := time.Now()
	items, err := BuildTimeline(context.Background(), s, now.Add(-24*time.Hour), now.Add(24*time.Hour))
	if err != nil {
		t.Fatalf("BuildTimeline: %v", err)
	}

	var count int
	for _, item := range items {
		if item.ID == "dual" {
			count++
		}
	}
	if count != 1 {
		t.Errorf("task appeared %d times, want exactly 1 (committed and due are two reasons for the same row)", count)
	}
}