| 1 | package apperr |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestNormalize(t *testing.T) { |
| 6 | cases := map[string]string{ |
| 7 | "user 4123 not found": "user <n> not found", |
| 8 | "user 9911 not found": "user <n> not found", |
| 9 | "HTTP 503 from upstream": "HTTP 503 from upstream", |
| 10 | "retry 3 failed": "retry 3 failed", |
| 11 | "v2 handler panicked": "v2 handler panicked", |
| 12 | "E404 for id 550e8400-e29b-41d4-a716-446655440000": "E404 for id <uuid>", |
| 13 | `missing key "stripe_secret"`: `missing key "?"`, |
| 14 | "trace 0123456789abcdef bad": "trace <hex> bad", |
| 15 | "deadbeef cafe": "<hex> cafe", |
| 16 | } |
| 17 | for in, want := range cases { |
| 18 | if got := Normalize(in); got != want { |
| 19 | t.Errorf("Normalize(%q) = %q, want %q", in, got, want) |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestTopFrame(t *testing.T) { |
| 25 | cases := map[string]string{ |
| 26 | "TypeError: x\n at render (https://app.example/assets/main.3f2a9c1b.js:12:34)\n at y": "TypeError: x", |
| 27 | "\n app/models/user.rb:42:in `find'\n app/x.rb:1": "app/models/user.rb:in `find'", |
| 28 | "": "", |
| 29 | "single line only": "single line only", |
| 30 | } |
| 31 | for in, want := range cases { |
| 32 | if got := TopFrame(in); got != want { |
| 33 | t.Errorf("TopFrame(%q) = %q, want %q", in, got, want) |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestFingerprint(t *testing.T) { |
| 39 | a := Fingerprint(SourceLog, KindLog, "user 4123 not found", "") |
| 40 | b := Fingerprint(SourceLog, KindLog, "user 9911 not found", "") |
| 41 | c := Fingerprint(SourceLog, KindLog, "user gone", "") |
| 42 | d := Fingerprint(SourceEdge, KindLog, "user 4123 not found", "") |
| 43 | if a != b { |
| 44 | t.Fatalf("digit runs must group: %s != %s", a, b) |
| 45 | } |
| 46 | if a == c || a == d { |
| 47 | t.Fatalf("different names or sources must not group: %s %s %s", a, c, d) |
| 48 | } |
| 49 | if len(a) != 16 { |
| 50 | t.Fatalf("fingerprint length %d, want 16 hex", len(a)) |
| 51 | } |
| 52 | // Length-delimited: ("ab","c") and ("a","bc") differ. |
| 53 | if Fingerprint("ab", "c", "n", "") == Fingerprint("a", "bc", "n", "") { |
| 54 | t.Fatal("components must be length-delimited") |
| 55 | } |
| 56 | // A redeploy's bundle hash and line numbers do not split a browser group. |
| 57 | s1 := "TypeError: x\n at render (https://a/main.3f2a9c1b.js:12:34)" |
| 58 | s2 := "TypeError: x\n at render (https://a/main.9b8c7d6e.js:99:1)" |
| 59 | if Fingerprint(SourceBrowser, "TypeError", "x", s1) != Fingerprint(SourceBrowser, "TypeError", "x", s2) { |
| 60 | t.Fatal("frame normalisation must drop bundle hash and line:col") |
| 61 | } |
| 62 | } |
| 63 | |