summaryrefslogtreecommitdiff
path: root/internal/commands/add_client_test.go
blob: 037300299af1994ceff5ebd3fdeb8c4a91ba30ec (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
package commands

import (
	"context"
	"testing"

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

func TestAddClientCommand(t *testing.T) {
	tests := []struct {
		name           string
		args           []string
		expectedOutput string
		expectError    bool
	}{
		{
			name:           "add client with name only",
			args:           []string{"add", "client", "TestCorp"},
			expectedOutput: "Created client: TestCorp (ID: 1)\n",
			expectError:    false,
		},
		{
			name:           "add client with name and email",
			args:           []string{"add", "client", "TechSolutions", "contact@techsolutions.com"},
			expectedOutput: "Created client: TechSolutions <contact@techsolutions.com> (ID: 1)\n",
			expectError:    false,
		},
		{
			name:           "add client with embedded email in name",
			args:           []string{"add", "client", "StartupXYZ <hello@startupxyz.io>"},
			expectedOutput: "Created client: StartupXYZ <hello@startupxyz.io> (ID: 1)\n",
			expectError:    false,
		},
		{
			name:           "add client with both embedded and separate email (prefer separate)",
			args:           []string{"add", "client", "GlobalInc <old@global.com>", "new@global.com"},
			expectedOutput: "Created client: GlobalInc <new@global.com> (ID: 1)\n",
			expectError:    false,
		},
		{
			name:           "add client with email format in email arg",
			args:           []string{"add", "client", "BigCorp", "Contact Person <contact@bigcorp.com>"},
			expectedOutput: "Created client: BigCorp <contact@bigcorp.com> (ID: 1)\n",
			expectError:    false,
		},
		{
			name:        "add client with no arguments",
			args:        []string{"add", "client"},
			expectError: true,
		},
		{
			name:        "add client with too many arguments",
			args:        []string{"add", "client", "name", "email", "extra"},
			expectError: true,
		},
		{
			name:           "add client with billable rate",
			args:           []string{"add", "client", "BillableClient", "--hourly-rate", "150.50"},
			expectedOutput: "Created client: BillableClient (ID: 1)\n",
			expectError:    false,
		},
		{
			name:           "add client with email and billable rate",
			args:           []string{"add", "client", "PremiumClient", "premium@example.com", "--hourly-rate", "200.75"},
			expectedOutput: "Created client: PremiumClient <premium@example.com> (ID: 1)\n",
			expectError:    false,
		},
	}

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

			// 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")
				}
				return
			}

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

			if tt.expectedOutput != "" && output != tt.expectedOutput {
				t.Errorf("expected output %q, got %q", tt.expectedOutput, output)
			}
		})
	}
}

func ptrval(i int64) *int64 {
	return &i
}

func TestAddClientBillableRateStorage(t *testing.T) {
	tests := []struct {
		name         string
		args         []string
		expectedRate *int64 // nil means NULL in database, values in cents
		expectError  bool
	}{
		{
			name:         "client without billable rate stores NULL",
			args:         []string{"add", "client", "NoRateClient"},
			expectedRate: nil,
			expectError:  false,
		},
		{
			name:         "client with zero billable rate stores NULL",
			args:         []string{"add", "client", "ZeroRateClient", "--hourly-rate", "0"},
			expectedRate: nil,
			expectError:  false,
		},
		{
			name:         "client with billable rate stores value",
			args:         []string{"add", "client", "RateClient", "--hourly-rate", "125.75"},
			expectedRate: ptrval(12575), // $125.75 = 12575 cents
			expectError:  false,
		},
	}

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

			_, 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
			}

			clientName := tt.args[2] // "add", "client", "<name>"
			clients, err := q.FindClient(context.Background(), queries.FindClientParams{
				ID:   0,
				Name: clientName,
			})
			if err != nil {
				t.Fatalf("Failed to query created client: %v", err)
			}
			if len(clients) != 1 {
				t.Fatalf("Expected 1 client, got %d", len(clients))
			}

			client := clients[0]
			if tt.expectedRate == nil {
				if client.BillableRate.Valid {
					t.Errorf("Expected NULL billable_rate, got %d", client.BillableRate.Int64)
				}
			} else {
				if !client.BillableRate.Valid {
					t.Errorf("Expected billable_rate %d, got NULL", *tt.expectedRate)
				} else if client.BillableRate.Int64 != *tt.expectedRate {
					t.Errorf("Expected billable_rate %d, got %d", *tt.expectedRate, client.BillableRate.Int64)
				}
			}
		})
	}
}