blib/lib/Langertha/Holmgang/Case.pm

filter: Statementstmt 80.2% 114 / 142 Branchbran 70.3% 38 / 54 Conditioncond 71.4% 40 / 56 MC/DCmcdc 61.2% 19 / 31 Subroutinesub 93.5% 29 / 31 Podpod 100% 10 / 10 totaltotal 77.1% 250 / 324 CC 82 SCAR 20.7
CC
82
Cov
76%
_build_initial_state
21.8
_build_initial_state
21.8
_build_prompt_messages
13.2
CRAP 7.9
« blib/lib/Langertha/Holmgang/Sim/Kueche.pm ↑ Summary blib/lib/Langertha/Holmgang/Sim/Roost.pm »
1 package Langertha::Holmgang::Case;
2 # ABSTRACT: One case of a suite: its own format 2 values and what derives from them
3  
4 37 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 37 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 37 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 37 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 37 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 37 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 37 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 37 ▶use Langertha::Content::Image;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 37 ▶use Langertha::Holmgang::Haystack;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 37 ▶use Langertha::Holmgang::Simulators;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 37 our $VERSION = '0.001';
17  
18 # built-in fallbacks of the settings, below the suite's defaults; a true constant
19 37 my %DEFAULT = ( temperature => 0, max_tokens => 2000, max_turns => 12, system => '' );
20  
21 37 my $JSON = JSON::MaybeXS->new( utf8 => 0, canonical => 1, allow_nonref => 1 );
22  
23  
24 37 has id => ( is => 'ro', required => 1 );
25  
26  
27 37 has $_ => ( is => 'ro' ) for qw(
28 title tags difficulty system messages state observe rules solution
29 alternatives wrong_solutions temperature max_tokens max_turns source weight
30 );
31  
32  
33 31 ▶has tools => ( is => 'ro', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
34  
35  
36 31 ▶has defaults => ( is => 'ro', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
37  
38  
39 37 has simulators => (
40 is => 'ro',
41 lazy => 1,
42 builder => '_build_simulators',
43 );
44 12 ▶sub _build_simulators { Langertha::Holmgang::Simulators->new }
Subroutine _build_simulators: called CC=1 SCAR=0.0
45  
46  
47 37 has haystack => (
48 is => 'ro',
49 lazy => 1,
50 builder => '_build_haystack',
51 );
52 1 ▶sub _build_haystack { Langertha::Holmgang::Haystack->new }
Subroutine _build_haystack: called CC=1 SCAR=0.0
53  
54  
55 37 has initial_state => (
56 is => 'ro',
57 lazy => 1,
58 builder => '_build_initial_state',
59 );
60  
61 14 ▶sub _build_initial_state {
Subroutine _build_initial_state: called CC=9 SCAR=30.8
62 0 ▶ my ( $self ) = @_;
Subroutine _build_initial_state: not called CC=9 SCAR=30.8
63 0 ▶ my $state = $JSON->decode( $JSON->encode( $self->state // {} ) );
Condition25%1/4
$self->state // {}
l!l
140
$self->state // {}
l!l
00
MC/DC0%0/1
$self->state // {}
$self->state
MC/DC0%0/1
$self->state // {}
$self->state
Truth table50%1/2
A
A= $self->state
Aresult
11
00
Truth table0%0/2
A
A= $self->state
Aresult
11
00
64 0 for my $dev ( sort keys %$state ) {
65 0 ▶ my $files = ref $state->{$dev} eq 'HASH' ? $state->{$dev}{files} : undef;
Branch: 2 branches
BranchTrueFalseTotal
ref $state->{$dev} eq 'HASH' ? : 14 4 18
ref $$state{$dev} eq 'HASH' ? : 0 0 0
66 0 ▶ next unless ref $files eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $files eq "HASH" 13 5 18
unless ref $files eq "HASH" 0 0 0
67 0 for my $p ( sort keys %$files ) {
68 0 my $f = $files->{$p};
69 0 ▶ next unless ref $f eq 'HASH' && ( ref $f->{content} eq 'HASH' || ref $f->{content} eq 'ARRAY' );
Branch: 2 branches
BranchTrueFalseTotal
unless ref $f eq "HASH" and ref $f->{"content"} eq "HASH" || ref $f->{"content"} eq "ARRAY" 7 1 8
unless ref $f eq "HASH" and ref $$f{"content"} eq "HASH" || ref $$f{"content"} eq "ARRAY" 0 0 0
Condition41%5/12
ref $f eq "HASH" and ref $f->{"content"} eq "HASH" || ref $f->{"content"} eq "ARRAY"
!ll&&!rl&&r
521
ref $f->{"content"} eq "HASH" or ref $f->{"content"} eq "ARRAY"
l!l&&r!l&&!r
012
ref $f eq "HASH" and ref $$f{"content"} eq "HASH" || ref $$f{"content"} eq "ARRAY"
!ll&&!rl&&r
000
ref $$f{"content"} eq "HASH" or ref $$f{"content"} eq "ARRAY"
l!l&&r!l&&!r
000
MC/DC66%2/3
ref $f eq "HASH" and ref $f->{"content"} eq "HASH" || ref $f->{"content"} eq "ARRAY"
ref $f eq "HASH" ref $f->{"content"} eq "HASH" ref $f->{"content"} eq "ARRAY"
MC/DC0%0/3
ref $f eq "HASH" and ref $$f{"content"} eq "HASH" || ref $$f{"content"} eq "ARRAY"
ref $f eq "HASH" ref $$f{"content"} eq "HASH" ref $$f{"content"} eq "ARRAY"
Truth table75%3/4
A and B or C
A= ref $f eq "HASH"B= ref $f->{"content"} eq "HASH"C= ref $f->{"content"} eq "ARRAY"
ABCresult
0XX0
1000
11X1
1011
Truth table0%0/4
A and B or C
A= ref $f eq "HASH"B= ref $$f{"content"} eq "HASH"C= ref $$f{"content"} eq "ARRAY"
ABCresult
0XX0
1000
11X1
1011
70 0 $f->{content} = $self->haystack->resolve( $f->{content}, 'state.'.$dev.'.files.'.$p.'.content' );
71 }
72 }
73 0 $self->simulators->prepare($state);
74 0 return $state;
75 }
76  
77  
78 37 has prompt_messages => (
79 is => 'ro',
80 lazy => 1,
81 builder => '_build_prompt_messages',
82 );
83  
84 3 ▶sub _build_prompt_messages {
Subroutine _build_prompt_messages: called CC=5 SCAR=25.8
85 0 ▶ my ( $self ) = @_;
Subroutine _build_prompt_messages: not called CC=5 SCAR=25.8
86 0 my $i = 0;
87 return [ map {
88 0 my $c = $_->{content};
89 0 ▶ $c = $self->haystack->resolve( $c, 'messages['.$i.'].content' ) if ref $c;
Branch: 2 branches
BranchTrueFalseTotal
if ref $c 1 2 3
if ref $c 0 0 0
90 0 ▶ if ( ref $_->{images} eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_->{"images"} eq "ARRAY") 0 3 3
if (ref $$_{"images"} eq "ARRAY") 0 0 0
91 $c = [ { type => 'text', text => $c }, map {
92 my $info = $self->image_files->{ $_->{file} }
93 0 ▶ or croak __PACKAGE__.': image '.$_->{file}.' of messages['.$i.'] is not in image_files';
Branch: 2 branches
BranchTrueFalseTotal
my $info = $self->image_files->{$_->{"file"}} or croak('Langertha::Holmgang::Case: image ' . $_->{"file"} . ' of messages[' . $i . '] is not in image_files') 0 0 0
my $info = $self->image_files->{$$_{"file"}} or croak('Langertha::Holmgang::Case: image ' . $$_{"file"} . ' of messages[' . $i . '] is not in image_files') 0 0 0
94 0 +{ image => $_->{file}, sha256 => $info->{sha256}, bytes => $info->{bytes} };
95 0 } @{ $_->{images} } ];
96 }
97 0 $i++;
98 0 +{ role => $_->{role}, content => $c };
99 0 ▶ } @{ $self->messages || [] } ];
Condition25%1/4
$self->messages || []
l!l
30
$self->messages || []
l!l
00
MC/DC0%0/1
$self->messages || []
$self->messages
MC/DC0%0/1
$self->messages || []
$self->messages
Truth table50%1/2
A
A= $self->messages
Aresult
11
00
Truth table0%0/2
A
A= $self->messages
Aresult
11
00
100 }
101  
102  
103 33 ▶has image_files => ( is => 'ro', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
104  
105  
106 372 ▶has _image_cache => ( is => 'ro', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
107  
108 2400 ▶sub has_images { %{ $_[0]->image_files } ? 1 : 0 }
Subroutine has_images: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
%{$_[0]->image_files;} ? : 269 2131 2400
109  
110  
111 315 ▶sub image_content {
Subroutine image_content: called CC=4 SCAR=14.0
Pod documented
112 315 my ( $self, $file ) = @_;
113 315 ▶ return $self->_image_cache->{$file} //= do {
Condition66%2/3
$self->_image_cache->{$file} //= do { my $info = $self->image_files->{$file} or croak('Langertha::Holmgang::Case: the case ' . $self->id . ' has no image ' . $file); my $bytes = path($info->{"path"})->slurp_raw; croak('Langertha::Holmgang::Case: the image ' . $file . ' of the case ' . $self->id . ' changed since the suite was loaded') unless &Digest::SHA::sha256_hex($bytes) eq $info->{"sha256"}; "Langertha::Content::Image"->from_data($bytes, "media_type", $info->{"media_type"}) }
l!l&&r!l&&!r
275400
MC/DC0%0/2
$self->_image_cache->{$file} //= do { my $info = $self->image_files->{$file} or croak('Langertha::Holmgang::Case: the case ' . $self->id . ' has no image ' . $file); my $bytes = path($info->{"path"})->slurp_raw; croak('Langertha::Holmgang::Case: the image ' . $file . ' of the case ' . $self->id . ' changed since the suite was loaded') unless &Digest::SHA::sha256_hex($bytes) eq $info->{"sha256"}; "Langertha::Content::Image"->from_data($bytes, "media_type", $info->{"media_type"}) }
$self->_image_cache->{$file} do { my $info = $self->image_files->{$file} or croak('Langertha::Holmgang::Case: the case ' . $self->id . ' has no image ' . $file); my $bytes = path($info->{"path"})->slurp_raw; croak('Langertha::Holmgang::Case: the image ' . $file . ' of the case ' . $self->id . ' changed since the suite was loaded') unless &Digest::SHA::sha256_hex($bytes) eq $info->{"sha256"}; "Langertha::Content::Image"->from_data($bytes, "media_type", $info->{"media_type"}) }
Truth table66%2/3
A //= B
A= $self->_image_cache->{$file}B= do { my $info = $self->image_files->{$file} or croak('Langertha::Holmgang::Case: the case ' . $self->id . ' has no image ' . $file); my $bytes = path($info->{"path"})->slurp_raw; croak('Langertha::Holmgang::Case: the image ' . $file . ' of the case ' . $self->id . ' changed since the suite was loaded') unless &Digest::SHA::sha256_hex($bytes) eq $info->{"sha256"}; "Langertha::Content::Image"->from_data($bytes, "media_type", $info->{"media_type"}) }
ABresult
1X1
011
000
114 40 ▶ my $info = $self->image_files->{$file}
Branch: 1 branch
BranchTrueFalseTotal
my $info = $self->image_files->{$file} or croak('Langertha::Holmgang::Case: the case ' . $self->id . ' has no image ' . $file) 40 0 40
115 or croak __PACKAGE__.': the case '.$self->id.' has no image '.$file;
116 40 my $bytes = path( $info->{path} )->slurp_raw;
117 croak __PACKAGE__.': the image '.$file.' of the case '.$self->id.' changed since the suite was loaded'
118 40 ▶ unless Digest::SHA::sha256_hex($bytes) eq $info->{sha256};
Branch: 1 branch
BranchTrueFalseTotal
unless &Digest::SHA::sha256_hex($bytes) eq $info->{"sha256"} 1 39 40
119 39 Langertha::Content::Image->from_data( $bytes, media_type => $info->{media_type} );
120 };
121 }
122  
123  
124 3502 ▶sub effective {
Subroutine effective: called CC=5 SCAR=16.1
Pod documented
125 3502 my ( $self, $key ) = @_;
126 3502 ▶ my $own = $self->can($key) && exists $DEFAULT{$key} ? $self->$key : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->can($key) && exists $DEFAULT{$key} ? : 3501 1 3502
Condition66%2/3
$self->can($key) and exists $DEFAULT{$key}
!ll&&!rl&&r
013501
MC/DC50%1/2
$self->can($key) and exists $DEFAULT{$key}
$self->can($key) exists $DEFAULT{$key}
Truth table66%2/3
A and B
A= $self->can($key)B= exists $DEFAULT{$key}
ABresult
0X0
100
111
127 3502 ▶ return $own // $self->defaults->{$key} // $DEFAULT{$key};
Condition100%6/6
($own // $self->defaults->{$key}) // $DEFAULT{$key}
l!l&&r!l&&!r
102624751
$own // $self->defaults->{$key}
l!l&&r!l&&!r
2210042476
MC/DC100%3/3
($own // $self->defaults->{$key}) // $DEFAULT{$key}
$own $self->defaults->{$key} $DEFAULT{$key}
Truth table100%4/4
A // B // C
A= $ownB= $self->defaults->{$key}C= $DEFAULT{$key}
ABCresult
1XX1
01X1
0011
0000
128 }
129  
130  
131 165 ▶sub effective_weight { $_[0]->weight // 1 }
Subroutine effective_weight: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]->weight // 1
l!l
10155
MC/DC100%1/1
$_[0]->weight // 1
$_[0]->weight
Truth table100%2/2
A
A= $_[0]->weight
Aresult
11
00
132  
133  
134 338 ▶sub tag_list { @{ $_[0]->tags || [] } }
Subroutine tag_list: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]->tags || []
l!l
34304
MC/DC100%1/1
$_[0]->tags || []
$_[0]->tags
Truth table100%2/2
A
A= $_[0]->tags
Aresult
11
00
135  
136  
137 171 ▶sub tier {
Subroutine tier: called CC=3 SCAR=11.0
Pod documented
138 171 my ( $self ) = @_;
139 171 ▶ for ( $self->tag_list ) { return $1 if /\Astufe-(\d+k)\z/ }
Branch: 1 branch
BranchTrueFalseTotal
if /\Astufe-(\d+k)\z/ 14 18 32
140 157 return undef;
141 }
142  
143  
144 167 ▶sub is_hard {
Subroutine is_hard: called CC=3 SCAR=11.0
Pod documented
145 167 my ( $self ) = @_;
146 167 ▶ return ( grep { $_ eq 'injection' || $_ eq 'verbot' } $self->tag_list ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq 'injection' or $_ eq 'verbot';} $self->tag_list) ? : 8 159 167
Condition100%3/3
$_ eq 'injection' || $_ eq 'verbot'
l!l&&r!l&&!r
2622
MC/DC100%2/2
$_ eq 'injection' || $_ eq 'verbot'
$_ eq 'injection' $_ eq 'verbot'
Truth table100%3/3
A || B
A= $_ eq 'injection'B= $_ eq 'verbot'
ABresult
1X1
011
000
147 }
148  
149  
150 2713 ▶sub has_untrusted {
Subroutine has_untrusted: called CC=8 SCAR=20.8
Pod documented
151 2713 my ( $self ) = @_;
152 2713 ▶ for my $m ( grep { ref $_ eq 'HASH' } @{ $self->messages || [] } ) {
Condition100%2/2
$self->messages || []
l!l
267538
MC/DC100%1/1
$self->messages || []
$self->messages
Truth table100%2/2
A
A= $self->messages
Aresult
11
00
153 2987 ▶ return 1 if $m->{untrusted};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"untrusted"} 34 2953 2987
154 2953 ▶ return 1 if ref $m->{images} eq 'ARRAY' && grep { ref $_ eq 'HASH' && $_->{untrusted} } @{ $m->{images} };
Branch: 1 branch
BranchTrueFalseTotal
if ref $m->{"images"} eq "ARRAY" and grep {ref $_ eq "HASH" and $_->{"untrusted"};} @{$$m{"images"};} 133 2820 2953
Condition83%5/6
ref $m->{"images"} eq "ARRAY" and grep {ref $_ eq "HASH" and $_->{"untrusted"};} @{$$m{"images"};}
!ll&&!rl&&r
2650170133
ref $_ eq 'HASH' && $_->{'untrusted'}
!ll&&!rl&&r
0316133
MC/DC100%2/2
ref $m->{"images"} eq "ARRAY" and grep {ref $_ eq "HASH" and $_->{"untrusted"};} @{$$m{"images"};}
ref $m->{"images"} eq "ARRAY" grep {ref $_ eq "HASH" and $_->{"untrusted"};} @{$$m{"images"};}
MC/DC50%1/2
ref $_ eq 'HASH' && $_->{'untrusted'}
ref $_ eq 'HASH' $_->{'untrusted'}
Truth table100%3/3
A and B
A= ref $m->{"images"} eq "ARRAY"B= grep {ref $_ eq "HASH" and $_->{"untrusted"};} @{$$m{"images"};}
ABresult
0X0
100
111
Truth table66%2/3
A && B
A= ref $_ eq 'HASH'B= $_->{'untrusted'}
ABresult
0X0
100
111
155 }
156 2546 ▶ return $self->_marked( $self->state ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_marked($self->state) ? : 12 2534 2546
157 }
158  
159 # a hash with untrusted: true anywhere in the state
160 14079 ▶sub _marked {
Subroutine _marked: called CC=8 SCAR=20.8
161 14079 my ( $self, $d ) = @_;
162 14079 ▶ if ( ref $d eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $d eq "HASH") 5807 8272 14079
163 5807 my $u = $d->{untrusted};
164 5807 ▶ return 1 if $u && ( !ref $u || JSON::MaybeXS::is_bool($u) );
Branch: 1 branch
BranchTrueFalseTotal
if $u and !ref($u) || JSON::MaybeXS::is_bool($u) 12 5795 5807
Condition100%6/6
$u and !ref($u) || JSON::MaybeXS::is_bool($u)
!ll&&!rl&&r
5794112
not ref $u or JSON::MaybeXS::is_bool($u)
l!l&&r!l&&!r
1111
MC/DC100%3/3
$u and !ref($u) || JSON::MaybeXS::is_bool($u)
$u not ref $u JSON::MaybeXS::is_bool($u)
Truth table100%4/4
A and not(B) or C
A= $uB= not ref $uC= JSON::MaybeXS::is_bool($u)
ABCresult
0XX0
1000
11X1
1011
165 5795 ▶ return ( grep { $self->_marked($_) } values %$d ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({$self->_marked($_);} values %$d) ? : 24 5771 5795
166 }
167 8272 ▶ return ( grep { $self->_marked($_) } @$d ) ? 1 : 0 if ref $d eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
if ref $d eq "ARRAY" 485 7787 8272
grep({$self->_marked($_);} @$d) ? : 1 484 485
168 7787 return 0;
169 }
170  
171  
172 332 ▶sub no_tool {
Subroutine no_tool: called CC=3 SCAR=11.0
Pod documented
173 332 my ( $self ) = @_;
174 332 ▶ return ref $self->solution eq 'ARRAY' && !@{ $self->solution } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $self->solution eq 'ARRAY' && !@{$self->solution;} ? : 9 323 332
Condition100%3/3
ref $self->solution eq "ARRAY" and not @{$self->solution;}
!ll&&!rl&&r
13229
MC/DC100%2/2
ref $self->solution eq "ARRAY" and not @{$self->solution;}
ref $self->solution eq "ARRAY" not @{$self->solution;}
Truth table100%3/3
A and not(B)
A= ref $self->solution eq "ARRAY"B= not @{$self->solution;}
ABresult
0X0
100
111
175 }
176  
177  
178 148 ▶sub report_meta {
Subroutine report_meta: called CC=1 SCAR=0.0
Pod documented
179 148 my ( $self ) = @_;
180 return {
181 148 case_id => $self->id,
182 title => $self->title,
183 tags => $self->tags,
184 tier => $self->tier,
185 difficulty => $self->difficulty,
186 weight => $self->effective_weight,
187 hard => $self->is_hard,
188 untrusted => $self->has_untrusted,
189 };
190 }
191  
192  
193 37 __PACKAGE__->meta->make_immutable;
194  
195 __END__
« blib/lib/Langertha/Holmgang/Sim/Kueche.pm ↑ Summary blib/lib/Langertha/Holmgang/Sim/Roost.pm »