-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers_test.go
More file actions
661 lines (543 loc) · 23.9 KB
/
handlers_test.go
File metadata and controls
661 lines (543 loc) · 23.9 KB
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
package main
import (
"database/sql"
"testing"
"time"
"charm.land/bubbles/v2/progress"
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
"github.com/stretchr/testify/assert"
)
func TestTimerContinuesAfterPause(t *testing.T) {
// Test that timer calculates elapsed time correctly even after a simulated pause
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix() - 30, // Started 30 seconds ago
targetDuration: 120, // 2 minute timer
title: "Test Task",
mode: timerView,
}
// Simulate the passage of time (another 30 seconds)
time.Sleep(100 * time.Millisecond)
// Calculate elapsed time as the timer does
elapsed := time.Now().Unix() - m.startTime
remaining := m.targetDuration - elapsed
// Verify elapsed time is approximately 30 seconds (within 1 second tolerance)
assert.True(t, elapsed >= 30 && elapsed <= 31, "Expected elapsed time to be ~30 seconds, got %d", elapsed)
assert.True(t, remaining >= 89 && remaining <= 90, "Expected remaining time to be ~90 seconds, got %d", remaining)
// Verify progress calculation
percentCompleted := float64(elapsed) / float64(m.targetDuration)
assert.InDelta(t, 0.25, percentCompleted, 0.01, "Expected ~25%% completion")
}
func TestResumeAfterCompletion(t *testing.T) {
// Set up a temporary database path
tempDBPath, cleanup := setupTestDB(t)
defer cleanup()
// Create a timer that started 70 seconds ago (past the 60 second target)
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix() - 70,
targetDuration: 60,
title: "Completed Task",
mode: timerView,
}
// Initial progress should be 0
assert.Equal(t, 0.0, m.progress.Percent(), "Initial progress should be 0")
// Simulate a tick after resuming
newModel, _ := m.Update(tickMsg(time.Now()))
modelTyped := newModel.(model)
// Verify the prompt is active
assert.True(t, modelTyped.promptActive, "Expected prompt to be active when resuming after completion")
// Complete the prompt
newModel, _ = modelTyped.Update(promptMsg{title: "Completed Task", logDB: true})
// Verify the session was saved to the database
db, err := sql.Open("sqlite", tempDBPath)
assert.NoError(t, err)
defer db.Close()
var count int
err = db.QueryRow("SELECT COUNT(*) FROM sessions WHERE title = ? AND completed = ?", "Completed Task", true).Scan(&count)
assert.NoError(t, err)
assert.Equal(t, 1, count, "Expected one completed session to be saved after prompt")
}
func TestTickAfterCompletion(t *testing.T) {
// Set up a temporary database path
tempDBPath, cleanup := setupTestDB(t)
defer cleanup()
// Create a timer that started 70 seconds ago (past the 60 second target)
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix() - 70,
targetDuration: 60,
title: "Tick After Complete",
mode: timerView,
}
// Simulate a regular tick (not resume)
newModel, _ := m.Update(tickMsg(time.Now()))
modelTyped := newModel.(model)
// Verify prompt is active
assert.True(t, modelTyped.promptActive, "Expected prompt to be active on tick after completion")
// Complete the prompt
newModel, _ = modelTyped.Update(promptMsg{title: "Tick After Complete", logDB: true})
// Verify the session was saved to the database
db, err := sql.Open("sqlite", tempDBPath)
assert.NoError(t, err)
defer db.Close()
var count int
err = db.QueryRow("SELECT COUNT(*) FROM sessions WHERE title = ? AND completed = ?", "Tick After Complete", true).Scan(&count)
assert.NoError(t, err)
assert.Equal(t, 1, count, "Expected one completed session to be saved after prompt on tick after completion")
}
func TestCtrlZSuspendsInTimerView(t *testing.T) {
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix(),
targetDuration: 60,
title: "Test Task",
mode: timerView,
}
// Create a Ctrl-Z key message
keyMsg := tea.KeyPressMsg{Code: 'z', Mod: tea.ModCtrl}
// Process the Ctrl-Z key
newModel, cmd := m.Update(keyMsg)
// Verify the model is returned unchanged
assert.NotNil(t, newModel, "Expected model to be returned")
// Verify a suspend command was returned
assert.NotNil(t, cmd, "Expected suspend command to be returned")
}
func TestCtrlZSuspendsInTableView(t *testing.T) {
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix(),
targetDuration: 60,
title: "Test Task",
mode: tableView,
}
// Create a Ctrl-Z key message
keyMsg := tea.KeyPressMsg{Code: 'z', Mod: tea.ModCtrl}
// Process the Ctrl-Z key
newModel, cmd := m.Update(keyMsg)
// Verify the model is still in table view (not exited)
modelTyped := newModel.(model)
assert.Equal(t, tableView, modelTyped.mode, "Expected to remain in table view after Ctrl-Z")
// Verify a suspend command was returned
assert.NotNil(t, cmd, "Expected suspend command to be returned")
}
func TestOtherKeysStillQuitTimerView(t *testing.T) {
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix(),
targetDuration: 60,
title: "Test Task",
mode: timerView,
}
// Test that pressing 'q' still quits
keyMsg := tea.KeyPressMsg{Code: 'q', Text: "q"}
// Process the key
_, cmd := m.Update(keyMsg)
// Verify a quit command was returned (we can't directly test if it's tea.Quit, but we know it's not nil)
assert.NotNil(t, cmd, "Expected quit command to be returned for non-special keys")
}
func TestCountUpModeInitialization(t *testing.T) {
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix(),
targetDuration: 3600,
countUpMode: true,
mode: timerView,
}
assert.True(t, m.countUpMode, "Expected count-up mode to be enabled")
assert.Equal(t, int64(3600), m.targetDuration, "Expected 1 hour default duration in count-up mode")
}
func TestCountUpModeElapsedTime(t *testing.T) {
startTime := time.Now().Unix() - 30
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: startTime,
targetDuration: 3600,
countUpMode: true,
mode: timerView,
}
elapsed := time.Now().Unix() - m.startTime
assert.True(t, elapsed >= 30 && elapsed <= 32, "Expected elapsed time to be approximately 30 seconds")
}
func TestCountUpModeKeysActivatePrompt(t *testing.T) {
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix(),
targetDuration: 3600,
countUpMode: true,
mode: timerView,
}
// Test 'd' key activates prompt for logging
keyMsg := tea.KeyPressMsg{Code: 'd', Text: "d"}
newModel, _ := m.Update(keyMsg)
modelTyped := newModel.(model)
assert.True(t, modelTyped.promptActive, "Expected prompt to be active after pressing 'd'")
assert.Equal(t, promptLogAndReset, modelTyped.promptType, "Expected promptType to be promptLogAndReset")
// Test 't' key activates prompt for title only
m.promptActive = false
m.title = "Test Title"
keyMsg = tea.KeyPressMsg{Code: 't', Text: "t"}
newModel, _ = m.Update(keyMsg)
modelTyped = newModel.(model)
assert.True(t, modelTyped.promptActive, "Expected prompt to be active after pressing 't'")
assert.Equal(t, promptEditTitle, modelTyped.promptType, "Expected promptType to be promptEditTitle")
}
func TestCountUpModePromptInput(t *testing.T) {
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix(),
targetDuration: 3600,
countUpMode: true,
mode: timerView,
promptActive: true,
inputBuffer: "",
promptType: promptLogAndReset,
}
// Test typing characters
keyMsg := tea.KeyPressMsg{Text: "Test"}
newModel, _ := m.Update(keyMsg)
modelTyped := newModel.(model)
assert.Equal(t, "Test", modelTyped.inputBuffer, "Expected input buffer to accumulate typed characters")
// Test backspace
keyMsg = tea.KeyPressMsg{Code: tea.KeyBackspace}
newModel, _ = modelTyped.Update(keyMsg)
modelTyped = newModel.(model)
assert.Equal(t, "Tes", modelTyped.inputBuffer, "Expected backspace to remove last character")
}
func TestCountUpModePromptLogAndReset(t *testing.T) {
tempDBPath, cleanup := setupTestDB(t)
defer cleanup()
startTime := time.Now().Unix() - 120
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: startTime,
targetDuration: 3600,
countUpMode: true,
mode: timerView,
}
// Simulate prompt completion (type 0 = log and reset)
newModel, _ := m.Update(promptMsg{title: "Test Task", logDB: true})
modelTyped := newModel.(model)
assert.Equal(t, "", modelTyped.title, "Expected title to be blank after logging")
assert.False(t, modelTyped.promptActive, "Expected prompt to be inactive after completion")
// Verify session was saved to DB
db, err := sql.Open("sqlite", tempDBPath)
assert.NoError(t, err)
defer db.Close()
var count int
var savedTitle string
err = db.QueryRow("SELECT COUNT(*), title FROM sessions WHERE title = ?", "Test Task").Scan(&count, &savedTitle)
assert.NoError(t, err)
assert.Equal(t, 1, count, "Expected session to be saved to database")
assert.Equal(t, "Test Task", savedTitle, "Expected task title to be saved")
}
func TestCountUpModePromptTitleOnly(t *testing.T) {
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix() - 50,
targetDuration: 3600,
countUpMode: true,
mode: timerView,
title: "Old Title",
}
// Test that pressing 't' activates title-only prompt
keyMsg := tea.KeyPressMsg{Code: 't', Text: "t"}
newModel, _ := m.Update(keyMsg)
modelTyped := newModel.(model)
assert.True(t, modelTyped.promptActive, "Expected prompt to be active after pressing 't'")
assert.Equal(t, promptEditTitle, modelTyped.promptType, "Expected promptType to be promptEditTitle")
assert.Equal(t, "Old Title", modelTyped.inputBuffer, "Expected input buffer to pre-fill with current title")
// Now test that input works in prompt mode (appending to pre-filled text)
m = modelTyped
keyMsg = tea.KeyPressMsg{Text: "New"}
newModel, _ = m.Update(keyMsg)
modelTyped = newModel.(model)
assert.Equal(t, "Old TitleNew", modelTyped.inputBuffer, "Expected input buffer to contain pre-filled text plus typed text")
}
func TestCountUpModePromptWithSpaces(t *testing.T) {
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix() - 50,
targetDuration: 3600,
countUpMode: true,
mode: timerView,
title: "Work",
}
// Activate prompt with 'd' key
keyMsg := tea.KeyPressMsg{Code: 'd', Text: "d"}
newModel, _ := m.Update(keyMsg)
m = newModel.(model)
assert.True(t, m.promptActive, "Expected prompt to be active")
// Type "Work on " with space
keyMsg = tea.KeyPressMsg{Text: "on task"}
newModel, _ = m.Update(keyMsg)
m = newModel.(model)
assert.Equal(t, "Workon task", m.inputBuffer, "Expected input to include space from runes")
// Also test explicit space key
m.inputBuffer = "Test"
keyMsg = tea.KeyPressMsg{Code: tea.KeySpace}
newModel, _ = m.Update(keyMsg)
m = newModel.(model)
assert.Equal(t, "Test ", m.inputBuffer, "Expected KeySpace to add space to input buffer")
}
func TestNormalModeSetTitle(t *testing.T) {
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix(),
targetDuration: 1500,
countUpMode: false,
mode: timerView,
title: "Old Title",
}
// Press 't' to activate prompt
keyMsg := tea.KeyPressMsg{Code: 't', Text: "t"}
newModel, _ := m.Update(keyMsg)
modelTyped := newModel.(model)
assert.True(t, modelTyped.promptActive, "Expected prompt to be active after pressing 't' in normal mode")
assert.Equal(t, promptEditTitle, modelTyped.promptType, "Expected promptType to be promptEditTitle in normal mode")
assert.Equal(t, "Old Title", modelTyped.inputBuffer, "Expected input buffer to pre-fill with current title")
// Simulate typing new title
modelTyped.inputBuffer = "New Title"
newModel, _ = modelTyped.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
modelTyped = newModel.(model)
assert.Equal(t, "New Title", modelTyped.title, "Expected title to be updated")
assert.False(t, modelTyped.promptActive, "Expected prompt to be inactive after Enter")
}
func TestHKeyShowsHistoryInCountdownMode(t *testing.T) {
// Set up a temporary database path
_, cleanup := setupTestDB(t)
defer cleanup()
// Initialize database
err := initDB()
assert.NoError(t, err)
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix(),
targetDuration: 1500,
countUpMode: false,
mode: timerView,
}
// Press 'h' to show history
keyMsg := tea.KeyPressMsg{Code: 'h', Text: "h"}
newModel, _ := m.Update(keyMsg)
modelTyped := newModel.(model)
assert.Equal(t, tableView, modelTyped.mode, "Expected to switch to table view after pressing 'h'")
}
func TestHKeyShowsHistoryInCountUpMode(t *testing.T) {
// Set up a temporary database path
_, cleanup := setupTestDB(t)
defer cleanup()
// Initialize database
err := initDB()
assert.NoError(t, err)
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix(),
targetDuration: 3600,
countUpMode: true,
mode: timerView,
}
// Press 'h' to show history
keyMsg := tea.KeyPressMsg{Code: 'h', Text: "h"}
newModel, _ := m.Update(keyMsg)
modelTyped := newModel.(model)
assert.Equal(t, tableView, modelTyped.mode, "Expected to switch to table view after pressing 'h'")
}
func TestDKeyMarksDoneInCountdownMode(t *testing.T) {
tempDBPath, cleanup := setupTestDB(t)
defer cleanup()
startTime := time.Now().Unix() - 120
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: startTime,
targetDuration: 1500,
countUpMode: false,
mode: timerView,
title: "Test Task",
}
// Press 'd' to mark done
keyMsg := tea.KeyPressMsg{Code: 'd', Text: "d"}
newModel, _ := m.Update(keyMsg)
modelTyped := newModel.(model)
assert.True(t, modelTyped.promptActive, "Expected prompt to be active after pressing 'd'")
assert.Equal(t, promptLogAndReset, modelTyped.promptType, "Expected promptType to be promptLogAndReset")
// Complete the prompt
newModel, _ = modelTyped.Update(promptMsg{title: "Test Task", logDB: true})
modelTyped = newModel.(model)
// Verify session was saved to DB
db, err := sql.Open("sqlite", tempDBPath)
assert.NoError(t, err)
defer db.Close()
var count int
err = db.QueryRow("SELECT COUNT(*) FROM sessions WHERE title = ?", "Test Task").Scan(&count)
assert.NoError(t, err)
assert.Equal(t, 1, count, "Expected session to be saved to database")
}
func TestDKeyMarksDoneInCountUpMode(t *testing.T) {
tempDBPath, cleanup := setupTestDB(t)
defer cleanup()
startTime := time.Now().Unix() - 120
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: startTime,
targetDuration: 3600,
countUpMode: true,
mode: timerView,
title: "Count Up Task",
}
// Press 'd' to mark done
keyMsg := tea.KeyPressMsg{Code: 'd', Text: "d"}
newModel, _ := m.Update(keyMsg)
modelTyped := newModel.(model)
assert.True(t, modelTyped.promptActive, "Expected prompt to be active after pressing 'd'")
assert.Equal(t, promptLogAndReset, modelTyped.promptType, "Expected promptType to be promptLogAndReset")
// Complete the prompt
newModel, _ = modelTyped.Update(promptMsg{title: "Count Up Task", logDB: true})
modelTyped = newModel.(model)
// Verify session was saved to DB
db, err := sql.Open("sqlite", tempDBPath)
assert.NoError(t, err)
defer db.Close()
var count int
err = db.QueryRow("SELECT COUNT(*) FROM sessions WHERE title = ?", "Count Up Task").Scan(&count)
assert.NoError(t, err)
assert.Equal(t, 1, count, "Expected session to be saved to database")
}
func TestTKeyEditsTitleInCountdownMode(t *testing.T) {
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix(),
targetDuration: 1500,
countUpMode: false,
mode: timerView,
title: "Old Title",
}
// Press 't' to edit title
keyMsg := tea.KeyPressMsg{Code: 't', Text: "t"}
newModel, _ := m.Update(keyMsg)
modelTyped := newModel.(model)
assert.True(t, modelTyped.promptActive, "Expected prompt to be active after pressing 't'")
assert.Equal(t, promptEditTitle, modelTyped.promptType, "Expected promptType to be promptEditTitle")
assert.Equal(t, "Old Title", modelTyped.inputBuffer, "Expected input buffer to pre-fill with current title")
// Complete the prompt with new title
newModel, _ = modelTyped.Update(promptMsg{title: "New Title", logDB: false})
modelTyped = newModel.(model)
assert.Equal(t, "New Title", modelTyped.title, "Expected title to be updated")
assert.False(t, modelTyped.promptActive, "Expected prompt to be inactive after completion")
}
func TestTKeyEditsTitleInCountUpMode(t *testing.T) {
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix(),
targetDuration: 3600,
countUpMode: true,
mode: timerView,
title: "Old Title",
}
// Press 't' to edit title
keyMsg := tea.KeyPressMsg{Code: 't', Text: "t"}
newModel, _ := m.Update(keyMsg)
modelTyped := newModel.(model)
assert.True(t, modelTyped.promptActive, "Expected prompt to be active after pressing 't'")
assert.Equal(t, promptEditTitle, modelTyped.promptType, "Expected promptType to be promptEditTitle")
assert.Equal(t, "Old Title", modelTyped.inputBuffer, "Expected input buffer to pre-fill with current title")
}
func TestMKeySetsDurationInCountdownMode(t *testing.T) {
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix(),
targetDuration: 1500, // 25 minutes
countUpMode: false,
mode: timerView,
}
// Press 'm' to set duration
keyMsg := tea.KeyPressMsg{Code: 'm', Text: "m"}
newModel, _ := m.Update(keyMsg)
modelTyped := newModel.(model)
assert.True(t, modelTyped.promptActive, "Expected prompt to be active after pressing 'm'")
assert.Equal(t, promptSetDuration, modelTyped.promptType, "Expected promptType to be promptSetDuration")
// Complete the prompt with 30 minutes
newModel, _ = modelTyped.Update(promptMsg{title: "30", logDB: false})
modelTyped = newModel.(model)
assert.Equal(t, int64(1800), modelTyped.targetDuration, "Expected duration to be 30 minutes (1800 seconds)")
assert.False(t, modelTyped.promptActive, "Expected prompt to be inactive after completion")
}
func TestMKeySetsDurationInCountUpMode(t *testing.T) {
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix(),
targetDuration: 3600, // 1 hour
countUpMode: true,
mode: timerView,
}
// Press 'm' to set duration
keyMsg := tea.KeyPressMsg{Code: 'm', Text: "m"}
newModel, _ := m.Update(keyMsg)
modelTyped := newModel.(model)
assert.True(t, modelTyped.promptActive, "Expected prompt to be active after pressing 'm'")
assert.Equal(t, promptSetDuration, modelTyped.promptType, "Expected promptType to be promptSetDuration")
// Complete the prompt with 45 minutes
newModel, _ = modelTyped.Update(promptMsg{title: "45", logDB: false})
modelTyped = newModel.(model)
assert.Equal(t, int64(2700), modelTyped.targetDuration, "Expected duration to be 45 minutes (2700 seconds)")
assert.False(t, modelTyped.promptActive, "Expected prompt to be inactive after completion")
}
func TestMKeyResetsTimerAfterSettingDuration(t *testing.T) {
startTime := time.Now().Unix() - 60
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: startTime,
targetDuration: 1500,
countUpMode: false,
mode: timerView,
}
// Press 'm' to set duration
keyMsg := tea.KeyPressMsg{Code: 'm', Text: "m"}
newModel, _ := m.Update(keyMsg)
modelTyped := newModel.(model)
// Complete the prompt with 20 minutes
newModel, _ = modelTyped.Update(promptMsg{title: "20", logDB: false})
modelTyped = newModel.(model)
// Verify timer was reset (startTime should be recent)
elapsed := time.Now().Unix() - modelTyped.startTime
assert.True(t, elapsed < 2, "Expected timer to be reset (elapsed time should be < 2 seconds)")
assert.Equal(t, int64(1200), modelTyped.targetDuration, "Expected duration to be 20 minutes (1200 seconds)")
}
func TestTimerEndPromptsForTitle(t *testing.T) {
// Set up a temporary database path
_, cleanup := setupTestDB(t)
defer cleanup()
// Create a timer that is just about to finish
m := model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix() - 61,
targetDuration: 60,
title: "Original Title",
mode: timerView,
}
// Simulate a tick
newModel, _ := m.Update(tickMsg(time.Now()))
modelTyped := newModel.(model)
// Verify that instead of quitting, it activated the prompt
assert.True(t, modelTyped.promptActive, "Expected prompt to be active when timer ends")
assert.Equal(t, promptLogAndReset, modelTyped.promptType, "Expected promptType to be promptLogAndReset")
assert.Equal(t, "Original Title", modelTyped.inputBuffer, "Expected input buffer to be pre-filled with current title")
}
func TestCountUpModeUsesPositionalArgAsTarget(t *testing.T) {
// This test simulates the logic in main.go for initializing the model
// We want to ensure that if countUpMode is true, targetDuration can still be set by positional args
// Case 1: Default count-up duration (no positional arg simulated)
m1 := model{
countUpMode: true,
targetDuration: defaultCountUpDuration,
}
assert.Equal(t, int64(3600), m1.targetDuration)
// Case 2: Positional arg provided (e.g., 5 minutes)
// In main.go, this would be: targetDuration = 5 * 60
m2 := model{
countUpMode: true,
targetDuration: 300,
}
assert.Equal(t, int64(300), m2.targetDuration, "Target duration should be 300 seconds (5 minutes) even in count-up mode")
}