| 1 |
// Copyright (c) 2020 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package pushrules_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
|
| 14 |
"maunium.net/go/mautrix/pushrules" |
| 15 |
) |
| 16 |
|
| 17 |
func TestPushActionArray_Should_EmptyArrayReturnsDefaults(t *testing.T) { |
| 18 |
should := pushrules.PushActionArray{}.Should() |
| 19 |
assert.False(t, should.NotifySpecified) |
| 20 |
assert.False(t, should.Notify) |
| 21 |
assert.False(t, should.Highlight) |
| 22 |
assert.False(t, should.PlaySound) |
| 23 |
assert.Empty(t, should.SoundName) |
| 24 |
} |
| 25 |
|
| 26 |
func TestPushActionArray_Should_MixedArrayReturnsExpected1(t *testing.T) { |
| 27 |
should := pushrules.PushActionArray{ |
| 28 |
{Action: pushrules.ActionNotify}, |
| 29 |
{Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakHighlight}, |
| 30 |
{Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakSound, Value: "ping"}, |
| 31 |
}.Should() |
| 32 |
assert.True(t, should.NotifySpecified) |
| 33 |
assert.True(t, should.Notify) |
| 34 |
assert.True(t, should.Highlight) |
| 35 |
assert.True(t, should.PlaySound) |
| 36 |
assert.Equal(t, "ping", should.SoundName) |
| 37 |
} |
| 38 |
|
| 39 |
func TestPushActionArray_Should_MixedArrayReturnsExpected2(t *testing.T) { |
| 40 |
should := pushrules.PushActionArray{ |
| 41 |
{Action: pushrules.ActionDontNotify}, |
| 42 |
{Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakHighlight, Value: false}, |
| 43 |
{Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakSound, Value: ""}, |
| 44 |
}.Should() |
| 45 |
assert.True(t, should.NotifySpecified) |
| 46 |
assert.False(t, should.Notify) |
| 47 |
assert.False(t, should.Highlight) |
| 48 |
assert.False(t, should.PlaySound) |
| 49 |
assert.Empty(t, should.SoundName) |
| 50 |
} |
| 51 |
|
| 52 |
func TestPushActionArray_Should_NotifySet(t *testing.T) { |
| 53 |
should := pushrules.PushActionArray{ |
| 54 |
{Action: pushrules.ActionNotify}, |
| 55 |
}.Should() |
| 56 |
assert.True(t, should.NotifySpecified) |
| 57 |
assert.True(t, should.Notify) |
| 58 |
assert.False(t, should.Highlight) |
| 59 |
assert.False(t, should.PlaySound) |
| 60 |
assert.Empty(t, should.SoundName) |
| 61 |
} |
| 62 |
|
| 63 |
func TestPushActionArray_Should_NotifyAndCoalesceDoTheSameThing(t *testing.T) { |
| 64 |
should1 := pushrules.PushActionArray{ |
| 65 |
{Action: pushrules.ActionNotify}, |
| 66 |
}.Should() |
| 67 |
should2 := pushrules.PushActionArray{ |
| 68 |
{Action: pushrules.ActionCoalesce}, |
| 69 |
}.Should() |
| 70 |
assert.Equal(t, should1, should2) |
| 71 |
} |
| 72 |
|
| 73 |
func TestPushActionArray_Should_DontNotify(t *testing.T) { |
| 74 |
should := pushrules.PushActionArray{ |
| 75 |
{Action: pushrules.ActionDontNotify}, |
| 76 |
}.Should() |
| 77 |
assert.True(t, should.NotifySpecified) |
| 78 |
assert.False(t, should.Notify) |
| 79 |
assert.False(t, should.Highlight) |
| 80 |
assert.False(t, should.PlaySound) |
| 81 |
assert.Empty(t, should.SoundName) |
| 82 |
} |
| 83 |
|
| 84 |
func TestPushActionArray_Should_HighlightBlank(t *testing.T) { |
| 85 |
should := pushrules.PushActionArray{ |
| 86 |
{Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakHighlight}, |
| 87 |
}.Should() |
| 88 |
assert.False(t, should.NotifySpecified) |
| 89 |
assert.False(t, should.Notify) |
| 90 |
assert.True(t, should.Highlight) |
| 91 |
assert.False(t, should.PlaySound) |
| 92 |
assert.Empty(t, should.SoundName) |
| 93 |
} |
| 94 |
|
| 95 |
func TestPushActionArray_Should_HighlightFalse(t *testing.T) { |
| 96 |
should := pushrules.PushActionArray{ |
| 97 |
{Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakHighlight, Value: false}, |
| 98 |
}.Should() |
| 99 |
assert.False(t, should.NotifySpecified) |
| 100 |
assert.False(t, should.Notify) |
| 101 |
assert.False(t, should.Highlight) |
| 102 |
assert.False(t, should.PlaySound) |
| 103 |
assert.Empty(t, should.SoundName) |
| 104 |
} |
| 105 |
|
| 106 |
func TestPushActionArray_Should_SoundName(t *testing.T) { |
| 107 |
should := pushrules.PushActionArray{ |
| 108 |
{Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakSound, Value: "ping"}, |
| 109 |
}.Should() |
| 110 |
assert.False(t, should.NotifySpecified) |
| 111 |
assert.False(t, should.Notify) |
| 112 |
assert.False(t, should.Highlight) |
| 113 |
assert.True(t, should.PlaySound) |
| 114 |
assert.Equal(t, "ping", should.SoundName) |
| 115 |
} |
| 116 |
|
| 117 |
func TestPushActionArray_Should_SoundNameEmpty(t *testing.T) { |
| 118 |
should := pushrules.PushActionArray{ |
| 119 |
{Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakSound, Value: ""}, |
| 120 |
}.Should() |
| 121 |
assert.False(t, should.NotifySpecified) |
| 122 |
assert.False(t, should.Notify) |
| 123 |
assert.False(t, should.Highlight) |
| 124 |
assert.False(t, should.PlaySound) |
| 125 |
assert.Empty(t, should.SoundName) |
| 126 |
} |
| 127 |
|
| 128 |
func TestPushAction_UnmarshalJSON_InvalidJSONFails(t *testing.T) { |
| 129 |
pa := &pushrules.PushAction{} |
| 130 |
err := pa.UnmarshalJSON([]byte("Not JSON")) |
| 131 |
assert.NotNil(t, err) |
| 132 |
} |
| 133 |
|
| 134 |
func TestPushAction_UnmarshalJSON_InvalidTypeDoesNothing(t *testing.T) { |
| 135 |
pa := &pushrules.PushAction{ |
| 136 |
Action: pushrules.PushActionType("unchanged"), |
| 137 |
Tweak: pushrules.PushActionTweak("unchanged"), |
| 138 |
Value: "unchanged", |
| 139 |
} |
| 140 |
|
| 141 |
err := pa.UnmarshalJSON([]byte(`{"foo": "bar"}`)) |
| 142 |
assert.Nil(t, err) |
| 143 |
err = pa.UnmarshalJSON([]byte(`9001`)) |
| 144 |
assert.Nil(t, err) |
| 145 |
|
| 146 |
assert.Equal(t, pushrules.PushActionType("unchanged"), pa.Action) |
| 147 |
assert.Equal(t, pushrules.PushActionTweak("unchanged"), pa.Tweak) |
| 148 |
assert.Equal(t, "unchanged", pa.Value) |
| 149 |
} |
| 150 |
|
| 151 |
func TestPushAction_UnmarshalJSON_StringChangesActionType(t *testing.T) { |
| 152 |
pa := &pushrules.PushAction{ |
| 153 |
Action: pushrules.PushActionType("unchanged"), |
| 154 |
Tweak: pushrules.PushActionTweak("unchanged"), |
| 155 |
Value: "unchanged", |
| 156 |
} |
| 157 |
|
| 158 |
err := pa.UnmarshalJSON([]byte(`"foo"`)) |
| 159 |
assert.Nil(t, err) |
| 160 |
|
| 161 |
assert.Equal(t, pushrules.PushActionType("foo"), pa.Action) |
| 162 |
assert.Equal(t, pushrules.PushActionTweak("unchanged"), pa.Tweak) |
| 163 |
assert.Equal(t, "unchanged", pa.Value) |
| 164 |
} |
| 165 |
|
| 166 |
func TestPushAction_UnmarshalJSON_SetTweakChangesTweak(t *testing.T) { |
| 167 |
pa := &pushrules.PushAction{ |
| 168 |
Action: pushrules.PushActionType("unchanged"), |
| 169 |
Tweak: pushrules.PushActionTweak("unchanged"), |
| 170 |
Value: "unchanged", |
| 171 |
} |
| 172 |
|
| 173 |
err := pa.UnmarshalJSON([]byte(`{"set_tweak": "foo", "value": 123.0}`)) |
| 174 |
assert.Nil(t, err) |
| 175 |
|
| 176 |
assert.Equal(t, pushrules.ActionSetTweak, pa.Action) |
| 177 |
assert.Equal(t, pushrules.PushActionTweak("foo"), pa.Tweak) |
| 178 |
assert.Equal(t, 123.0, pa.Value) |
| 179 |
} |
| 180 |
|
| 181 |
func TestPushAction_MarshalJSON_TweakOutputWorks(t *testing.T) { |
| 182 |
pa := &pushrules.PushAction{ |
| 183 |
Action: pushrules.ActionSetTweak, |
| 184 |
Tweak: pushrules.PushActionTweak("foo"), |
| 185 |
Value: "bar", |
| 186 |
} |
| 187 |
data, err := pa.MarshalJSON() |
| 188 |
assert.Nil(t, err) |
| 189 |
assert.Equal(t, []byte(`{"set_tweak":"foo","value":"bar"}`), data) |
| 190 |
} |
| 191 |
|
| 192 |
func TestPushAction_MarshalJSON_OtherOutputWorks(t *testing.T) { |
| 193 |
pa := &pushrules.PushAction{ |
| 194 |
Action: pushrules.PushActionType("something else"), |
| 195 |
Tweak: pushrules.PushActionTweak("foo"), |
| 196 |
Value: "bar", |
| 197 |
} |
| 198 |
data, err := pa.MarshalJSON() |
| 199 |
assert.Nil(t, err) |
| 200 |
assert.Equal(t, []byte(`"something else"`), data) |
| 201 |
} |