|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestMessageExtraction(t *testing.T) { |
| 8 | + testCases := []struct { |
| 9 | + name string |
| 10 | + input string |
| 11 | + expected map[string]any |
| 12 | + }{ |
| 13 | + { |
| 14 | + name: "error messages on multiple lines", |
| 15 | + input: "2024-01-16T08:53:51.919Z 4b995efa-75f8-4fdc-92af-0882c79f47a1 ERROR testing sending an error\nand this is a new line inside the error \n and a new line \n bye", |
| 16 | + expected: map[string]any{ |
| 17 | + "level": "error", |
| 18 | + "message": "SAME_AS_INPUT_NO_NEED_TO_DUPLICATE_INPUT_HERE", |
| 19 | + "record": map[string]any{"requestId": "4b995efa-75f8-4fdc-92af-0882c79f47a1", "message": "testing sending an error\nand this is a new line inside the error \n and a new line \n bye", "timestamp": "2024-01-16T08:53:51.919Z", "level": "error"}, |
| 20 | + }, |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "info messages", |
| 24 | + input: "2024-01-16T08:53:51.919Z 4b995efa-75f8-4fdc-92af-0882c79f47a2 INFO Hello, world!", |
| 25 | + expected: map[string]any{ |
| 26 | + "level": "info", |
| 27 | + "message": "SAME_AS_INPUT_NO_NEED_TO_DUPLICATE_INPUT_HERE", |
| 28 | + "record": map[string]any{"requestId": "4b995efa-75f8-4fdc-92af-0882c79f47a2", "message": "Hello, world!", "timestamp": "2024-01-16T08:53:51.919Z", "level": "info"}, |
| 29 | + }, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "warn messages", |
| 33 | + input: "2024-01-16T08:53:51.919Z 4b995efa-75f8-4fdc-92af-0882c79f47a3 WARN head my warning", |
| 34 | + expected: map[string]any{ |
| 35 | + "level": "warn", |
| 36 | + "message": "SAME_AS_INPUT_NO_NEED_TO_DUPLICATE_INPUT_HERE", |
| 37 | + "record": map[string]any{"requestId": "4b995efa-75f8-4fdc-92af-0882c79f47a3", "message": "head my warning", "timestamp": "2024-01-16T08:53:51.919Z", "level": "warn"}, |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "trace messages", |
| 42 | + input: "2024-01-16T08:53:51.919Z 4b995efa-75f8-4fdc-92af-0882c79f47a4 TRACE this is a trace \n with information on a new line.", |
| 43 | + expected: map[string]any{ |
| 44 | + "level": "trace", |
| 45 | + "message": "SAME_AS_INPUT_NO_NEED_TO_DUPLICATE_INPUT_HERE", |
| 46 | + "record": map[string]any{"requestId": "4b995efa-75f8-4fdc-92af-0882c79f47a4", "message": "this is a trace \n with information on a new line.", "timestamp": "2024-01-16T08:53:51.919Z", "level": "trace"}, |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "debug messages", |
| 51 | + input: "2024-01-16T08:53:51.919Z 4b995efa-75f8-4fdc-92af-0882c79f47a5 DEBUG Debugging is fun!", |
| 52 | + expected: map[string]any{ |
| 53 | + "level": "debug", |
| 54 | + "message": "SAME_AS_INPUT_NO_NEED_TO_DUPLICATE_INPUT_HERE", |
| 55 | + "record": map[string]any{"requestId": "4b995efa-75f8-4fdc-92af-0882c79f47a5", "message": "Debugging is fun!", "timestamp": "2024-01-16T08:53:51.919Z", "level": "debug"}, |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "testing json messages", |
| 60 | + input: `{"timestamp":"2024-01-08T16:48:45.316Z","level":"INFO","requestId":"de126cf0-6124-426c-818a-174983fbfc4b","message":"foo != bar"}`, |
| 61 | + expected: map[string]any{ |
| 62 | + "level": "info", |
| 63 | + "message": "SAME_AS_INPUT_NO_NEED_TO_DUPLICATE_INPUT_HERE", |
| 64 | + "record": map[string]any{"requestId": "de126cf0-6124-426c-818a-174983fbfc4b", "message": "foo != bar", "timestamp": "2024-01-08T16:48:45.316Z", "level": "info"}, |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + for _, testCase := range testCases { |
| 70 | + t.Run(testCase.name, func(t *testing.T) { |
| 71 | + e := make(map[string]any) |
| 72 | + e["record"] = testCase.input |
| 73 | + extractEventMessage(e) |
| 74 | + if e["level"] != testCase.expected["level"] { |
| 75 | + t.Errorf("Expected level to be %s, got %s", testCase.expected["level"], e["level"]) |
| 76 | + } |
| 77 | + if e["message"] != testCase.input { // the message field should contain the original input |
| 78 | + t.Errorf("Expected message to be %s, got %s", testCase.input, e["message"]) |
| 79 | + } |
| 80 | + |
| 81 | + expectedRecord := testCase.expected["record"].(map[string]any) |
| 82 | + outputRecord := e["record"].(map[string]any) |
| 83 | + |
| 84 | + if outputRecord["timestamp"] != expectedRecord["timestamp"] { |
| 85 | + t.Errorf("Expected timestamp to be %s, got %s", testCase.expected["timestamp"], e["timestamp"]) |
| 86 | + } |
| 87 | + if outputRecord["level"] != expectedRecord["level"] { |
| 88 | + t.Errorf("Expected record.level to be %s, got %s", expectedRecord["level"], outputRecord["level"]) |
| 89 | + } |
| 90 | + if outputRecord["requestId"] != expectedRecord["requestId"] { |
| 91 | + t.Errorf("Expected record.requestId to be %s, got %s", expectedRecord["requestId"], outputRecord["requestId"]) |
| 92 | + } |
| 93 | + if outputRecord["message"] != expectedRecord["message"] { |
| 94 | + t.Errorf("Expected record.message to be %s, got %s", expectedRecord["message"], outputRecord["message"]) |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
0 commit comments