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,469 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # Responsibility: the two sides of a top-level comparison claim, read as the
5
+ # TypeScript engine reads them: where each value sits (its place, whose
6
+ # label a reason names), the plain JSON copy a claim judges, and what stops
7
+ # a claim before any compare runs (a value that is undefined, one that is
8
+ # not plain JSON, a view that cannot read it, a claim that compares a value
9
+ # with itself or with one observe could have returned). It reads; the
10
+ # compares stay in Claims.compare. A claim on each item of a list is read
11
+ # elsewhere.
12
+ module Nukadoko
13
+ module Engine
14
+ module ClaimSides
15
+ Place = Data.define(
16
+ :origin, #: String
17
+ :segments, #: Array[String]
18
+ :label, #: String
19
+ :open, #: bool
20
+ )
21
+
22
+ # What one side read: a value, or why the claim breaks or is not judged.
23
+ Side = Data.define(
24
+ :place, #: Place
25
+ :found, #: Hash[Symbol, untyped]
26
+ )
27
+
28
+ OBSERVE = Place.new(origin: "observe", segments: [], label: "observe:", open: false)
29
+ LITERAL = Place.new(origin: "literal", segments: [], label: "literal:", open: false)
30
+ SAFE = 2**53 - 1
31
+ MAX_DEPTH = 256
32
+ INDEX = /\A(0|[1-9][0-9]*)\z/
33
+
34
+ # Where a claim reads from: what observe returned with the from values
35
+ # merged in, the args, whether the step observes, its from keys, the
36
+ # label of each args key's origin, and the args keys observe received.
37
+ Inputs = Data.define(
38
+ :observed, #: untyped
39
+ :args, #: untyped
40
+ :observes, #: bool
41
+ :from_keys, #: Array[String]
42
+ :labels, #: ^(String) -> String
43
+ :received, #: Array[String]
44
+ )
45
+
46
+ #: (Place, String?) -> Place
47
+ def self.place_at(place, path)
48
+ return place if path.nil? || path.empty?
49
+
50
+ separator = if place.open then " at "
51
+ elsif place.label.end_with?(":") then ""
52
+ else "."
53
+ end
54
+ Place.new(origin: place.origin, segments: place.segments + path.split("."), label: "#{place.label}#{separator}#{path}", open: false)
55
+ end
56
+
57
+ #: (Place, String?) -> Place
58
+ def self.place_viewed(place, view)
59
+ return place unless view
60
+
61
+ Place.new(origin: place.origin, segments: [*place.segments, "##{view}"], label: "#{place.label} as #{view}", open: true)
62
+ end
63
+
64
+ #: (Place, bool?) -> Place
65
+ def self.place_counted(place, count)
66
+ return place unless count == true
67
+
68
+ Place.new(origin: place.origin, segments: [*place.segments, "#count"], label: "count of #{place.label}", open: false)
69
+ end
70
+
71
+ #: (Place, Place) -> bool
72
+ def self.same_place?(a, b) = a.origin == b.origin && a.segments == b.segments
73
+
74
+ #: (Place, Place) -> bool
75
+ def self.overlap?(a, b)
76
+ size = [a.segments.size, b.segments.size].min
77
+ a.origin == b.origin && a.segments.take(size) == b.segments.take(size)
78
+ end
79
+
80
+ #: (String, Inputs) -> Place
81
+ def self.arg_place(key, inputs)
82
+ Place.new(origin: inputs.labels.(key), segments: [], label: inputs.labels.(key), open: false)
83
+ end
84
+
85
+ #: (String) -> Array[String]
86
+ def self.segments(path)
87
+ parts = path.split(".", -1)
88
+ parts.all? { !_1.empty? } ? parts : [path]
89
+ end
90
+
91
+ # The value at `segments` inside `root`, or the index of the first
92
+ # segment that finds nothing. nil is JSON's null, which is a value.
93
+ #: (untyped, Array[String]) -> Hash[Symbol, untyped]
94
+ def self.value_at(root, segments)
95
+ current = root
96
+ segments.each_with_index do |segment, index|
97
+ case current
98
+ when Hash
99
+ key = if current.key?(segment) then segment
100
+ elsif current.key?(segment.to_sym) then segment.to_sym
101
+ end
102
+ return { missing: index } if key.nil?
103
+
104
+ current = current[key]
105
+ when Array
106
+ return { missing: index } unless segment.match?(INDEX) && segment.to_i < current.size
107
+
108
+ current = current[segment.to_i]
109
+ else return { missing: index }
110
+ end
111
+ end
112
+ { found: current }
113
+ end
114
+
115
+ #: (untyped, Array[String]) -> Hash[Symbol, untyped]
116
+ def self.read_at(root, segments)
117
+ found = value_at(root, segments)
118
+ return found unless found.key?(:found)
119
+
120
+ taken = snapshot(found[:found])
121
+ taken[:refused] ? { refused: taken[:refused].merge(at: segments + taken[:refused][:at]) } : taken
122
+ end
123
+
124
+ #: (String) -> String
125
+ def self.article(word) = word.match?(/\A[aeiou]/i) ? "an #{word}" : "a #{word}"
126
+
127
+ # The plain JSON copy a claim judges, as JavaScript holds it, or where
128
+ # the first part that is not plain JSON sits and what it is. A whole
129
+ # float is the integer JavaScript's one number type would hold, so
130
+ # 2500.0 is judged and shown as 2500; a symbol is its name.
131
+ #: (untyped, ?Integer) -> Hash[Symbol, untyped]
132
+ def self.snapshot(value, max_depth = MAX_DEPTH)
133
+ ancestors = {} #: Hash[Integer, true]
134
+ refused = nil #: Hash[Symbol, untyped]?
135
+ refuse = lambda do |at, problem, limit = nil|
136
+ refused = { at: at, problem: problem, **(limit ? { limit: limit } : {}) }
137
+ nil
138
+ end
139
+ copy = lambda do |current, at|
140
+ case current
141
+ when nil, true, false, String then current
142
+ when Symbol then current.to_s
143
+ # JavaScript holds such an integer only as the nearest double.
144
+ when Integer then current.abs <= SAFE ? current : refuse.(at, "is the integer #{AriaSnapshot.js_number(current.to_f)}, beyond Number.MAX_SAFE_INTEGER")
145
+ when Float
146
+ next refuse.(at, "is NaN") if current.nan?
147
+ next refuse.(at, "is #{current.positive? ? "" : "-"}Infinity") if current.infinite?
148
+ next current unless current == current.floor
149
+ next current.to_i if current.abs <= SAFE
150
+
151
+ refuse.(at, "is the integer #{AriaSnapshot.js_number(current)}, beyond Number.MAX_SAFE_INTEGER")
152
+ when Array, Hash
153
+ next refuse.(at, "refers back to a value that holds it") if ancestors[current.object_id]
154
+ next refuse.([], "nests deeper than #{max_depth} levels", max_depth) if at.size >= max_depth
155
+
156
+ ancestors[current.object_id] = true
157
+ out = if current.is_a?(Array)
158
+ current.each_with_index.map do |item, index|
159
+ item = copy.(item, [*at, index.to_s])
160
+ break if refused
161
+
162
+ item
163
+ end
164
+ else
165
+ pairs = current.map do |key, item|
166
+ next refuse.(at, "has a key that is #{article(key.class.name.to_s)}") unless key.is_a?(String) || key.is_a?(Symbol)
167
+
168
+ item = copy.(item, [*at, key.to_s])
169
+ break if refused
170
+
171
+ [key.to_s, item]
172
+ end
173
+ refused ? nil : js_order(pairs.to_h)
174
+ end
175
+ ancestors.delete(current.object_id)
176
+ out
177
+ else
178
+ name = current.class.name
179
+ refuse.(at, name.nil? || name.empty? ? "is an object of no class" : "is #{article(name.split("::").last.to_s)}")
180
+ end
181
+ end
182
+ result = copy.(value, [])
183
+ refused ? { refused: refused } : { value: result }
184
+ end
185
+
186
+ # A JavaScript object lists its array-index keys first, in ascending
187
+ # order, then its other keys in the order they were added.
188
+ #: (Hash[String, untyped]) -> Hash[String, untyped]
189
+ def self.js_order(hash)
190
+ index = hash.keys.select { _1.match?(INDEX) && _1.to_i < 2**32 - 1 }
191
+ return hash if index.empty?
192
+
193
+ (index.sort_by(&:to_i) + (hash.keys - index)).to_h { [_1, hash[_1]] }
194
+ end
195
+
196
+ #: (Place) -> String
197
+ def self.name_of(place) = place.label.end_with?(":") ? "the value observe returned" : place.label
198
+
199
+ #: (Place, Hash[Symbol, untyped]) -> String
200
+ def self.refusal_reason(place, refusal)
201
+ name = name_of(place_at(place, refusal[:at].join(".")))
202
+ return "#{name} #{refusal[:problem]}, so the claim was not judged: the record keeps no value nested that deep" if refusal[:limit]
203
+
204
+ "#{name} #{refusal[:problem]}, which is not plain JSON, so the claim was not judged: a claim judges a plain JSON copy of the value it reads"
205
+ end
206
+
207
+ #: (Integer, Place, Array[String]) -> String
208
+ def self.first_missing(index, base, rest)
209
+ return "" if index >= rest.size - 1
210
+ return ", since #{name_of(base)} is undefined" if index.negative?
211
+
212
+ ", since #{name_of(place_at(base, rest.take(index).join(".")))} has nothing at #{rest[index]}"
213
+ end
214
+
215
+ #: (Hash[Symbol, untyped], Place, Array[String], Integer, ?^(Place) -> String) -> Side
216
+ def self.side_of(read, base, rest, skip, name = method(:name_of))
217
+ place = place_at(base, rest.join("."))
218
+ if read[:refused]
219
+ return Side.new(place: place, found: { unjudged: refusal_reason(base, read[:refused].merge(at: read[:refused][:at].drop(skip))) })
220
+ end
221
+ return Side.new(place: place, found: { broken: "#{name.(place)} is undefined#{first_missing(read[:missing] - skip, base, rest)}" }) if read.key?(:missing)
222
+
223
+ Side.new(place: place, found: { value: read[:value] })
224
+ end
225
+
226
+ #: (Array[String], Inputs, ?^(Place) -> String) -> Side
227
+ def self.arg_side(segments, inputs, name = method(:name_of))
228
+ head, *rest = segments
229
+ side_of(read_at(inputs.args, segments), arg_place(head.to_s, inputs), rest, 1, name)
230
+ end
231
+
232
+ # The side a claim's actual names, read at its path and, with no
233
+ # view, at its at too; `at_read` says the at was read here.
234
+ #: (Hash[Symbol, untyped], Inputs) -> [Side, bool]
235
+ def self.top_side(claim, inputs)
236
+ path = segments(claim[:actual].to_s)
237
+ read_segments = claim[:view].nil? && claim[:at] ? path + claim[:at].to_s.split(".") : path
238
+ at_read = read_segments.size > path.size
239
+ from = inputs.from_keys.include?(path.first) || !inputs.observes
240
+ read = from ? read_at(inputs.args, read_segments) : read_at(inputs.observed, read_segments)
241
+ base = from ? arg_place(path.first.to_s, inputs) : OBSERVE
242
+ skip = from ? 1 : 0
243
+ rest = path.drop(skip)
244
+ if at_read && read.key?(:missing) && read[:missing] >= path.size
245
+ place = place_at(base, rest.join("."))
246
+ return [Side.new(place: place, found: { broken: "#{place.label} has nothing at #{claim[:at]}" }), at_read]
247
+ end
248
+
249
+ [side_of(read, base, rest, skip), at_read]
250
+ end
251
+
252
+ # The value after a view, or why the view could not read it.
253
+ #: (String?, untyped) -> Hash[Symbol, untyped]
254
+ def self.apply_view(view, value)
255
+ return { value: value } unless view
256
+ return { reason: "view #{view} reads a string, and the value is #{article(Claims.json_type(value))}" } unless value.is_a?(String)
257
+ return { value: Claims.view(view, value) } unless view == "json"
258
+
259
+ parsed = begin
260
+ JsJson.parse(value)
261
+ rescue JSON::ParserError => e
262
+ return { reason: "view json could not parse the value: #{e.message.lines.first.to_s.strip}" }
263
+ end
264
+ snapshot(parsed)
265
+ end
266
+
267
+ #: (Side, String?, String?, ?bool) -> Side
268
+ def self.shaped(side, view, at, at_read = false)
269
+ viewed_place = place_viewed(side.place, view)
270
+ place = place_at(viewed_place, at)
271
+ return Side.new(place: place, found: side.found) unless side.found.key?(:value)
272
+ return Side.new(place: place, found: side.found) if at_read
273
+
274
+ viewed = apply_view(view, side.found[:value])
275
+ return Side.new(place: place, found: { broken: viewed[:reason] }) if viewed[:reason]
276
+ return Side.new(place: place, found: { unjudged: refusal_reason(viewed_place, viewed[:refused]) }) if viewed[:refused]
277
+ return Side.new(place: viewed_place, found: { value: viewed[:value] }) if at.nil?
278
+
279
+ read = read_at(viewed[:value], at.split("."))
280
+ return Side.new(place: place, found: { value: read[:value] }) if read.key?(:value)
281
+
282
+ Side.new(place: place, found: { broken: "#{viewed_place.label} has nothing at #{at}" })
283
+ end
284
+
285
+ #: (Side, bool?) -> Side
286
+ def self.counted(side, count)
287
+ return side unless count == true
288
+
289
+ place = place_counted(side.place, count)
290
+ return Side.new(place: place, found: side.found) unless side.found.key?(:value)
291
+
292
+ value = side.found[:value]
293
+ return Side.new(place: place, found: { broken: "count reads an array, and the value is #{article(Claims.json_type(value))}" }) unless value.is_a?(Array)
294
+
295
+ Side.new(place: place, found: { value: value.size })
296
+ end
297
+
298
+ # The expected side, and whether observe gave it.
299
+ #: (Hash[Symbol, untyped], Inputs) -> [Side, bool]
300
+ def self.expected_side(claim, inputs)
301
+ name = ->(place) { "the expected value #{place.label}" }
302
+ case claim[:expected]
303
+ in String => key then [shaped(arg_side([key], inputs, name), claim[:expected_view], nil), false]
304
+ in { from: String => key } then [arg_side(segments(key), inputs, name), false]
305
+ in { observe: String => path } then [side_of(read_at(inputs.observed, segments(path)), OBSERVE, segments(path), 0, name), true]
306
+ in { literal: literal } then [Side.new(place: LITERAL, found: { value: literal.is_a?(Array) ? [] : literal }), false]
307
+ end
308
+ end
309
+
310
+ # What stops a claim before any compare runs, or nil.
311
+ #: (Side, Side, bool, Inputs) -> Hash[Symbol, String]?
312
+ def self.precondition(actual, expected, from_observe, inputs)
313
+ [actual, expected].each do |side|
314
+ return { broken: side.found[:broken] } if side.found[:broken]
315
+ return { unjudged: side.found[:unjudged] } if side.found[:unjudged]
316
+ end
317
+ return { broken: "both sides read #{actual.place.label}, so the claim compares a value with itself" } if same_place?(actual.place, expected.place)
318
+
319
+ received = observe_received(expected.place, inputs, "the expected value reads")
320
+ received ||= observe_received(actual.place, inputs, "the actual value reads") if from_observe
321
+ return received if received
322
+ if from_observe && [true, false].include?(expected.found[:value])
323
+ return { broken: "the expected value #{expected.place.label} is a boolean observe computed, which is a judgment made in code: " \
324
+ "return the thing measured and let the claim compare it" }
325
+ end
326
+
327
+ nil
328
+ end
329
+
330
+ #: (Place, Inputs, String) -> Hash[Symbol, String]?
331
+ def self.observe_received(place, inputs, reads)
332
+ inputs.received.each do |key|
333
+ input = arg_place(key, inputs)
334
+ next unless overlap?(input, place)
335
+
336
+ return { broken: "observe received args.#{key}, which is #{input.label}, and #{reads} #{place.label}, " \
337
+ "so observe could return the value the claim compares it with" }
338
+ end
339
+ nil
340
+ end
341
+
342
+ # A top-level comparison claim, judged as Claims.judge_one reports one.
343
+ #: (Hash[Symbol, untyped], Inputs) -> Hash[Symbol, untyped]
344
+ def self.judge(claim, inputs)
345
+ raw, at_read = top_side(claim, inputs)
346
+ judge_comparison(claim, raw, inputs, at_read)
347
+ rescue StandardError => e
348
+ unjudgeable(claim, e)
349
+ end
350
+
351
+ # One comparison on a side already read: a top-level claim's, or one
352
+ # item's under an each.
353
+ #: (Hash[Symbol, untyped], Side, Inputs, ?bool) -> Hash[Symbol, untyped]
354
+ def self.judge_comparison(claim, raw, inputs, at_read = false)
355
+ expected, from_observe = expected_side(claim, inputs)
356
+ actual = counted(shaped(raw, claim[:view], claim[:at], at_read), claim[:count])
357
+ base = { **(actual.found.key?(:value) ? { actual: actual.found[:value] } : {}), compare: claim[:compare],
358
+ **(expected.found.key?(:value) ? { expected: expected.found[:value] } : {}) }
359
+ stop = precondition(actual, expected, from_observe, inputs)
360
+ return base.merge(holds: false, reason: stop[:broken] || stop[:unjudged], **(stop[:unjudged] ? { unjudged: true } : {})) if stop
361
+ return base.merge(holds: true, for_judgment: true) if claim[:compare] == "judged-as"
362
+
363
+ result = Claims.compare(claim[:compare], actual.found[:value], expected.found[:value])
364
+ base.merge(holds: result[:holds] == true, **(result[:reason] ? { reason: result[:reason] } : {}),
365
+ **(result[:stopped] ? { reason: result[:stopped], unjudged: true } : {}))
366
+ rescue StandardError => e
367
+ unjudgeable(claim, e)
368
+ end
369
+
370
+ #: (Hash[Symbol, untyped], StandardError) -> Hash[Symbol, untyped]
371
+ def self.unjudgeable(claim, error)
372
+ { compare: claim[:compare], holds: false, reason: "nukadoko could not judge this claim: #{error.message}", unjudged: true }
373
+ end
374
+
375
+ # The array an each reads, after its view and at.
376
+ #: (Hash[Symbol, untyped], Inputs) -> Side
377
+ def self.array_side(claim, inputs)
378
+ raw, at_read = top_side(claim, inputs)
379
+ shaped(raw, claim[:view], claim[:at], at_read)
380
+ end
381
+
382
+ # The side an item claim reads: the item, or a path inside it. The item
383
+ # is part of a snapshot, so a read inside it finds a value or nothing.
384
+ #: (untyped, Place, Hash[Symbol, untyped]) -> Side
385
+ def self.item_side(item, item_place, entry)
386
+ path = entry[:actual] ? segments(entry[:actual].to_s) : []
387
+ side_of(read_at(item, path), item_place, path, 0)
388
+ end
389
+
390
+ # What ends an each before any item: a declaration whose expected value
391
+ # observe received, or reads through what observe returned while observe
392
+ # received the array, does not depend on an item, and an empty array
393
+ # would hide it. Then a where clause that implies an item claim.
394
+ #: (Hash[Symbol, untyped], Place, Inputs) -> Hash[Symbol, String]?
395
+ def self.each_stop(claim, array, inputs)
396
+ entries = (claim[:each][:where] || []).each_with_index.map { ["each.where[#{_2}]", _1] } +
397
+ claim[:each][:claims].each_with_index.map { ["each.claims[#{_2}]", _1] }
398
+ entries.each do |label, entry|
399
+ expected, from_observe = expected_side(entry, inputs)
400
+ stop = observe_received(expected.place, inputs, "#{label}'s expected value reads")
401
+ stop ||= observe_received(array, inputs, "#{label} reads each item of") if from_observe
402
+ return stop if stop
403
+ end
404
+ implied = where_implication(claim, inputs)
405
+ implied ? { broken: implied } : nil
406
+ end
407
+
408
+ # The first where clause and item claim that read one value where the
409
+ # clause implies the claim whatever the value is, as the reason that
410
+ # breaks the each: the claim holds on every item where selects, so
411
+ # "every item" measures nothing.
412
+ #: (Hash[Symbol, untyped], Inputs) -> String?
413
+ def self.where_implication(claim, inputs)
414
+ value_of = lambda do |entry|
415
+ side, = expected_side(entry, inputs)
416
+ side.found.key?(:value) ? side.found : nil
417
+ end
418
+ (claim[:each][:where] || []).each_with_index do |where, where_index|
419
+ next unless where[:compare] == "not-equal" || ClaimRules::ORDER_COMPARES.include?(where[:compare])
420
+
421
+ path = where[:actual] ? segments(where[:actual].to_s) : []
422
+ path += where[:at].to_s.split(".") if where[:view].nil? && where[:at]
423
+ claim[:each][:claims].each_with_index do |entry, claim_index|
424
+ next unless ClaimRules.one_item_value?(where, entry)
425
+
426
+ selects = value_of.(where)
427
+ holds = value_of.(entry)
428
+ next unless selects && holds && where_implies?(where[:compare], selects[:value], entry[:compare], holds[:value])
429
+
430
+ viewed = where[:view] ? " as #{where[:view]}#{" at #{where[:at]}" if where[:at]}" : ""
431
+ what = "#{"count of " if where[:count] == true}#{path.empty? ? "value" : path.join(".")}#{viewed}"
432
+ return "each.where[#{where_index}] selects the items whose #{what} is #{where[:compare]} #{JsJson.generate(selects[:value])}, " \
433
+ "and every such item holds each.claims[#{claim_index}], #{entry[:compare]} #{JsJson.generate(holds[:value])}, " \
434
+ "whatever the data, so the claim measures nothing"
435
+ end
436
+ end
437
+ nil
438
+ end
439
+
440
+ # Whether every value a where clause selects holds an item claim on the
441
+ # same value: the same order direction with a bound at least as strict,
442
+ # not-equal against the same value, or an is-a whose kind is the one JSON
443
+ # type the clause can compare.
444
+ #: (String, untyped, String, untyped) -> bool
445
+ def self.where_implies?(where, where_value, claim, claim_value)
446
+ if claim == "is-a"
447
+ kind = claim_value.is_a?(String) ? Spaces.trim(claim_value).downcase : nil
448
+ type = if where == "not-equal" then Claims.json_type(where_value)
449
+ elsif ClaimRules::ORDER_COMPARES.include?(where) && where_value.is_a?(Numeric) then "number"
450
+ end
451
+ return !kind.nil? && !type.nil? && ClaimRules::KIND_JSON_TYPES[kind] == type
452
+ end
453
+ if where == "not-equal"
454
+ return claim == "not-equal" && Claims.json_type(where_value) == Claims.json_type(claim_value) && Claims.deep_equal?(where_value, claim_value)
455
+ end
456
+ return false unless where_value.is_a?(Numeric) && claim_value.is_a?(Numeric)
457
+
458
+ up = %w[greater-than at-least]
459
+ down = %w[less-than at-most]
460
+ strict = %w[greater-than less-than]
461
+ loose_to_strict = !strict.include?(where) && strict.include?(claim)
462
+ return loose_to_strict ? where_value > claim_value : where_value >= claim_value if up.include?(where) && up.include?(claim)
463
+ return loose_to_strict ? where_value < claim_value : where_value <= claim_value if down.include?(where) && down.include?(claim)
464
+
465
+ false
466
+ end
467
+ end
468
+ end
469
+ end