summaryrefslogtreecommitdiff
path: root/internal/store/chains_test.go
blob: 1d165084e5c63cfbd5612838c81dc107932e4e85 (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
package store

import (
	"testing"

	"task-dashboard/internal/models"
)

// chainTasks builds bare-content ChainTaskInputs (no description/priority)
// for tests that only care about title ordering and lock state.
func chainTasks(titles ...string) []models.ChainTaskInput {
	tasks := make([]models.ChainTaskInput, len(titles))
	for i, t := range titles {
		tasks[i] = models.ChainTaskInput{Content: t}
	}
	return tasks
}

func TestCreateChain_SeedsPositionsCorrectly(t *testing.T) {
	s := newNativeTasksTestStore(t)

	chain, err := s.CreateChain("Ham Radio Track", chainTasks("Study Technician", "Pass Technician exam", "Study General"))
	if err != nil {
		t.Fatalf("CreateChain: %v", err)
	}
	if chain.Status != "active" {
		t.Errorf("chain.Status = %q, want active", chain.Status)
	}

	tasks, err := s.GetChainTasks(chain.ID)
	if err != nil {
		t.Fatalf("GetChainTasks: %v", err)
	}
	if len(tasks) != 3 {
		t.Fatalf("len(tasks) = %d, want 3", len(tasks))
	}
	if !tasks[0].ChainUnlocked || tasks[0].DueDate == nil {
		t.Errorf("position 0: ChainUnlocked=%v DueDate=%v, want unlocked with a due date", tasks[0].ChainUnlocked, tasks[0].DueDate)
	}
	for i := 1; i < 3; i++ {
		if tasks[i].ChainUnlocked || tasks[i].DueDate != nil {
			t.Errorf("position %d: ChainUnlocked=%v DueDate=%v, want locked with no due date", i, tasks[i].ChainUnlocked, tasks[i].DueDate)
		}
	}
	if tasks[0].Content != "Study Technician" || tasks[1].Content != "Pass Technician exam" || tasks[2].Content != "Study General" {
		t.Errorf("unexpected content order: %q, %q, %q", tasks[0].Content, tasks[1].Content, tasks[2].Content)
	}
}

func TestCompleteNativeTask_AdvancesChain(t *testing.T) {
	s := newNativeTasksTestStore(t)

	chain, err := s.CreateChain("Track", chainTasks("Step 1", "Step 2"))
	if err != nil {
		t.Fatal(err)
	}
	tasks, err := s.GetChainTasks(chain.ID)
	if err != nil {
		t.Fatal(err)
	}

	if err := s.CompleteNativeTask(tasks[0].ID); err != nil {
		t.Fatalf("CompleteNativeTask: %v", err)
	}

	after, err := s.GetChainTasks(chain.ID)
	if err != nil {
		t.Fatal(err)
	}
	if !after[1].ChainUnlocked || after[1].DueDate == nil {
		t.Errorf("position 1 after completing position 0: ChainUnlocked=%v DueDate=%v, want unlocked with a due date", after[1].ChainUnlocked, after[1].DueDate)
	}

	updatedChain, err := s.GetChain(chain.ID)
	if err != nil {
		t.Fatal(err)
	}
	if updatedChain.Status != "active" {
		t.Errorf("chain.Status = %q, want active (not yet done)", updatedChain.Status)
	}
}

func TestCompleteNativeTask_LastPosition_MarksChainCompleted(t *testing.T) {
	s := newNativeTasksTestStore(t)

	chain, err := s.CreateChain("Track", chainTasks("Only step"))
	if err != nil {
		t.Fatal(err)
	}
	tasks, err := s.GetChainTasks(chain.ID)
	if err != nil {
		t.Fatal(err)
	}

	if err := s.CompleteNativeTask(tasks[0].ID); err != nil {
		t.Fatalf("CompleteNativeTask: %v", err)
	}

	updatedChain, err := s.GetChain(chain.ID)
	if err != nil {
		t.Fatal(err)
	}
	if updatedChain.Status != "completed" {
		t.Errorf("chain.Status = %q, want completed", updatedChain.Status)
	}
}

func TestCompleteNativeTask_PausedChain_DoesNotAdvance(t *testing.T) {
	s := newNativeTasksTestStore(t)

	chain, err := s.CreateChain("Track", chainTasks("Step 1", "Step 2"))
	if err != nil {
		t.Fatal(err)
	}
	if err := s.SetChainStatus(chain.ID, "paused"); err != nil {
		t.Fatal(err)
	}
	tasks, err := s.GetChainTasks(chain.ID)
	if err != nil {
		t.Fatal(err)
	}

	if err := s.CompleteNativeTask(tasks[0].ID); err != nil {
		t.Fatalf("CompleteNativeTask: %v", err)
	}

	after, err := s.GetChainTasks(chain.ID)
	if err != nil {
		t.Fatal(err)
	}
	if after[1].ChainUnlocked {
		t.Error("position 1 should still be locked while chain is paused")
	}

	// Resuming re-enables advancement on the *next* completion.
	if err := s.SetChainStatus(chain.ID, "active"); err != nil {
		t.Fatal(err)
	}
	if err := s.CompleteNativeTask(tasks[1].ID); err != nil {
		t.Fatalf("CompleteNativeTask after resume: %v", err)
	}
	updatedChain, err := s.GetChain(chain.ID)
	if err != nil {
		t.Fatal(err)
	}
	if updatedChain.Status != "completed" {
		t.Errorf("chain.Status = %q, want completed after resuming and finishing the last step", updatedChain.Status)
	}
}

func TestGetUndatedNativeTasks_ExcludesLockedChainTasks(t *testing.T) {
	s := newNativeTasksTestStore(t)

	chain, err := s.CreateChain("Track", chainTasks("Step 1", "Step 2", "Step 3"))
	if err != nil {
		t.Fatal(err)
	}
	_ = chain

	undated, err := s.GetUndatedNativeTasks()
	if err != nil {
		t.Fatalf("GetUndatedNativeTasks: %v", err)
	}
	for _, task := range undated {
		if task.ChainID != "" && !task.ChainUnlocked {
			t.Errorf("locked chain task %q leaked into GetUndatedNativeTasks", task.ID)
		}
	}
}

func TestCreateChain_PersistsDescriptionAndPriority(t *testing.T) {
	s := newNativeTasksTestStore(t)

	chain, err := s.CreateChain("Track", []models.ChainTaskInput{
		{Content: "Step 1", Description: "do the thing", Priority: 4},
		{Content: "Step 2"}, // no priority given -- should default to 1
	})
	if err != nil {
		t.Fatal(err)
	}
	tasks, err := s.GetChainTasks(chain.ID)
	if err != nil {
		t.Fatal(err)
	}
	if tasks[0].Description != "do the thing" || tasks[0].Priority != 4 {
		t.Errorf("tasks[0] = %+v, want description=%q priority=4", tasks[0], "do the thing")
	}
	if tasks[1].Priority != 1 {
		t.Errorf("tasks[1].Priority = %d, want default of 1", tasks[1].Priority)
	}
}

func TestGetChain_UnknownID_ReturnsErrNotFound(t *testing.T) {
	s := newNativeTasksTestStore(t)

	if _, err := s.GetChain("does-not-exist"); err != ErrNativeTaskNotFound {
		t.Errorf("err = %v, want ErrNativeTaskNotFound", err)
	}
}