1module Filter exposing (parseMatcher, stringifyFilter, toUrl)
2
3import Expect
4import Fuzz exposing (string, tuple)
5import Helpers exposing (isNotEmptyTrimmedAlphabetWord)
6import Test exposing (..)
7import Utils.Filter exposing (MatchOperator(..), Matcher)
8
9
10parseMatcher : Test
11parseMatcher =
12    describe "parseMatcher"
13        [ test "should parse empty matcher string" <|
14            \() ->
15                Expect.equal Nothing (Utils.Filter.parseMatcher "")
16        , test "should parse empty matcher value" <|
17            \() ->
18                Expect.equal (Just (Matcher "alertname" Eq "")) (Utils.Filter.parseMatcher "alertname=\"\"")
19        , fuzz (tuple ( string, string )) "should parse random matcher string" <|
20            \( key, value ) ->
21                if List.map isNotEmptyTrimmedAlphabetWord [ key, value ] /= [ True, True ] then
22                    Expect.equal
23                        Nothing
24                        (Utils.Filter.parseMatcher <| String.concat [ key, "=", value ])
25
26                else
27                    Expect.equal
28                        (Just (Matcher key Eq value))
29                        (Utils.Filter.parseMatcher <| String.concat [ key, "=", "\"", value, "\"" ])
30        ]
31
32
33toUrl : Test
34toUrl =
35    describe "toUrl"
36        [ test "should not render keys with Nothing value except the silenced, inhibited, and active parameters, which default to false, false, true, respectively." <|
37            \() ->
38                Expect.equal "/alerts?silenced=false&inhibited=false&active=true"
39                    (Utils.Filter.toUrl "/alerts" { receiver = Nothing, group = Nothing, customGrouping = False, text = Nothing, showSilenced = Nothing, showInhibited = Nothing, showActive = Nothing })
40        , test "should not render filter key with empty value" <|
41            \() ->
42                Expect.equal "/alerts?silenced=false&inhibited=false&active=true"
43                    (Utils.Filter.toUrl "/alerts" { receiver = Nothing, group = Nothing, customGrouping = False, text = Just "", showSilenced = Nothing, showInhibited = Nothing, showActive = Nothing })
44        , test "should render filter key with values" <|
45            \() ->
46                Expect.equal "/alerts?silenced=false&inhibited=false&active=true&filter=%7Bfoo%3D%22bar%22%2C%20baz%3D~%22quux.*%22%7D"
47                    (Utils.Filter.toUrl "/alerts" { receiver = Nothing, group = Nothing, customGrouping = False, text = Just "{foo=\"bar\", baz=~\"quux.*\"}", showSilenced = Nothing, showInhibited = Nothing, showActive = Nothing })
48        , test "should render silenced key with bool" <|
49            \() ->
50                Expect.equal "/alerts?silenced=true&inhibited=false&active=true"
51                    (Utils.Filter.toUrl "/alerts" { receiver = Nothing, group = Nothing, customGrouping = False, text = Nothing, showSilenced = Just True, showInhibited = Nothing, showActive = Nothing })
52        , test "should render inhibited key with bool" <|
53            \() ->
54                Expect.equal "/alerts?silenced=false&inhibited=true&active=true"
55                    (Utils.Filter.toUrl "/alerts" { receiver = Nothing, group = Nothing, customGrouping = False, text = Nothing, showSilenced = Nothing, showInhibited = Just True, showActive = Nothing })
56        , test "should render active key with bool" <|
57            \() ->
58                Expect.equal "/alerts?silenced=false&inhibited=false&active=false"
59                    (Utils.Filter.toUrl "/alerts" { receiver = Nothing, group = Nothing, customGrouping = False, text = Nothing, showSilenced = Nothing, showInhibited = Nothing, showActive = Just False })
60        , test "should add customGrouping key" <|
61            \() ->
62                Expect.equal "/alerts?silenced=false&inhibited=false&active=true&customGrouping=true"
63                    (Utils.Filter.toUrl "/alerts" { receiver = Nothing, group = Nothing, customGrouping = True, text = Nothing, showSilenced = Nothing, showInhibited = Nothing, showActive = Nothing })
64        ]
65
66
67stringifyFilter : Test
68stringifyFilter =
69    describe "stringifyFilter"
70        [ test "empty" <|
71            \() ->
72                Expect.equal ""
73                    (Utils.Filter.stringifyFilter [])
74        , test "non-empty" <|
75            \() ->
76                Expect.equal "{foo=\"bar\", baz=~\"quux.*\"}"
77                    (Utils.Filter.stringifyFilter
78                        [ { key = "foo", op = Eq, value = "bar" }
79                        , { key = "baz", op = RegexMatch, value = "quux.*" }
80                        ]
81                    )
82        ]
83