summaryrefslogtreecommitdiff
path: root/internal/commands/in_test.go
blob: b0a096098f932a2b0c3d427e6fc57c377e35bc5f (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
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
package commands

import (
	"context"
	"database/sql"
	"strings"
	"testing"
	"time"

	"git.tjp.lol/punchcard/internal/queries"
)

func TestInCommand(t *testing.T) {
	tests := []struct {
		name            string
		setupData       func(*queries.Queries) (clientID, projectID int64)
		args            []string
		expectedOutputs []string // Multiple possible outputs to check
		expectError     bool
		errorContains   string
	}{
		{
			name: "punch in with client by name",
			setupData: func(q *queries.Queries) (int64, int64) {
				client, _ := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name:         "TestCorp",
					Email:        sql.NullString{String: "test@testcorp.com", Valid: true},
					BillableRate: sql.NullInt64{},
				})
				return client.ID, 0
			},
			args:            []string{"in", "-c", "TestCorp"},
			expectedOutputs: []string{"Started timer (ID: 1) for client: TestCorp"},
			expectError:     false,
		},
		{
			name: "punch in with client by ID",
			setupData: func(q *queries.Queries) (int64, int64) {
				client, _ := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name:         "TechSolutions",
					Email:        sql.NullString{String: "contact@techsolutions.com", Valid: true},
					BillableRate: sql.NullInt64{},
				})
				return client.ID, 0
			},
			args:            []string{"in", "--client", "1"},
			expectedOutputs: []string{"Started timer (ID: 1) for client: TechSolutions"},
			expectError:     false,
		},
		{
			name: "punch in with client and project",
			setupData: func(q *queries.Queries) (int64, int64) {
				client, _ := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name:  "StartupXYZ",
					Email: sql.NullString{String: "hello@startupxyz.io", Valid: true},
				})
				project, _ := q.CreateProject(context.Background(), queries.CreateProjectParams{
					Name:         "Website Redesign",
					ClientID:     client.ID,
					BillableRate: sql.NullInt64{},
				})
				return client.ID, project.ID
			},
			args:            []string{"in", "-c", "StartupXYZ", "-p", "Website Redesign"},
			expectedOutputs: []string{"Started timer (ID: 1) for client: StartupXYZ, project: Website Redesign"},
			expectError:     false,
		},
		{
			name: "punch in with description",
			setupData: func(q *queries.Queries) (int64, int64) {
				client, _ := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name:  "BigCorp",
					Email: sql.NullString{String: "contact@bigcorp.com", Valid: true},
				})
				return client.ID, 0
			},
			args:            []string{"in", "-c", "BigCorp", "Working on frontend"},
			expectedOutputs: []string{"Started timer (ID: 1) for client: BigCorp, description: Working on frontend"},
			expectError:     false,
		},
		{
			name: "punch in with client, project, and description",
			setupData: func(q *queries.Queries) (int64, int64) {
				client, _ := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name:  "MegaCorp",
					Email: sql.NullString{String: "info@megacorp.com", Valid: true},
				})
				project, _ := q.CreateProject(context.Background(), queries.CreateProjectParams{
					Name:     "Mobile App",
					ClientID: client.ID,
				})
				return client.ID, project.ID
			},
			args:            []string{"in", "-c", "MegaCorp", "-p", "Mobile App", "Implementing login flow"},
			expectedOutputs: []string{"Started timer (ID: 1) for client: MegaCorp, project: Mobile App, description: Implementing login flow"},
			expectError:     false,
		},
		{
			name:          "punch in without client",
			setupData:     func(q *queries.Queries) (int64, int64) { return 0, 0 },
			args:          []string{"in"},
			expectError:   true,
			errorContains: "no previous time entries found",
		},
		{
			name:          "punch in with nonexistent client",
			setupData:     func(q *queries.Queries) (int64, int64) { return 0, 0 },
			args:          []string{"in", "-c", "NonexistentClient"},
			expectError:   true,
			errorContains: "invalid client",
		},
		{
			name:          "punch in with nonexistent project but no client",
			setupData:     func(q *queries.Queries) (int64, int64) { return 0, 0 },
			args:          []string{"in", "-p", "SomeProject"},
			expectError:   true,
			errorContains: "invalid project: project not found",
		},
		{
			name: "punch in with project not belonging to client",
			setupData: func(q *queries.Queries) (int64, int64) {
				client1, _ := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name:  "Client1",
					Email: sql.NullString{String: "test1@example.com", Valid: true},
				})
				client2, _ := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name:  "Client2",
					Email: sql.NullString{String: "test2@example.com", Valid: true},
				})
				project, _ := q.CreateProject(context.Background(), queries.CreateProjectParams{
					Name:     "Project1",
					ClientID: client2.ID,
				})
				return client1.ID, project.ID
			},
			args:          []string{"in", "-c", "Client1", "-p", "Project1"},
			expectError:   true,
			errorContains: "does not belong to client",
		},
		{
			name: "punch in with project but no client - uses project's client",
			setupData: func(q *queries.Queries) (int64, int64) {
				client, _ := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name:  "ProjectClient",
					Email: sql.NullString{String: "project@client.com", Valid: true},
				})
				project, _ := q.CreateProject(context.Background(), queries.CreateProjectParams{
					Name:     "TestProject",
					ClientID: client.ID,
				})
				return client.ID, project.ID
			},
			args:            []string{"in", "-p", "TestProject"},
			expectedOutputs: []string{"Started timer (ID: 1) for client: ProjectClient, project: TestProject"},
			expectError:     false,
		},
		{
			name:          "punch in with no flags - no previous entries",
			setupData:     func(q *queries.Queries) (int64, int64) { return 0, 0 },
			args:          []string{"in"},
			expectError:   true,
			errorContains: "no previous time entries found",
		},
		{
			name: "punch in with billable rate flag",
			setupData: func(q *queries.Queries) (int64, int64) {
				client, _ := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name:         "BillableClient",
					Email:        sql.NullString{},
					BillableRate: sql.NullInt64{},
				})
				return client.ID, 0
			},
			args:            []string{"in", "-c", "BillableClient", "--hourly-rate", "250.75", "Premium work"},
			expectedOutputs: []string{"Started timer (ID: 1) for client: BillableClient, description: Premium work"},
			expectError:     false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Setup fresh database for each test
			q, cleanup := setupTestDB(t)
			defer cleanup()

			// Setup test data
			tt.setupData(q)

			// Execute command
			output, err := executeCommandWithDB(t, q, tt.args...)

			// Check error expectation
			if tt.expectError {
				if err == nil {
					t.Errorf("Expected error but got none")
				} else if tt.errorContains != "" && !strings.Contains(err.Error(), tt.errorContains) {
					t.Errorf("Expected error to contain %q, got %q", tt.errorContains, err.Error())
				}
				return
			}

			if err != nil {
				t.Errorf("Unexpected error: %v", err)
				return
			}

			// Check output contains expected strings
			found := false
			for _, expectedOutput := range tt.expectedOutputs {
				if strings.Contains(output, expectedOutput) {
					found = true
					break
				}
			}

			if !found {
				t.Errorf("Expected output to contain one of %v, got %q", tt.expectedOutputs, output)
			}
		})
	}
}

func TestInCommandActiveTimerBehaviors(t *testing.T) {
	tests := []struct {
		name           string
		setupData      func(*queries.Queries) error
		firstArgs      []string
		secondArgs     []string
		expectFirstIn  string
		expectSecondIn string
	}{
		{
			name: "no-op when identical timer already active",
			setupData: func(q *queries.Queries) error {
				client, err := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name: "TestClient",
				})
				if err != nil {
					return err
				}

				_, err = q.CreateProject(context.Background(), queries.CreateProjectParams{
					Name:     "TestProject",
					ClientID: client.ID,
				})
				return err
			},
			firstArgs:      []string{"in", "-c", "TestClient", "-p", "TestProject", "Working on tests"},
			secondArgs:     []string{"in", "-c", "TestClient", "-p", "TestProject", "Working on tests"}, // Identical
			expectFirstIn:  "Started timer (ID: 1)",
			expectSecondIn: "Timer already active with same parameters (ID: 1)",
		},
		{
			name: "stop current and start new when different description",
			setupData: func(q *queries.Queries) error {
				_, err := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name: "TestClient",
				})
				return err
			},
			firstArgs:      []string{"in", "-c", "TestClient", "First task"},
			secondArgs:     []string{"in", "-c", "TestClient", "Second task"}, // Different description
			expectFirstIn:  "Started timer (ID: 1)",
			expectSecondIn: "Stopped previous timer (ID: 1)",
		},
		{
			name: "stop current and start new when different client",
			setupData: func(q *queries.Queries) error {
				_, err := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name: "Client1",
				})
				if err != nil {
					return err
				}

				_, err = q.CreateClient(context.Background(), queries.CreateClientParams{
					Name: "Client2",
				})
				return err
			},
			firstArgs:      []string{"in", "-c", "Client1"},
			secondArgs:     []string{"in", "-c", "Client2"}, // Different client
			expectFirstIn:  "Started timer (ID: 1)",
			expectSecondIn: "Stopped previous timer (ID: 1)",
		},
		{
			name: "copy most recent entry when no flags provided",
			setupData: func(q *queries.Queries) error {
				client, err := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name: "TestClient",
				})
				if err != nil {
					return err
				}

				// Create and immediately stop a time entry
				if _, err := q.CreateTimeEntry(context.Background(), queries.CreateTimeEntryParams{
					Description: sql.NullString{String: "Previous work", Valid: true},
					ClientID:    client.ID,
				}); err != nil {
					return err
				}

				_, err = q.StopTimeEntry(context.Background())
				return err
			},
			firstArgs:      []string{"in"}, // No flags - should copy most recent
			secondArgs:     []string{"in"}, // Same - should be no-op
			expectFirstIn:  "Started timer (ID: 2) for client: TestClient, description: Previous work",
			expectSecondIn: "Timer already active with same parameters (ID: 2)",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Setup fresh database for each test
			q, cleanup := setupTestDB(t)
			defer cleanup()

			// Setup test data
			if err := tt.setupData(q); err != nil {
				t.Fatalf("Failed to setup test data: %v", err)
			}

			// Execute first command
			output1, err1 := executeCommandWithDB(t, q, tt.firstArgs...)
			if err1 != nil {
				t.Fatalf("First command failed: %v", err1)
			}

			if !strings.Contains(output1, tt.expectFirstIn) {
				t.Errorf("First command output should contain %q, got: %s", tt.expectFirstIn, output1)
			}

			// Execute second command
			output2, err2 := executeCommandWithDB(t, q, tt.secondArgs...)
			if err2 != nil {
				t.Fatalf("Second command failed: %v", err2)
			}

			if !strings.Contains(output2, tt.expectSecondIn) {
				t.Errorf("Second command output should contain %q, got: %s", tt.expectSecondIn, output2)
			}
		})
	}
}

func TestInCommandBillableRateStorage(t *testing.T) {
	tests := []struct {
		name              string
		setupData         func(*queries.Queries) (clientID, projectID int64)
		args              []string
		expectedRate      *int64 // nil means should use coalesced value, values in cents
		expectExplicitNil bool   // true means should be NULL despite coalescing options
	}{
		{
			name: "explicit billable rate overrides client rate",
			setupData: func(q *queries.Queries) (int64, int64) {
				client, _ := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name:         "ClientWithRate",
					Email:        sql.NullString{},
					BillableRate: sql.NullInt64{Int64: 10000, Valid: true}, // $100.00
				})
				return client.ID, 0
			},
			args:         []string{"in", "-c", "ClientWithRate", "--hourly-rate", "175.25"},
			expectedRate: func() *int64 { f := int64(17525); return &f }(), // $175.25
		},
		{
			name: "explicit billable rate overrides project rate",
			setupData: func(q *queries.Queries) (int64, int64) {
				client, _ := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name:         "TestClient",
					Email:        sql.NullString{},
					BillableRate: sql.NullInt64{Int64: 10000, Valid: true}, // $100.00
				})
				project, _ := q.CreateProject(context.Background(), queries.CreateProjectParams{
					Name:         "ProjectWithRate",
					ClientID:     client.ID,
					BillableRate: sql.NullInt64{Int64: 15000, Valid: true}, // $150.00
				})
				return client.ID, project.ID
			},
			args:         []string{"in", "-c", "TestClient", "-p", "ProjectWithRate", "--hourly-rate", "200.50"},
			expectedRate: func() *int64 { f := int64(20050); return &f }(), // $200.50
		},
		{
			name: "no explicit rate results in NULL (even with project rate)",
			setupData: func(q *queries.Queries) (int64, int64) {
				client, _ := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name:         "TestClient",
					Email:        sql.NullString{},
					BillableRate: sql.NullInt64{Int64: 10000, Valid: true}, // $100.00
				})
				project, _ := q.CreateProject(context.Background(), queries.CreateProjectParams{
					Name:         "ProjectWithRate",
					ClientID:     client.ID,
					BillableRate: sql.NullInt64{Int64: 12500, Valid: true}, // $125.00
				})
				return client.ID, project.ID
			},
			args:              []string{"in", "-c", "TestClient", "-p", "ProjectWithRate"},
			expectExplicitNil: true, // NULL because no explicit rate override
		},
		{
			name: "no explicit rate results in NULL (even with client rate)",
			setupData: func(q *queries.Queries) (int64, int64) {
				client, _ := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name:         "ClientOnly",
					Email:        sql.NullString{},
					BillableRate: sql.NullInt64{Int64: 9000, Valid: true}, // $90.00
				})
				return client.ID, 0
			},
			args:              []string{"in", "-c", "ClientOnly"},
			expectExplicitNil: true, // NULL because no explicit rate override
		},
		{
			name: "no rates anywhere results in NULL",
			setupData: func(q *queries.Queries) (int64, int64) {
				client, _ := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name:         "NoRateClient",
					Email:        sql.NullString{},
					BillableRate: sql.NullInt64{},
				})
				return client.ID, 0
			},
			args:              []string{"in", "-c", "NoRateClient"},
			expectExplicitNil: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Setup fresh database for each test
			q, cleanup := setupTestDB(t)
			defer cleanup()

			// Setup test data
			tt.setupData(q)

			// Execute command
			_, err := executeCommandWithDB(t, q, tt.args...)
			if err != nil {
				t.Fatalf("Unexpected error: %v", err)
			}

			// Query the created time entry to verify billable_rate
			timeEntry, err := q.GetActiveTimeEntry(context.Background())
			if err != nil {
				t.Fatalf("Failed to query created time entry: %v", err)
			}

			if tt.expectExplicitNil {
				if timeEntry.BillableRate.Valid {
					t.Errorf("Expected NULL billable_rate, got %d", timeEntry.BillableRate.Int64)
				}
			} else if tt.expectedRate != nil {
				if !timeEntry.BillableRate.Valid {
					t.Errorf("Expected billable_rate %d, got NULL", *tt.expectedRate)
				} else if timeEntry.BillableRate.Int64 != *tt.expectedRate {
					t.Errorf("Expected billable_rate %d, got %d", *tt.expectedRate, timeEntry.BillableRate.Int64)
				}
			}
		})
	}
}

// TestFindFunctions tests both findClient and findProject functions with consolidated test cases
func TestFindFunctions(t *testing.T) {
	q, cleanup := setupTestDB(t)
	defer cleanup()

	// Create test client
	testClient, err := q.CreateClient(context.Background(), queries.CreateClientParams{
		Name:  "Test Client",
		Email: sql.NullString{String: "test@example.com", Valid: true},
	})
	if err != nil {
		t.Fatalf("Failed to create test client: %v", err)
	}

	// Create test project
	testProject, err := q.CreateProject(context.Background(), queries.CreateProjectParams{
		Name:     "Test Project",
		ClientID: testClient.ID,
	})
	if err != nil {
		t.Fatalf("Failed to create test project: %v", err)
	}

	// Create entities with name "1" to test ambiguous lookup
	_, err = q.CreateClient(context.Background(), queries.CreateClientParams{
		Name:  "1",
		Email: sql.NullString{String: "one@example.com", Valid: true},
	})
	if err != nil {
		t.Fatalf("Failed to create ambiguous test client: %v", err)
	}

	_, err = q.CreateProject(context.Background(), queries.CreateProjectParams{
		Name:     "1",
		ClientID: testClient.ID,
	})
	if err != nil {
		t.Fatalf("Failed to create ambiguous test project: %v", err)
	}

	tests := []struct {
		name        string
		entityType  string // "client" or "project"
		ref         string
		expectError bool
		expectedID  int64
	}{
		// Client tests
		{"find client by name", "client", "Test Client", false, testClient.ID},
		{"find client by ID", "client", "2", false, 2}, // Client with name "1" gets ID 2
		{"find client - ambiguous (name matches ID)", "client", "1", true, 0},
		{"find client - nonexistent name", "client", "Nonexistent Client", true, 0},
		{"find client - nonexistent ID", "client", "999", true, 0},

		// Project tests
		{"find project by name", "project", "Test Project", false, testProject.ID},
		{"find project by ID", "project", "2", false, 2}, // Project with name "1" gets ID 2
		{"find project - ambiguous (name matches ID)", "project", "1", true, 0},
		{"find project - nonexistent name", "project", "Nonexistent Project", true, 0},
		{"find project - nonexistent ID", "project", "999", true, 0},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var err error
			var actualID int64

			if tt.entityType == "client" {
				client, findErr := findClient(context.Background(), q, tt.ref)
				err = findErr
				if findErr == nil {
					actualID = client.ID
				}
			} else {
				project, findErr := findProject(context.Background(), q, tt.ref)
				err = findErr
				if findErr == nil {
					actualID = project.ID
				}
			}

			if tt.expectError {
				if err == nil {
					t.Errorf("Expected error but got none")
				}
				return
			}

			if err != nil {
				t.Errorf("Unexpected error: %v", err)
				return
			}

			if actualID != tt.expectedID {
				t.Errorf("Expected %s ID %d, got %d", tt.entityType, tt.expectedID, actualID)
			}
		})
	}
}

func TestInCommandTimezoneHandling(t *testing.T) {
	// Test that punch in commands store timestamps in UTC regardless of system timezone
	tests := []struct {
		name        string
		setupData   func(*queries.Queries) (clientID int64)
		args        []string
		expectError bool
	}{
		{
			name: "punch in stores UTC timestamp regardless of system timezone",
			setupData: func(q *queries.Queries) int64 {
				client, _ := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name: "TimezoneTestClient",
				})
				return client.ID
			},
			args:        []string{"in", "-c", "TimezoneTestClient", "Working across timezones"},
			expectError: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			q, cleanup := setupTestDB(t)
			defer cleanup()

			clientID := tt.setupData(q)

			// Execute command
			_, err := executeCommandWithDB(t, q, tt.args...)

			if tt.expectError {
				if err == nil {
					t.Errorf("Expected error but got none")
				}
				return
			}

			if err != nil {
				t.Errorf("Unexpected error: %v", err)
				return
			}

			// Verify the time entry was created and has UTC timestamp
			activeEntry, err := q.GetActiveTimeEntry(context.Background())
			if err != nil {
				t.Fatalf("Failed to get active time entry: %v", err)
			}

			// Verify that the stored timestamp is in UTC (Location should be UTC)
			if activeEntry.StartTime.Location() != time.UTC {
				t.Errorf("Expected start time to be in UTC, got location: %v", activeEntry.StartTime.Location())
			}

			// Verify client ID matches
			if activeEntry.ClientID != clientID {
				t.Errorf("Expected client ID %d, got %d", clientID, activeEntry.ClientID)
			}

			// Verify description was stored
			if !activeEntry.Description.Valid || activeEntry.Description.String != "Working across timezones" {
				t.Errorf("Expected description 'Working across timezones', got %v", activeEntry.Description)
			}
		})
	}
}

func TestInCommandDSTTransition(t *testing.T) {
	// Test punch in behavior during DST transitions
	// Note: This test focuses on verifying UTC storage, actual DST testing would require
	// mocking system time which is complex with the current architecture

	tests := []struct {
		name      string
		setupData func(*queries.Queries) (clientID int64)
		args      []string
	}{
		{
			name: "punch in during potential DST transition stores UTC",
			setupData: func(q *queries.Queries) int64 {
				client, _ := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name: "DSTTestClient",
				})
				return client.ID
			},
			args: []string{"in", "-c", "DSTTestClient", "DST transition work"},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			q, cleanup := setupTestDB(t)
			defer cleanup()

			clientID := tt.setupData(q)

			// Execute command
			_, err := executeCommandWithDB(t, q, tt.args...)
			if err != nil {
				t.Fatalf("Unexpected error: %v", err)
			}

			// Verify entry was created in UTC
			activeEntry, err := q.GetActiveTimeEntry(context.Background())
			if err != nil {
				t.Fatalf("Failed to get active time entry: %v", err)
			}

			if activeEntry.StartTime.Location() != time.UTC {
				t.Errorf("Expected UTC timezone, got: %v", activeEntry.StartTime.Location())
			}

			if activeEntry.ClientID != clientID {
				t.Errorf("Expected client ID %d, got %d", clientID, activeEntry.ClientID)
			}
		})
	}
}

func TestInCommandCopyFromRecentWithTimezone(t *testing.T) {
	// Test that copying from most recent entry works correctly across timezone scenarios
	tests := []struct {
		name      string
		setupData func(*queries.Queries) error
		args      []string
	}{
		{
			name: "copy most recent entry maintains timezone independence",
			setupData: func(q *queries.Queries) error {
				// Create client
				client, err := q.CreateClient(context.Background(), queries.CreateClientParams{
					Name: "RecentTestClient",
				})
				if err != nil {
					return err
				}

				// Create and immediately stop a time entry to have a "most recent"
				_, err = q.CreateTimeEntry(context.Background(), queries.CreateTimeEntryParams{
					Description: sql.NullString{String: "Previous work", Valid: true},
					ClientID:    client.ID,
				})
				if err != nil {
					return err
				}

				_, err = q.StopTimeEntry(context.Background())
				return err
			},
			args: []string{"in"}, // No flags - should copy most recent
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			q, cleanup := setupTestDB(t)
			defer cleanup()

			if err := tt.setupData(q); err != nil {
				t.Fatalf("Failed to setup test data: %v", err)
			}

			// Execute command
			output, err := executeCommandWithDB(t, q, tt.args...)
			if err != nil {
				t.Fatalf("Unexpected error: %v", err)
			}

			// Verify it found and copied the recent entry
			if !strings.Contains(output, "Started timer") || !strings.Contains(output, "RecentTestClient") {
				t.Errorf("Expected output to indicate copying recent entry, got: %s", output)
			}

			// Verify the new entry is stored in UTC
			activeEntry, err := q.GetActiveTimeEntry(context.Background())
			if err != nil {
				t.Fatalf("Failed to get active time entry: %v", err)
			}

			if activeEntry.StartTime.Location() != time.UTC {
				t.Errorf("Expected new entry to be in UTC, got: %v", activeEntry.StartTime.Location())
			}
		})
	}
}