Skip to content

Commit

Permalink
Add some golden tests to ensure the encoding works properly
Browse files Browse the repository at this point in the history
Co-authored-by: Violet <violet@mercury.com>
  • Loading branch information
Jade Lovelace and ctypeh committed Dec 3, 2022
1 parent 950094e commit cc07e8d
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ cabal.project.local
# generated by flake.nix
.pre-commit-config.yaml
*.actual
*.actual.json
6 changes: 5 additions & 1 deletion slack-web.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extra-source-files:
tests/golden/UsersConversationsResponse/*.golden
tests/golden/UpdateRsp/*.json
tests/golden/UpdateRsp/*.golden
tests/golden/BlockKitBuilderMessage/*.golden.json

category: Web

Expand Down Expand Up @@ -180,6 +181,7 @@ test-suite tests
Web.Slack.UsersConversationsSpec
Web.Slack.Experimental.RequestVerificationSpec
Web.Slack.Experimental.Events.TypesSpec
Web.Slack.Experimental.BlocksSpec
TestImport
build-tool-depends:
hspec-discover:hspec-discover >=2.6.0 && <2.11
Expand All @@ -194,12 +196,14 @@ test-suite tests
, hspec
, hspec-core
, hspec-golden
, mtl
, pretty-simple
, quickcheck-instances
, slack-web
, string-conversions
, text
, string-variants
, template-haskell
, text
, th-compat
, time
default-language:
Expand Down
6 changes: 3 additions & 3 deletions tests/JSONGolden.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ goldenTestEncode name value = do
let output = AP.encodePretty @a value
theTypeName = typeName @a
in Golden
{ output = toStrict . pShowNoColor $ output
{ output = cs output
, encodePretty = unpack
, writeToFile = T.writeFile
, -- deal with vim related EOF
readFromFile = \fname -> stripEnd <$> T.readFile fname
, goldenFile = filename theTypeName name ++ ".golden"
, actualFile = Just $ filename theTypeName name ++ ".actual"
, goldenFile = filename theTypeName name ++ ".golden.json"
, actualFile = Just $ filename theTypeName name ++ ".actual.json"
, failFirstTime = True
}

Expand Down
89 changes: 89 additions & 0 deletions tests/Web/Slack/Experimental/BlocksSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
module Web.Slack.Experimental.BlocksSpec where

import Control.Monad.Writer.Strict
import Data.StringVariants (mkNonEmptyText)
import JSONGolden (oneGoldenTestEncode)
import TestImport
import Web.Slack.Experimental.Blocks
import Web.Slack.Experimental.Blocks.Types

-- FIXME(jadel, violet): These builder things should probably be put into a
-- module to make them available, but that's later work

headerBlock :: SlackText -> WriterT [SlackBlock] Identity ()
headerBlock = tell . pure . SlackBlockHeader . SlackPlainTextOnly

sectionBlock :: SlackText -> WriterT [SlackBlock] Identity ()
sectionBlock text = tell . pure $ SlackBlockSection text Nothing

sectionBlockWithAccessory ::
SlackText ->
SlackAction ->
WriterT [SlackBlock] Identity ()
sectionBlockWithAccessory a b = tell . pure $ SlackBlockSection a (Just . SlackButtonAccessory $ b)

dividerBlock :: WriterT [SlackBlock] Identity ()
dividerBlock = tell . pure $ SlackBlockDivider

contextBlock :: [SlackContent] -> WriterT [SlackBlock] Identity ()
contextBlock = tell . pure . SlackBlockContext . SlackContext

actionsBlock ::
Maybe SlackBlockId ->
SlackActionList ->
WriterT [SlackBlock] Identity ()
actionsBlock a b = tell . pure $ SlackBlockActions a b

slackActionDoNothing :: SlackActionId
slackActionDoNothing = SlackActionId . fromJust . mkNonEmptyText $ "doNothing"

slackActionDoNothing2 :: SlackActionId
slackActionDoNothing2 = SlackActionId . fromJust . mkNonEmptyText $ "doNothing2"

slackActionDoNothing3 :: SlackActionId
slackActionDoNothing3 = SlackActionId . fromJust . mkNonEmptyText $ "doNothing3"

slackShowBlockFormat :: BlockKitBuilderMessage
slackShowBlockFormat =
BlockKitBuilderMessage $
execWriter $ do
headerBlock ("Blah: " <> list ["a", "b", "c"])
dividerBlock
sectionBlock (bold $ list ["blah", "blah2", "blah3"])
sectionBlockWithAccessory
(monospaced @Text "blah")
(button slackActionDoNothing ":mag: Look at it" buttonSettings {buttonUrl = google})
sectionBlockWithAccessory
(bold $ list ["blah", "blah2", "blah3"])
(button slackActionDoNothing ":office: Look at it but different" buttonSettings {buttonUrl = google})
sectionBlock (bold (textMessage "Letters:") <> newline (list ["a", "b", "c"]))
sectionBlock (list ["blah1", "blah2", "blah3"])
dividerBlock
sectionBlock (bold . textMessage $ "blah")
sectionBlockWithAccessory
(bold . textMessage $ "blah")
(button slackActionDoNothing ":bank: Look at it!" buttonSettings {buttonUrl = google})
contextBlock [SlackContentText ":key: Context!"]
dividerBlock
actionsBlock Nothing $
toSlackActionList
( button slackActionDoNothing ":mag: View" buttonSettings {buttonUrl = google}
, button slackActionDoNothing2 ":office: View" buttonSettings {buttonUrl = google}
, button slackActionDoNothing3 ":bank: View" buttonSettings {buttonUrl = google}
)
where
textMessage = message @Text
list x = textMessage (intercalate ", " x)
google = OptionalSetting . mkNonEmptyText @3000 $ "https://google.com"

-- | This is used so that we can paste the output of these tests directly into
-- the block kit builder:
-- <https://app.slack.com/block-kit-builder>
newtype BlockKitBuilderMessage = BlockKitBuilderMessage {blocks :: [SlackBlock]}

instance ToJSON BlockKitBuilderMessage where
toJSON BlockKitBuilderMessage {blocks} = object [("blocks", toJSON blocks)]

spec :: Spec
spec = describe "Slack block builder" do
oneGoldenTestEncode "simple_blocks" slackShowBlockFormat
137 changes: 137 additions & 0 deletions tests/golden/BlockKitBuilderMessage/simple_blocks.golden.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{
"blocks": [
{
"text": {
"text": "Blah: \na, b, c",
"type": "plain_text"
},
"type": "header"
},
{
"type": "divider"
},
{
"text": {
"text": "*blah, blah2, blah3*",
"type": "mrkdwn"
},
"type": "section"
},
{
"accessory": {
"action_id": "doNothing",
"text": {
"text": ":mag: Look at it",
"type": "plain_text"
},
"type": "button",
"url": "https://google.com"
},
"text": {
"text": "`blah`",
"type": "mrkdwn"
},
"type": "section"
},
{
"accessory": {
"action_id": "doNothing",
"text": {
"text": ":office: Look at it but different",
"type": "plain_text"
},
"type": "button",
"url": "https://google.com"
},
"text": {
"text": "*blah, blah2, blah3*",
"type": "mrkdwn"
},
"type": "section"
},
{
"text": {
"text": "*Letters:*\na, b, c",
"type": "mrkdwn"
},
"type": "section"
},
{
"text": {
"text": "blah1, blah2, blah3",
"type": "mrkdwn"
},
"type": "section"
},
{
"type": "divider"
},
{
"text": {
"text": "*blah*",
"type": "mrkdwn"
},
"type": "section"
},
{
"accessory": {
"action_id": "doNothing",
"text": {
"text": ":bank: Look at it!",
"type": "plain_text"
},
"type": "button",
"url": "https://google.com"
},
"text": {
"text": "*blah*",
"type": "mrkdwn"
},
"type": "section"
},
{
"elements": [
{
"text": ":key: Context!",
"type": "mrkdwn"
}
],
"type": "context"
},
{
"type": "divider"
},
{
"elements": [
{
"action_id": "doNothing",
"text": {
"text": ":mag: View",
"type": "plain_text"
},
"type": "button",
"url": "https://google.com"
},
{
"action_id": "doNothing2",
"text": {
"text": ":office: View",
"type": "plain_text"
},
"type": "button",
"url": "https://google.com"
},
{
"action_id": "doNothing3",
"text": {
"text": ":bank: View",
"type": "plain_text"
},
"type": "button",
"url": "https://google.com"
}
],
"type": "actions"
}
]
}

0 comments on commit cc07e8d

Please sign in to comment.