aboutsummaryrefslogtreecommitdiff
path: root/matrix/pushrules/action_test.go
blob: 41a1056a5ce14ba5b54eb6e3630dfadc0ee99236 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2018 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package pushrules_test

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"maunium.net/go/gomuks/matrix/pushrules"
)

func TestPushActionArray_Should_EmptyArrayReturnsDefaults(t *testing.T) {
	should := pushrules.PushActionArray{}.Should()
	assert.False(t, should.NotifySpecified)
	assert.False(t, should.Notify)
	assert.False(t, should.Highlight)
	assert.False(t, should.PlaySound)
	assert.Empty(t, should.SoundName)
}

func TestPushActionArray_Should_MixedArrayReturnsExpected1(t *testing.T) {
	should := pushrules.PushActionArray{
		{Action: pushrules.ActionNotify},
		{Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakHighlight},
		{Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakSound, Value: "ping"},
	}.Should()
	assert.True(t, should.NotifySpecified)
	assert.True(t, should.Notify)
	assert.True(t, should.Highlight)
	assert.True(t, should.PlaySound)
	assert.Equal(t, "ping", should.SoundName)
}

func TestPushActionArray_Should_MixedArrayReturnsExpected2(t *testing.T) {
	should := pushrules.PushActionArray{
		{Action: pushrules.ActionDontNotify},
		{Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakHighlight, Value: false},
		{Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakSound, Value: ""},
	}.Should()
	assert.True(t, should.NotifySpecified)
	assert.False(t, should.Notify)
	assert.False(t, should.Highlight)
	assert.False(t, should.PlaySound)
	assert.Empty(t, should.SoundName)
}

func TestPushActionArray_Should_NotifySet(t *testing.T) {
	should := pushrules.PushActionArray{
		{Action: pushrules.ActionNotify},
	}.Should()
	assert.True(t, should.NotifySpecified)
	assert.True(t, should.Notify)
	assert.False(t, should.Highlight)
	assert.False(t, should.PlaySound)
	assert.Empty(t, should.SoundName)
}

func TestPushActionArray_Should_NotifyAndCoalesceDoTheSameThing(t *testing.T) {
	should1 := pushrules.PushActionArray{
		{Action: pushrules.ActionNotify},
	}.Should()
	should2 := pushrules.PushActionArray{
		{Action: pushrules.ActionCoalesce},
	}.Should()
	assert.Equal(t, should1, should2)
}

func TestPushActionArray_Should_DontNotify(t *testing.T) {
	should := pushrules.PushActionArray{
		{Action: pushrules.ActionDontNotify},
	}.Should()
	assert.True(t, should.NotifySpecified)
	assert.False(t, should.Notify)
	assert.False(t, should.Highlight)
	assert.False(t, should.PlaySound)
	assert.Empty(t, should.SoundName)
}

func TestPushActionArray_Should_HighlightBlank(t *testing.T) {
	should := pushrules.PushActionArray{
		{Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakHighlight},
	}.Should()
	assert.False(t, should.NotifySpecified)
	assert.False(t, should.Notify)
	assert.True(t, should.Highlight)
	assert.False(t, should.PlaySound)
	assert.Empty(t, should.SoundName)
}

func TestPushActionArray_Should_HighlightFalse(t *testing.T) {
	should := pushrules.PushActionArray{
		{Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakHighlight, Value: false},
	}.Should()
	assert.False(t, should.NotifySpecified)
	assert.False(t, should.Notify)
	assert.False(t, should.Highlight)
	assert.False(t, should.PlaySound)
	assert.Empty(t, should.SoundName)
}

func TestPushActionArray_Should_SoundName(t *testing.T) {
	should := pushrules.PushActionArray{
		{Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakSound, Value: "ping"},
	}.Should()
	assert.False(t, should.NotifySpecified)
	assert.False(t, should.Notify)
	assert.False(t, should.Highlight)
	assert.True(t, should.PlaySound)
	assert.Equal(t, "ping", should.SoundName)
}

func TestPushActionArray_Should_SoundNameEmpty(t *testing.T) {
	should := pushrules.PushActionArray{
		{Action: pushrules.ActionSetTweak, Tweak: pushrules.TweakSound, Value: ""},
	}.Should()
	assert.False(t, should.NotifySpecified)
	assert.False(t, should.Notify)
	assert.False(t, should.Highlight)
	assert.False(t, should.PlaySound)
	assert.Empty(t, should.SoundName)
}

func TestPushAction_UnmarshalJSON_InvalidJSONFails(t *testing.T) {
	pa := &pushrules.PushAction{}
	err := pa.UnmarshalJSON([]byte("Not JSON"))
	assert.NotNil(t, err)
}

func TestPushAction_UnmarshalJSON_InvalidTypeDoesNothing(t *testing.T) {
	pa := &pushrules.PushAction{
		Action: pushrules.PushActionType("unchanged"),
		Tweak:  pushrules.PushActionTweak("unchanged"),
		Value:  "unchanged",
	}

	err := pa.UnmarshalJSON([]byte(`{"foo": "bar"}`))
	assert.Nil(t, err)
	err = pa.UnmarshalJSON([]byte(`9001`))
	assert.Nil(t, err)

	assert.Equal(t, pushrules.PushActionType("unchanged"), pa.Action)
	assert.Equal(t, pushrules.PushActionTweak("unchanged"), pa.Tweak)
	assert.Equal(t, "unchanged", pa.Value)
}

func TestPushAction_UnmarshalJSON_StringChangesActionType(t *testing.T) {
	pa := &pushrules.PushAction{
		Action: pushrules.PushActionType("unchanged"),
		Tweak:  pushrules.PushActionTweak("unchanged"),
		Value:  "unchanged",
	}

	err := pa.UnmarshalJSON([]byte(`"foo"`))
	assert.Nil(t, err)

	assert.Equal(t, pushrules.PushActionType("foo"), pa.Action)
	assert.Equal(t, pushrules.PushActionTweak("unchanged"), pa.Tweak)
	assert.Equal(t, "unchanged", pa.Value)
}

func TestPushAction_UnmarshalJSON_SetTweakChangesTweak(t *testing.T) {
	pa := &pushrules.PushAction{
		Action: pushrules.PushActionType("unchanged"),
		Tweak:  pushrules.PushActionTweak("unchanged"),
		Value:  "unchanged",
	}

	err := pa.UnmarshalJSON([]byte(`{"set_tweak": "foo", "value": 123.0}`))
	assert.Nil(t, err)

	assert.Equal(t, pushrules.ActionSetTweak, pa.Action)
	assert.Equal(t, pushrules.PushActionTweak("foo"), pa.Tweak)
	assert.Equal(t, 123.0, pa.Value)
}

func TestPushAction_MarshalJSON_TweakOutputWorks(t *testing.T) {
	pa := &pushrules.PushAction{
		Action: pushrules.ActionSetTweak,
		Tweak:  pushrules.PushActionTweak("foo"),
		Value:  "bar",
	}
	data, err := pa.MarshalJSON()
	assert.Nil(t, err)
	assert.Equal(t, []byte(`{"set_tweak":"foo","value":"bar"}`), data)
}

func TestPushAction_MarshalJSON_OtherOutputWorks(t *testing.T) {
	pa := &pushrules.PushAction{
		Action: pushrules.PushActionType("something else"),
		Tweak:  pushrules.PushActionTweak("foo"),
		Value:  "bar",
	}
	data, err := pa.MarshalJSON()
	assert.Nil(t, err)
	assert.Equal(t, []byte(`"something else"`), data)
}