nukadoko 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +417 -0
  3. data/README.ja.md +62 -7
  4. data/README.md +19 -5
  5. data/exe/nuka +8 -0
  6. data/lib/nukadoko/aria_snapshot.rb +1008 -0
  7. data/lib/nukadoko/aria_tree.rb +150 -0
  8. data/lib/nukadoko/base.rb +294 -0
  9. data/lib/nukadoko/check.rb +669 -22
  10. data/lib/nukadoko/claim_check.rb +177 -0
  11. data/lib/nukadoko/claim_check_run.rb +293 -0
  12. data/lib/nukadoko/claim_line.rb +381 -0
  13. data/lib/nukadoko/claim_rules.rb +519 -0
  14. data/lib/nukadoko/claim_sides.rb +469 -0
  15. data/lib/nukadoko/claims.rb +576 -37
  16. data/lib/nukadoko/cli.rb +814 -22
  17. data/lib/nukadoko/codes.json +947 -0
  18. data/lib/nukadoko/codes.rb +19 -0
  19. data/lib/nukadoko/command.rb +214 -12
  20. data/lib/nukadoko/criteria.rb +172 -0
  21. data/lib/nukadoko/definition.rb +33 -41
  22. data/lib/nukadoko/env_check.rb +88 -0
  23. data/lib/nukadoko/features.rb +80 -10
  24. data/lib/nukadoko/fixtures.rb +329 -11
  25. data/lib/nukadoko/http_recording.rb +68 -0
  26. data/lib/nukadoko/init.rb +129 -0
  27. data/lib/nukadoko/js_json.rb +274 -0
  28. data/lib/nukadoko/js_pattern.rb +531 -0
  29. data/lib/nukadoko/judge.rb +221 -0
  30. data/lib/nukadoko/judgment_file.rb +78 -0
  31. data/lib/nukadoko/judgments.rb +256 -0
  32. data/lib/nukadoko/literal.rb +129 -0
  33. data/lib/nukadoko/measured.rb +105 -0
  34. data/lib/nukadoko/order.rb +84 -0
  35. data/lib/nukadoko/parts.rb +43 -22
  36. data/lib/nukadoko/pattern.rb +73 -3
  37. data/lib/nukadoko/poll.rb +21 -8
  38. data/lib/nukadoko/program.rb +105 -17
  39. data/lib/nukadoko/project_files.rb +17 -0
  40. data/lib/nukadoko/project_fixtures.rb +17 -2
  41. data/lib/nukadoko/project_program.rb +119 -0
  42. data/lib/nukadoko/report_allure.rb +751 -0
  43. data/lib/nukadoko/report_messages.rb +167 -0
  44. data/lib/nukadoko/report_run.rb +169 -0
  45. data/lib/nukadoko/run.rb +898 -91
  46. data/lib/nukadoko/run_record.rb +535 -0
  47. data/lib/nukadoko/scaffold.rb +1015 -0
  48. data/lib/nukadoko/schema.rb +120 -21
  49. data/lib/nukadoko/secrets.rb +375 -0
  50. data/lib/nukadoko/selection.rb +163 -0
  51. data/lib/nukadoko/signature.rb +4 -1
  52. data/lib/nukadoko/skill.rb +36 -0
  53. data/lib/nukadoko/spaces.rb +28 -0
  54. data/lib/nukadoko/step.rb +68 -21
  55. data/lib/nukadoko/summary.rb +662 -0
  56. data/lib/nukadoko/table.rb +34 -2
  57. data/lib/nukadoko/text_fit.rb +179 -0
  58. data/lib/nukadoko/trace.rb +244 -0
  59. data/lib/nukadoko/trace_actions.rb +116 -0
  60. data/lib/nukadoko/tree_passes.rb +122 -0
  61. data/lib/nukadoko/verdict.rb +128 -0
  62. data/lib/nukadoko/version.rb +2 -1
  63. data/lib/nukadoko/vocabulary.rb +6 -0
  64. data/lib/nukadoko/walk.rb +305 -0
  65. data/lib/nukadoko/walk_files.rb +473 -0
  66. data/lib/nukadoko/walk_step.rb +149 -0
  67. data/lib/nukadoko.rb +60 -2
  68. data/llms.txt +297 -0
  69. data/sig/generated/nukadoko/aria_snapshot.rbs +331 -0
  70. data/sig/generated/nukadoko/aria_tree.rbs +142 -0
  71. data/sig/generated/nukadoko/base.rbs +142 -0
  72. data/sig/generated/nukadoko/check.rbs +255 -0
  73. data/sig/generated/nukadoko/claim_check.rbs +118 -0
  74. data/sig/generated/nukadoko/claim_check_run.rbs +88 -0
  75. data/sig/generated/nukadoko/claim_line.rbs +158 -0
  76. data/sig/generated/nukadoko/claim_rules.rbs +172 -0
  77. data/sig/generated/nukadoko/claim_sides.rbs +207 -0
  78. data/sig/generated/nukadoko/claims.rbs +208 -0
  79. data/sig/generated/nukadoko/cli.rbs +182 -0
  80. data/sig/generated/nukadoko/codes.rbs +16 -0
  81. data/sig/generated/nukadoko/command.rbs +109 -0
  82. data/sig/generated/nukadoko/criteria.rbs +83 -0
  83. data/sig/generated/nukadoko/definition.rbs +30 -0
  84. data/sig/generated/nukadoko/env_check.rbs +34 -0
  85. data/sig/generated/nukadoko/features.rbs +101 -0
  86. data/sig/generated/nukadoko/fixtures.rbs +175 -0
  87. data/sig/generated/nukadoko/http_recording.rbs +29 -0
  88. data/sig/generated/nukadoko/init.rbs +64 -0
  89. data/sig/generated/nukadoko/js_json.rbs +109 -0
  90. data/sig/generated/nukadoko/js_pattern.rbs +194 -0
  91. data/sig/generated/nukadoko/judge.rbs +64 -0
  92. data/sig/generated/nukadoko/judgment_file.rbs +39 -0
  93. data/sig/generated/nukadoko/judgments.rbs +120 -0
  94. data/sig/generated/nukadoko/literal.rbs +71 -0
  95. data/sig/generated/nukadoko/measured.rbs +42 -0
  96. data/sig/generated/nukadoko/order.rbs +54 -0
  97. data/sig/generated/nukadoko/parts.rbs +35 -0
  98. data/sig/generated/nukadoko/pattern.rbs +62 -0
  99. data/sig/generated/nukadoko/poll.rbs +41 -0
  100. data/sig/generated/nukadoko/program.rbs +101 -0
  101. data/sig/generated/nukadoko/project_files.rbs +19 -0
  102. data/sig/generated/nukadoko/project_fixtures.rbs +57 -0
  103. data/sig/generated/nukadoko/project_program.rbs +55 -0
  104. data/sig/generated/nukadoko/report_allure.rbs +268 -0
  105. data/sig/generated/nukadoko/report_messages.rbs +68 -0
  106. data/sig/generated/nukadoko/report_run.rbs +102 -0
  107. data/sig/generated/nukadoko/run.rbs +264 -0
  108. data/sig/generated/nukadoko/run_record.rbs +175 -0
  109. data/sig/generated/nukadoko/scaffold.rbs +328 -0
  110. data/sig/generated/nukadoko/schema.rbs +155 -0
  111. data/sig/generated/nukadoko/secrets.rbs +172 -0
  112. data/sig/generated/nukadoko/selection.rbs +73 -0
  113. data/sig/generated/nukadoko/signature.rbs +17 -0
  114. data/sig/generated/nukadoko/skill.rbs +20 -0
  115. data/sig/generated/nukadoko/spaces.rbs +31 -0
  116. data/sig/generated/nukadoko/step.rbs +115 -0
  117. data/sig/generated/nukadoko/summary.rbs +204 -0
  118. data/sig/generated/nukadoko/table.rbs +43 -0
  119. data/sig/generated/nukadoko/text_fit.rbs +70 -0
  120. data/sig/generated/nukadoko/trace.rbs +127 -0
  121. data/sig/generated/nukadoko/trace_actions.rbs +40 -0
  122. data/sig/generated/nukadoko/tree_passes.rbs +48 -0
  123. data/sig/generated/nukadoko/verdict.rbs +56 -0
  124. data/sig/generated/nukadoko/version.rbs +5 -0
  125. data/sig/generated/nukadoko/vocabulary.rbs +25 -0
  126. data/sig/generated/nukadoko/walk.rbs +139 -0
  127. data/sig/generated/nukadoko/walk_files.rbs +189 -0
  128. data/sig/generated/nukadoko/walk_step.rbs +50 -0
  129. data/sig/generated/nukadoko.rbs +26 -0
  130. data/sig/nukadoko.rbs +5 -48
  131. data/skills/nukadoko/SKILL.md +120 -0
  132. data/skills/nukadoko/references/checking-steps.md +200 -0
  133. data/skills/nukadoko/references/diagnosing.md +200 -0
  134. data/skills/nukadoko/references/done.md +99 -0
  135. data/skills/nukadoko/references/evidence.md +124 -0
  136. data/skills/nukadoko/references/examples/cli.md +230 -0
  137. data/skills/nukadoko/references/examples/http-api.md +342 -0
  138. data/skills/nukadoko/references/examples/workflow.md +173 -0
  139. data/skills/nukadoko/references/exploring.md +184 -0
  140. data/skills/nukadoko/references/fixtures.md +198 -0
  141. data/skills/nukadoko/references/from-prose.md +51 -0
  142. data/skills/nukadoko/references/judgments.md +95 -0
  143. data/skills/nukadoko/references/maintenance.md +97 -0
  144. data/skills/nukadoko/references/merge-risk.md +74 -0
  145. data/skills/nukadoko/references/parts.md +152 -0
  146. data/skills/nukadoko/references/perturbed-worlds.md +109 -0
  147. data/skills/nukadoko/references/program.md +200 -0
  148. data/skills/nukadoko/references/reviewing.md +200 -0
  149. data/skills/nukadoko/references/running.md +200 -0
  150. data/skills/nukadoko/references/scenario-rules.md +188 -0
  151. data/skills/nukadoko/references/states.md +66 -0
  152. data/skills/nukadoko/references/step-files.md +200 -0
  153. data/skills/nukadoko/references/verdicts.md +200 -0
  154. data/skills/nukadoko/references/writing-steps.md +200 -0
  155. data/skills/nukadoko/references/writing-the-feature.md +165 -0
  156. data/skills/nukadoko/scripts/claude-code-verdict-hook.mjs +99 -0
  157. data/skills/nukadoko/scripts/merge-risk.jq +56 -0
  158. data/skills/nukadoko-verification/SKILL.md +263 -0
  159. metadata +133 -1
@@ -0,0 +1,177 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # What `nuka check` can tell about a claim step's claims from the line it
5
+ # is bound on and the earlier lines of its scenario, in the TypeScript
6
+ # engine's words and order. A `from` value comes from the nearest earlier
7
+ # line that runs its one producer bound before this line, as a run fills
8
+ # it, so the producer's `returns` says what the value can hold: a path
9
+ # into it that is not there (`claim-path-unknown`), a path the step's own
10
+ # args drops (`claim-args-dropped`), a lacks-field on a field its object
11
+ # never keeps (`claim-field-stripped`), a count or an each on a value that
12
+ # is never an array (`claim-not-an-array`), and two sides that read one
13
+ # place, or an observe that receives the value it is compared with
14
+ # (`claim-compares-itself`). A line whose producer is missing or
15
+ # ambiguous is left to `from-order-violation`, and a type this module
16
+ # cannot read (`unknown`) is taken to have every path, so each finding can
17
+ # only end one way. A view reads text the step returned at run time, so no
18
+ # path after it is checked.
19
+ module Nukadoko
20
+ module Engine
21
+ module ClaimCheck
22
+ Producer = Data.define(
23
+ :line, #: Integer
24
+ :step, #: singleton(Step)
25
+ :key, #: String
26
+ )
27
+
28
+ # A place a side reads: the line of its producer, or observe, and the
29
+ # path from there, a view or a count as one segment of its own and an
30
+ # item of each as ANY.
31
+ Place = Data.define(
32
+ :origin, #: String
33
+ :segments, #: Array[String]
34
+ :text, #: String
35
+ )
36
+
37
+ INDEX = /\A(0|[1-9][0-9]*)\z/
38
+ ANY = "*"
39
+
40
+ module_function
41
+
42
+ #: (String?) -> Array[String]?
43
+ def segments_of(path)
44
+ return [] if path.nil?
45
+
46
+ parts = path.to_s.split(".", -1)
47
+ parts.all? { !_1.empty? } && !parts.empty? ? parts : nil
48
+ end
49
+
50
+ # Every finding about the claim step bound on `index` of `resolved`,
51
+ # the [step, captures] of each line, as [code, message].
52
+ #: (Array[[singleton(Step), Hash[String, untyped]]?], Array[Integer], Integer) -> Array[[String, String]]
53
+ def issues(resolved, lines, index)
54
+ step, captures = resolved[index]
55
+ claims = step&.claims
56
+ return [] unless step && claims
57
+
58
+ Run.new(step, claims, captures || {}, resolved, lines, index).issues
59
+ end
60
+
61
+ # The nearest earlier line that runs the one candidate producer bound
62
+ # before this line, or nil when none or several are.
63
+ #: (singleton(Step), String, Integer, Array[[singleton(Step), Hash[String, untyped]]?], Array[Integer]) -> Producer?
64
+ def producer(step, key, index, resolved, lines)
65
+ bound = (step.from[key] || []).filter_map do |candidate, field|
66
+ before = (0...index).to_a.reverse.find { resolved[_1]&.first == candidate }
67
+ before && Producer.new(line: lines[before], step: candidate, key: field.to_s)
68
+ end
69
+ bound.size == 1 ? bound.first : nil
70
+ end
71
+
72
+ # Follows `segments` through a schema as the run reads a value:
73
+ # { schema: type } at the end, { open: true } past a type this module
74
+ # cannot read, or { missing: index, keys: } at the first segment the
75
+ # type does not have.
76
+ #: (untyped, Array[String]) -> Hash[Symbol, untyped]
77
+ def walk(schema, segments)
78
+ return { open: true } if schema.nil?
79
+
80
+ current = schema #: untyped
81
+ segments.each_with_index do |segment, at|
82
+ current = current[1] while current.is_a?(Array) && current[0] == :nullable
83
+ case current
84
+ when Schema
85
+ field = current.fields.find { _1.name.to_s == segment }
86
+ return { missing: at, keys: current.fields.map { _1.name.to_s } } unless field
87
+
88
+ current = field.type
89
+ when Array
90
+ return { missing: at, keys: nil } unless current[0] == :array && segment.match?(INDEX)
91
+
92
+ current = current[1]
93
+ when :unknown then return { open: true }
94
+ else return { missing: at, keys: nil }
95
+ end
96
+ end
97
+ { schema: current }
98
+ end
99
+
100
+ NOUNS = { string: "a string", number: "a number", integer: "a number", boolean: "a boolean" }.freeze
101
+
102
+ # What a value of `type` can be, as nouns, when none of them is an
103
+ # array; nil when one may be.
104
+ #: (untyped) -> Array[String]?
105
+ def non_array_nouns(type)
106
+ case type
107
+ in [:nullable, inner] then (nouns = non_array_nouns(inner)) && (nouns | ["null"])
108
+ in [:enum, _] then ["a string"]
109
+ in Schema then ["an object"]
110
+ in Symbol then NOUNS[type] && [NOUNS.fetch(type)]
111
+ else nil
112
+ end
113
+ end
114
+
115
+ # The keys of the object at `segments`, which keeps no key it does not
116
+ # declare, as every object a Ruby schema declares does; nil for any
117
+ # other type.
118
+ #: (Schema?, Array[String]) -> Array[String]?
119
+ def object_keys_at(schema, segments)
120
+ type = walk(schema, segments)[:schema]
121
+ type = type[1] while type.is_a?(Array) && type[0] == :nullable
122
+ type.is_a?(Schema) ? type.fields.map { _1.name.to_s } : nil
123
+ end
124
+
125
+ # The field a lacks-field claim names on this line: a literal, or a
126
+ # plain string key a capture fills with its own text.
127
+ #: (Hash[Symbol, untyped], Hash[String, untyped], singleton(Step)) -> String?
128
+ def lacked_field(entry, captures, step)
129
+ return nil unless entry[:compare] == "lacks-field"
130
+
131
+ expected = entry[:expected]
132
+ name = if expected.is_a?(Hash) then expected[:literal].is_a?(String) ? expected[:literal] : nil
133
+ elsif [nil, "text"].include?(entry[:expected_view]) && step.args[expected]&.type == :string && captures[expected].is_a?(String)
134
+ entry[:expected_view] == "text" ? Spaces.trim_end(captures[expected]) : captures[expected]
135
+ end
136
+ name.nil? || Spaces.blank?(name) ? nil : name
137
+ end
138
+
139
+ # The segments a claim reads before its view, as the run reads them:
140
+ # its path, then its at when no view comes first; nil for a path that
141
+ # is not one.
142
+ #: (String?, String?, String?) -> Array[String]?
143
+ def read_segments(path, view, at)
144
+ segments = segments_of(path) or return nil
145
+ view.nil? && at ? [*segments, *at.to_s.split(".")] : segments
146
+ end
147
+
148
+ #: (String, String?, String?) -> String
149
+ def read_text(path, view, at) = view.nil? && at ? "#{path} at #{at}" : path
150
+
151
+ #: (Hash[Symbol, untyped]) -> Array[String]?
152
+ def item_segments(item)
153
+ actual = segments_of(item[:actual]&.to_s)
154
+ at = segments_of(item[:at]&.to_s)
155
+ actual && at ? [*actual, *at] : nil
156
+ end
157
+
158
+ #: (Place, String?, String?, bool?) -> Place
159
+ def shaped(place, view, at, count)
160
+ place = Place.new(origin: place.origin, segments: [*place.segments, "##{view}"], text: "#{place.text} as #{view}") if view
161
+ place = Place.new(origin: place.origin, segments: [*place.segments, *at.to_s.split(".")], text: "#{place.text} at #{at}") unless at.nil? || at.to_s.empty?
162
+ count ? Place.new(origin: place.origin, segments: [*place.segments, "#count"], text: "the count of #{place.text}") : place
163
+ end
164
+
165
+ #: (Place, Place) -> bool
166
+ def same_place?(a, b) = a.origin == b.origin && a.segments == b.segments
167
+
168
+ #: (Place, Place) -> bool
169
+ def overlap?(a, b)
170
+ return false unless a.origin == b.origin
171
+
172
+ size = [a.segments.size, b.segments.size].min
173
+ a.segments.first(size).zip(b.segments.first(size)).all? { |x, y| x == y || (x == ANY && y.to_s.match?(INDEX)) || (y == ANY && x.to_s.match?(INDEX)) }
174
+ end
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,293 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # The claim checks of one claim step on one scenario line, kept apart from
5
+ # ClaimCheck's own module functions, which rbs-inline would otherwise read
6
+ # into this class.
7
+ module Nukadoko
8
+ module Engine
9
+ # One claim step on one line.
10
+ class ClaimCheck::Run
11
+ #: (singleton(Step), Array[Hash[Symbol, untyped]], Hash[String, untyped], Array[[singleton(Step), Hash[String, untyped]]?], Array[Integer], Integer) -> void
12
+ def initialize(step, claims, captures, resolved, lines, index)
13
+ @step = step
14
+ @claims = claims
15
+ @captures = captures
16
+ @resolved = resolved
17
+ @lines = lines
18
+ @index = index
19
+ @from_keys = step.from.keys.map(&:to_s) #: Array[String]
20
+ @producers = {} #: Hash[String, ClaimCheck::Producer?]
21
+ @found = [] #: Array[[String, String]]
22
+ @not_observe = [] #: Array[[String, ClaimCheck::Place]]
23
+ end
24
+
25
+ #: () -> Array[[String, String]]
26
+ def issues
27
+ @claims.each_with_index { |claim, number| claim_issues(claim, "claims[#{number}]") }
28
+ observe_receives if @step.method_defined?(:observe)
29
+ @found
30
+ end
31
+
32
+ private
33
+
34
+ #: (String, String) -> void
35
+ def report(code, message) = @found << [code, %(Step "#{@step.step_name}"'s #{message})]
36
+
37
+ #: (String) -> ClaimCheck::Producer?
38
+ def producer_of(key)
39
+ return @producers[key] if @producers.key?(key)
40
+
41
+ @producers[key] = @from_keys.include?(key) ? ClaimCheck.producer(@step, key, @index, @resolved, @lines) : nil
42
+ end
43
+
44
+ #: (String?) -> ClaimCheck::Producer?
45
+ def from_producer(head)
46
+ head && @from_keys.include?(head) ? producer_of(head) : nil
47
+ end
48
+
49
+ # The type a from path ends in, reporting a path the producer does
50
+ # not return; nil when the path is not a from path, is unresolved,
51
+ # or ends in a type this module cannot read.
52
+ #: (String, String) -> untyped
53
+ def from_path(label, path)
54
+ segments = ClaimCheck.segments_of(path)
55
+ producer = from_producer(segments&.first) or return nil
56
+ upstream = [producer.key, *segments.to_a.drop(1)]
57
+ walked = ClaimCheck.walk(producer.step.returns, upstream)
58
+ if walked[:missing]
59
+ report("claim-path-unknown", missing(label, path, producer, walked, upstream))
60
+ return nil
61
+ end
62
+ walked[:schema]
63
+ end
64
+
65
+ #: (String, String, ClaimCheck::Producer, Hash[Symbol, untyped], Array[String]) -> String
66
+ def missing(label, path, producer, walked, segments)
67
+ at = walked[:missing]
68
+ where = at.zero? ? "at the top of its result" : "under #{segments.first(at).join(".")}"
69
+ has = if walked[:keys].nil? then ""
70
+ elsif walked[:keys].empty? then ", which has no keys"
71
+ else ", which has #{walked[:keys].join(", ")}"
72
+ end
73
+ %(#{label} reads "#{path}", and step "#{producer.step.step_name}" on line #{producer.line} returns nothing at "#{segments[at]}" #{where}#{has}. ) +
74
+ "The value would be undefined, so the claim would always break"
75
+ end
76
+
77
+ # A from path the step's own args takes out of the value before a
78
+ # claim reads it: the first key an object of args does not declare,
79
+ # where the producer may return one.
80
+ #: (String, String, Array[String]?) -> void
81
+ def args_drop(label, path, segments)
82
+ producer = from_producer(segments&.first) or return
83
+ read = segments || []
84
+ walked = ClaimCheck.walk(@step.args, read)
85
+ return unless walked[:missing] && walked[:missing] >= 1 && walked[:keys]
86
+ return if ClaimCheck.walk(producer.step.returns, [producer.key, *read.drop(1)])[:missing]
87
+
88
+ at = walked[:missing] #: Integer
89
+ keys = walked[:keys].empty? ? "no keys" : walked[:keys].join(", ")
90
+ report("claim-args-dropped",
91
+ "#{label} reads #{path}, and the step's own args declares #{read.first(at).join(".")} through an object schema that declares #{keys}, " \
92
+ "which drops #{read[at]} as a key it does not declare, or refuses it as a strict object. Step \"#{producer.step.step_name}\" on line #{producer.line} " \
93
+ "may return a value there: the run leaves a claim unjudged when its args schema changed the value it reads, and breaks it when the value is undefined, " \
94
+ "so the claim never holds, whatever the behaviour is")
95
+ end
96
+
97
+ #: (String, untyped) -> void
98
+ def expected_args_drop(label, expected)
99
+ return unless expected.is_a?(Hash) && expected[:from]
100
+
101
+ args_drop("#{label}.expected.from", expected[:from].to_s, ClaimCheck.segments_of(expected[:from].to_s))
102
+ end
103
+
104
+ #: (String, String, String, Array[String], String) -> void
105
+ def not_an_array(label, reads, path, inside, what)
106
+ segments = ClaimCheck.segments_of(path)
107
+ producer = from_producer(segments&.first) or return
108
+ walked = ClaimCheck.walk(producer.step.returns, [producer.key, *segments.to_a.drop(1), *inside])
109
+ nouns = walked.key?(:schema) ? ClaimCheck.non_array_nouns(walked[:schema]) : nil
110
+ return unless nouns
111
+
112
+ report("claim-not-an-array", %(#{label} #{reads} #{what}, and step "#{producer.step.step_name}" on line #{producer.line} returns #{nouns.join(" or ")} there, never an array, so the claim never holds, whatever the behaviour is))
113
+ end
114
+
115
+ #: (String, Hash[Symbol, untyped], String, Array[String], String, bool) -> void
116
+ def stripped(label, entry, actual_path, inside, path, where)
117
+ field = ClaimCheck.lacked_field(entry, @captures, @step) or return
118
+ segments = ClaimCheck.segments_of(actual_path)
119
+ producer = from_producer(segments&.first) or return
120
+ segments = segments.to_a
121
+ keys = ClaimCheck.object_keys_at(producer.step.returns, [producer.key, *segments.drop(1), *inside])
122
+ outcome = where ? "where never leaves an item out, whatever the behaviour is" : "the claim never breaks, whatever the behaviour is"
123
+ if keys.nil? || keys.include?(field)
124
+ args_keys = ClaimCheck.object_keys_at(@step.args, [*segments, *inside])
125
+ return if args_keys.nil? || args_keys.include?(field)
126
+
127
+ report("claim-args-dropped",
128
+ "#{label} is lacks-field #{JSON.generate(field)} on #{path}, and the step's own args declares that value through an object schema that declares " \
129
+ "#{args_keys.empty? ? "no keys" : args_keys.join(", ")}, which drops any key it does not declare. Step \"#{producer.step.step_name}\" on line #{producer.line} " \
130
+ "may return #{field}, and the run leaves a claim unjudged when its args schema took a key out of the value it reads, so #{outcome}")
131
+ return
132
+ end
133
+ report("claim-field-stripped",
134
+ "#{label} is lacks-field #{JSON.generate(field)} on #{path}, and step \"#{producer.step.step_name}\" on line #{producer.line} " \
135
+ "returns that value through an object schema that declares #{keys.empty? ? "no keys" : keys.join(", ")}, which drops any key it does not declare, or refuses it as a strict object. " \
136
+ "#{field} is never there, so #{outcome}")
137
+ end
138
+
139
+ #: (String, String) -> ClaimCheck::Place?
140
+ def place(source, path)
141
+ segments = ClaimCheck.segments_of(path) or return nil
142
+ return ClaimCheck::Place.new(origin: "observe", segments: segments, text: "the observe path #{path}") if source == "observe"
143
+
144
+ producer = producer_of(segments.first.to_s) or return nil
145
+ upstream = [producer.key, *segments.drop(1)]
146
+ ClaimCheck::Place.new(origin: "line:#{producer.line}", segments: upstream, text: %(#{upstream.join(".")} of step "#{producer.step.step_name}" on line #{producer.line}))
147
+ end
148
+
149
+ #: (untyped) -> ClaimCheck::Place?
150
+ def expected_place(expected)
151
+ return nil unless expected.is_a?(Hash)
152
+ return place("from", expected[:from].to_s) if expected[:from]
153
+ return place("observe", expected[:observe].to_s) if expected[:observe]
154
+
155
+ nil
156
+ end
157
+
158
+ #: (String, ClaimCheck::Place?, ClaimCheck::Place?) -> void
159
+ def compares_itself(label, actual, expected)
160
+ return unless actual && expected && ClaimCheck.same_place?(actual, expected)
161
+
162
+ where = actual.text == expected.text ? "#{actual.text} on both sides" : "#{actual.text} as its actual value and #{expected.text} as its expected value, one place"
163
+ report("claim-compares-itself", "#{label} reads #{where}, so it holds whatever the behaviour is")
164
+ end
165
+
166
+ #: (String, untyped) -> void
167
+ def note_expected_from(label, expected)
168
+ return unless expected.is_a?(Hash) && expected[:from]
169
+
170
+ at = place("from", expected[:from].to_s)
171
+ @not_observe << ["#{label}.expected", at] if at
172
+ end
173
+
174
+ #: (String, untyped, ClaimCheck::Place?, String) -> void
175
+ def note_actual_from(label, expected, actual, source)
176
+ return unless actual && source == "from" && expected.is_a?(Hash) && expected.key?(:observe)
177
+
178
+ @not_observe << ["#{label}.actual", actual]
179
+ end
180
+
181
+ # Each where clause and item claim of an each, with its label and
182
+ # whether it is a where clause.
183
+ #: (String, Hash[Symbol, untyped]) -> Array[[String, Hash[Symbol, untyped], bool]]
184
+ def each_entries(label, claim)
185
+ each = claim[:each]
186
+ [*(each[:where] || []).each_with_index.map { ["#{label}.each.where[#{_2}]", _1, true] },
187
+ *each[:claims].each_with_index.map { ["#{label}.each.claims[#{_2}]", _1, false] }]
188
+ end
189
+
190
+ #: (Hash[Symbol, untyped], String) -> void
191
+ def claim_issues(claim, label)
192
+ actual = claim[:actual].to_s
193
+ read_path = ClaimCheck.read_segments(actual, claim[:view], claim[:at]&.to_s)
194
+ args_drop("#{label}.actual", ClaimCheck.read_text(actual, claim[:view], claim[:at]&.to_s), read_path)
195
+ actual_schema = from_path("#{label}.actual", actual)
196
+ source = @from_keys.include?(ClaimCheck.segments_of(actual)&.first.to_s) ? "from" : "observe"
197
+ top = place(source, actual)
198
+ actual_place = top && ClaimCheck.shaped(top, claim[:view], claim[:at], claim[:count])
199
+ return each_issues(claim, label, actual, read_path, actual_schema, source, actual_place) if claim[:each]
200
+
201
+ expected = claim[:expected]
202
+ from_path("#{label}.expected.from", expected[:from].to_s) if expected.is_a?(Hash) && expected[:from]
203
+ expected_args_drop(label, expected)
204
+ at = claim[:view].nil? ? (claim[:at].nil? ? [] : ClaimCheck.segments_of(claim[:at].to_s)) : nil
205
+ named = claim[:at].nil? ? actual : "#{actual} at #{claim[:at]}"
206
+ not_an_array(label, "counts the items of", actual, at, named) if at && claim[:count] == true
207
+ stripped(label, claim, actual, at, named, false) if !actual_schema.nil? && at
208
+ note_expected_from(label, expected)
209
+ note_actual_from(label, expected, actual_place, source)
210
+ compares_itself(label, actual_place, expected_place(expected))
211
+ end
212
+
213
+ #: (Hash[Symbol, untyped], String, String, Array[String]?, untyped, String, ClaimCheck::Place?) -> void
214
+ def each_issues(claim, label, actual, read_path, actual_schema, source, actual_place)
215
+ each_at = claim[:view].nil? ? (claim[:at].nil? ? [] : ClaimCheck.segments_of(claim[:at].to_s)) : nil
216
+ not_an_array(label, "reads each item of", actual, each_at, claim[:at].nil? ? actual : "#{actual} at #{claim[:at]}") if each_at
217
+ entries = each_entries(label, claim)
218
+ if !actual_schema.nil? && claim[:view].nil? && claim[:at].nil?
219
+ item_paths(label, claim, actual_schema)
220
+ entries.each do |item_label, item, where|
221
+ inside = item[:view].nil? ? ClaimCheck.item_segments(item) : nil
222
+ next unless inside
223
+
224
+ named = inside.empty? ? "each item" : "#{inside.join(".")} of each item"
225
+ stripped(item_label, item, actual, ["0", *inside], "#{named} of #{actual}", where)
226
+ not_an_array(item_label, "counts the items of", actual, ["0", *inside], "#{named} of #{actual}") if item[:count] == true
227
+ end
228
+ end
229
+ entries.each do |item_label, item, _where|
230
+ expected = item[:expected]
231
+ from_path("#{item_label}.expected.from", expected[:from].to_s) if expected.is_a?(Hash) && expected[:from]
232
+ expected_args_drop(item_label, expected)
233
+ in_item = claim[:view].nil? && read_path ? ClaimCheck.read_segments(item[:actual]&.to_s, item[:view], item[:at]&.to_s) : nil
234
+ if in_item && read_path
235
+ text = ClaimCheck.read_text(item[:actual]&.to_s || "the item", item[:view], item[:at]&.to_s)
236
+ args_drop("#{item_label}.actual", "#{text} of each item of #{actual}", [*read_path, "0", *in_item])
237
+ end
238
+ note_expected_from(item_label, expected)
239
+ item_path = item[:actual].nil? ? [] : ClaimCheck.segments_of(item[:actual].to_s)
240
+ if actual_place.nil? || item_path.nil?
241
+ note_actual_from(item_label, expected, actual_place, source)
242
+ next
243
+ end
244
+ under = ClaimCheck::Place.new(origin: actual_place.origin, segments: [*actual_place.segments, ClaimCheck::ANY, *item_path], text: "#{item[:actual] || "item"} of each item of #{actual_place.text}")
245
+ item_place = ClaimCheck.shaped(under, item[:view], item[:at], item[:count])
246
+ note_actual_from(item_label, expected, item_place, source)
247
+ compares_itself(item_label, item_place, expected_place(expected))
248
+ end
249
+ end
250
+
251
+ # Item paths of an each against the array's item type.
252
+ #: (String, Hash[Symbol, untyped], untyped) -> void
253
+ def item_paths(label, claim, array_type)
254
+ element = ClaimCheck.walk(array_type, ["0"])
255
+ return unless element.key?(:schema)
256
+
257
+ each_entries(label, claim).each do |item_label, item, _where|
258
+ next if item[:actual].nil?
259
+
260
+ segments = ClaimCheck.segments_of(item[:actual].to_s) or next
261
+ walked = ClaimCheck.walk(element[:schema], segments)
262
+ next unless walked[:missing]
263
+
264
+ has = if walked[:keys].nil? then ""
265
+ elsif walked[:keys].empty? then ", which has no keys"
266
+ else ", which has #{walked[:keys].join(", ")}"
267
+ end
268
+ report("claim-path-unknown", %(#{item_label}.actual reads "#{item[:actual]}" on each item of #{claim[:actual]}, and an item has nothing at "#{segments[walked[:missing]]}"#{has}. ) +
269
+ "The value would be undefined, so the claim would always break")
270
+ end
271
+ end
272
+
273
+ # An observe that receives a value an expected from reads, or the
274
+ # value an actual from reads when the claim takes its expected
275
+ # value from observe, could return the value it is compared with.
276
+ #: () -> void
277
+ def observe_receives
278
+ withheld = ClaimRules.withheld_keys(@claims, @from_keys)
279
+ @from_keys.each do |key|
280
+ next if withheld.key?(key)
281
+
282
+ input = place("from", key) or next
283
+ hit = @not_observe.find { ClaimCheck.overlap?(input, _2) }
284
+ next unless hit
285
+
286
+ report("claim-compares-itself",
287
+ "observe receives args.#{key}, which is #{input.text}, and its #{hit[0]} reads #{hit[1].text}, so observe could return the value the claim compares it with. " \
288
+ "Take #{key} out of this step's from, so observe does not receive it, or fill it from another step")
289
+ end
290
+ end
291
+ end
292
+ end
293
+ end