|
| 1 | +package errors |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestCLIError_Error(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + err *CLIError |
| 12 | + want string |
| 13 | + }{ |
| 14 | + { |
| 15 | + name: "message only", |
| 16 | + err: &CLIError{Message: "something failed"}, |
| 17 | + want: "something failed", |
| 18 | + }, |
| 19 | + { |
| 20 | + name: "message with hint", |
| 21 | + err: &CLIError{Message: "auth failed", Hint: "run heygen auth login"}, |
| 22 | + want: "auth failed (hint: run heygen auth login)", |
| 23 | + }, |
| 24 | + } |
| 25 | + for _, tt := range tests { |
| 26 | + t.Run(tt.name, func(t *testing.T) { |
| 27 | + if got := tt.err.Error(); got != tt.want { |
| 28 | + t.Errorf("Error() = %q, want %q", got, tt.want) |
| 29 | + } |
| 30 | + }) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestCLIError_ToErrorEnvelope(t *testing.T) { |
| 35 | + err := &CLIError{ |
| 36 | + Code: "not_found", |
| 37 | + Message: "Video abc123 not found", |
| 38 | + Hint: "Check the video ID with: heygen video list", |
| 39 | + RequestID: "req_xyz", |
| 40 | + } |
| 41 | + |
| 42 | + envelope := err.ToErrorEnvelope() |
| 43 | + |
| 44 | + // Marshal and re-parse to verify JSON shape |
| 45 | + data, marshalErr := json.Marshal(envelope) |
| 46 | + if marshalErr != nil { |
| 47 | + t.Fatalf("failed to marshal envelope: %v", marshalErr) |
| 48 | + } |
| 49 | + |
| 50 | + var parsed map[string]map[string]string |
| 51 | + if jsonErr := json.Unmarshal(data, &parsed); jsonErr != nil { |
| 52 | + t.Fatalf("failed to unmarshal envelope: %v", jsonErr) |
| 53 | + } |
| 54 | + |
| 55 | + inner := parsed["error"] |
| 56 | + if inner["code"] != "not_found" { |
| 57 | + t.Errorf("code = %q, want %q", inner["code"], "not_found") |
| 58 | + } |
| 59 | + if inner["message"] != "Video abc123 not found" { |
| 60 | + t.Errorf("message = %q, want %q", inner["message"], "Video abc123 not found") |
| 61 | + } |
| 62 | + if inner["hint"] != "Check the video ID with: heygen video list" { |
| 63 | + t.Errorf("hint = %q, want expected value", inner["hint"]) |
| 64 | + } |
| 65 | + if inner["request_id"] != "req_xyz" { |
| 66 | + t.Errorf("request_id = %q, want %q", inner["request_id"], "req_xyz") |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestCLIError_ToErrorEnvelope_OmitsEmpty(t *testing.T) { |
| 71 | + err := &CLIError{Code: "error", Message: "fail"} |
| 72 | + envelope := err.ToErrorEnvelope() |
| 73 | + inner := envelope["error"].(map[string]any) |
| 74 | + |
| 75 | + if _, ok := inner["hint"]; ok { |
| 76 | + t.Error("hint should be omitted when empty") |
| 77 | + } |
| 78 | + if _, ok := inner["request_id"]; ok { |
| 79 | + t.Error("request_id should be omitted when empty") |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestFromAPIError_ExitCodes(t *testing.T) { |
| 84 | + tests := []struct { |
| 85 | + name string |
| 86 | + statusCode int |
| 87 | + wantExit int |
| 88 | + wantCode string |
| 89 | + }{ |
| 90 | + {"401 → auth", 401, ExitAuth, "auth_error"}, |
| 91 | + {"403 → auth", 403, ExitAuth, "auth_error"}, |
| 92 | + {"400 → general", 400, ExitGeneral, "validation_error"}, |
| 93 | + {"404 → general", 404, ExitGeneral, "not_found"}, |
| 94 | + {"429 → general", 429, ExitGeneral, "rate_limited"}, |
| 95 | + {"500 → general", 500, ExitGeneral, "server_error"}, |
| 96 | + {"502 → general", 502, ExitGeneral, "server_error"}, |
| 97 | + {"503 → general", 503, ExitGeneral, "server_error"}, |
| 98 | + } |
| 99 | + for _, tt := range tests { |
| 100 | + t.Run(tt.name, func(t *testing.T) { |
| 101 | + apiErr := &APIError{Message: "test error"} |
| 102 | + cliErr := FromAPIError(tt.statusCode, apiErr, "req_123") |
| 103 | + |
| 104 | + if cliErr.ExitCode != tt.wantExit { |
| 105 | + t.Errorf("ExitCode = %d, want %d", cliErr.ExitCode, tt.wantExit) |
| 106 | + } |
| 107 | + if cliErr.Code != tt.wantCode { |
| 108 | + t.Errorf("Code = %q, want %q", cliErr.Code, tt.wantCode) |
| 109 | + } |
| 110 | + if cliErr.RequestID != "req_123" { |
| 111 | + t.Errorf("RequestID = %q, want %q", cliErr.RequestID, "req_123") |
| 112 | + } |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestFromAPIError_PreservesAPICode(t *testing.T) { |
| 118 | + apiErr := &APIError{Code: "custom_code", Message: "custom error"} |
| 119 | + cliErr := FromAPIError(400, apiErr, "") |
| 120 | + |
| 121 | + if cliErr.Code != "custom_code" { |
| 122 | + t.Errorf("Code = %q, want %q (should preserve API code)", cliErr.Code, "custom_code") |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func TestConstructors(t *testing.T) { |
| 127 | + t.Run("New", func(t *testing.T) { |
| 128 | + err := New("something broke") |
| 129 | + if err.ExitCode != ExitGeneral { |
| 130 | + t.Errorf("ExitCode = %d, want %d", err.ExitCode, ExitGeneral) |
| 131 | + } |
| 132 | + }) |
| 133 | + |
| 134 | + t.Run("NewAuth", func(t *testing.T) { |
| 135 | + err := NewAuth("no key", "set HEYGEN_API_KEY") |
| 136 | + if err.ExitCode != ExitAuth { |
| 137 | + t.Errorf("ExitCode = %d, want %d", err.ExitCode, ExitAuth) |
| 138 | + } |
| 139 | + if err.Hint != "set HEYGEN_API_KEY" { |
| 140 | + t.Errorf("Hint = %q, want expected value", err.Hint) |
| 141 | + } |
| 142 | + }) |
| 143 | + |
| 144 | + t.Run("NewUsage", func(t *testing.T) { |
| 145 | + err := NewUsage("bad flag") |
| 146 | + if err.ExitCode != ExitUsage { |
| 147 | + t.Errorf("ExitCode = %d, want %d", err.ExitCode, ExitUsage) |
| 148 | + } |
| 149 | + }) |
| 150 | +} |
0 commit comments