bin/swaig-test

filter: Statementstmt 25.1% 36 / 143 Branchbran 9.0% 6 / 66 Conditioncond 8.8% 3 / 34 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 57.1% 8 / 14 Podpod n/a totaltotal 19.2% 53 / 275 CC 45 SCAR 57.0
CC
45
Cov
19%
do_list_tools
650.0
do_exec
90.0
http_get
30.0
CRAP 299.5
↑ Summary blib/lib/SignalWire/Agents/SWAIG/FunctionResult.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  
13 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 my $VERSION = '1.0.0';
22  
23 # --- Parse CLI options ---
24 3 my ($url, $dump_swml, $list_tools, $exec_name, @params, $raw, $verbose, $help);
25  
26 3 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("url=s", \$url, "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() 3 0 3
27 'url=s' => \$url,
28 'dump-swml' => \$dump_swml,
29 'list-tools' => \$list_tools,
30 'exec=s' => \$exec_name,
31 'param=s@' => \@params,
32 'raw' => \$raw,
33 'verbose' => \$verbose,
34 'help|h' => \$help,
35 ) or usage_exit();
36  
37 3 ▶if ($help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 1 2 3
38 1 usage_exit(0);
39 }
40  
41 2 ▶unless ($url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 1 1 2
42 1 print STDERR "Error: --url is required\n\n";
43 1 usage_exit(1);
44 }
45  
46 1 ▶unless ($dump_swml || $list_tools || $exec_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dump_swml or $list_tools or $exec_name) 1 0 1
Condition33%2/6
$dump_swml or $list_tools or $exec_name
l!l&&r!l&&!r
001
$dump_swml or $list_tools
l!l&&r!l&&!r
001
MC/DC0%0/3
$dump_swml or $list_tools or $exec_name
$dump_swml $list_tools $exec_name
Truth table25%1/4
A or B or C
A= $dump_swmlB= $list_toolsC= $exec_name
ABCresult
1XX1
01X1
0011
0000
47 1 print STDERR "Error: one of --dump-swml, --list-tools, or --exec NAME is required\n\n";
48 1 usage_exit(1);
49 }
50  
51 # --- Parse URL and extract auth ---
52 0 my $uri = URI->new($url);
53 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
54 0 my ($auth_user, $auth_pass) = split(/:/, $userinfo, 2);
55  
56 # Build base URL without credentials
57 0 my $clean_uri = $uri->clone;
58 0 $clean_uri->userinfo(undef);
59 0 my $base_url = $clean_uri->as_string;
60  
61 # Remove trailing slash
62 0 $base_url =~ s{/$}{};
63  
64 0 my $http = HTTP::Tiny->new(timeout => 30);
65  
66 # --- Execute the requested operation ---
67  
68 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
69 0 do_dump_swml();
70 }
71 elsif ($list_tools) {
72 0 do_list_tools();
73 }
74 elsif ($exec_name) {
75 0 do_exec($exec_name);
76 }
77  
78 0 exit 0;
79  
80 # ============================================================
81 # Operations
82 # ============================================================
83  
84 0 ▶sub do_dump_swml {
Subroutine do_dump_swml: not called CC=4 SCAR=30.0
85 0 my $response = http_get($base_url);
86 0 ▶ if ($raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw) { } 0 0 0
87 0 print $response->{content};
88 0 ▶ print "\n" unless $response->{content} =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"content"} =~ /\n$/ 0 0 0
89 } else {
90 0 my $data = eval { JSON::decode_json($response->{content}) };
91 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
92 0 die "Error: Failed to parse JSON response: $@\n";
93 }
94 0 print JSON->new->utf8->pretty->canonical->encode($data);
95 }
96 }
97  
98 0 ▶sub do_list_tools {
Subroutine do_list_tools: not called CC=25 SCAR=64.8
99 0 my $response = http_get($base_url);
100 0 my $data = eval { JSON::decode_json($response->{content}) };
101 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
102 0 die "Error: Failed to parse JSON response: $@\n";
103 }
104  
105 # Extract SWAIG functions from the SWML document
106 0 my @functions;
107  
108 # Navigate the SWML structure to find AI verb functions
109 0 ▶ if (my $sections = $data->{sections}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sections = $data->{"sections"}) 0 0 0
110 0 for my $section_name (keys %$sections) {
111 0 my $verbs = $sections->{$section_name};
112 0 ▶ next unless ref $verbs eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $verbs eq "ARRAY" 0 0 0
113 0 for my $verb (@$verbs) {
114 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
115 0 my $ai = $verb->{ai};
116 0 ▶ if (my $swaig = $ai->{SWAIG}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $swaig = $ai->{"SWAIG"}) 0 0 0
117 0 ▶ if (my $funcs = $swaig->{functions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $funcs = $swaig->{"functions"}) 0 0 0
118 0 push @functions, @$funcs;
119 }
120 }
121 }
122 }
123 }
124 }
125  
126 0 ▶ if (!@functions) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@functions) 0 0 0
127 0 print "No SWAIG functions found.\n";
128 0 return;
129 }
130  
131 0 ▶ if ($raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw) { } 0 0 0
132 0 for my $f (@functions) {
133 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
134 }
135 } else {
136 0 printf "Found %d SWAIG function(s):\n\n", scalar @functions;
137 0 for my $f (@functions) {
138 0 ▶ my $name = $f->{function} // 'unnamed';
Condition0%0/2
$f->{"function"} // "unnamed"
l!l
00
MC/DC0%0/1
$f->{"function"} // "unnamed"
$f->{"function"}
Truth table0%0/2
A
A= $f->{"function"}
Aresult
11
00
139 0 ▶ my $desc = $f->{description} // '(no description)';
Condition0%0/2
$f->{"description"} // "(no description)"
l!l
00
MC/DC0%0/1
$f->{"description"} // "(no description)"
$f->{"description"}
Truth table0%0/2
A
A= $f->{"description"}
Aresult
11
00
140 0 printf " %-30s %s\n", $name, $desc;
141  
142 # Show parameters if any
143 0 ▶ if (my $params_schema = $f->{parameters}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $params_schema = $f->{"parameters"}) 0 0 0
144 0 ▶ if (my $props = $params_schema->{properties}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $props = $params_schema->{"properties"}) 0 0 0
145 0 ▶ my $required = $params_schema->{required} // [];
Condition0%0/2
$params_schema->{"required"} // []
l!l
00
MC/DC0%0/1
$params_schema->{"required"} // []
$params_schema->{"required"}
Truth table0%0/2
A
A= $params_schema->{"required"}
Aresult
11
00
146 0 my %required_map = map { $_ => 1 } @$required;
147 0 for my $pname (sort keys %$props) {
148 0 ▶ my $ptype = $props->{$pname}{type} // 'any';
Condition0%0/2
$props->{$pname}{"type"} // "any"
l!l
00
MC/DC0%0/1
$props->{$pname}{"type"} // "any"
$props->{$pname}{"type"}
Truth table0%0/2
A
A= $props->{$pname}{"type"}
Aresult
11
00
149 0 ▶ my $pdesc = $props->{$pname}{description} // '';
Condition0%0/2
$props->{$pname}{"description"} // ""
l!l
00
MC/DC0%0/1
$props->{$pname}{"description"} // ""
$props->{$pname}{"description"}
Truth table0%0/2
A
A= $props->{$pname}{"description"}
Aresult
11
00
150 0 ▶ my $req_marker = $required_map{$pname} ? '*' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$required_map{$pname} ? : 0 0 0
151 0 printf " %s %-20s %-10s %s\n", $req_marker, $pname, "($ptype)", $pdesc;
152 }
153 }
154 }
155 0 print "\n";
156 }
157 }
158 }
159  
160 0 ▶sub do_exec {
Subroutine do_exec: not called CC=9 SCAR=45.0
161 0 my ($func_name) = @_;
162  
163 # Parse --param key=value pairs
164 0 my %args;
165 0 for my $p (@params) {
166 0 ▶ if ($p =~ /^([^=]+)=(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p =~ /^([^=]+)=(.*)$/) { } 0 0 0
167 0 $args{$1} = $2;
168 } else {
169 0 die "Error: Invalid --param format '$p'. Use key=value\n";
170 }
171 }
172  
173 0 my $swaig_url = $base_url . '/swaig';
174  
175 0 my $payload = {
176 function => $func_name,
177 argument => {
178 parsed => [ \%args ],
179 },
180 };
181  
182 0 my $json_body = JSON::encode_json($payload);
183  
184 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
185 0 print STDERR ">>> POST $swaig_url\n";
186 0 print STDERR ">>> Body: $json_body\n";
187 }
188  
189 0 my $response = http_post($swaig_url, $json_body);
190  
191 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
192 0 print STDERR "<<< Status: $response->{status}\n";
193 0 print STDERR "<<< Body: $response->{content}\n";
194 }
195  
196 0 ▶ if ($raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw) { } 0 0 0
197 0 print $response->{content};
198 0 ▶ print "\n" unless $response->{content} =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"content"} =~ /\n$/ 0 0 0
199 } else {
200 0 my $data = eval { JSON::decode_json($response->{content}) };
201 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
202 0 print $response->{content};
203 0 ▶ print "\n" unless $response->{content} =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"content"} =~ /\n$/ 0 0 0
204 } else {
205 0 print JSON->new->utf8->pretty->canonical->encode($data);
206 }
207 }
208 }
209  
210 # ============================================================
211 # HTTP helpers
212 # ============================================================
213  
214 0 ▶sub _auth_headers {
Subroutine _auth_headers: not called CC=3 SCAR=24.8
215 0 my %headers;
216 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
217 0 my $encoded = encode_base64("$auth_user:$auth_pass", '');
218 0 $headers{Authorization} = "Basic $encoded";
219 }
220 0 return %headers;
221 }
222  
223 0 ▶sub http_get {
Subroutine http_get: not called CC=5 SCAR=34.0
224 0 my ($target_url) = @_;
225 0 my %headers = _auth_headers();
226  
227 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
228 0 print STDERR ">>> GET $target_url\n";
229 }
230  
231 0 my $response = $http->get($target_url, { headers => \%headers });
232  
233 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
234 0 print STDERR "<<< Status: $response->{status}\n";
235 }
236  
237 0 ▶ unless ($response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
238 die sprintf("Error: HTTP %s %s\n%s\n",
239 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
240 }
241  
242 0 return $response;
243 }
244  
245 0 ▶sub http_post {
Subroutine http_post: not called CC=3 SCAR=24.8
246 0 my ($target_url, $body) = @_;
247 0 my %headers = _auth_headers();
248 0 $headers{'Content-Type'} = 'application/json';
249  
250 0 my $response = $http->post($target_url, {
251 headers => \%headers,
252 content => $body,
253 });
254  
255 0 ▶ unless ($response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
256 die sprintf("Error: HTTP %s %s\n%s\n",
257 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
258 }
259  
260 0 return $response;
261 }
262  
263 3 ▶sub usage_exit {
Subroutine usage_exit: called CC=2 SCAR=7.0
264 3 my ($code) = @_;
265 3 ▶ $code //= 1;
Condition50%1/2
$code //= 1
l!l
30
MC/DC0%0/1
$code //= 1
$code
Truth table50%1/2
A
A= $code
Aresult
11
00
266 3 print STDERR <<'USAGE';
267 swaig-test - CLI tool for testing SWAIG agent endpoints
268  
269 Usage:
270 swaig-test --url URL [OPTIONS]
271  
272 Options:
273 --url URL Agent URL with embedded auth (http://user:pass@host:port/route)
274 --dump-swml Fetch and display the SWML document
275 --list-tools List available SWAIG functions
276 --exec NAME Execute a SWAIG function by name
277 --param key=value Parameter for --exec (repeatable)
278 --raw Output compact JSON (no pretty-printing)
279 --verbose Show request/response details on stderr
280 --help, -h Show this help message
281  
282 Examples:
283 swaig-test --url http://user:pass@localhost:3000/ --dump-swml
284 swaig-test --url http://user:pass@localhost:3000/ --list-tools
285 swaig-test --url http://user:pass@localhost:3000/ --exec get_weather --param location=London
286 swaig-test --url http://user:pass@localhost:3000/ --exec get_weather --param location=London --raw
287 USAGE
288 3 exit $code;
289 }
↑ Summary blib/lib/SignalWire/Agents/SWAIG/FunctionResult.pm »