bin/swaig-test

filter: Statementstmt 44.2% 111 / 251 Branchbran 26.6% 33 / 124 Conditioncond 30.4% 21 / 69 MC/DCmcdc 15.3% 6 / 39 Subroutinesub 65.3% 17 / 26 Podpod n/a totaltotal 36.9% 188 / 509 CC 85 SCAR 38.0
CC
85
Cov
37%
do_list_tools
132.0
do_exec
90.0
_resolve_service_instance
75.4
CRAP 44.8
« blib/lib/SignalWire/Security/WebhookMiddleware.pm ↑ Summary blib/lib/SignalWire/Relay/Call.pm »
1 #!/usr/bin/env perl
2 # Copyright (c) 2025 SignalWire
3 # Licensed under the MIT License.
4 #
5 # swaig-test - CLI tool for testing SWAIG agent endpoints
6 #
7 # Usage:
8 # swaig-test --url http://user:pass@host:port/route --dump-swml
9 # swaig-test --url http://user:pass@host:port/route --list-tools
10 # swaig-test --url http://user:pass@host:port/route --exec tool_name --param key=value
11 #
12 # File mode (no HTTP — load a Perl example/script in-process and read
13 # the runtime SWAIG tool registry):
14 # swaig-test --file examples/swmlservice_swaig_standalone.pl --list-tools
15 #
16  
17 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 10 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 10 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 10 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 10 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 10 my $VERSION = '1.0.0';
28  
29 # --- Parse CLI options ---
30 10 my ($url, $file, $dump_swml, $list_tools, $exec_name, @params, $raw, $verbose, $help);
31  
32 10 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("url=s", \$url, "file=s", \$file, "dump-swml", \$dump_swml, "list-tools", \$list_tools, "exec=s", \$exec_name, "param=s\@", \@params, "raw", \$raw, "verbose", \$verbose, "help|h", \$help) or usage_exit() 10 0 10
33 'url=s' => \$url,
34 'file=s' => \$file,
35 'dump-swml' => \$dump_swml,
36 'list-tools' => \$list_tools,
37 'exec=s' => \$exec_name,
38 'param=s@' => \@params,
39 'raw' => \$raw,
40 'verbose' => \$verbose,
41 'help|h' => \$help,
42 ) or usage_exit();
43  
44 10 ▶if ($help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 2 8 10
45 2 usage_exit(0);
46 }
47  
48 8 ▶unless ($url || $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url or $file) 1 7 8
Condition100%3/3
$url or $file
l!l&&r!l&&!r
251
MC/DC100%2/2
$url or $file
$url $file
Truth table100%3/3
A or B
A= $urlB= $file
ABresult
1X1
011
000
49 1 print STDERR "Error: --url or --file is required\n\n";
50 1 usage_exit(1);
51 }
52  
53 7 ▶if ($url && $file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url and $file) 1 6 7
Condition100%3/3
$url and $file
!ll&&!rl&&r
511
MC/DC100%2/2
$url and $file
$url $file
Truth table100%3/3
A and B
A= $urlB= $file
ABresult
0X0
100
111
54 1 print STDERR "Error: --url and --file are mutually exclusive\n\n";
55 1 usage_exit(1);
56 }
57  
58 6 ▶unless ($dump_swml || $list_tools || $exec_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dump_swml or $list_tools or $exec_name) 2 4 6
Condition83%5/6
$dump_swml or $list_tools or $exec_name
l!l&&r!l&&!r
312
$dump_swml or $list_tools
l!l&&r!l&&!r
033
MC/DC66%2/3
$dump_swml or $list_tools or $exec_name
$dump_swml $list_tools $exec_name
Truth table75%3/4
A or B or C
A= $dump_swmlB= $list_toolsC= $exec_name
ABCresult
1XX1
01X1
0011
0000
59 2 print STDERR "Error: one of --dump-swml, --list-tools, or --exec NAME is required\n\n";
60 2 usage_exit(1);
61 }
62  
63 # --- File mode: load Perl script in-process and walk runtime tool registry ---
64 4 ▶if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 4 0 4
65 4 do_file_mode();
66 3 exit 0;
67 }
68  
69 # --- URL mode: HTTP path ---
70 0 my $uri = URI->new($url);
71 0 ▶my $userinfo = $uri->userinfo // '';
Condition0%0/2
$uri->userinfo // ""
l!l
00
MC/DC0%0/1
$uri->userinfo // ""
$uri->userinfo
Truth table0%0/2
A
A= $uri->userinfo
Aresult
11
00
72 0 my ($auth_user, $auth_pass) = split(/:/, $userinfo, 2);
73  
74 # Build base URL without credentials
75 0 my $clean_uri = $uri->clone;
76 0 $clean_uri->userinfo(undef);
77 0 my $base_url = $clean_uri->as_string;
78  
79 # Remove trailing slash
80 0 $base_url =~ s{/$}{};
81  
82 0 my $http = HTTP::Tiny->new(timeout => 30);
83  
84 # --- Execute the requested operation ---
85  
86 0 ▶if ($dump_swml) {
Branch: 3 branches
BranchTrueFalseTotal
if ($dump_swml) { } 0 0 0
elsif ($list_tools) { } 0 0 0
elsif ($exec_name) { } 0 0 0
87 0 do_dump_swml();
88 }
89 elsif ($list_tools) {
90 0 do_list_tools();
91 }
92 elsif ($exec_name) {
93 0 do_exec($exec_name);
94 }
95  
96 0 exit 0;
97  
98 # ============================================================
99 # Operations
100 # ============================================================
101  
102 0 ▶sub do_dump_swml {
Subroutine do_dump_swml: not called CC=4 SCAR=30.0
103 0 my $response = http_get($base_url);
104 0 ▶ if ($raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw) { } 0 0 0
105 0 print $response->{content};
106 0 ▶ print "\n" unless $response->{content} =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"content"} =~ /\n$/ 0 0 0
107 } else {
108 0 my $data = eval { JSON::decode_json($response->{content}) };
109 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
110 0 die "Error: Failed to parse JSON response: $@\n";
111 }
112 0 print JSON->new->utf8->pretty->canonical->encode($data);
113 }
114 }
115  
116 0 ▶sub do_list_tools {
Subroutine do_list_tools: not called CC=11 SCAR=48.8
117 0 my $response = http_get($base_url);
118 0 my $data = eval { JSON::decode_json($response->{content}) };
119 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
120 0 die "Error: Failed to parse JSON response: $@\n";
121 }
122  
123 # Extract SWAIG functions from the SWML document
124 0 my @functions;
125  
126 # Navigate the SWML structure to find AI verb functions
127 0 ▶ if (my $sections = $data->{sections}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sections = $data->{"sections"}) 0 0 0
128 0 for my $section_name (keys %$sections) {
129 0 my $verbs = $sections->{$section_name};
130 0 ▶ next unless ref $verbs eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $verbs eq "ARRAY" 0 0 0
131 0 for my $verb (@$verbs) {
132 0 ▶ if (ref $verb eq 'HASH' && exists $verb->{ai}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $verb eq "HASH" and exists $verb->{"ai"}) 0 0 0
Condition0%0/3
ref $verb eq "HASH" and exists $verb->{"ai"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $verb eq "HASH" and exists $verb->{"ai"}
ref $verb eq "HASH" exists $verb->{"ai"}
Truth table0%0/3
A and B
A= ref $verb eq "HASH"B= exists $verb->{"ai"}
ABresult
0X0
100
111
133 0 my $ai = $verb->{ai};
134 0 ▶ if (my $swaig = $ai->{SWAIG}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $swaig = $ai->{"SWAIG"}) 0 0 0
135 0 ▶ if (my $funcs = $swaig->{functions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $funcs = $swaig->{"functions"}) 0 0 0
136 0 push @functions, @$funcs;
137 }
138 }
139 }
140 }
141 }
142 }
143  
144 0 ▶ if (!@functions) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@functions) 0 0 0
145 0 print "No SWAIG functions found.\n";
146 0 return;
147 }
148  
149 0 print_tool_list(\@functions);
150 }
151  
152 0 ▶sub do_exec {
Subroutine do_exec: not called CC=9 SCAR=45.0
153 0 my ($func_name) = @_;
154  
155 # Parse --param key=value pairs
156 0 my %args;
157 0 for my $p (@params) {
158 0 ▶ if ($p =~ /^([^=]+)=(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p =~ /^([^=]+)=(.*)$/) { } 0 0 0
159 0 $args{$1} = $2;
160 } else {
161 0 die "Error: Invalid --param format '$p'. Use key=value\n";
162 }
163 }
164  
165 0 my $swaig_url = $base_url . '/swaig';
166  
167 0 my $payload = {
168 function => $func_name,
169 argument => {
170 parsed => [ \%args ],
171 },
172 };
173  
174 0 my $json_body = JSON::encode_json($payload);
175  
176 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
177 0 print STDERR ">>> POST $swaig_url\n";
178 0 print STDERR ">>> Body: $json_body\n";
179 }
180  
181 0 my $response = http_post($swaig_url, $json_body);
182  
183 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
184 0 print STDERR "<<< Status: $response->{status}\n";
185 0 print STDERR "<<< Body: $response->{content}\n";
186 }
187  
188 0 ▶ if ($raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw) { } 0 0 0
189 0 print $response->{content};
190 0 ▶ print "\n" unless $response->{content} =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"content"} =~ /\n$/ 0 0 0
191 } else {
192 0 my $data = eval { JSON::decode_json($response->{content}) };
193 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
194 0 print $response->{content};
195 0 ▶ print "\n" unless $response->{content} =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"content"} =~ /\n$/ 0 0 0
196 } else {
197 0 print JSON->new->utf8->pretty->canonical->encode($data);
198 }
199 }
200 }
201  
202 # ============================================================
203 # File mode: in-process loader
204 # ============================================================
205  
206 4 ▶sub do_file_mode {
Subroutine do_file_mode: called CC=10 SCAR=26.9
207 4 ▶ unless (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 1 3 4
208 1 die "Error: --file '$file' does not exist or is not a regular file\n";
209 }
210  
211 # Resolve to absolute path so `do` doesn't depend on @INC.
212 3 my $abs = File::Spec->rel2abs($file);
213  
214 3 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 3 3
215 0 print STDERR ">>> do '$abs'\n";
216 }
217  
218 # Run the file. The example scripts guard their server-start with
219 # `unless caller`, so loading via `do` does NOT spin a server.
220 3 my $rv = do $abs;
221 3 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 3 3
222 0 die "Error: failed to compile '$file': $e\n";
223 }
224 3 ▶ if (!defined $rv && $!) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv and $!) 0 3 3
Condition33%1/3
not defined $rv and $!
!ll&&!rl&&r
300
MC/DC0%0/2
not defined $rv and $!
not defined $rv $!
Truth table33%1/3
not(A) and B
A= not defined $rvB= $!
ABresult
0X0
100
111
225 0 die "Error: failed to read '$file': $!\n";
226 }
227  
228 3 my $svc = _resolve_service_instance($rv);
229 3 ▶ unless ($svc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($svc) 0 3 3
230 0 die "Error: could not locate a SignalWire::SWML::Service instance in '$file'.\n"
231 . "Hint: define a build_service() sub that returns an instance, return one as\n"
232 . "the file's last value, or declare a package that ISA SignalWire::SWML::Service.\n";
233 }
234  
235 3 ▶ if ($dump_swml) {
Branch: 3 branches
BranchTrueFalseTotal
if ($dump_swml) { } 0 3 3
elsif ($list_tools) { } 2 1 3
elsif ($exec_name) { } 1 0 1
236 0 do_file_dump_swml($svc);
237 }
238 elsif ($list_tools) {
239 2 do_file_list_tools($svc);
240 }
241 elsif ($exec_name) {
242 1 do_file_exec($svc, $exec_name);
243 }
244 }
245  
246 # Resolve a Service instance from the loaded script. Strategy, in order:
247 # 1. If the file's last value (the result of `do`) is itself a
248 # SignalWire::SWML::Service object, use that.
249 # 2. If main::build_service exists, call it.
250 # 3. Scan %:: for packages that ISA SignalWire::SWML::Service (skip
251 # Service and AgentBase themselves) and instantiate one.
252 3 ▶sub _resolve_service_instance {
Subroutine _resolve_service_instance: called CC=13 SCAR=43.2
253 3 my ($do_rv) = @_;
254  
255 3 ▶ if (blessed($do_rv) && $do_rv->isa('SignalWire::SWML::Service')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($do_rv) and $do_rv->isa("SignalWire::SWML::Service")) 0 3 3
Condition33%1/3
builtin::blessed($do_rv) and $do_rv->isa("SignalWire::SWML::Service")
!ll&&!rl&&r
300
MC/DC0%0/2
builtin::blessed($do_rv) and $do_rv->isa("SignalWire::SWML::Service")
builtin::blessed($do_rv) $do_rv->isa("SignalWire::SWML::Service")
Truth table33%1/3
A and B
A= builtin::blessed($do_rv)B= $do_rv->isa("SignalWire::SWML::Service")
ABresult
0X0
100
111
256 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
257 0 print STDERR ">>> using service instance returned by file (" . ref($do_rv) . ")\n";
258 }
259 0 return $do_rv;
260 }
261  
262 3 ▶ if (my $builder = main->can('build_service')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $builder = "main"->can("build_service")) 3 0 3
263 3 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 3 3
264 0 print STDERR ">>> calling main::build_service\n";
265 }
266 3 my $svc = $builder->();
267 3 ▶ if (blessed($svc) && $svc->isa('SignalWire::SWML::Service')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($svc) and $svc->isa("SignalWire::SWML::Service")) 3 0 3
Condition33%1/3
builtin::blessed($svc) and $svc->isa("SignalWire::SWML::Service")
!ll&&!rl&&r
003
MC/DC0%0/2
builtin::blessed($svc) and $svc->isa("SignalWire::SWML::Service")
builtin::blessed($svc) $svc->isa("SignalWire::SWML::Service")
Truth table33%1/3
A and B
A= builtin::blessed($svc)B= $svc->isa("SignalWire::SWML::Service")
ABresult
0X0
100
111
268 3 return $svc;
269 }
270 }
271  
272 # Walk the symbol table for user subclasses of Service.
273 0 my @candidates = _find_service_subclasses();
274 0 for my $pkg (@candidates) {
275 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
276 0 print STDERR ">>> attempting parameterless construction of $pkg\n";
277 }
278 0 my $svc = eval { $pkg->new };
279 0 ▶ if (!$@ && blessed($svc) && $svc->isa('SignalWire::SWML::Service')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ and builtin::blessed($svc) and $svc->isa("SignalWire::SWML::Service")) 0 0 0
Condition0%0/6
not $@ and builtin::blessed($svc) and $svc->isa("SignalWire::SWML::Service")
!ll&&!rl&&r
000
not $@ and builtin::blessed($svc)
!ll&&!rl&&r
000
MC/DC0%0/3
not $@ and builtin::blessed($svc) and $svc->isa("SignalWire::SWML::Service")
not $@ builtin::blessed($svc) $svc->isa("SignalWire::SWML::Service")
Truth table0%0/4
not(A) and B and C
A= not $@B= builtin::blessed($svc)C= $svc->isa("SignalWire::SWML::Service")
ABCresult
0XX0
10X0
1100
1111
280 0 return $svc;
281 }
282 }
283  
284 0 return undef;
285 }
286  
287 0 ▶sub _find_service_subclasses {
Subroutine _find_service_subclasses: not called CC=1 SCAR=5.0
288 0 my @found;
289 my %seen;
290  
291 # Recursive walk of %:: → all loaded packages.
292 0 my $walk;
293 0 ▶ $walk = sub {
Subroutine __ANON__: not called CC=10 SCAR=42.6
294 0 my ($stash, $prefix) = @_;
295 0 for my $entry (keys %$stash) {
296 0 ▶ next unless $entry =~ /::$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry =~ /::$/ 0 0 0
297 0 my $name = $entry;
298 0 $name =~ s/::$//;
299 0 ▶ next if $name eq '' || $name eq 'main';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "" or $name eq "main" 0 0 0
Condition0%0/3
$name eq "" or $name eq "main"
l!l&&r!l&&!r
000
MC/DC0%0/2
$name eq "" or $name eq "main"
$name eq "" $name eq "main"
Truth table0%0/3
A or B
A= $name eq ""B= $name eq "main"
ABresult
1X1
011
000
300 0 ▶ my $pkg = $prefix ? "${prefix}::${name}" : $name;
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 0 0
301 0 ▶ next if $seen{$pkg}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$pkg}++ 0 0 0
302  
303 # Only treat as a class if it has any symbols at all.
304 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
305 0 my $sub_stash = \%{ "${pkg}::" };
306 10 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
307  
308 0 ▶ if (eval { $pkg->isa('SignalWire::SWML::Service') }
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $pkg->isa("SignalWire::SWML::Service") } } and $pkg ne "SignalWire::SWML::Service" and $pkg ne "SignalWire::Agent::AgentBase") 0 0 0
Condition0%0/6
eval { do { $pkg->isa("SignalWire::SWML::Service") } } and $pkg ne "SignalWire::SWML::Service" and $pkg ne "SignalWire::Agent::AgentBase"
!ll&&!rl&&r
000
eval { do { $pkg->isa("SignalWire::SWML::Service") } } and $pkg ne "SignalWire::SWML::Service"
!ll&&!rl&&r
000
MC/DC0%0/3
eval { do { $pkg->isa("SignalWire::SWML::Service") } } and $pkg ne "SignalWire::SWML::Service" and $pkg ne "SignalWire::Agent::AgentBase"
eval { do { $pkg->isa("SignalWire::SWML::Service") } } $pkg ne "SignalWire::SWML::Service" $pkg ne "SignalWire::Agent::AgentBase"
Truth table0%0/4
A and B and C
A= eval { do { $pkg->isa("SignalWire::SWML::Service") } }B= $pkg ne "SignalWire::SWML::Service"C= $pkg ne "SignalWire::Agent::AgentBase"
ABCresult
0XX0
10X0
1100
1111
309 && $pkg ne 'SignalWire::SWML::Service'
310 && $pkg ne 'SignalWire::Agent::AgentBase')
311 {
312 0 push @found, $pkg;
313 }
314 0 $walk->($sub_stash, $pkg);
315 }
316 0 };
317 0 $walk->(\%::, '');
318 0 return @found;
319 }
320  
321 0 ▶sub do_file_dump_swml {
Subroutine do_file_dump_swml: not called CC=2 SCAR=17.9
322 0 my ($svc) = @_;
323 # Render the SWML document the same way the PSGI handler does. We
324 # pass a minimal env hash so dynamic renderers don't blow up.
325 0 my $doc = $svc->render_main_swml({});
326 0 ▶ if ($raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw) { } 0 0 0
327 0 print JSON::encode_json($doc), "\n";
328 } else {
329 0 print JSON->new->utf8->pretty->canonical->encode($doc);
330 }
331 }
332  
333 2 ▶sub do_file_list_tools {
Subroutine do_file_list_tools: called CC=3 SCAR=11.8
334 2 my ($svc) = @_;
335 2 my @names = $svc->list_tool_names;
336 2 ▶ my $registry = $svc->tools // {};
Condition50%1/2
$svc->tools // {}
l!l
20
MC/DC0%0/1
$svc->tools // {}
$svc->tools
Truth table50%1/2
A
A= $svc->tools
Aresult
11
00
337  
338 2 ▶ if (!@names) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 0 2 2
339 0 print "No SWAIG functions found.\n";
340 0 return;
341 }
342  
343 2 my @functions = map { $registry->{$_} } @names;
344 2 print_tool_list(\@functions);
345 }
346  
347 1 ▶sub do_file_exec {
Subroutine do_file_exec: called CC=8 SCAR=26.3
348 1 my ($svc, $func_name) = @_;
349  
350 1 my %args;
351 1 for my $p (@params) {
352 1 ▶ if ($p =~ /^([^=]+)=(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p =~ /^([^=]+)=(.*)$/) { } 1 0 1
353 1 $args{$1} = $2;
354 } else {
355 0 die "Error: Invalid --param format '$p'. Use key=value\n";
356 }
357 }
358  
359 1 my $payload = {
360 function => $func_name,
361 argument => { parsed => [ \%args ] },
362 };
363  
364 1 my $result = $svc->on_function_call($func_name, \%args, $payload);
365 1 ▶ unless (defined $result) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 0 1 1
366 0 die "Error: function '$func_name' is not registered or returned undef.\n";
367 }
368  
369 1 my $hash;
370 1 ▶ if (ref $result eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $result eq 'HASH') { } 1 0 1
elsif (builtin::blessed($result) and $result->can('to_hash')) { } 0 0 0
371 1 $hash = $result;
372 ▶ } elsif (blessed($result) && $result->can('to_hash')) {
Condition0%0/3
builtin::blessed($result) and $result->can("to_hash")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($result) and $result->can("to_hash")
builtin::blessed($result) $result->can("to_hash")
Truth table0%0/3
A and B
A= builtin::blessed($result)B= $result->can("to_hash")
ABresult
0X0
100
111
373 0 $hash = $result->to_hash;
374 } else {
375 0 $hash = { response => "$result" };
376 }
377  
378 1 ▶ if ($raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw) { } 0 1 1
379 0 print JSON::encode_json($hash), "\n";
380 } else {
381 1 print JSON->new->utf8->pretty->canonical->encode($hash);
382 }
383 }
384  
385 # ============================================================
386 # Shared rendering
387 # ============================================================
388  
389 2 ▶sub print_tool_list {
Subroutine print_tool_list: called CC=15 SCAR=32.3
390 2 my ($functions) = @_;
391 2 ▶ if ($raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw) { } 0 2 2
392 0 for my $f (@$functions) {
393 0 ▶ printf "%s\t%s\n", $f->{function} // 'unnamed', $f->{description} // '';
Condition0%0/4
$f->{"function"} // "unnamed"
l!l
00
$f->{"description"} // ""
l!l
00
MC/DC0%0/1
$f->{"function"} // "unnamed"
$f->{"function"}
MC/DC0%0/1
$f->{"description"} // ""
$f->{"description"}
Truth table0%0/2
A
A= $f->{"function"}
Aresult
11
00
Truth table0%0/2
A
A= $f->{"description"}
Aresult
11
00
394 }
395 } else {
396 2 printf "Found %d SWAIG function(s):\n\n", scalar @$functions;
397 2 for my $f (@$functions) {
398 2 ▶ my $name = $f->{function} // 'unnamed';
Condition50%1/2
$f->{"function"} // "unnamed"
l!l
20
MC/DC0%0/1
$f->{"function"} // "unnamed"
$f->{"function"}
Truth table50%1/2
A
A= $f->{"function"}
Aresult
11
00
399 2 ▶ my $desc = $f->{description} // '(no description)';
Condition50%1/2
$f->{"description"} // "(no description)"
l!l
20
MC/DC0%0/1
$f->{"description"} // "(no description)"
$f->{"description"}
Truth table50%1/2
A
A= $f->{"description"}
Aresult
11
00
400 2 printf " %-30s %s\n", $name, $desc;
401  
402 # Show parameters if any
403 2 ▶ if (my $params_schema = $f->{parameters}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $params_schema = $f->{"parameters"}) 2 0 2
404 2 ▶ if (my $props = $params_schema->{properties}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $props = $params_schema->{"properties"}) 2 0 2
405 2 ▶ my $required = $params_schema->{required} // [];
Condition50%1/2
$params_schema->{"required"} // []
l!l
20
MC/DC0%0/1
$params_schema->{"required"} // []
$params_schema->{"required"}
Truth table50%1/2
A
A= $params_schema->{"required"}
Aresult
11
00
406 2 my %required_map = map { $_ => 1 } @$required;
407 2 for my $pname (sort keys %$props) {
408 2 ▶ my $ptype = $props->{$pname}{type} // 'any';
Condition50%1/2
$props->{$pname}{"type"} // "any"
l!l
20
MC/DC0%0/1
$props->{$pname}{"type"} // "any"
$props->{$pname}{"type"}
Truth table50%1/2
A
A= $props->{$pname}{"type"}
Aresult
11
00
409 2 ▶ my $pdesc = $props->{$pname}{description} // '';
Condition50%1/2
$props->{$pname}{"description"} // ""
l!l
20
MC/DC0%0/1
$props->{$pname}{"description"} // ""
$props->{$pname}{"description"}
Truth table50%1/2
A
A= $props->{$pname}{"description"}
Aresult
11
00
410 2 ▶ my $req_marker = $required_map{$pname} ? '*' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$required_map{$pname} ? : 2 0 2
411 2 printf " %s %-20s %-10s %s\n", $req_marker, $pname, "($ptype)", $pdesc;
412 }
413 }
414 }
415 2 print "\n";
416 }
417 }
418 }
419  
420 # ============================================================
421 # HTTP helpers
422 # ============================================================
423  
424 0 ▶sub _auth_headers {
Subroutine _auth_headers: not called CC=3 SCAR=24.8
425 0 my %headers;
426 0 ▶ if (defined $auth_user && defined $auth_pass) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $auth_user and defined $auth_pass) 0 0 0
Condition0%0/3
defined $auth_user and defined $auth_pass
!ll&&!rl&&r
000
MC/DC0%0/2
defined $auth_user and defined $auth_pass
defined $auth_user defined $auth_pass
Truth table0%0/3
A and B
A= defined $auth_userB= defined $auth_pass
ABresult
0X0
100
111
427 0 my $encoded = encode_base64("$auth_user:$auth_pass", '');
428 0 $headers{Authorization} = "Basic $encoded";
429 }
430 0 return %headers;
431 }
432  
433 0 ▶sub http_get {
Subroutine http_get: not called CC=5 SCAR=34.0
434 0 my ($target_url) = @_;
435 0 my %headers = _auth_headers();
436  
437 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
438 0 print STDERR ">>> GET $target_url\n";
439 }
440  
441 0 my $response = $http->get($target_url, { headers => \%headers });
442  
443 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
444 0 print STDERR "<<< Status: $response->{status}\n";
445 }
446  
447 0 ▶ unless ($response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
448 die sprintf("Error: HTTP %s %s\n%s\n",
449 0 ▶ $response->{status}, $response->{reason}, $response->{content} // '');
Condition0%0/2
$response->{"content"} // ""
l!l
00
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table0%0/2
A
A= $response->{"content"}
Aresult
11
00
450 }
451  
452 0 return $response;
453 }
454  
455 0 ▶sub http_post {
Subroutine http_post: not called CC=3 SCAR=24.8
456 0 my ($target_url, $body) = @_;
457 0 my %headers = _auth_headers();
458 0 $headers{'Content-Type'} = 'application/json';
459  
460 0 my $response = $http->post($target_url, {
461 headers => \%headers,
462 content => $body,
463 });
464  
465 0 ▶ unless ($response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
466 die sprintf("Error: HTTP %s %s\n%s\n",
467 0 ▶ $response->{status}, $response->{reason}, $response->{content} // '');
Condition0%0/2
$response->{"content"} // ""
l!l
00
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table0%0/2
A
A= $response->{"content"}
Aresult
11
00
468 }
469  
470 0 return $response;
471 }
472  
473 6 ▶sub usage_exit {
Subroutine usage_exit: called CC=2 SCAR=7.0
474 6 my ($code) = @_;
475 6 ▶ $code //= 1;
Condition50%1/2
$code //= 1
l!l
60
MC/DC0%0/1
$code //= 1
$code
Truth table50%1/2
A
A= $code
Aresult
11
00
476 6 print STDERR <<'USAGE';
477 swaig-test - CLI tool for testing SWAIG agent endpoints
478  
479 Usage:
480 swaig-test --url URL [OPTIONS]
481 swaig-test --file PATH [OPTIONS]
482  
483 Options:
484 --url URL Agent URL with embedded auth (http://user:pass@host:port/route)
485 --file PATH Path to a Perl script that builds an SWML::Service /
486 AgentBase instance. Loads the file in-process (no HTTP)
487 and reads the runtime tool registry.
488 --dump-swml Fetch and display the SWML document
489 --list-tools List available SWAIG functions
490 --exec NAME Execute a SWAIG function by name
491 --param key=value Parameter for --exec (repeatable)
492 --raw Output compact JSON (no pretty-printing)
493 --verbose Show request/response details on stderr
494 --help, -h Show this help message
495  
496 Examples:
497 swaig-test --url http://user:pass@localhost:3000/ --dump-swml
498 swaig-test --url http://user:pass@localhost:3000/ --list-tools
499 swaig-test --url http://user:pass@localhost:3000/ --exec get_weather --param location=London
500 swaig-test --url http://user:pass@localhost:3000/ --exec get_weather --param location=London --raw
501  
502 swaig-test --file examples/swmlservice_swaig_standalone.pl --list-tools
503 swaig-test --file examples/swmlservice_swaig_standalone.pl --exec lookup_competitor --param competitor=ACME
504 USAGE
505 6 exit $code;
506 }
« blib/lib/SignalWire/Security/WebhookMiddleware.pm ↑ Summary blib/lib/SignalWire/Relay/Call.pm »