From 0e065b3b0f5e935fc769be2f1e84779fa9897e99 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 28 Jun 2025 00:00:15 +0300 Subject: fix: resolve test failures and improve code quality MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fix file handle leak in SetDebugLog by tracking and closing previous files - Add stderr capture to all taskwarrior commands for better error messages - Fix timezone issues in date handling tests by normalizing to UTC - Change Update method to pointer receiver for consistency - Update all test type assertions to handle pointer receivers correctly - Remove unused imports and variables All tests now pass successfully. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- internal/task/operations_test.go | 133 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 internal/task/operations_test.go (limited to 'internal/task/operations_test.go') diff --git a/internal/task/operations_test.go b/internal/task/operations_test.go new file mode 100644 index 0000000..7abd4bd --- /dev/null +++ b/internal/task/operations_test.go @@ -0,0 +1,133 @@ +package task + +import ( + "strings" + "testing" +) + +func TestModifyTask(t *testing.T) { + tests := []struct { + name string + id int + args []string + wantErr bool + errMsg string + }{ + { + name: "valid ID", + id: 1, + args: []string{"status:pending"}, + wantErr: false, + }, + { + name: "zero ID", + id: 0, + args: []string{"status:pending"}, + wantErr: true, + errMsg: "invalid task ID: 0", + }, + { + name: "negative ID", + id: -1, + args: []string{"status:pending"}, + wantErr: true, + errMsg: "invalid task ID: -1", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := modifyTask(tt.id, tt.args...) + + // We can't test actual taskwarrior commands without it installed + // So we just test the validation + if tt.wantErr { + if err == nil { + t.Errorf("modifyTask() error = nil, wantErr %v", tt.wantErr) + } else if !strings.Contains(err.Error(), tt.errMsg) { + t.Errorf("modifyTask() error = %v, want error containing %v", err, tt.errMsg) + } + } + }) + } +} + +func TestSimpleTaskCommand(t *testing.T) { + tests := []struct { + name string + id int + command string + wantErr bool + errMsg string + }{ + { + name: "valid ID", + id: 1, + command: "done", + wantErr: false, + }, + { + name: "zero ID", + id: 0, + command: "done", + wantErr: true, + errMsg: "invalid task ID: 0", + }, + { + name: "negative ID", + id: -5, + command: "done", + wantErr: true, + errMsg: "invalid task ID: -5", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := simpleTaskCommand(tt.id, tt.command) + + // We can't test actual taskwarrior commands without it installed + // So we just test the validation + if tt.wantErr { + if err == nil { + t.Errorf("simpleTaskCommand() error = nil, wantErr %v", tt.wantErr) + } else if !strings.Contains(err.Error(), tt.errMsg) { + t.Errorf("simpleTaskCommand() error = %v, want error containing %v", err, tt.errMsg) + } + } + }) + } +} + +func TestTaskOperationsValidation(t *testing.T) { + // Test that all task operations validate IDs + invalidID := -1 + + operations := []struct { + name string + fn func() error + }{ + {"SetStatus", func() error { return SetStatus(invalidID, "pending") }}, + {"Start", func() error { return Start(invalidID) }}, + {"Stop", func() error { return Stop(invalidID) }}, + {"Done", func() error { return Done(invalidID) }}, + {"Delete", func() error { return Delete(invalidID) }}, + {"SetPriority", func() error { return SetPriority(invalidID, "H") }}, + {"SetRecurrence", func() error { return SetRecurrence(invalidID, "daily") }}, + {"SetDueDate", func() error { return SetDueDate(invalidID, "tomorrow") }}, + {"SetDescription", func() error { return SetDescription(invalidID, "test") }}, + {"Annotate", func() error { return Annotate(invalidID, "note") }}, + {"Denotate", func() error { return Denotate(invalidID, "note") }}, + } + + for _, op := range operations { + t.Run(op.name, func(t *testing.T) { + err := op.fn() + if err == nil { + t.Errorf("%s() with invalid ID = nil, want error", op.name) + } else if !strings.Contains(err.Error(), "invalid task ID") { + t.Errorf("%s() error = %v, want error containing 'invalid task ID'", op.name, err) + } + }) + } +} \ No newline at end of file -- cgit v1.2.3