-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
256 lines (235 loc) · 6.28 KB
/
errors_test.go
File metadata and controls
256 lines (235 loc) · 6.28 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
package awn
import (
"encoding/json"
"errors"
"fmt"
"testing"
)
func TestAwnError_Error(t *testing.T) {
e := &AwnError{
Code: "SESSION_NOT_FOUND",
Message: "session \"abc\" not found",
}
if e.Error() != `session "abc" not found` {
t.Fatalf("Error() = %q", e.Error())
}
}
func TestAwnError_JSON(t *testing.T) {
e := &AwnError{
Code: "SESSION_NOT_FOUND",
Category: "session",
Message: "session not found",
Retryable: false,
Suggestion: "check session ID with awn list",
}
data, err := json.Marshal(e)
if err != nil {
t.Fatalf("Marshal: %v", err)
}
var got map[string]any
json.Unmarshal(data, &got) //nolint:errcheck
if got["code"] != "SESSION_NOT_FOUND" {
t.Fatalf("code = %v", got["code"])
}
if got["category"] != "session" {
t.Fatalf("category = %v", got["category"])
}
if got["suggestion"] != "check session ID with awn list" {
t.Fatalf("suggestion = %v", got["suggestion"])
}
}
func TestAwnError_JSONWithContext(t *testing.T) {
e := ErrSessionNotFound("abc123")
data, err := json.Marshal(e)
if err != nil {
t.Fatalf("Marshal: %v", err)
}
var got map[string]any
json.Unmarshal(data, &got) //nolint:errcheck
ctx, ok := got["context"].(map[string]any)
if !ok {
t.Fatal("context field missing or not an object")
}
if ctx["session_id"] != "abc123" {
t.Fatalf("context.session_id = %v", ctx["session_id"])
}
}
func TestAwnError_JSONOmitsEmptyContext(t *testing.T) {
e := &AwnError{Code: "TEST", Message: "test"}
data, _ := json.Marshal(e)
var got map[string]any
json.Unmarshal(data, &got) //nolint:errcheck
if _, exists := got["context"]; exists {
t.Fatal("context should be omitted when nil")
}
}
func TestErrSessionNotFound(t *testing.T) {
e := ErrSessionNotFound("abc123")
if e.Code != "SESSION_NOT_FOUND" {
t.Fatalf("Code = %q", e.Code)
}
if e.Category != CategorySession {
t.Fatalf("Category = %q", e.Category)
}
if e.Retryable {
t.Fatal("should not be retryable")
}
if e.Context["session_id"] != "abc123" {
t.Fatalf("Context[session_id] = %q", e.Context["session_id"])
}
}
func TestErrSessionExited(t *testing.T) {
e := ErrSessionExited("xyz")
if e.Code != "SESSION_EXITED" {
t.Fatalf("Code = %q", e.Code)
}
if e.Retryable {
t.Fatal("should not be retryable")
}
}
func TestErrTimeout(t *testing.T) {
e := ErrTimeout("wait_for_text", 5000)
if e.Code != "TIMEOUT" {
t.Fatalf("Code = %q", e.Code)
}
if !e.Retryable {
t.Fatal("should be retryable")
}
}
func TestErrValidation(t *testing.T) {
e := ErrValidation("missing field: text")
if e.Code != "VALIDATION_ERROR" {
t.Fatalf("Code = %q", e.Code)
}
}
func TestErrDaemonNotRunning(t *testing.T) {
e := ErrDaemonNotRunning()
if e.Code != "DAEMON_NOT_RUNNING" {
t.Fatalf("Code = %q", e.Code)
}
if !e.Retryable {
t.Fatal("should be retryable")
}
}
func TestErrDaemonAlreadyRunning(t *testing.T) {
e := ErrDaemonAlreadyRunning()
if e.Code != "DAEMON_ALREADY_RUNNING" {
t.Fatalf("Code = %q", e.Code)
}
}
func TestErrAuthRequired(t *testing.T) {
e := ErrAuthRequired()
if e.Code != "AUTH_REQUIRED" {
t.Fatalf("Code = %q", e.Code)
}
if e.Category != CategoryAuth {
t.Fatalf("Category = %q", e.Category)
}
}
func TestErrAuthFailed(t *testing.T) {
e := ErrAuthFailed()
if e.Code != "AUTH_FAILED" {
t.Fatalf("Code = %q", e.Code)
}
}
func TestErrInvalidKey(t *testing.T) {
e := ErrInvalidKey("FooBar")
if e.Code != "INVALID_KEY" {
t.Fatalf("Code = %q", e.Code)
}
if e.Context["key"] != "FooBar" {
t.Fatalf("Context[key] = %q", e.Context["key"])
}
}
func TestErrPipelineStepFailed(t *testing.T) {
e := ErrPipelineStepFailed(3, "timeout")
if e.Code != "PIPELINE_STEP_FAILED" {
t.Fatalf("Code = %q", e.Code)
}
if e.Context["step_index"] != "3" {
t.Fatalf("Context[step_index] = %q", e.Context["step_index"])
}
}
func TestErrConnectionFailed(t *testing.T) {
e := ErrConnectionFailed("dial timeout")
if e.Code != "CONNECTION_FAILED" {
t.Fatalf("Code = %q", e.Code)
}
if !e.Retryable {
t.Fatal("should be retryable")
}
}
func TestErrInvalidDir(t *testing.T) {
e := ErrInvalidDir("/no/such/path")
if e.Code != "INVALID_INPUT" {
t.Fatalf("Code = %q", e.Code)
}
if e.Context["dir"] != "/no/such/path" {
t.Fatalf("Context[dir] = %q", e.Context["dir"])
}
}
func TestIsRetryable(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{"timeout is retryable", ErrTimeout("test", 1000), true},
{"session not found is not retryable", ErrSessionNotFound("x"), false},
{"daemon not running is retryable", ErrDaemonNotRunning(), true},
{"connection failed is retryable", ErrConnectionFailed("x"), true},
{"validation is not retryable", ErrValidation("x"), false},
{"plain error is not retryable", fmt.Errorf("boom"), false},
{"nil is not retryable", nil, false},
{"wrapped AwnError", fmt.Errorf("wrap: %w", ErrTimeout("x", 1)), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsRetryable(tt.err); got != tt.want {
t.Errorf("IsRetryable = %v, want %v", got, tt.want)
}
})
}
}
func TestErrorCode(t *testing.T) {
if got := ErrorCode(ErrTimeout("x", 1)); got != "TIMEOUT" {
t.Errorf("ErrorCode = %q, want TIMEOUT", got)
}
if got := ErrorCode(fmt.Errorf("plain")); got != "" {
t.Errorf("ErrorCode = %q, want empty", got)
}
}
func TestErrorsAs(t *testing.T) {
err := fmt.Errorf("wrapped: %w", ErrSessionNotFound("test"))
var ae *AwnError
if !errors.As(err, &ae) {
t.Fatal("errors.As should unwrap AwnError")
}
if ae.Code != "SESSION_NOT_FOUND" {
t.Fatalf("Code = %q", ae.Code)
}
}
func TestCategoryConstants(t *testing.T) {
// Verify categories are used consistently in constructors.
cases := []struct {
name string
err *AwnError
category string
}{
{"session", ErrSessionNotFound("x"), CategorySession},
{"timeout", ErrTimeout("x", 1), CategoryTimeout},
{"validation", ErrValidation("x"), CategoryValidation},
{"daemon", ErrDaemonNotRunning(), CategoryDaemon},
{"auth", ErrAuthRequired(), CategoryAuth},
{"transport", ErrConnectionFailed("x"), CategoryTransport},
{"input", ErrInvalidKey("x"), CategoryInput},
{"pipeline", ErrPipelineStepFailed(0, "x"), CategoryPipeline},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if tt.err.Category != tt.category {
t.Errorf("Category = %q, want %q", tt.err.Category, tt.category)
}
})
}
}