package main import ( "fmt" "os" "path" "testing" ) func TestParseSlackTestdata(t *testing.T) { cases := map[string]struct { slackMessage slackMessage message Message }{ "human_thread_message_from_opsgenie_alert.json": { slackMessage: slackMessage{ TS: 1712930706, Event: slackEvent{ ID: "1712930706.598629", Channel: "C06U1DDBBU4", ParentID: "1712927439.728409", Text: "I gotta do this", Blocks: []slackBlock{{ Elements: []slackElement{{ Elements: []slackElement{{ RichText: "I gotta do this", }}, }}, }}, Bot: slackBot{ Name: "", }, Attachments: []slackAttachment{}, }, }, message: Message{ ID: "1712930706", TS: 1712930706, Source: "https://renderinc.slack.com/archives/C06U1DDBBU4/p1712927439728409", Channel: "C06U1DDBBU4", Thread: "1712927439.728409", EventName: "TODO", Event: "TODO", Plaintext: "I gotta do this", Asset: "TODO", }, }, "opsgenie_alert.json": { slackMessage: slackMessage{ TS: 1712927439, Event: slackEvent{ ID: "1712927439.728409", Channel: "C06U1DDBBU4", Bot: slackBot{ Name: "Opsgenie for Alert Management", }, Attachments: []slackAttachment{{ Color: "F4511E", Title: "#11071: [Grafana]: Firing: Alertconfig Workflow Failed", Text: "At least one alertconfig run has failed unexpectedly.\nDashboard: \nPanel: \nSource: ", Fields: []slackField{ {Value: "P3", Title: "Priority"}, {Value: "alertname:Alertconfig Workflow Failed, grafana_folder:Datastores, rule_uid:a7639f7e-6950-41be-850a-b22119f74cbb", Title: "Tags"}, {Value: "Datastores Non-Critical", Title: "Routed Teams"}, }, Actions: []slackAction{{}, {}, {}}, }}, }, }, message: Message{ ID: "1712927439", TS: 1712927439, Source: "https://renderinc.slack.com/archives/C06U1DDBBU4/p1712927439728409", Channel: "C06U1DDBBU4", Thread: "1712927439.728409", EventName: "Alertconfig Workflow Failed", Event: "#11071", Plaintext: "At least one alertconfig run has failed unexpectedly.\nDashboard: \nPanel: \nSource: ", Asset: "TODO", }, }, } for name, d := range cases { want := d t.Run(name, func(t *testing.T) { b, err := os.ReadFile(path.Join("testdata", "slack_events", name)) if err != nil { t.Fatal(err) } t.Run("parseSlack", func(t *testing.T) { got, err := parseSlack(b) if err != nil { t.Fatal(err) } if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", want.slackMessage) { t.Errorf("wanted \n\t%+v, got\n\t%+v", want.slackMessage, got) } }) t.Run("ParseSlack", func(t *testing.T) { got, err := ParseSlack(b) if err != nil { t.Fatal(err) } if got != want.message { t.Errorf("wanted \n\t%+v, got\n\t%+v", want.message, got) } }) }) } }