|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +const ( |
| 9 | + testShortID = "311510" |
| 10 | + testFullID = "6a12a4502f0a2396b3311510" |
| 11 | +) |
| 12 | + |
| 13 | +func incidentListData(items ...map[string]any) map[string]any { |
| 14 | + return map[string]any{"items": items, "total": len(items)} |
| 15 | +} |
| 16 | + |
| 17 | +func incidentItem(id, num, title string) map[string]any { |
| 18 | + return map[string]any{"incident_id": id, "num": num, "title": title} |
| 19 | +} |
| 20 | + |
| 21 | +// TestIncidentDetailShortIDResolves: `detail <6-hex>` first resolves the short |
| 22 | +// id via /incident/list (nums + a 30-day window), then fetches /incident/info |
| 23 | +// with the RESOLVED full id — never the short id. |
| 24 | +func TestIncidentDetailShortIDResolves(t *testing.T) { |
| 25 | + saveAndResetGlobals(t) |
| 26 | + stub := newGFStub(t) |
| 27 | + |
| 28 | + var paths []string |
| 29 | + stub.dataForPath = func(path string, body map[string]any) any { |
| 30 | + paths = append(paths, path) |
| 31 | + switch path { |
| 32 | + case "/incident/list": |
| 33 | + return incidentListData(incidentItem(testFullID, testShortID, "kafka backlog")) |
| 34 | + case "/incident/info": |
| 35 | + return incidentItem(testFullID, testShortID, "kafka backlog") |
| 36 | + default: |
| 37 | + return nil |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if _, err := execCommand("incident", "detail", testShortID); err != nil { |
| 42 | + t.Fatalf("execCommand: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + if want := []string{"/incident/list", "/incident/info"}; !equalStrings(paths, want) { |
| 46 | + t.Fatalf("paths = %v, want %v", paths, want) |
| 47 | + } |
| 48 | + |
| 49 | + // Resolve call: nums set, 30-day span. |
| 50 | + listBody := stub.bodies[0] |
| 51 | + nums, _ := listBody["nums"].([]any) |
| 52 | + if len(nums) != 1 || nums[0] != testShortID { |
| 53 | + t.Errorf("resolve nums = %#v, want [%q]", listBody["nums"], testShortID) |
| 54 | + } |
| 55 | + st, _ := listBody["start_time"].(float64) |
| 56 | + et, _ := listBody["end_time"].(float64) |
| 57 | + if et <= st { |
| 58 | + t.Errorf("end_time %v must be > start_time %v", et, st) |
| 59 | + } |
| 60 | + if span := et - st; span != float64(shortIDResolveDays*24*60*60) { |
| 61 | + t.Errorf("resolve span = %v s, want %d-day span", span, shortIDResolveDays) |
| 62 | + } |
| 63 | + |
| 64 | + // Detail call: the resolved full id, not the short id. |
| 65 | + if got := stub.bodies[1]["incident_id"]; got != testFullID { |
| 66 | + t.Errorf("info incident_id = %#v, want resolved full id %q", got, testFullID) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// TestIncidentDetailShortIDAmbiguous: a short id that matches >1 incident fails |
| 71 | +// with a candidate list and never calls /incident/info (no silent wrong-pick). |
| 72 | +func TestIncidentDetailShortIDAmbiguous(t *testing.T) { |
| 73 | + saveAndResetGlobals(t) |
| 74 | + stub := newGFStub(t) |
| 75 | + |
| 76 | + other := "6a0ea49000000000b3311510" |
| 77 | + var paths []string |
| 78 | + stub.dataForPath = func(path string, body map[string]any) any { |
| 79 | + paths = append(paths, path) |
| 80 | + if path == "/incident/list" { |
| 81 | + return incidentListData( |
| 82 | + incidentItem(testFullID, testShortID, "kafka backlog"), |
| 83 | + incidentItem(other, testShortID, "another incident"), |
| 84 | + ) |
| 85 | + } |
| 86 | + return nil |
| 87 | + } |
| 88 | + |
| 89 | + _, err := execCommand("incident", "detail", testShortID) |
| 90 | + if err == nil { |
| 91 | + t.Fatal("want ambiguous error, got nil") |
| 92 | + } |
| 93 | + if !strings.Contains(err.Error(), testFullID) || !strings.Contains(err.Error(), other) { |
| 94 | + t.Errorf("error should list both candidate ids, got: %v", err) |
| 95 | + } |
| 96 | + if want := []string{"/incident/list"}; !equalStrings(paths, want) { |
| 97 | + t.Errorf("paths = %v, want %v (info must be skipped on ambiguity)", paths, want) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// TestIncidentDetailShortIDNotFound: a short id with no match in the window |
| 102 | +// fails with a descriptive error pointing at the full-id fallback. |
| 103 | +func TestIncidentDetailShortIDNotFound(t *testing.T) { |
| 104 | + saveAndResetGlobals(t) |
| 105 | + stub := newGFStub(t) |
| 106 | + stub.dataForPath = func(path string, body map[string]any) any { |
| 107 | + if path == "/incident/list" { |
| 108 | + return incidentListData() |
| 109 | + } |
| 110 | + return nil |
| 111 | + } |
| 112 | + |
| 113 | + _, err := execCommand("incident", "detail", testShortID) |
| 114 | + if err == nil || !strings.Contains(err.Error(), "no incident with short id") { |
| 115 | + t.Errorf("want not-found error, got %v", err) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// TestIncidentDetailFullIDSkipsResolve: a 24-hex id goes straight to |
| 120 | +// /incident/info with no resolve round-trip (existing behavior preserved). |
| 121 | +func TestIncidentDetailFullIDSkipsResolve(t *testing.T) { |
| 122 | + saveAndResetGlobals(t) |
| 123 | + stub := newGFStub(t) |
| 124 | + var paths []string |
| 125 | + stub.dataForPath = func(path string, body map[string]any) any { |
| 126 | + paths = append(paths, path) |
| 127 | + if path == "/incident/info" { |
| 128 | + return incidentItem(testFullID, testShortID, "kafka backlog") |
| 129 | + } |
| 130 | + return nil |
| 131 | + } |
| 132 | + |
| 133 | + if _, err := execCommand("incident", "detail", testFullID); err != nil { |
| 134 | + t.Fatalf("execCommand: %v", err) |
| 135 | + } |
| 136 | + if want := []string{"/incident/info"}; !equalStrings(paths, want) { |
| 137 | + t.Fatalf("paths = %v, want %v (full id must not trigger a resolve)", paths, want) |
| 138 | + } |
| 139 | + if got := stub.bodies[0]["incident_id"]; got != testFullID { |
| 140 | + t.Errorf("info incident_id = %#v, want %q", got, testFullID) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// TestIncidentGetShortIDResolves: `get <6-hex>` resolves the short id, then |
| 145 | +// fetches by the resolved full id via incident_ids. |
| 146 | +func TestIncidentGetShortIDResolves(t *testing.T) { |
| 147 | + saveAndResetGlobals(t) |
| 148 | + stub := newGFStub(t) |
| 149 | + stub.dataForPath = func(path string, body map[string]any) any { |
| 150 | + // Both the resolve and the final fetch hit /incident/list; the canned |
| 151 | + // row is fine for either. |
| 152 | + return incidentListData(incidentItem(testFullID, testShortID, "kafka backlog")) |
| 153 | + } |
| 154 | + |
| 155 | + if _, err := execCommand("incident", "get", testShortID); err != nil { |
| 156 | + t.Fatalf("execCommand: %v", err) |
| 157 | + } |
| 158 | + if stub.requests != 2 { |
| 159 | + t.Fatalf("requests = %d, want 2 (resolve + fetch)", stub.requests) |
| 160 | + } |
| 161 | + |
| 162 | + // Resolve sent nums; final fetch sent the resolved full id via incident_ids. |
| 163 | + if _, ok := stub.bodies[0]["nums"]; !ok { |
| 164 | + t.Errorf("first request should carry nums, got %#v", stub.bodies[0]) |
| 165 | + } |
| 166 | + ids, _ := stub.bodies[1]["incident_ids"].([]any) |
| 167 | + if len(ids) != 1 || ids[0] != testFullID { |
| 168 | + t.Errorf("fetch incident_ids = %#v, want [%q]", stub.bodies[1]["incident_ids"], testFullID) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +// TestIncidentListNumsReachesWire: --nums is split and sent as the nums array. |
| 173 | +func TestIncidentListNumsReachesWire(t *testing.T) { |
| 174 | + saveAndResetGlobals(t) |
| 175 | + stub := newGFStub(t) |
| 176 | + stub.data = incidentListData() |
| 177 | + |
| 178 | + if _, err := execCommand("incident", "list", "--nums", "311510,ABC123"); err != nil { |
| 179 | + t.Fatalf("execCommand: %v", err) |
| 180 | + } |
| 181 | + if stub.lastPath != "/incident/list" { |
| 182 | + t.Fatalf("path = %q, want /incident/list", stub.lastPath) |
| 183 | + } |
| 184 | + nums, _ := stub.lastBody["nums"].([]any) |
| 185 | + if len(nums) != 2 || nums[0] != "311510" || nums[1] != "ABC123" { |
| 186 | + t.Errorf("nums = %#v, want [311510 ABC123]", stub.lastBody["nums"]) |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +func equalStrings(a, b []string) bool { |
| 191 | + if len(a) != len(b) { |
| 192 | + return false |
| 193 | + } |
| 194 | + for i := range a { |
| 195 | + if a[i] != b[i] { |
| 196 | + return false |
| 197 | + } |
| 198 | + } |
| 199 | + return true |
| 200 | +} |
0 commit comments