nukadoko 0.1.0 → 0.2.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 (147) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +291 -0
  3. data/README.ja.md +58 -5
  4. data/README.md +15 -3
  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 +274 -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/claims.rb +407 -30
  15. data/lib/nukadoko/cli.rb +802 -19
  16. data/lib/nukadoko/codes.json +942 -0
  17. data/lib/nukadoko/codes.rb +19 -0
  18. data/lib/nukadoko/command.rb +214 -12
  19. data/lib/nukadoko/criteria.rb +172 -0
  20. data/lib/nukadoko/definition.rb +33 -41
  21. data/lib/nukadoko/env_check.rb +90 -0
  22. data/lib/nukadoko/features.rb +80 -10
  23. data/lib/nukadoko/fixtures.rb +296 -10
  24. data/lib/nukadoko/http_recording.rb +68 -0
  25. data/lib/nukadoko/init.rb +130 -0
  26. data/lib/nukadoko/judge.rb +221 -0
  27. data/lib/nukadoko/judgment_file.rb +78 -0
  28. data/lib/nukadoko/judgments.rb +256 -0
  29. data/lib/nukadoko/literal.rb +129 -0
  30. data/lib/nukadoko/measured.rb +105 -0
  31. data/lib/nukadoko/order.rb +84 -0
  32. data/lib/nukadoko/parts.rb +42 -21
  33. data/lib/nukadoko/pattern.rb +11 -1
  34. data/lib/nukadoko/poll.rb +17 -6
  35. data/lib/nukadoko/program.rb +105 -17
  36. data/lib/nukadoko/project_fixtures.rb +17 -2
  37. data/lib/nukadoko/report_allure.rb +751 -0
  38. data/lib/nukadoko/report_messages.rb +167 -0
  39. data/lib/nukadoko/report_run.rb +169 -0
  40. data/lib/nukadoko/run.rb +865 -88
  41. data/lib/nukadoko/run_record.rb +532 -0
  42. data/lib/nukadoko/scaffold.rb +1015 -0
  43. data/lib/nukadoko/schema.rb +61 -4
  44. data/lib/nukadoko/secrets.rb +359 -0
  45. data/lib/nukadoko/selection.rb +163 -0
  46. data/lib/nukadoko/signature.rb +4 -1
  47. data/lib/nukadoko/skill.rb +36 -0
  48. data/lib/nukadoko/step.rb +62 -21
  49. data/lib/nukadoko/summary.rb +662 -0
  50. data/lib/nukadoko/table.rb +34 -2
  51. data/lib/nukadoko/text_fit.rb +179 -0
  52. data/lib/nukadoko/trace.rb +244 -0
  53. data/lib/nukadoko/trace_actions.rb +116 -0
  54. data/lib/nukadoko/tree_passes.rb +119 -0
  55. data/lib/nukadoko/verdict.rb +128 -0
  56. data/lib/nukadoko/version.rb +2 -1
  57. data/lib/nukadoko/vocabulary.rb +6 -0
  58. data/lib/nukadoko/walk.rb +305 -0
  59. data/lib/nukadoko/walk_files.rb +473 -0
  60. data/lib/nukadoko/walk_step.rb +149 -0
  61. data/lib/nukadoko.rb +36 -0
  62. data/llms.txt +296 -0
  63. data/sig/generated/nukadoko/aria_snapshot.rbs +331 -0
  64. data/sig/generated/nukadoko/aria_tree.rbs +142 -0
  65. data/sig/generated/nukadoko/base.rbs +136 -0
  66. data/sig/generated/nukadoko/check.rbs +255 -0
  67. data/sig/generated/nukadoko/claim_check.rbs +118 -0
  68. data/sig/generated/nukadoko/claim_check_run.rbs +88 -0
  69. data/sig/generated/nukadoko/claim_line.rbs +158 -0
  70. data/sig/generated/nukadoko/claim_rules.rbs +172 -0
  71. data/sig/generated/nukadoko/claims.rbs +165 -0
  72. data/sig/generated/nukadoko/cli.rbs +182 -0
  73. data/sig/generated/nukadoko/codes.rbs +16 -0
  74. data/sig/generated/nukadoko/command.rbs +109 -0
  75. data/sig/generated/nukadoko/criteria.rbs +83 -0
  76. data/sig/generated/nukadoko/definition.rbs +30 -0
  77. data/sig/generated/nukadoko/env_check.rbs +38 -0
  78. data/sig/generated/nukadoko/features.rbs +101 -0
  79. data/sig/generated/nukadoko/fixtures.rbs +152 -0
  80. data/sig/generated/nukadoko/http_recording.rbs +29 -0
  81. data/sig/generated/nukadoko/init.rbs +66 -0
  82. data/sig/generated/nukadoko/judge.rbs +64 -0
  83. data/sig/generated/nukadoko/judgment_file.rbs +39 -0
  84. data/sig/generated/nukadoko/judgments.rbs +120 -0
  85. data/sig/generated/nukadoko/literal.rbs +71 -0
  86. data/sig/generated/nukadoko/measured.rbs +42 -0
  87. data/sig/generated/nukadoko/order.rbs +54 -0
  88. data/sig/generated/nukadoko/parts.rbs +35 -0
  89. data/sig/generated/nukadoko/pattern.rbs +28 -0
  90. data/sig/generated/nukadoko/poll.rbs +40 -0
  91. data/sig/generated/nukadoko/program.rbs +101 -0
  92. data/sig/generated/nukadoko/project_fixtures.rbs +57 -0
  93. data/sig/generated/nukadoko/report_allure.rbs +268 -0
  94. data/sig/generated/nukadoko/report_messages.rbs +68 -0
  95. data/sig/generated/nukadoko/report_run.rbs +102 -0
  96. data/sig/generated/nukadoko/run.rbs +257 -0
  97. data/sig/generated/nukadoko/run_record.rbs +174 -0
  98. data/sig/generated/nukadoko/scaffold.rbs +328 -0
  99. data/sig/generated/nukadoko/schema.rbs +137 -0
  100. data/sig/generated/nukadoko/secrets.rbs +157 -0
  101. data/sig/generated/nukadoko/selection.rbs +73 -0
  102. data/sig/generated/nukadoko/signature.rbs +17 -0
  103. data/sig/generated/nukadoko/skill.rbs +20 -0
  104. data/sig/generated/nukadoko/step.rbs +115 -0
  105. data/sig/generated/nukadoko/summary.rbs +204 -0
  106. data/sig/generated/nukadoko/table.rbs +43 -0
  107. data/sig/generated/nukadoko/text_fit.rbs +70 -0
  108. data/sig/generated/nukadoko/trace.rbs +127 -0
  109. data/sig/generated/nukadoko/trace_actions.rbs +40 -0
  110. data/sig/generated/nukadoko/tree_passes.rbs +48 -0
  111. data/sig/generated/nukadoko/verdict.rbs +56 -0
  112. data/sig/generated/nukadoko/version.rbs +5 -0
  113. data/sig/generated/nukadoko/vocabulary.rbs +25 -0
  114. data/sig/generated/nukadoko/walk.rbs +139 -0
  115. data/sig/generated/nukadoko/walk_files.rbs +189 -0
  116. data/sig/generated/nukadoko/walk_step.rbs +50 -0
  117. data/sig/generated/nukadoko.rbs +15 -0
  118. data/sig/nukadoko.rbs +5 -48
  119. data/skills/nukadoko/SKILL.md +120 -0
  120. data/skills/nukadoko/references/checking-steps.md +200 -0
  121. data/skills/nukadoko/references/diagnosing.md +200 -0
  122. data/skills/nukadoko/references/done.md +99 -0
  123. data/skills/nukadoko/references/evidence.md +124 -0
  124. data/skills/nukadoko/references/examples/cli.md +230 -0
  125. data/skills/nukadoko/references/examples/http-api.md +339 -0
  126. data/skills/nukadoko/references/examples/workflow.md +173 -0
  127. data/skills/nukadoko/references/exploring.md +184 -0
  128. data/skills/nukadoko/references/fixtures.md +198 -0
  129. data/skills/nukadoko/references/from-prose.md +51 -0
  130. data/skills/nukadoko/references/judgments.md +95 -0
  131. data/skills/nukadoko/references/maintenance.md +97 -0
  132. data/skills/nukadoko/references/merge-risk.md +74 -0
  133. data/skills/nukadoko/references/parts.md +152 -0
  134. data/skills/nukadoko/references/perturbed-worlds.md +107 -0
  135. data/skills/nukadoko/references/program.md +200 -0
  136. data/skills/nukadoko/references/reviewing.md +200 -0
  137. data/skills/nukadoko/references/running.md +200 -0
  138. data/skills/nukadoko/references/scenario-rules.md +188 -0
  139. data/skills/nukadoko/references/states.md +66 -0
  140. data/skills/nukadoko/references/step-files.md +200 -0
  141. data/skills/nukadoko/references/verdicts.md +200 -0
  142. data/skills/nukadoko/references/writing-steps.md +200 -0
  143. data/skills/nukadoko/references/writing-the-feature.md +165 -0
  144. data/skills/nukadoko/scripts/claude-code-verdict-hook.mjs +99 -0
  145. data/skills/nukadoko/scripts/merge-risk.jq +56 -0
  146. data/skills/nukadoko-verification/SKILL.md +263 -0
  147. metadata +121 -1
@@ -0,0 +1,256 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "digest"
5
+ require "json"
6
+
7
+ # Judgments (docs/spec.md "Judgments"): the verdicts on a criterion that no
8
+ # compare can give, such as whether a text is friendly. A Then asks for one
9
+ # with a `judged-as` claim; the run reads the value and records it, and a
10
+ # reader judges it after the run and commits the judgment. nukadoko checks
11
+ # two things alone, both exactly: that a judgment cites a value this run's
12
+ # records hold (by its digest), and that the value did not change since.
13
+ # The judgment itself is never checked, and never folded into the machine
14
+ # verdict: a row keeps its verdict from the cells, and gains a `judgment`
15
+ # beside it, whose state has a gate of its own.
16
+ #
17
+ # Pure, and in the words the TypeScript engine uses. The files on disk are
18
+ # lib/nukadoko/judgment_file.rb's, and recording one is the judge command's.
19
+ #
20
+ # The digest is of the value's JSON with object keys sorted, so a program
21
+ # that writes its keys in another order does not make a judgment stale,
22
+ # and with numbers written the way JavaScript writes them, so both engines
23
+ # give one value one digest.
24
+ module Nukadoko
25
+ module Engine
26
+ module Judgments
27
+ # The order a row's state is chosen in: a judge's no first, then what
28
+ # no judgment can fix, then what a new judgment fixes.
29
+ STATE_ORDER = %w[misses uncitable stale pending judged].freeze
30
+ # The states in the order the summary's title counts them.
31
+ TITLE_ORDER = %w[judged misses stale pending uncitable].freeze
32
+ ARIA_LABEL = "ARIA snapshot of"
33
+ RECORD_FIX = "a judge who did not write the change reads the value against the sentence, then records it with nuka judge and --meets or --misses, --by, and --because"
34
+ UNCITABLE_FIX = "read the value whole from a channel the step record holds (a response, a process's output, a file) and narrow it with view and at, " \
35
+ "or return the locator or page from observe so nukadoko takes its ARIA snapshot"
36
+ ENTRY_STRINGS = %w[criterion then value_digest because by run_id at].freeze
37
+
38
+ module_function
39
+
40
+ #: (untyped) -> String
41
+ def canonical_json(value)
42
+ case value
43
+ when Array then "[#{value.map { canonical_json(_1) }.join(",")}]"
44
+ when Hash
45
+ pairs = value.to_a.map { |key, item| [key.to_s, item] }.sort_by { _1[0].codepoints }
46
+ "{#{pairs.map { |key, item| "#{JSON.generate(key)}:#{canonical_json(item)}" }.join(",")}}"
47
+ when Float then value.finite? ? AriaSnapshot.js_number(value) : "null"
48
+ when Integer, String, true, false, nil then JSON.generate(value)
49
+ else JSON.generate(value.to_s)
50
+ end
51
+ end
52
+
53
+ #: (untyped) -> String
54
+ def value_digest(value) = "sha256:#{Digest::SHA256.hexdigest(canonical_json(value))}"
55
+
56
+ # Where a judged value was measured, as a read names it: the channel,
57
+ # with the step record that holds it when that is another step's,
58
+ # `aria-snapshot` for a tree nukadoko took, or nil.
59
+ #: (Hash[Symbol, untyped], String?) -> String?
60
+ def measured_of(claim, own_record_id)
61
+ return "aria-snapshot" if claim[:actual_from].to_s.start_with?(ARIA_LABEL)
62
+
63
+ measured = claim[:measured] or return nil
64
+ measured[:step_record_id] == own_record_id ? measured[:channel] : "#{measured[:channel]} of #{measured[:step_record_id]}"
65
+ end
66
+
67
+ # Every value the passing required head executions of `row` read for a
68
+ # judgment, once per Then, claim, and value.
69
+ #: (Hash[Symbol, untyped], Array[Hash[Symbol, untyped]]) -> Array[Hash[Symbol, untyped]]
70
+ def judged_values(row, scenarios)
71
+ required = row[:cells].select { _1[:world] == "head" && _1[:role] == "required" }.map { _1[:environment] }
72
+ values = [] #: Array[Hash[Symbol, untyped]]
73
+ scenarios.each do |scenario|
74
+ next if scenario[:world] || scenario[:feature] != row[:feature] || scenario[:line] != row[:line]
75
+ next if scenario[:role] == "control" || scenario[:status] != "passed" || !required.include?(scenario[:environment])
76
+
77
+ scenario[:steps].each do |step|
78
+ (step[:claims] || []).each_with_index do |claim, index|
79
+ next if claim[:each] || claim[:outcome] != "for-judgment" || claim[:value_digest].nil?
80
+
81
+ digest = claim[:value_digest]
82
+ next if values.any? { _1[:step] == step[:text] && _1[:claim] == index && _1[:value_digest] == digest }
83
+
84
+ values << { line: step[:line] || scenario[:line], step: step[:text], claim: index, environment: scenario[:environment],
85
+ quality: claim[:expected_text], value_from: claim[:actual_from], value_digest: digest,
86
+ measured: measured_of(claim, step[:step_record_id]), record: step[:record], value: claim[:actual] }
87
+ end
88
+ end
89
+ end
90
+ values
91
+ end
92
+
93
+ #: (Hash[Symbol, untyped], ?Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
94
+ def cited(entry, extra = {})
95
+ { value_digest: entry[:value_digest], verdict: entry[:verdict], by: entry[:by], because: entry[:because], run_id: entry[:run_id], at: entry[:at], **extra }
96
+ end
97
+
98
+ # Where one value stands against the judgments of its criterion: a
99
+ # value in no recorded channel is `uncitable` whatever a file says; an
100
+ # entry with its digest decides it; entries of the same claim that
101
+ # cite only values this run did not read make it `stale`, citing the
102
+ # latest of them; otherwise it is `pending`. `read_now` is every digest
103
+ # the run read for the same claim, so a value one environment reads
104
+ # beside a judged value another reads is a value of its own to judge.
105
+ #: (Hash[Symbol, untyped], String, Array[Hash[Symbol, untyped]], ?Array[String]?) -> Hash[Symbol, untyped]
106
+ def read_against(value, criterion, entries, read_now = nil)
107
+ read_now ||= [value[:value_digest]]
108
+ rest = value.except(:value)
109
+ return { **rest, state: "uncitable" } if value[:measured].nil?
110
+
111
+ same = entries.select { _1[:criterion] == criterion && _1[:then] == value[:step] && _1[:claim] == value[:claim] }
112
+ match = same.find { _1[:value_digest] == value[:value_digest] }
113
+ return { **rest, state: match[:verdict] == "meets" ? "judged" : "misses", judgment: cited(match) } if match
114
+ return { **rest, state: "pending" } if same.empty? || same.any? { read_now.include?(_1[:value_digest]) }
115
+
116
+ latest = same.sort_by { _1[:at].to_s }.last #: Hash[Symbol, untyped]
117
+ difference = ClaimLine.equal_difference(value[:value], latest[:value])
118
+ { **rest, state: "stale", judgment: cited(latest, { value: latest[:value], **(difference ? { difference: difference } : {}) }) }
119
+ end
120
+
121
+ #: (Array[Hash[Symbol, untyped]], String, Array[Hash[Symbol, untyped]]) -> Array[Hash[Symbol, untyped]]
122
+ def read_each(values, criterion, entries)
123
+ values.map do |value|
124
+ now = values.select { _1[:step] == value[:step] && _1[:claim] == value[:claim] }.map { _1[:value_digest] }
125
+ read_against(value, criterion, entries, now)
126
+ end
127
+ end
128
+
129
+ # A value as a reason quotes it, cut at 120 characters of its JSON.
130
+ #: (untyped) -> String
131
+ def value_preview(value)
132
+ text = JSON.generate(value)
133
+ text.size > 120 ? "#{text[0, 117]}..." : text
134
+ end
135
+
136
+ #: (Hash[Symbol, untyped]) -> String
137
+ def quality(read) = read[:quality] || "what the sentence says"
138
+
139
+ #: (Hash[Symbol, untyped], untyped) -> String
140
+ def read_clause(read, value)
141
+ at = "line #{read[:line]}"
142
+ judgment = read[:judgment] || {}
143
+ case read[:state]
144
+ when "judged" then "#{judgment[:by]} judged the value #{at} read as #{quality(read)}: #{judgment[:because]}"
145
+ when "misses" then "#{judgment[:by]} judged the value #{at} read, #{value_preview(value)}, not #{quality(read)}: #{judgment[:because]}"
146
+ when "uncitable" then "#{at} asks for a judgment of #{read[:value_from]}, which no channel of a step record holds, so no judgment can cite it"
147
+ when "stale"
148
+ # Where the two differ says more than two cut previews of a long
149
+ # value, which can read the same.
150
+ if judgment[:difference].nil?
151
+ "#{at} read #{value_preview(value)}, and the judgment by #{judgment[:by]} in #{judgment[:run_id]} cited #{value_preview(judgment[:value])}, so it does not cover what ran"
152
+ else
153
+ "#{at} read another value than the one the judgment by #{judgment[:by]} in #{judgment[:run_id]} cited, so it does not cover what ran; " \
154
+ "#{judgment[:difference]} (actual is this run's value, expected the judged one)"
155
+ end
156
+ else "#{at} read #{value_preview(value)} for a judgment as #{quality(read)}, and no judgment cites it"
157
+ end
158
+ end
159
+
160
+ # The judgment of one row, or nil for a row that read no value for a
161
+ # judgment.
162
+ #: (Hash[Symbol, untyped], Array[Hash[Symbol, untyped]], Array[Hash[Symbol, untyped]]) -> Hash[Symbol, untyped]?
163
+ def row_judgment(row, scenarios, entries)
164
+ values = judged_values(row, scenarios)
165
+ return nil if values.empty?
166
+
167
+ reads = read_each(values, row[:name], entries)
168
+ state = STATE_ORDER.find { |candidate| reads.any? { _1[:state] == candidate } }
169
+ shown = reads.each_with_index.filter_map { |read, index| read_clause(read, values.fetch(index)[:value]) if read[:state] == state }
170
+ { state: state, reason: shown.join("; "), reads: reads }
171
+ end
172
+
173
+ # The table's rows with a `judgment` on each row whose required head
174
+ # cells all passed and read a value for a judgment. The verdict is
175
+ # never touched.
176
+ #: (Array[Hash[Symbol, untyped]], Array[Hash[Symbol, untyped]], ^(String) -> Array[Hash[Symbol, untyped]]) -> Array[Hash[Symbol, untyped]]
177
+ def with_judgments(rows, scenarios, entries_of)
178
+ rows.map do |row|
179
+ required = row[:cells].select { _1[:world] == "head" && _1[:role] == "required" }
180
+ next row if required.empty? || required.any? { _1[:outcome] != "pass" }
181
+
182
+ judgment = row_judgment(row, scenarios, entries_of.(row[:feature]))
183
+ judgment ? { **row, judgment: judgment } : row
184
+ end
185
+ end
186
+
187
+ #: (Array[Hash[Symbol, untyped]]) -> bool
188
+ def open?(rows) = rows.any? { _1[:judgment] && _1[:judgment][:state] != "judged" }
189
+
190
+ #: (Hash[Symbol, untyped], String) -> String
191
+ def show_command(row, run_id) = ["nuka", "judge", "#{row[:feature]}:#{row[:line]}", "--run", run_id].map { Selection.shell_word(_1) }.join(" ")
192
+
193
+ # One finding per row whose judgment leaves the gate red.
194
+ #: (Array[Hash[Symbol, untyped]], String) -> Array[Hash[Symbol, untyped]]
195
+ def findings(rows, run_id)
196
+ rows.flat_map do |row|
197
+ judgment = row[:judgment]
198
+ next [] if judgment.nil? || judgment[:state] == "judged"
199
+
200
+ base = { severity: "failed", code: "judgment-#{judgment[:state]}", file: row[:feature], line: row[:line], message: %(Scenario "#{row[:name]}" #{judgment[:reason]}) }
201
+ case judgment[:state]
202
+ when "uncitable" then [{ **base, fix: UNCITABLE_FIX }]
203
+ when "misses" then [{ **base, fix: "change the program until the value meets the sentence, then run it again and have it judged again", do: show_command(row, run_id) }]
204
+ else [{ **base, fix: RECORD_FIX, do: show_command(row, run_id) }]
205
+ end
206
+ end
207
+ end
208
+
209
+ # `entries` with `entry` in: it replaces an entry that judged the same
210
+ # value of the same claim, and is added beside the entries of others.
211
+ #: (Array[Hash[Symbol, untyped]], Hash[Symbol, untyped]) -> Array[Hash[Symbol, untyped]]
212
+ def add(entries, entry)
213
+ keys = %i[criterion then claim value_digest]
214
+ [*entries.reject { |other| keys.all? { other[_1] == entry[_1] } }, entry]
215
+ end
216
+
217
+ # A judgment file's text: its entries ordered by criterion, Then,
218
+ # claim, and time, so two judges who add one each make a diff of two
219
+ # hunks. Keys keep the order the TypeScript engine writes.
220
+ #: (Array[Hash[Symbol, untyped]]) -> String
221
+ def file_text(entries)
222
+ compare = ->(a, b) { a.to_s.codepoints <=> b.to_s.codepoints }
223
+ ordered = entries.sort do |a, b|
224
+ [compare.(a[:criterion], b[:criterion]), compare.(a[:then], b[:then]), a[:claim] <=> b[:claim], compare.(a[:at], b[:at]),
225
+ compare.(a[:value_digest], b[:value_digest])].find { _1 != 0 } || 0
226
+ end
227
+ "#{JSON.pretty_generate({ judgments: ordered })}\n"
228
+ end
229
+
230
+ # A judgment file's entries, or why its text holds none: a file a
231
+ # person edited by hand can break, and a judgment it no longer holds
232
+ # must read as unread, never as absent.
233
+ #: (String) -> Hash[Symbol, untyped]
234
+ def parse_file(text)
235
+ parsed = begin
236
+ JSON.parse(text)
237
+ rescue JSON::ParserError => e
238
+ return { problem: "it is not JSON: #{e.message.lines.first.to_s.chomp}" }
239
+ end
240
+ list = (parsed.is_a?(Hash) ? parsed["judgments"] : nil) #: untyped
241
+ return { problem: "it holds no judgments array" } unless list.is_a?(Array)
242
+
243
+ list.each_with_index do |entry, index|
244
+ return { problem: "judgments[#{index}] is not an object" } unless entry.is_a?(Hash)
245
+
246
+ missing = ENTRY_STRINGS.find { !entry[_1].is_a?(String) }
247
+ return { problem: "judgments[#{index}] has no text #{missing}" } if missing
248
+ unless entry["claim"].is_a?(Numeric) && %w[meets misses].include?(entry["verdict"])
249
+ return { problem: "judgments[#{index}] needs a claim number and a verdict of meets or misses" }
250
+ end
251
+ end
252
+ { entries: list.map { |entry| entry.to_h { |key, item| [key.to_sym, item] } } }
253
+ end
254
+ end
255
+ end
256
+ end
@@ -0,0 +1,129 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # The literal rule, as the TypeScript engine reads it. A literal expected
5
+ # value is in neither the sentence's data nor an earlier step's result, so
6
+ # it would let step code carry the criterion again ("the command succeeds"
7
+ # claiming 0). It is allowed only when the sentence states it: its text is
8
+ # a run of the pattern's fixed words joined by one space, a whole number
9
+ # from zero to twelve a fixed word spells (or once and twice), or the fixed
10
+ # value of a word in WORDS. A word inside a capture, an optional group, or
11
+ # an alternation is not fixed, since the sentence a line binds may not say
12
+ # it. A value WORDS fixes is compared with equal alone, or with not-equal
13
+ # right after "not", "neither", or "nor".
14
+ module Nukadoko
15
+ module Engine
16
+ module Literal
17
+ Statement = Data.define(:words, :by_table, :compare, :negated_by)
18
+
19
+ WORDS = { "no" => [0, "", []], "none" => [0, "", []], "nothing" => [0, "", []], "neither" => [0, "", []],
20
+ "never" => [0], "empty" => ["", []] }.freeze
21
+ NUMBERS = %w[zero one two three four five six seven eight nine ten eleven twelve].freeze
22
+ TIMES = { "once" => 1, "twice" => 2 }.freeze
23
+ NEGATING = %w[not neither nor].freeze
24
+ EDGE = /\A[.,;:!?"'`]+|[.,;:!?"'`]+\z/
25
+
26
+ module_function
27
+
28
+ # Every word of the pattern, with whether a matching sentence always holds it.
29
+ #: (String) -> Array[[String, bool]]
30
+ def words(pattern)
31
+ list = []
32
+ word = +""
33
+ fixed = true
34
+ finish = lambda do
35
+ text = word.gsub(EDGE, "")
36
+ list << [text, fixed] if !fixed || !text.empty?
37
+ word = +""
38
+ fixed = true
39
+ end
40
+ i = 0
41
+ while i < pattern.length
42
+ ch = pattern[i].to_s
43
+ if ch == "\\" && i + 1 < pattern.length
44
+ word << pattern[i + 1].to_s
45
+ i += 2
46
+ elsif ch.match?(/\s/)
47
+ finish.()
48
+ i += 1
49
+ elsif ["{", "("].include?(ch)
50
+ close = ch == "{" ? "}" : ")"
51
+ fixed = false
52
+ i += 1
53
+ i += pattern[i] == "\\" ? 2 : 1 while i < pattern.length && pattern[i] != close
54
+ i += 1
55
+ else
56
+ fixed = false if ch == "/"
57
+ word << ch
58
+ i += 1
59
+ end
60
+ end
61
+ finish.()
62
+ list
63
+ end
64
+
65
+ #: (untyped) -> String?
66
+ def text(literal)
67
+ case literal
68
+ when String then literal
69
+ when Integer then literal.to_s
70
+ when Float then literal.finite? ? (literal == literal.floor ? literal.to_i.to_s : literal.to_s) : nil
71
+ when true, false, nil then literal.nil? ? "null" : literal.to_s
72
+ end
73
+ end
74
+
75
+ #: (String) -> Integer?
76
+ def number_of(word)
77
+ lower = word.downcase
78
+ NUMBERS.index(lower) || TIMES[lower]
79
+ end
80
+
81
+ #: (untyped, untyped) -> bool
82
+ def same?(a, b) = a.is_a?(Array) && b.is_a?(Array) ? a.empty? && b.empty? : a.instance_of?(b.class) && a == b
83
+
84
+ #: (String, untyped) -> Statement?
85
+ def statement(pattern, literal)
86
+ all = words(pattern)
87
+ fixed = all.select(&:last).map(&:first)
88
+ said = text(literal)
89
+ return Statement.new(said, false, nil, nil) if said && !said.empty? && run_of?(fixed, said)
90
+
91
+ spelled = literal.is_a?(Integer) && fixed.find { number_of(_1) == literal }
92
+ return Statement.new(spelled, false, nil, nil) if spelled
93
+
94
+ all.each_with_index do |(word, is_fixed), index|
95
+ next unless is_fixed
96
+ next unless WORDS.fetch(word.downcase, []).any? { same?(literal, _1) }
97
+ next if word.downcase == "neither" && all.drop(index + 1).any? { |w, f| f && w.downcase == "nor" }
98
+
99
+ before = (index.positive? ? all[index - 1] : nil) #: [String, bool]?
100
+ return Statement.new(word, true, "not-equal", before[0]) if before && before[1] && NEGATING.include?(before[0].downcase)
101
+
102
+ return Statement.new(word, true, "equal", nil)
103
+ end
104
+ nil
105
+ end
106
+
107
+ #: (Array[String], String) -> bool
108
+ def run_of?(words, text)
109
+ (0...words.size).any? { |start| (start + 1..words.size).any? { words[start...(_1)].to_a.join(" ") == text } }
110
+ end
111
+
112
+ # How every pattern states `literal`, or nil when one does not: a line
113
+ # may bind any of them, and the first pattern's words name it.
114
+ #: (Array[String], untyped) -> Statement?
115
+ def statement_of_all(patterns, literal)
116
+ return nil if patterns.empty?
117
+
118
+ stated = patterns.filter_map { statement(_1, literal) }
119
+ first = stated.first
120
+ return nil unless first && stated.size == patterns.size
121
+
122
+ Statement.new(first.words, stated.any? { _1.by_table }, first.compare, first.negated_by)
123
+ end
124
+
125
+ #: (Array[String], untyped) -> untyped
126
+ def stated_by_all(patterns, literal) = statement_of_all(patterns, literal)&.words
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # The `measured` of each claim record: which recorded channel of which step
5
+ # record holds exactly the raw value the claim read, as the TypeScript
6
+ # engine names it. A claim on a `from` value looks in the record of the step
7
+ # that returned it; any other looks in the claim step's own channels. The
8
+ # channels are a process's stdout, stderr, and exit code, an HTTP
9
+ # response's status and body, and a changed file's content. A number
10
+ # channel matches only a number, and a number another channel of the record
11
+ # also holds names no channel: content alone cannot tell which one the value
12
+ # came from, and nil says less than a source that may be false. So does any
13
+ # value while a channel's content is unknown. It is information for a
14
+ # reader, never a condition of the claim.
15
+ module Nukadoko
16
+ module Engine
17
+ module Measured
18
+ module_function
19
+
20
+ # Every channel of a step record, in record order: processes, then
21
+ # HTTP responses, then files. A channel whose content is not on disk
22
+ # whole is unknown.
23
+ #: (String, Hash[Symbol, untyped], String?) -> Array[Hash[Symbol, untyped]]
24
+ def channels(dir, observed, http)
25
+ list = []
26
+ (observed[:processes] || []).each_with_index do |process, index|
27
+ %i[stdout stderr].each do |stream|
28
+ next unless process[stream]
29
+
30
+ text = read(File.join(dir, process[stream]))
31
+ list << (text ? { channel: "observed.processes[#{index}].#{stream}", text: text } : { channel: "observed.processes[#{index}].#{stream}", unknown: true })
32
+ end
33
+ list << { channel: "observed.processes[#{index}].exit_code", number: process[:exit_code] } if process[:exit_code].is_a?(Integer)
34
+ end
35
+ if http
36
+ (read(File.join(dir, http)) || "").each_line.with_index do |line, index|
37
+ entry = begin
38
+ JSON.parse(line, symbolize_names: true)
39
+ rescue JSON::ParserError
40
+ next
41
+ end
42
+ list << { channel: "http.jsonl[#{index}].status", number: entry[:status] } if entry[:status].to_i != 0
43
+ list << kept("http.jsonl[#{index}].response_body", entry[:response_body]) if entry[:response_body]
44
+ end
45
+ end
46
+ (observed[:files] || []).each_with_index do |file, index|
47
+ list << kept("observed.files[#{index}].content", file[:content]) if file[:content]
48
+ end
49
+ list
50
+ end
51
+
52
+ #: (String, untyped) -> Hash[Symbol, untyped]
53
+ def kept(channel, text)
54
+ text = { text: text } if text.is_a?(String)
55
+ whole = text[:text] && !text[:truncated] && !text[:incomplete]
56
+ whole ? { channel: channel, text: text[:text] } : { channel: channel, unknown: true }
57
+ end
58
+
59
+ #: (String) -> String?
60
+ def read(path)
61
+ File.read(path, encoding: "UTF-8")
62
+ rescue SystemCallError
63
+ nil
64
+ end
65
+
66
+ # The channel of `record_id` that holds `raw`, or nil; `skip` is a
67
+ # channel the other side already matched.
68
+ #: (String?, untyped, ^(String) -> Array[Hash[Symbol, untyped]], ?Hash[Symbol, untyped]?) -> Hash[Symbol, untyped]?
69
+ def channel_for(record_id, raw, channels_of, skip = nil)
70
+ return nil unless record_id
71
+
72
+ channels = channels_of.(record_id).reject { skip && skip[:step_record_id] == record_id && skip[:channel] == _1[:channel] }
73
+ return nil if channels.any? { _1[:unknown] }
74
+
75
+ match = channels.find { same?(raw, _1) }
76
+ return nil unless match
77
+ return nil if raw.is_a?(Numeric) && channels.any? { !_1.equal?(match) && holds_number?(_1, raw) }
78
+
79
+ { step_record_id: record_id, channel: match[:channel] }
80
+ end
81
+
82
+ #: (untyped, Hash[Symbol, untyped]) -> bool
83
+ def same?(raw, channel)
84
+ return false if channel[:unknown]
85
+ return raw.is_a?(Numeric) && raw == channel[:number] if channel.key?(:number)
86
+ return raw == channel[:text] if raw.is_a?(String)
87
+ return false if raw.nil?
88
+
89
+ JSON.parse(JSON.generate(raw)) == JSON.parse(channel[:text])
90
+ rescue JSON::ParserError
91
+ false
92
+ end
93
+
94
+ # Whether a channel holds `n` as a number of its own, not as part of another.
95
+ #: (Hash[Symbol, untyped], Numeric) -> bool
96
+ def holds_number?(channel, number)
97
+ return false if channel[:unknown]
98
+ return channel[:number] == number if channel.key?(:number)
99
+
100
+ written = Regexp.escape(number.to_s)
101
+ channel[:text].match?(/(?<![\w.-])#{written}(?![\w.])/)
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # The order `nuka run` plays its selection in, and the rerun line that plays
5
+ # it again. File order is the order the targets select. A shuffled order
6
+ # sorts the feature files, then each file's scenarios, by a key made from the
7
+ # seed and each one's path or line. A file stays whole, so one seed names one
8
+ # order at every concurrency. The key does not depend on what else was
9
+ # selected, so any part of a selection runs in the order the whole gave it,
10
+ # and a rerun line can name what ran before a failed scenario.
11
+ #
12
+ # The key is the TypeScript engine's, bit for bit: a seed names the same
13
+ # order in either engine. It works on 32-bit words, and on the UTF-16 code
14
+ # units of the text, as JavaScript's charCodeAt reads them.
15
+ module Nukadoko
16
+ module Engine
17
+ module Order
18
+ MAX_SEED = 0xffff_ffff
19
+ MASK = 0xffff_ffff
20
+ FILE = { kind: "file" }.freeze
21
+
22
+ # `--order`'s value read, or nil when it is none of file, shuffled,
23
+ # and shuffled:<seed>. shuffled alone draws its seed.
24
+ #: (String?) -> Hash[Symbol, untyped]?
25
+ def self.read(value)
26
+ return FILE if value.nil? || value == "file"
27
+ return { kind: "shuffled", seed: SecureRandom.random_number(MAX_SEED + 1) } if value == "shuffled"
28
+
29
+ seed = value[/\Ashuffled:(0|[1-9][0-9]{0,9})\z/, 1]&.to_i
30
+ seed && seed <= MAX_SEED ? { kind: "shuffled", seed: seed } : nil
31
+ end
32
+
33
+ #: (Hash[Symbol, untyped]) -> String
34
+ def self.text(order) = order[:kind] == "file" ? "file" : "shuffled:#{order[:seed]}"
35
+ #: (Hash[Symbol, untyped]) -> Array[String]
36
+ def self.flags(order) = order[:kind] == "file" ? [] : ["--order", text(order)]
37
+
38
+ # The scenarios in the run's order: by file in the order the files
39
+ # first appear, or by the shuffled keys.
40
+ #: (Array[Scenario], Hash[Symbol, untyped]) -> Array[Scenario]
41
+ def self.arrange(scenarios, order)
42
+ files = scenarios.group_by(&:uri)
43
+ return files.values.flatten if order[:kind] == "file"
44
+
45
+ seed = order[:seed]
46
+ files.keys.sort_by { [key(seed, _1), _1] }.flat_map do |uri|
47
+ files[uri].sort_by { [key(seed, "#{uri}:#{_1.line}"), _1.line] }
48
+ end
49
+ end
50
+
51
+ # The targets that run, in a shuffled order, what ran before `scenario`
52
+ # in its pass and then `scenario`. A file whose every scenario of the
53
+ # pass is among them is named whole when the run named it whole.
54
+ #: (Array[Scenario], Array[String], Scenario) -> Array[String]
55
+ def self.replay_targets(sequence, whole_files, scenario)
56
+ at = sequence.index { _1.uri == scenario.uri && _1.line == scenario.line }
57
+ ran = at ? sequence.first(at + 1) : [scenario]
58
+ in_pass = sequence.map(&:uri).tally
59
+ ran.chunk_while { _1.uri == _2.uri }.flat_map do |group|
60
+ uri = group.first.uri
61
+ whole_files.include?(uri) && group.size == in_pass[uri] ? [uri] : group.map { "#{uri}:#{_1.line}" }
62
+ end
63
+ end
64
+
65
+ # The seed is mixed before the text and the result after it, so two
66
+ # seeds a step apart give unrelated orders.
67
+ #: (Integer, String) -> Integer
68
+ def self.key(seed, text)
69
+ hash = mix(seed ^ 0x9e37_79b9)
70
+ units = text.encode("UTF-16LE").unpack("v*") #: Array[Integer]
71
+ units.each { |unit| hash = ((hash ^ unit) * 0x0100_0193) & MASK }
72
+ mix(hash)
73
+ end
74
+
75
+ #: (Integer) -> Integer
76
+ def self.mix(value)
77
+ hash = value & MASK
78
+ hash = ((hash ^ (hash >> 16)) * 0x85eb_ca6b) & MASK
79
+ hash = ((hash ^ (hash >> 13)) * 0xc2b2_ae35) & MASK
80
+ (hash ^ (hash >> 16)) & MASK
81
+ end
82
+ end
83
+ end
84
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rbs_inline: enabled
2
3
 
3
4
  require "time"
4
5
 
@@ -15,6 +16,7 @@ module Nukadoko
15
16
 
16
17
  attr_reader :calls
17
18
 
19
+ #: (singleton(Nukadoko::Step), Hash[Symbol, untyped]) -> void
18
20
  def initialize(step, fixtures)
19
21
  @fixtures = fixtures
20
22
  @stack = [step]
@@ -22,46 +24,65 @@ module Nukadoko
22
24
  @into = [@calls]
23
25
  end
24
26
 
27
+
28
+ #: () -> Proc
25
29
  def to_proc = method(:call).to_proc
26
30
 
31
+ # A refused call starts nothing, so it records nothing. Every other
32
+ # call is recorded in the order it started, with its result or its
33
+ # error, as the TypeScript engine records it.
34
+ #: (singleton(Nukadoko::Step), ?Hash[String | Symbol, untyped], **untyped) -> untyped
27
35
  def call(part, args = {}, **keywords)
28
36
  caller = @stack.last
29
37
  unless part.is_a?(Class) && part < Step && caller.parts.include?(part)
30
- raise Refused, "#{caller.step_name} calls #{part.respond_to?(:step_name) ? part.step_name : part.inspect}, which its parts do not declare"
38
+ raise Refused, %(Step "#{caller.step_name}" called "#{part.respond_to?(:step_name) ? part.step_name : part.inspect}" through call(), but does not list it in its own ) +
39
+ %("parts" (docs/spec.md "Parts"). Add it there. The fixture bag this step runs with is ) +
40
+ %(built from that list before "run" is called, so a part the list does not name has nothing to run with.)
31
41
  end
32
42
 
33
43
  args = args.merge(keywords).transform_keys(&:to_s)
34
- entry = { part: part.step_name, args: args, started_at: Time.now.utc.iso8601(3) }
44
+ entry = { step: part.step_name, args: args }
45
+ started = Time.now.utc.iso8601(3)
35
46
  @into.last << entry
36
- run(part, args, entry)
37
- ensure
38
- entry[:finished_at] = Time.now.utc.iso8601(3) if entry
47
+ run(part, args, entry, started)
39
48
  end
40
49
 
41
50
  private
42
51
 
43
- def run(part, args, entry)
52
+ #: (untyped, Hash[String, untyped], Hash[Symbol, untyped], String) -> untyped
53
+ def run(part, args, entry, started)
54
+ nested = []
55
+ fail_with = lambda do |kind, message|
56
+ entry.merge!(error: { message: message, kind: kind }, started_at: started, finished_at: Time.now.utc.iso8601(3), **(nested.any? ? { calls: nested } : {}))
57
+ end
44
58
  problems = part.args.problems(args)
45
- raise TypeError, "args of #{part.step_name}: #{problems.join("; ")}" if problems.any?
59
+ if problems.any?
60
+ message = "args validation failed: #{problems.join("; ")}"
61
+ fail_with.("args_invalid", message)
62
+ raise TypeError, message
63
+ end
46
64
 
47
65
  @stack << part
48
- @into << (entry[:calls] = [])
49
- value = part.new.run(**@fixtures.slice(*part.fixtures), args: part.inputs.load(args))
50
- problems = part.returns ? part.returns.problems(value) : []
51
- raise TypeError, "returns of #{part.step_name}: #{problems.join("; ")}" if problems.any?
52
-
53
- value = part.returns.strip(value) if part.returns
54
- entry[:result] = value
55
- part.returns ? part.returns.load(value) : value
56
- rescue StandardError => e
57
- entry[:error] ||= "#{e.class}: #{e.message}"
58
- raise
59
- ensure
60
- if @stack.last == part
66
+ @into << nested
67
+ begin
68
+ value = part.new.run(**part.keywords(@fixtures.slice(*part.fixtures), part.inputs.load(args)))
69
+ rescue StandardError => e
70
+ fail_with.("step_error", e.message) unless entry[:error]
71
+ raise
72
+ ensure
61
73
  @stack.pop
62
74
  @into.pop
63
- entry.delete(:calls) if entry[:calls]&.empty?
64
75
  end
76
+ value, problems = part.returned(value)
77
+ if problems.any?
78
+ message = "returns validation failed: #{problems.join("; ")}"
79
+ fail_with.("result_invalid", message)
80
+ raise TypeError, message
81
+ end
82
+
83
+ value = part.returns.strip(value) if part.returns
84
+ entry.merge!(result: value, started_at: started, finished_at: Time.now.utc.iso8601(3), **(nested.any? ? { calls: nested } : {}))
85
+ part.returns ? part.returns.load(value) : value
65
86
  end
66
87
  end
67
88
  end