| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | usage_exit(0); | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | ▶ | unless ($url) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
$dump_swml or $list_tools or $exec_name
$dump_swml or $list_tools
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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 // ""
MC/DC0%0/1
$uri->userinfo // ""
$uri->userinfo
Truth table0%0/2
A
A= $uri->userinfo
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | print $response->{content}; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | print "\n" unless $response->{content} =~ /\n$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | my $data = eval { JSON::decode_json($response->{content}) }; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | for my $verb (@$verbs) { | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if (ref $verb eq 'HASH' && exists $verb->{ai}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $verb eq "HASH" and exists $verb->{"ai"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | my $ai = $verb->{ai}; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if (my $swaig = $ai->{SWAIG}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if (my $funcs = $swaig->{functions}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | push @functions, @$funcs; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if (!@functions) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | print "No SWAIG functions found.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | if ($raw) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | for my $f (@functions) { | |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | printf "%s\t%s\n", $f->{function} // 'unnamed', $f->{description} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/4
$f->{"function"} // "unnamed"
$f->{"description"} // ""
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"}
Truth table0%0/2
A
A= $f->{"description"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$f->{"function"} // "unnamed"
$f->{"function"}
Truth table0%0/2
A
A= $f->{"function"}
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | my $desc = $f->{description} // '(no description)'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$f->{"description"} // "(no description)"
MC/DC0%0/1
$f->{"description"} // "(no description)"
$f->{"description"}
Truth table0%0/2
A
A= $f->{"description"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if (my $props = $params_schema->{properties}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | my $required = $params_schema->{required} // []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$params_schema->{"required"} // []
MC/DC0%0/1
$params_schema->{"required"} // []
$params_schema->{"required"}
Truth table0%0/2
A
A= $params_schema->{"required"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$props->{$pname}{"type"} // "any"
$props->{$pname}{"type"}
Truth table0%0/2
A
A= $props->{$pname}{"type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | my $pdesc = $props->{$pname}{description} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$props->{$pname}{"description"} // ""
MC/DC0%0/1
$props->{$pname}{"description"} // ""
$props->{$pname}{"description"}
Truth table0%0/2
A
A= $props->{$pname}{"description"}
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | my $req_marker = $required_map{$pname} ? '*' : ' '; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | print $response->{content}; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | ▶ | print "\n" unless $response->{content} =~ /\n$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | my $data = eval { JSON::decode_json($response->{content}) }; | |||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | print $response->{content}; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | print "\n" unless $response->{content} =~ /\n$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $auth_user and defined $auth_pass
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | print STDERR "<<< Status: $response->{status}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | unless ($response->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | die sprintf("Error: HTTP %s %s\n%s\n", | ||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | ▶ | $response->{status}, $response->{reason}, $response->{content} // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$response->{"content"} // ""
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table0%0/2
A
A= $response->{"content"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | die sprintf("Error: HTTP %s %s\n%s\n", | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | $response->{status}, $response->{reason}, $response->{content} // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$response->{"content"} // ""
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table0%0/2
A
A= $response->{"content"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$code //= 1
$code
Truth table50%1/2
A
A= $code
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | } | ||||||||||||||||||||||||||||||||||||||||||