1package cli
2
3import (
4 "errors"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9
10 "github.com/bnema/gordon/internal/adapters/in/cli/remote"
11 "github.com/bnema/gordon/internal/domain"
12)
13
14func TestFormatDeployFailure_RemoteStructuredError(t *testing.T) {
15 root := &remote.HTTPError{
16 StatusCode: 500,
17 Status: "500 Internal Server Error",
18 Body: "failed to deploy",
19 Cause: "health check failed",
20 Hint: "check DATABASE_URL",
21 Logs: []string{
22 "booting app",
23 "connection refused",
24 },
25 }
26 err := formatDeployFailure(root)
27
28 require.Error(t, err)
29 assert.Equal(t, "failed to deploy\nCause: health check failed\nHint: check DATABASE_URL\n\nRecent container logs:\n booting app\n connection refused", err.Error())
30 assert.ErrorIs(t, err, root)
31}
32
33func TestFormatDeployFailure_LocalTypedError(t *testing.T) {
34 root := &domain.DeployFailureError{
35 Summary: "failed to deploy",
36 Cause: "health check failed",
37 Hint: "check DATABASE_URL",
38 Logs: []string{
39 "booting app",
40 "connection refused",
41 },
42 }
43 err := formatDeployFailure(root)
44
45 require.Error(t, err)
46 assert.Equal(t, "failed to deploy\nCause: health check failed\nHint: check DATABASE_URL\n\nRecent container logs:\n booting app\n connection refused", err.Error())
47 assert.ErrorIs(t, err, root)
48}
49
50func TestFormatDeployFailure_GenericFallback(t *testing.T) {
51 root := errors.New("boom")
52 err := formatDeployFailure(root)
53
54 require.Error(t, err)
55 assert.Equal(t, "failed to deploy: boom", err.Error())
56 assert.ErrorIs(t, err, root)
57}
58
59func TestFormatDeployFailure_SkipsEmptySanitizedLogsSection(t *testing.T) {
60 root := &domain.DeployFailureError{
61 Summary: "failed to deploy",
62 Cause: "health check failed",
63 Logs: []string{"\x1b[31m\x1b[0m", "\n\r\t"},
64 }
65 err := formatDeployFailure(root)
66
67 require.Error(t, err)
68 assert.Equal(t, "failed to deploy\nCause: health check failed", err.Error())
69 assert.ErrorIs(t, err, root)
70}