summaryrefslogtreecommitdiff
path: root/internal/store/chains_test.go
blob: bcfc09e936f5abe3ff023009feba66e2e78a5f9e (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
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 performs the deferred unlock itself -- position 1 becomes
	// completable immediately, not only after some future completion.
	if err := s.SetChainStatus(chain.ID, "active"); err != nil {
		t.Fatal(err)
	}
	resumed, err := s.GetChainTasks(chain.ID)
	if err != nil {
		t.Fatal(err)
	}
	if !resumed[1].ChainUnlocked {
		t.Fatal("expected resume to unlock position 1 immediately (deferred advancement catch-up)")
	}

	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 TestCompleteNativeTask_LockedChainTask_ReturnsErrChainTaskLocked(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[1].ID); err != ErrChainTaskLocked {
		t.Errorf("err = %v, want ErrChainTaskLocked", err)
	}

	// Confirm nothing was mutated -- still locked, still not completed.
	after, err := s.GetChainTasks(chain.ID)
	if err != nil {
		t.Fatal(err)
	}
	if after[1].Completed || after[1].ChainUnlocked {
		t.Errorf("locked task should be untouched by the rejected completion attempt: %+v", after[1])
	}
}

func TestSetChainStatus_ResumeWithNothingStuck_DoesNotResetDueDate(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)
	}
	originalDueDate := *tasks[0].DueDate

	// Pause and resume with nothing completed yet -- position 0 is already
	// unlocked and should be left untouched by the resume catch-up.
	if err := s.SetChainStatus(chain.ID, "paused"); err != nil {
		t.Fatal(err)
	}
	if err := s.SetChainStatus(chain.ID, "active"); err != nil {
		t.Fatal(err)
	}

	after, err := s.GetChainTasks(chain.ID)
	if err != nil {
		t.Fatal(err)
	}
	if !after[0].DueDate.Equal(originalDueDate) {
		t.Errorf("DueDate = %v, want unchanged %v (resume catch-up should be a no-op when nothing was stuck)", after[0].DueDate, originalDueDate)
	}
}

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)
	}
}

// TestDeleteNativeTask_LockedChainTask_ClosesPositionGap proves deleting a
// not-yet-reached chain step doesn't strand the chain: positions after the
// deleted one shift down by one and stay a contiguous 0..N-1 sequence, since
// advanceChain's chain_position+1 lookup depends on that contiguity.
func TestDeleteNativeTask_LockedChainTask_ClosesPositionGap(t *testing.T) {
	s := newNativeTasksTestStore(t)

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

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

	after, err := s.GetChainTasks(chain.ID)
	if err != nil {
		t.Fatal(err)
	}
	if len(after) != 2 {
		t.Fatalf("len(after) = %d, want 2", len(after))
	}
	if after[0].Content != "Step 1" || after[1].Content != "Step 3" {
		t.Errorf("unexpected content order: %q, %q", after[0].Content, after[1].Content)
	}
	if after[1].ChainPosition != 1 {
		t.Errorf("Step 3 chain_position = %d, want 1 (gap closed)", after[1].ChainPosition)
	}
	if !after[0].ChainUnlocked {
		t.Errorf("position 0 should still be unlocked")
	}
	if after[1].ChainUnlocked {
		t.Errorf("position 1 (formerly locked position 2) should still be locked")
	}

	// Completing position 0 should now correctly advance to the
	// renumbered position 1 (Step 3), proving advanceChain's
	// chain_position+1 lookup still works post-deletion.
	if err := s.CompleteNativeTask(after[0].ID); err != nil {
		t.Fatalf("CompleteNativeTask: %v", err)
	}
	final, err := s.GetChainTasks(chain.ID)
	if err != nil {
		t.Fatal(err)
	}
	if !final[1].ChainUnlocked {
		t.Errorf("Step 3 should be unlocked after completing Step 1")
	}
}

// TestDeleteNativeTask_UnlockedChainTask_PromotesSuccessor proves deleting
// the currently-actionable (unlocked) step unlocks whatever now sits at its
// position, rather than leaving the chain with nothing unlocked.
func TestDeleteNativeTask_UnlockedChainTask_PromotesSuccessor(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.DeleteNativeTask(tasks[0].ID); err != nil {
		t.Fatalf("DeleteNativeTask: %v", err)
	}

	after, err := s.GetChainTasks(chain.ID)
	if err != nil {
		t.Fatal(err)
	}
	if len(after) != 1 {
		t.Fatalf("len(after) = %d, want 1", len(after))
	}
	if !after[0].ChainUnlocked || after[0].DueDate == nil {
		t.Errorf("Step 2 should be promoted to unlocked with a due date, got ChainUnlocked=%v DueDate=%v", after[0].ChainUnlocked, after[0].DueDate)
	}
}

// TestDeleteNativeTask_LastRemainingChainTask_MarksChainCompleted proves
// deleting the sole unlocked task with nothing left to promote finishes the
// chain instead of leaving it active with zero unlocked tasks forever.
func TestDeleteNativeTask_LastRemainingChainTask_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.DeleteNativeTask(tasks[0].ID); err != nil {
		t.Fatalf("DeleteNativeTask: %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)
	}
}