File size: 3,748 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package harness

import (
	"github.com/target/goalert/devtools/mocktwilio"
)

type twilioAssertionDevice struct {
	*twilioAssertionAPIContext
	number string
}

func (dev *twilioAssertionDevice) newMatcher(keywords []string) messageMatcher {
	return messageMatcher{number: dev.number, keywords: keywords}
}

func (dev *twilioAssertionDevice) _expectVoice(keywords ...string) *twilioAssertionVoiceCall {
	dev.t.Helper()
	dev.mx.Lock()
	defer dev.mx.Unlock()

	m := dev.newMatcher(keywords)

	for i, call := range dev.calls {
		if !m.match(call) {
			continue
		}

		dev.calls = append(dev.calls[:i], dev.calls[i+1:]...)

		return &twilioAssertionVoiceCall{twilioAssertionDevice: dev, VoiceCall: call}
	}

	timeout, cancel := dev.triggerTimeout()
	defer cancel()
	for {
		var call *mocktwilio.VoiceCall
		select {
		case call = <-dev.VoiceCalls():
		case msg := <-timeout:
			dev.t.Fatalf("Twilio: timeout after %s waiting for voice call to %s with keywords: %v", msg, dev.formatNumber(dev.number), keywords)
		}
		dev.t.Logf("received voice call to %s: %s", dev.formatNumber(call.To()), call.Body())
		if !m.match(call) {
			dev.calls = append(dev.calls, call)
			continue
		}

		return &twilioAssertionVoiceCall{twilioAssertionDevice: dev, VoiceCall: call}
	}
}

func (dev *twilioAssertionDevice) _expectSMS(includePrev bool, keywords ...string) *twilioAssertionSMS {
	dev.t.Helper()
	dev.mx.Lock()
	defer dev.mx.Unlock()

	m := dev.newMatcher(keywords)

	if includePrev {
		for i, sms := range dev.messages {
			if !m.match(sms) {
				continue
			}

			dev.messages = append(dev.messages[:i], dev.messages[i+1:]...)
			return &twilioAssertionSMS{twilioAssertionDevice: dev, SMS: sms}
		}
	}

	timeout, cancel := dev.triggerTimeout()
	defer cancel()
	for {
		var sms *mocktwilio.SMS
		select {
		case sms = <-dev.SMS():
		case msg := <-timeout:
			dev.t.Fatalf("Twilio: timeout after %s waiting for an SMS to %s with keywords: %v", msg, dev.formatNumber(dev.number), keywords)
		}
		dev.t.Logf("received SMS to %s: %s", dev.formatNumber(sms.To()), sms.Body())
		if !m.match(sms) {
			dev.messages = append(dev.messages, sms)
			continue
		}

		return &twilioAssertionSMS{twilioAssertionDevice: dev, SMS: sms}
	}
}

func (dev *twilioAssertionDevice) ExpectSMS(keywords ...string) ExpectedSMS {
	dev.t.Helper()
	sms := dev._expectSMS(true, keywords...)
	sms.Accept()
	return sms
}

func (dev *twilioAssertionDevice) RejectSMS(keywords ...string) {
	dev.t.Helper()
	sms := dev._expectSMS(true, keywords...)
	sms.Reject()
}

func (sms *twilioAssertionSMS) ThenExpect(keywords ...string) ExpectedSMS {
	sms.t.Helper()
	sms = sms._expectSMS(false, keywords...)
	sms.Accept()
	return sms
}

func (dev *twilioAssertionDevice) ExpectVoice(keywords ...string) ExpectedCall {
	dev.t.Helper()
	call := dev._expectVoice(keywords...)
	dev.mx.Lock()
	call.Accept()
	dev.activeCalls = append(dev.activeCalls, call.VoiceCall)
	dev.mx.Unlock()
	return call
}

func (dev *twilioAssertionDevice) RejectVoice(keywords ...string) {
	dev.t.Helper()
	call := dev._expectVoice(keywords...)
	call.Reject()
}

func (dev *twilioAssertionDevice) SendSMS(body string) {
	dev.t.Helper()
	err := dev.Server.SendSMS(dev.number, dev.sendSMSDest, body)
	if err != nil {
		dev.t.Fatalf("send SMS: from %s: %v", dev.formatNumber(dev.number), err)
	}
}

func (dev *twilioAssertionDevice) IgnoreUnexpectedSMS(keywords ...string) {
	dev.mx.Lock()
	defer dev.mx.Unlock()
	dev.ignoredSMS = append(dev.ignoredSMS, messageMatcher{number: dev.number, keywords: keywords})
}

func (dev *twilioAssertionDevice) IgnoreUnexpectedVoice(keywords ...string) {
	dev.mx.Lock()
	defer dev.mx.Unlock()
	dev.ignoredVoice = append(dev.ignoredVoice, messageMatcher{number: dev.number, keywords: keywords})
}