|
| 1 | +package instant_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/InstaNode-dev/sdk-go/instant" |
| 13 | +) |
| 14 | + |
| 15 | +// TestDeploymentEvents_HappyPath verifies GET /api/v1/deployments/:id/events |
| 16 | +// parsing: the autopsy timeline (kind/reason/exit_code/last_lines/hint), the |
| 17 | +// nullable exit_code (present on one row, null on another), and the |
| 18 | +// deployment_id + count envelope. It also asserts the limit query is sent. |
| 19 | +func TestDeploymentEvents_HappyPath(t *testing.T) { |
| 20 | + var gotLimit string |
| 21 | + |
| 22 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 23 | + if r.URL.Path != "/api/v1/deployments/6fffcc21/events" { |
| 24 | + t.Errorf("path = %q; want /api/v1/deployments/6fffcc21/events", r.URL.Path) |
| 25 | + http.Error(w, "wrong path", http.StatusNotFound) |
| 26 | + return |
| 27 | + } |
| 28 | + if r.Method != http.MethodGet { |
| 29 | + t.Errorf("method = %q; want GET", r.Method) |
| 30 | + } |
| 31 | + gotLimit = r.URL.Query().Get("limit") |
| 32 | + |
| 33 | + w.Header().Set("Content-Type", "application/json") |
| 34 | + _ = json.NewEncoder(w).Encode(map[string]any{ |
| 35 | + "ok": true, |
| 36 | + "deployment_id": "11111111-2222-3333-4444-555555555555", |
| 37 | + "count": 2, |
| 38 | + "events": []map[string]any{ |
| 39 | + { |
| 40 | + "kind": "build", |
| 41 | + "reason": "BackoffLimitExceeded", |
| 42 | + "event": "Job has reached the specified backoff limit", |
| 43 | + "exit_code": 1, |
| 44 | + "last_lines": "npm ERR! missing script: build", |
| 45 | + "hint": "Add a build script to package.json", |
| 46 | + "created_at": "2026-06-10T12:00:00Z", |
| 47 | + }, |
| 48 | + { |
| 49 | + "kind": "rollout", |
| 50 | + "reason": "ProgressDeadlineExceeded", |
| 51 | + "event": "Deployment exceeded its progress deadline", |
| 52 | + "exit_code": nil, |
| 53 | + "last_lines": "", |
| 54 | + "hint": "Check the container's readiness probe", |
| 55 | + "created_at": "2026-06-10T12:05:00Z", |
| 56 | + }, |
| 57 | + }, |
| 58 | + }) |
| 59 | + })) |
| 60 | + defer srv.Close() |
| 61 | + |
| 62 | + client := instant.New(instant.WithBaseURL(srv.URL)) |
| 63 | + list, err := client.DeploymentEvents(context.Background(), "6fffcc21", 25) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("DeploymentEvents: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + if gotLimit != "25" { |
| 69 | + t.Errorf("limit query = %q; want 25", gotLimit) |
| 70 | + } |
| 71 | + if list.DeploymentID != "11111111-2222-3333-4444-555555555555" { |
| 72 | + t.Errorf("DeploymentID = %q", list.DeploymentID) |
| 73 | + } |
| 74 | + if list.Count != 2 { |
| 75 | + t.Errorf("Count = %d; want 2", list.Count) |
| 76 | + } |
| 77 | + if len(list.Events) != 2 { |
| 78 | + t.Fatalf("len(Events) = %d; want 2", len(list.Events)) |
| 79 | + } |
| 80 | + |
| 81 | + first := list.Events[0] |
| 82 | + if first.Kind != "build" || first.Reason != "BackoffLimitExceeded" { |
| 83 | + t.Errorf("Events[0] kind/reason = %q/%q", first.Kind, first.Reason) |
| 84 | + } |
| 85 | + if first.ExitCode == nil || *first.ExitCode != 1 { |
| 86 | + t.Errorf("Events[0].ExitCode = %v; want 1", first.ExitCode) |
| 87 | + } |
| 88 | + if first.LastLines != "npm ERR! missing script: build" { |
| 89 | + t.Errorf("Events[0].LastLines = %q", first.LastLines) |
| 90 | + } |
| 91 | + if first.Hint != "Add a build script to package.json" { |
| 92 | + t.Errorf("Events[0].Hint = %q", first.Hint) |
| 93 | + } |
| 94 | + |
| 95 | + second := list.Events[1] |
| 96 | + if second.ExitCode != nil { |
| 97 | + t.Errorf("Events[1].ExitCode = %v; want nil (null exit_code)", *second.ExitCode) |
| 98 | + } |
| 99 | + if second.Reason != "ProgressDeadlineExceeded" { |
| 100 | + t.Errorf("Events[1].Reason = %q", second.Reason) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// TestDeploymentEvents_DefaultLimit verifies that passing limit <= 0 omits the |
| 105 | +// limit query so the server applies its own default (50). |
| 106 | +func TestDeploymentEvents_DefaultLimit(t *testing.T) { |
| 107 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 108 | + if r.URL.RawQuery != "" { |
| 109 | + t.Errorf("query = %q; want empty (no limit) when limit <= 0", r.URL.RawQuery) |
| 110 | + } |
| 111 | + w.Header().Set("Content-Type", "application/json") |
| 112 | + _ = json.NewEncoder(w).Encode(map[string]any{ |
| 113 | + "ok": true, "deployment_id": "d", "count": 0, "events": []any{}, |
| 114 | + }) |
| 115 | + })) |
| 116 | + defer srv.Close() |
| 117 | + |
| 118 | + client := instant.New(instant.WithBaseURL(srv.URL)) |
| 119 | + list, err := client.DeploymentEvents(context.Background(), "6fffcc21", 0) |
| 120 | + if err != nil { |
| 121 | + t.Fatalf("DeploymentEvents: %v", err) |
| 122 | + } |
| 123 | + if list.Count != 0 || len(list.Events) != 0 { |
| 124 | + t.Errorf("expected empty timeline, got count=%d len=%d", list.Count, len(list.Events)) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// TestDeploymentEvents_NotFound pins the 404 path → *APIError (IsNotFound), and |
| 129 | +// the empty-id preflight guard. |
| 130 | +func TestDeploymentEvents_NotFound(t *testing.T) { |
| 131 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 132 | + w.Header().Set("Content-Type", "application/json") |
| 133 | + w.WriteHeader(http.StatusNotFound) |
| 134 | + _ = json.NewEncoder(w).Encode(map[string]any{"ok": false, "error": "not_found", "message": "Deployment not found"}) |
| 135 | + })) |
| 136 | + defer srv.Close() |
| 137 | + |
| 138 | + client := instant.New(instant.WithBaseURL(srv.URL)) |
| 139 | + _, err := client.DeploymentEvents(context.Background(), "missing0", 0) |
| 140 | + if !instant.IsNotFound(err) { |
| 141 | + t.Fatalf("expected IsNotFound; got %v", err) |
| 142 | + } |
| 143 | + var apiErr *instant.APIError |
| 144 | + if !errors.As(err, &apiErr) { |
| 145 | + t.Fatalf("expected *APIError; got %T", err) |
| 146 | + } |
| 147 | + |
| 148 | + // Empty-id preflight. |
| 149 | + if _, err := client.DeploymentEvents(context.Background(), "", 0); err == nil || !strings.Contains(err.Error(), "id is required") { |
| 150 | + t.Errorf("empty id: got err = %v", err) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +// TestDeploymentEvents_SurfacesUnauthorized confirms a 401 (no/invalid API key) |
| 155 | +// surfaces as *APIError with the right helper predicate — the events endpoint |
| 156 | +// requires auth. |
| 157 | +func TestDeploymentEvents_SurfacesUnauthorized(t *testing.T) { |
| 158 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 159 | + w.Header().Set("Content-Type", "application/json") |
| 160 | + w.WriteHeader(http.StatusUnauthorized) |
| 161 | + _ = json.NewEncoder(w).Encode(map[string]any{ |
| 162 | + "ok": false, |
| 163 | + "error": "unauthorized", |
| 164 | + "error_code": "missing_credentials", |
| 165 | + "message": "A valid API key is required", |
| 166 | + "agent_action": "Have the user log in and pass the API key.", |
| 167 | + }) |
| 168 | + })) |
| 169 | + defer srv.Close() |
| 170 | + |
| 171 | + client := instant.New(instant.WithBaseURL(srv.URL)) |
| 172 | + _, err := client.DeploymentEvents(context.Background(), "6fffcc21", 0) |
| 173 | + if !instant.IsUnauthorized(err) { |
| 174 | + t.Fatalf("expected IsUnauthorized; got %v", err) |
| 175 | + } |
| 176 | + var apiErr *instant.APIError |
| 177 | + if !errors.As(err, &apiErr) { |
| 178 | + t.Fatalf("expected *APIError; got %T", err) |
| 179 | + } |
| 180 | + if apiErr.CanonicalCode() != "missing_credentials" { |
| 181 | + t.Errorf("CanonicalCode = %q; want missing_credentials", apiErr.CanonicalCode()) |
| 182 | + } |
| 183 | +} |
0 commit comments