blib/lib/SignalWire/Relay/Action.pm

filter: Statementstmt 65.5% 112 / 171 Branchbran 28.2% 13 / 46 Conditioncond 7.8% 3 / 38 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 66.1% 39 / 59 Podpod 0.0% 0 / 17 totaltotal 47.5% 167 / 351 CC 41 SCAR 24.1
CC
41
Cov
50%
_handle_event
52.8
_handle_event
42.0
_handle_event
20.0
CRAP 11.1
« blib/lib/SignalWire/Agent/AgentBase.pm ↑ Summary blib/lib/SignalWire/SWML/Service.pm »
1 package SignalWire::Relay::Action;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # Base Action class for long-running RELAY operations.
7 # Tracks control_id, completion state, and supports blocking wait.
8  
9 4 has 'control_id' => ( is => 'ro', required => 1 );
10 16 ▶has 'call_id' => ( is => 'ro', default => sub { '' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
11 16 ▶has 'node_id' => ( is => 'ro', default => sub { '' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
12 18 ▶has 'state' => ( is => 'rw', default => sub { 'created' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
13 18 ▶has 'completed' => ( is => 'rw', default => sub { 0 } ); # boolean
Subroutine __ANON__: called CC=1 SCAR=0.0
14 18 ▶has 'result' => ( is => 'rw', default => sub { undef } );
Subroutine __ANON__: called CC=1 SCAR=0.0
15 18 ▶has 'events' => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
16 18 ▶has 'payload' => ( is => 'rw', default => sub { {} } ); # latest event payload
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18 18 ▶has '_on_completed' => ( is => 'rw', default => sub { undef } );
Subroutine __ANON__: called CC=1 SCAR=0.0
19 18 ▶has '_client' => ( is => 'rw', default => sub { undef } );
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 # Register on_completed callback
22 3 ▶sub on_completed {
Subroutine on_completed: called CC=4 SCAR=14.2
Pod undocumented
23 3 my ($self, $cb) = @_;
24 3 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 3 0 3
25 3 $self->_on_completed($cb);
26 # If already done, fire immediately
27 3 ▶ if ($self->completed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->completed) 1 2 3
28 1 eval { $cb->($self) };
29 1 ▶ warn "on_completed callback error: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
30 }
31 3 return $self;
32 }
33 0 return $self->_on_completed;
34 }
35  
36 # Check if the action is done
37 4 ▶sub is_done {
Subroutine is_done: called CC=1 SCAR=0.0
Pod undocumented
38 4 my ($self) = @_;
39 4 return $self->completed;
40 }
41  
42 # Blocking wait using select() polling loop
43 1 ▶sub wait {
Subroutine wait: called CC=4 SCAR=15.9
Pod undocumented
44 1 my ($self, %opts) = @_;
45 1 ▶ my $timeout = $opts{timeout} || 30;
Condition50%1/2
$opts{'timeout'} || 30
l!l
10
MC/DC0%0/1
$opts{'timeout'} || 30
$opts{'timeout'}
Truth table50%1/2
A
A= $opts{'timeout'}
Aresult
11
00
46 1 my $start = time();
47 1 ▶ while (!$self->completed && (time() - $start) < $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->completed and time - $start < $timeout) 0 1 1
Condition33%1/3
not $self->completed and time - $start < $timeout
!ll&&!rl&&r
100
MC/DC0%0/2
not $self->completed and time - $start < $timeout
not $self->completed time - $start < $timeout
Truth table33%1/3
not(A) and B
A= not $self->completedB= time - $start < $timeout
ABresult
0X0
100
111
48 0 select(undef, undef, undef, 0.1); # sleep 100ms
49 }
50 1 return $self->result;
51 }
52  
53 # Called by event dispatch when an event is received for this action
54 0 ▶sub _handle_event {
Subroutine _handle_event: not called CC=4 SCAR=30.0
55 0 my ($self, $event) = @_;
56 0 push @{$self->events}, $event;
57 0 ▶ $self->payload($event->params // {});
Condition0%0/2
$event->params // {}
l!l
00
MC/DC0%0/1
$event->params // {}
$event->params
Truth table0%0/2
A
A= $event->params
Aresult
11
00
58  
59 0 ▶ my $state = $event->can('state') ? $event->state : '';
Branch: 1 branch
BranchTrueFalseTotal
$event->can('state') ? : 0 0 0
60 0 ▶ $self->state($state) if $state;
Branch: 1 branch
BranchTrueFalseTotal
if $state 0 0 0
61 }
62  
63 # Mark the action as completed with a result
64 7 ▶sub _resolve {
Subroutine _resolve: called CC=4 SCAR=13.9
65 7 my ($self, $result) = @_;
66 7 ▶ return if $self->completed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->completed 1 6 7
67 6 $self->completed(1);
68 6 $self->result($result);
69  
70 6 ▶ if (my $cb = $self->_on_completed) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->_on_completed) 2 4 6
71 2 eval { $cb->($self) };
72 2 ▶ warn "on_completed callback error: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
73 }
74 }
75  
76 # Send a sub-command on this action (e.g., play.stop, record.pause)
77 0 ▶sub _execute_subcommand {
Subroutine _execute_subcommand: not called CC=2 SCAR=17.9
78 0 my ($self, $method) = @_;
79 0 my $client = $self->_client;
80 0 ▶ return unless $client;
Branch: 1 branch
BranchTrueFalseTotal
unless $client 0 0 0
81 0 return $client->execute($method, {
82 node_id => $self->node_id,
83 call_id => $self->call_id,
84 control_id => $self->control_id,
85 });
86 }
87  
88 # Stop the action
89 0 ▶sub stop {
Subroutine stop: not called CC=2 SCAR=17.9
Pod undocumented
90 0 my ($self) = @_;
91 0 ▶ return if $self->completed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->completed 0 0 0
92 0 return $self->_execute_subcommand($self->_stop_method);
93 }
94  
95 # Override in subclasses
96 0 ▶sub _stop_method { return '' }
Subroutine _stop_method: not called CC=1 SCAR=6.9
97  
98 # --- PlayAction ---
99 package SignalWire::Relay::Action::Play;
100 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 4 extends 'SignalWire::Relay::Action';
102  
103 1 ▶sub _stop_method { 'calling.play.stop' }
Subroutine _stop_method: called CC=1 SCAR=0.0
104  
105 0 ▶sub pause {
Subroutine pause: not called CC=1 SCAR=6.9
Pod undocumented
106 0 my ($self) = @_;
107 0 return $self->_execute_subcommand('calling.play.pause');
108 }
109  
110 0 ▶sub resume {
Subroutine resume: not called CC=1 SCAR=6.9
Pod undocumented
111 0 my ($self) = @_;
112 0 return $self->_execute_subcommand('calling.play.resume');
113 }
114  
115 0 ▶sub volume {
Subroutine volume: not called CC=2 SCAR=17.9
Pod undocumented
116 0 my ($self, $vol) = @_;
117 0 my $client = $self->_client;
118 0 ▶ return unless $client;
Branch: 1 branch
BranchTrueFalseTotal
unless $client 0 0 0
119 0 return $client->execute('calling.play.volume', {
120 node_id => $self->node_id,
121 call_id => $self->call_id,
122 control_id => $self->control_id,
123 volume => $vol,
124 });
125 }
126  
127 # --- RecordAction ---
128 package SignalWire::Relay::Action::Record;
129 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
130 4 extends 'SignalWire::Relay::Action';
131  
132 1 ▶sub _stop_method { 'calling.record.stop' }
Subroutine _stop_method: called CC=1 SCAR=0.0
133  
134 0 ▶sub pause {
Subroutine pause: not called CC=3 SCAR=24.8
Pod undocumented
135 0 my ($self, %opts) = @_;
136 0 my $client = $self->_client;
137 0 ▶ return unless $client;
Branch: 1 branch
BranchTrueFalseTotal
unless $client 0 0 0
138 0 my $params = {
139 node_id => $self->node_id,
140 call_id => $self->call_id,
141 control_id => $self->control_id,
142 };
143 0 ▶ $params->{behavior} = $opts{behavior} if $opts{behavior};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"behavior"} 0 0 0
144 0 return $client->execute('calling.record.pause', $params);
145 }
146  
147 0 ▶sub resume {
Subroutine resume: not called CC=1 SCAR=6.9
Pod undocumented
148 0 my ($self) = @_;
149 0 return $self->_execute_subcommand('calling.record.resume');
150 }
151  
152 # Result accessors
153 0 ▶sub url { $_[0]->payload->{url} // '' }
Subroutine url: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
$_[0]->payload->{"url"} // ""
l!l
00
MC/DC0%0/1
$_[0]->payload->{"url"} // ""
$_[0]->payload->{"url"}
Truth table0%0/2
A
A= $_[0]->payload->{"url"}
Aresult
11
00
154 0 ▶sub duration { $_[0]->payload->{duration} // 0 }
Subroutine duration: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
$_[0]->payload->{"duration"} // 0
l!l
00
MC/DC0%0/1
$_[0]->payload->{"duration"} // 0
$_[0]->payload->{"duration"}
Truth table0%0/2
A
A= $_[0]->payload->{"duration"}
Aresult
11
00
155 0 ▶sub size { $_[0]->payload->{size} // 0 }
Subroutine size: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
$_[0]->payload->{"size"} // 0
l!l
00
MC/DC0%0/1
$_[0]->payload->{"size"} // 0
$_[0]->payload->{"size"}
Truth table0%0/2
A
A= $_[0]->payload->{"size"}
Aresult
11
00
156  
157 # --- DetectAction ---
158 package SignalWire::Relay::Action::Detect;
159 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
160 4 extends 'SignalWire::Relay::Action';
161  
162 1 ▶sub _stop_method { 'calling.detect.stop' }
Subroutine _stop_method: called CC=1 SCAR=0.0
163  
164 0 ▶sub detect_result { $_[0]->payload->{detect} // {} }
Subroutine detect_result: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
$_[0]->payload->{"detect"} // {}
l!l
00
MC/DC0%0/1
$_[0]->payload->{"detect"} // {}
$_[0]->payload->{"detect"}
Truth table0%0/2
A
A= $_[0]->payload->{"detect"}
Aresult
11
00
165  
166 # Detect resolves on the FIRST `params.detect` payload (the actual
167 # detection result), not on a state(finished). Mirror Python's
168 # ``DetectAction._check_event``.
169 0 ▶sub _handle_event {
Subroutine _handle_event: not called CC=6 SCAR=37.4
170 0 my ($self, $event) = @_;
171 0 $self->SUPER::_handle_event($event);
172 0 ▶ return if $self->completed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->completed 0 0 0
173 0 ▶ my $params = $event->params // {};
Condition0%0/2
$event->params // {}
l!l
00
MC/DC0%0/1
$event->params // {}
$event->params
Truth table0%0/2
A
A= $event->params
Aresult
11
00
174 0 ▶ if (ref $params eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params eq "HASH" and ref $params->{"detect"} eq "HASH" and %{$params->{"detect"};}) 0 0 0
Condition0%0/6
ref $params eq "HASH" and ref $params->{"detect"} eq "HASH" and %{$params->{"detect"};}
!ll&&!rl&&r
000
ref $params eq "HASH" and ref $params->{"detect"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
ref $params eq "HASH" and ref $params->{"detect"} eq "HASH" and %{$params->{"detect"};}
ref $params eq "HASH" ref $params->{"detect"} eq "HASH" %{$params->{"detect"};}
Truth table0%0/4
A and B and C
A= ref $params eq "HASH"B= ref $params->{"detect"} eq "HASH"C= %{$params->{"detect"};}
ABCresult
0XX0
10X0
1100
1111
175 && ref $params->{detect} eq 'HASH'
176 0 && %{$params->{detect}})
177 {
178 0 $self->_resolve($event);
179 }
180 }
181  
182 # --- CollectAction (used by play_and_collect) ---
183 package SignalWire::Relay::Action::Collect;
184 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
185 4 extends 'SignalWire::Relay::Action';
186  
187 # play_and_collect's stop verb is calling.play_and_collect.stop, not
188 # calling.collect.stop. The standalone collect uses StandaloneCollect
189 # below.
190 1 ▶sub _stop_method { 'calling.play_and_collect.stop' }
Subroutine _stop_method: called CC=1 SCAR=0.0
191  
192 0 ▶sub start_input_timers {
Subroutine start_input_timers: not called CC=1 SCAR=6.9
Pod undocumented
193 0 my ($self) = @_;
194 0 return $self->_execute_subcommand('calling.collect.start_input_timers');
195 }
196  
197 0 ▶sub collect_result { $_[0]->payload->{result} // {} }
Subroutine collect_result: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
$_[0]->payload->{"result"} // {}
l!l
00
MC/DC0%0/1
$_[0]->payload->{"result"} // {}
$_[0]->payload->{"result"}
Truth table0%0/2
A
A= $_[0]->payload->{"result"}
Aresult
11
00
198  
199 # Override event handling: for play_and_collect, ignore play events
200 # (play(finished) must NOT resolve a play_and_collect; only the collect
201 # terminal event should — see RELAY_IMPLEMENTATION_GUIDE). Resolves on a
202 # calling.call.collect event that carries a result, or a state in the
203 # terminal-state map.
204 1 ▶sub _handle_event {
Subroutine _handle_event: called CC=9 SCAR=39.7
205 1 my ($self, $event) = @_;
206 # Defense-in-depth: even if a caller hands us a play event (e.g. the
207 # legacy unit test that drives the action directly), we drop it so
208 # state doesn't update.
209 1 ▶ if (($event->event_type // '') eq 'calling.call.play') {
Branch: 1 branch
BranchTrueFalseTotal
if (($event->event_type // "") eq "calling.call.play") 1 0 1
Condition50%1/2
$event->event_type // ""
l!l
10
MC/DC0%0/1
$event->event_type // ""
$event->event_type
Truth table50%1/2
A
A= $event->event_type
Aresult
11
00
210 1 return;
211 }
212 0 $self->SUPER::_handle_event($event);
213 0 ▶ return if $self->completed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->completed 0 0 0
214 0 ▶ if ($event->event_type eq 'calling.call.collect') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->event_type eq "calling.call.collect") 0 0 0
215 0 ▶ my $params = $event->params // {};
Condition0%0/2
$event->params // {}
l!l
00
MC/DC0%0/1
$event->params // {}
$event->params
Truth table0%0/2
A
A= $event->params
Aresult
11
00
216 0 ▶ my $result = ref $params eq 'HASH' ? $params->{result} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $params eq 'HASH' ? : 0 0 0
217 0 ▶ if (ref $result eq 'HASH' && %$result) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result eq "HASH" and %$result) 0 0 0
Condition0%0/3
ref $result eq "HASH" and %$result
!ll&&!rl&&r
000
MC/DC0%0/2
ref $result eq "HASH" and %$result
ref $result eq "HASH" %$result
Truth table0%0/3
A and B
A= ref $result eq "HASH"B= %$result
ABresult
0X0
100
111
218 0 $self->_resolve($event);
219 }
220 }
221 }
222  
223 # Filter calling.call.play events: Call's dispatcher consults this method
224 # before handing the event to the action so play events neither dispatch
225 # nor (more importantly) trigger terminal-state auto-resolve.
226 0 ▶sub _should_consume_event {
Subroutine _should_consume_event: not called CC=3 SCAR=24.8
227 0 my ($self, $event) = @_;
228 0 ▶ return 0 if ($event->event_type // '') eq 'calling.call.play';
Branch: 1 branch
BranchTrueFalseTotal
if ($event->event_type // "") eq "calling.call.play" 0 0 0
Condition0%0/2
$event->event_type // ""
l!l
00
MC/DC0%0/1
$event->event_type // ""
$event->event_type
Truth table0%0/2
A
A= $event->event_type
Aresult
11
00
229 0 return 1;
230 }
231  
232 # --- StandaloneCollectAction ---
233 package SignalWire::Relay::Action::StandaloneCollect;
234 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
235 4 extends 'SignalWire::Relay::Action::Collect';
236  
237 # Standalone collect uses calling.collect.stop, not the
238 # play_and_collect.stop variant.
239 1 ▶sub _stop_method { 'calling.collect.stop' }
Subroutine _stop_method: called CC=1 SCAR=0.0
240  
241 # --- FaxAction ---
242 package SignalWire::Relay::Action::Fax;
243 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
244 4 extends 'SignalWire::Relay::Action';
245  
246 0 ▶has '_fax_type' => ( is => 'ro', default => sub { 'send' } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
247  
248 2 ▶sub _stop_method {
Subroutine _stop_method: called CC=2 SCAR=6.9
249 2 my ($self) = @_;
250 2 ▶ return $self->_fax_type eq 'receive'
Branch: 1 branch
BranchTrueFalseTotal
$self->_fax_type eq 'receive' ? : 1 1 2
251 ? 'calling.receive_fax.stop'
252 : 'calling.send_fax.stop';
253 }
254  
255 0 ▶sub fax_result { $_[0]->payload->{fax} // {} }
Subroutine fax_result: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
$_[0]->payload->{"fax"} // {}
l!l
00
MC/DC0%0/1
$_[0]->payload->{"fax"} // {}
$_[0]->payload->{"fax"}
Truth table0%0/2
A
A= $_[0]->payload->{"fax"}
Aresult
11
00
256  
257 # --- TapAction ---
258 package SignalWire::Relay::Action::Tap;
259 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
260 4 extends 'SignalWire::Relay::Action';
261  
262 1 ▶sub _stop_method { 'calling.tap.stop' }
Subroutine _stop_method: called CC=1 SCAR=0.0
263  
264 # --- StreamAction ---
265 package SignalWire::Relay::Action::Stream;
266 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
267 4 extends 'SignalWire::Relay::Action';
268  
269 1 ▶sub _stop_method { 'calling.stream.stop' }
Subroutine _stop_method: called CC=1 SCAR=0.0
270  
271 # --- PayAction ---
272 package SignalWire::Relay::Action::Pay;
273 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
274 4 extends 'SignalWire::Relay::Action';
275  
276 1 ▶sub _stop_method { 'calling.pay.stop' }
Subroutine _stop_method: called CC=1 SCAR=0.0
277  
278 0 ▶sub pay_result { $_[0]->payload->{result} // {} }
Subroutine pay_result: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
$_[0]->payload->{"result"} // {}
l!l
00
MC/DC0%0/1
$_[0]->payload->{"result"} // {}
$_[0]->payload->{"result"}
Truth table0%0/2
A
A= $_[0]->payload->{"result"}
Aresult
11
00
279  
280 # --- TranscribeAction ---
281 package SignalWire::Relay::Action::Transcribe;
282 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
283 4 extends 'SignalWire::Relay::Action';
284  
285 1 ▶sub _stop_method { 'calling.transcribe.stop' }
Subroutine _stop_method: called CC=1 SCAR=0.0
286  
287 # --- AIAction ---
288 package SignalWire::Relay::Action::AI;
289 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
290 4 extends 'SignalWire::Relay::Action';
291  
292 1 ▶sub _stop_method { 'calling.ai.stop' }
Subroutine _stop_method: called CC=1 SCAR=0.0
293  
294 4 1;
« blib/lib/SignalWire/Agent/AgentBase.pm ↑ Summary blib/lib/SignalWire/SWML/Service.pm »