nukadoko 0.6.0 → 0.7.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 (200) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +541 -3
  3. data/README.ja.md +20 -5
  4. data/README.md +12 -6
  5. data/docs/ci.ja.md +542 -0
  6. data/docs/ci.md +635 -0
  7. data/docs/commands.ja.md +1524 -0
  8. data/docs/commands.md +1281 -0
  9. data/docs/engines.json +19 -0
  10. data/docs/execution-events.ja.md +336 -0
  11. data/docs/execution-events.md +423 -0
  12. data/docs/glossary.json +1348 -0
  13. data/docs/guide.ja.md +733 -0
  14. data/docs/guide.md +815 -0
  15. data/docs/intent.ja.md +133 -0
  16. data/docs/intent.md +156 -0
  17. data/docs/layout.ja.md +442 -0
  18. data/docs/layout.md +399 -0
  19. data/docs/releasing.ja.md +186 -0
  20. data/docs/releasing.md +237 -0
  21. data/docs/ruby/reference.ja.md +302 -0
  22. data/docs/ruby/reference.md +368 -0
  23. data/docs/spec.ja.md +8916 -0
  24. data/docs/spec.md +11250 -0
  25. data/docs/tutorial.ja.md +451 -0
  26. data/docs/tutorial.md +458 -0
  27. data/docs/typescript/adopting.ja.md +231 -0
  28. data/docs/typescript/adopting.md +251 -0
  29. data/docs/typescript/doors.ja.md +301 -0
  30. data/docs/typescript/doors.md +329 -0
  31. data/docs/typescript/migration-playwright-test.ja.md +189 -0
  32. data/docs/typescript/migration-playwright-test.md +212 -0
  33. data/docs/typescript/migration.ja.md +391 -0
  34. data/docs/typescript/migration.md +505 -0
  35. data/docs/typescript/reference.ja.md +176 -0
  36. data/docs/typescript/reference.md +197 -0
  37. data/docs/typescript/upgrading.ja.md +1774 -0
  38. data/docs/typescript/upgrading.md +2224 -0
  39. data/docs/web-app.ja.md +627 -0
  40. data/docs/web-app.md +659 -0
  41. data/docs/writing-scenarios.ja.md +544 -0
  42. data/docs/writing-scenarios.md +563 -0
  43. data/lib/nukadoko/base.rb +12 -5
  44. data/lib/nukadoko/changed_files.rb +95 -0
  45. data/lib/nukadoko/check.rb +459 -51
  46. data/lib/nukadoko/claim_drops.rb +271 -0
  47. data/lib/nukadoko/claim_line.rb +24 -4
  48. data/lib/nukadoko/claim_rules.rb +143 -1
  49. data/lib/nukadoko/claim_sides.rb +157 -9
  50. data/lib/nukadoko/cli.rb +395 -48
  51. data/lib/nukadoko/codes.json +246 -21
  52. data/lib/nukadoko/codes.rb +21 -4
  53. data/lib/nukadoko/command.rb +120 -13
  54. data/lib/nukadoko/command_help.rb +128 -0
  55. data/lib/nukadoko/compat.rb +469 -0
  56. data/lib/nukadoko/criteria.rb +1 -1
  57. data/lib/nukadoko/definition.rb +157 -7
  58. data/lib/nukadoko/error_frames.rb +6 -3
  59. data/lib/nukadoko/events/count.rb +146 -0
  60. data/lib/nukadoko/events/counted.rb +114 -0
  61. data/lib/nukadoko/events/format.rb +257 -0
  62. data/lib/nukadoko/events/lists.rb +154 -0
  63. data/lib/nukadoko/events/path.rb +285 -0
  64. data/lib/nukadoko/events/read.rb +284 -0
  65. data/lib/nukadoko/events/reader.rb +490 -0
  66. data/lib/nukadoko/events/selection.rb +252 -0
  67. data/lib/nukadoko/events/tally.rb +438 -0
  68. data/lib/nukadoko/features.rb +2 -2
  69. data/lib/nukadoko/fixtures.rb +160 -35
  70. data/lib/nukadoko/http_recording.rb +6 -2
  71. data/lib/nukadoko/intent.rb +364 -0
  72. data/lib/nukadoko/intent_file.rb +81 -0
  73. data/lib/nukadoko/intent_view.rb +190 -0
  74. data/lib/nukadoko/js_json.rb +2 -1
  75. data/lib/nukadoko/judge.rb +2 -2
  76. data/lib/nukadoko/judgment_file.rb +1 -1
  77. data/lib/nukadoko/judgments.rb +4 -4
  78. data/lib/nukadoko/measure_mcp.rb +202 -0
  79. data/lib/nukadoko/parts.rb +8 -2
  80. data/lib/nukadoko/path_view.rb +252 -0
  81. data/lib/nukadoko/place_rows.rb +161 -0
  82. data/lib/nukadoko/program.rb +1265 -51
  83. data/lib/nukadoko/program_log.rb +122 -0
  84. data/lib/nukadoko/program_shape.rb +385 -0
  85. data/lib/nukadoko/project_fixtures.rb +43 -10
  86. data/lib/nukadoko/project_program.rb +5 -4
  87. data/lib/nukadoko/repeated_prefix.rb +310 -0
  88. data/lib/nukadoko/report_run.rb +3 -3
  89. data/lib/nukadoko/run.rb +1148 -159
  90. data/lib/nukadoko/run_record.rb +70 -46
  91. data/lib/nukadoko/scaffold.rb +24 -6
  92. data/lib/nukadoko/schema.rb +15 -2
  93. data/lib/nukadoko/secrets.rb +55 -5
  94. data/lib/nukadoko/states.rb +478 -0
  95. data/lib/nukadoko/step.rb +71 -2
  96. data/lib/nukadoko/step_fixtures.rb +52 -3
  97. data/lib/nukadoko/summary.rb +799 -29
  98. data/lib/nukadoko/table.rb +4 -2
  99. data/lib/nukadoko/text_fit.rb +20 -7
  100. data/lib/nukadoko/trace.rb +152 -45
  101. data/lib/nukadoko/tree_passes.rb +95 -33
  102. data/lib/nukadoko/underneath.rb +311 -0
  103. data/lib/nukadoko/version.rb +1 -1
  104. data/lib/nukadoko/vocabulary.rb +166 -23
  105. data/lib/nukadoko/walk.rb +2 -2
  106. data/lib/nukadoko/walk_step.rb +1 -1
  107. data/lib/nukadoko.rb +23 -0
  108. data/llms.txt +41 -34
  109. data/sig/generated/nukadoko/base.rbs +4 -4
  110. data/sig/generated/nukadoko/changed_files.rbs +42 -0
  111. data/sig/generated/nukadoko/check.rbs +147 -10
  112. data/sig/generated/nukadoko/claim_drops.rbs +135 -0
  113. data/sig/generated/nukadoko/claim_line.rbs +11 -0
  114. data/sig/generated/nukadoko/claim_rules.rbs +49 -4
  115. data/sig/generated/nukadoko/claim_sides.rbs +63 -12
  116. data/sig/generated/nukadoko/cli.rbs +80 -7
  117. data/sig/generated/nukadoko/codes.rbs +19 -4
  118. data/sig/generated/nukadoko/command.rbs +68 -1
  119. data/sig/generated/nukadoko/command_help.rbs +18 -0
  120. data/sig/generated/nukadoko/compat.rbs +392 -0
  121. data/sig/generated/nukadoko/criteria.rbs +1 -1
  122. data/sig/generated/nukadoko/definition.rbs +50 -0
  123. data/sig/generated/nukadoko/error_frames.rbs +1 -1
  124. data/sig/generated/nukadoko/events/count.rbs +69 -0
  125. data/sig/generated/nukadoko/events/counted.rbs +70 -0
  126. data/sig/generated/nukadoko/events/format.rbs +163 -0
  127. data/sig/generated/nukadoko/events/lists.rbs +47 -0
  128. data/sig/generated/nukadoko/events/path.rbs +161 -0
  129. data/sig/generated/nukadoko/events/read.rbs +109 -0
  130. data/sig/generated/nukadoko/events/reader.rbs +226 -0
  131. data/sig/generated/nukadoko/events/selection.rbs +100 -0
  132. data/sig/generated/nukadoko/events/tally.rbs +227 -0
  133. data/sig/generated/nukadoko/features.rbs +6 -4
  134. data/sig/generated/nukadoko/fixtures.rbs +67 -11
  135. data/sig/generated/nukadoko/http_recording.rbs +1 -1
  136. data/sig/generated/nukadoko/intent.rbs +132 -0
  137. data/sig/generated/nukadoko/intent_file.rbs +46 -0
  138. data/sig/generated/nukadoko/intent_view.rbs +69 -0
  139. data/sig/generated/nukadoko/judgment_file.rbs +1 -1
  140. data/sig/generated/nukadoko/judgments.rbs +1 -1
  141. data/sig/generated/nukadoko/measure_mcp.rbs +104 -0
  142. data/sig/generated/nukadoko/parts.rbs +2 -2
  143. data/sig/generated/nukadoko/path_view.rbs +105 -0
  144. data/sig/generated/nukadoko/place_rows.rbs +43 -0
  145. data/sig/generated/nukadoko/program.rbs +627 -23
  146. data/sig/generated/nukadoko/program_log.rbs +67 -0
  147. data/sig/generated/nukadoko/program_shape.rbs +147 -0
  148. data/sig/generated/nukadoko/project_fixtures.rbs +16 -3
  149. data/sig/generated/nukadoko/project_program.rbs +2 -1
  150. data/sig/generated/nukadoko/repeated_prefix.rbs +172 -0
  151. data/sig/generated/nukadoko/report_run.rbs +1 -1
  152. data/sig/generated/nukadoko/run.rbs +286 -41
  153. data/sig/generated/nukadoko/run_record.rbs +20 -12
  154. data/sig/generated/nukadoko/scaffold.rbs +3 -0
  155. data/sig/generated/nukadoko/schema.rbs +5 -0
  156. data/sig/generated/nukadoko/secrets.rbs +26 -3
  157. data/sig/generated/nukadoko/states.rbs +214 -0
  158. data/sig/generated/nukadoko/step.rbs +58 -1
  159. data/sig/generated/nukadoko/step_fixtures.rbs +39 -0
  160. data/sig/generated/nukadoko/summary.rbs +256 -2
  161. data/sig/generated/nukadoko/table.rbs +3 -2
  162. data/sig/generated/nukadoko/trace.rbs +108 -33
  163. data/sig/generated/nukadoko/tree_passes.rbs +36 -8
  164. data/sig/generated/nukadoko/underneath.rbs +141 -0
  165. data/sig/generated/nukadoko/vocabulary.rbs +45 -6
  166. data/skills/nukadoko/SKILL.md +18 -18
  167. data/skills/nukadoko/references/checking-steps.md +16 -83
  168. data/skills/nukadoko/references/diagnosing.md +6 -6
  169. data/skills/nukadoko/references/evidence.md +19 -6
  170. data/skills/nukadoko/references/exploring.md +22 -7
  171. data/skills/nukadoko/references/fixtures.md +12 -32
  172. data/skills/nukadoko/references/judgments.md +12 -0
  173. data/skills/nukadoko/references/maintenance.md +1 -1
  174. data/skills/nukadoko/references/merge-risk.md +1 -1
  175. data/skills/nukadoko/references/parts.md +46 -23
  176. data/skills/nukadoko/references/{perturbed-worlds.md → perturbed-systems.md} +30 -12
  177. data/skills/nukadoko/references/program.md +168 -169
  178. data/skills/nukadoko/references/reviewing.md +20 -20
  179. data/skills/nukadoko/references/ruby/checking-steps.md +106 -0
  180. data/skills/nukadoko/references/ruby/examples/cli.md +212 -0
  181. data/skills/nukadoko/references/ruby/fixtures.md +34 -0
  182. data/skills/nukadoko/references/ruby/program.md +112 -0
  183. data/skills/nukadoko/references/ruby/step-files.md +115 -0
  184. data/skills/nukadoko/references/running.md +34 -34
  185. data/skills/nukadoko/references/scenario-rules.md +7 -6
  186. data/skills/nukadoko/references/states.md +7 -1
  187. data/skills/nukadoko/references/step-files.md +41 -120
  188. data/skills/nukadoko/references/typescript/checking-steps.md +129 -0
  189. data/skills/nukadoko/references/{examples → typescript/examples}/cli.md +1 -1
  190. data/skills/nukadoko/references/{examples → typescript/examples}/http-api.md +6 -6
  191. data/skills/nukadoko/references/{examples → typescript/examples}/workflow.md +1 -1
  192. data/skills/nukadoko/references/typescript/fixtures.md +56 -0
  193. data/skills/nukadoko/references/typescript/program.md +111 -0
  194. data/skills/nukadoko/references/typescript/step-files.md +104 -0
  195. data/skills/nukadoko/references/verdicts.md +10 -10
  196. data/skills/nukadoko/references/writing-steps.md +67 -67
  197. data/skills/nukadoko/references/writing-the-feature.md +1 -1
  198. data/skills/nukadoko-verification/SKILL.md +13 -8
  199. metadata +103 -7
  200. data/skills/nukadoko/references/ruby.md +0 -80
@@ -28,6 +28,7 @@ module Nukadoko
28
28
  "the expected value and where it came from, whether it held, and the recorded channel that holds the actual value " \
29
29
  "(`measured: none` when no channel does, or when another channel holds the same number). " \
30
30
  "An expected value an earlier step returned or `observe` read gets the same, as `expected measured`."
31
+ LATE_NOTE = " A claim on a count of execution events that the system's stop counted again and judged otherwise says what the whole count came to, after its outcome: it was judged on lines still on their way."
31
32
  CONTROL_NOTE = " An entry marked `control` is a control's: a claim it broke is evidence that the scenario tells the behaviour apart, not a failure."
32
33
  LIMIT_KINDS = %w[left-out check-skipped control-not-proving no-claim judgment unmeasured after-record unrecorded-log unrecorded-events].freeze
33
34
  LIMIT_HEADINGS = {
@@ -38,7 +39,11 @@ module Nukadoko
38
39
  "no-claim" => ["Scenarios with no claim", "in each scenario below, no claim on a Then line was judged (held, broken, or unjudged)"],
39
40
  "judgment" => ["Held on a judgment",
40
41
  "each criterion below asks a reader to judge a value its Then read, and a committed judgment cites that value; nukadoko checks the citation, never the judgment"],
41
- "unmeasured" => ["Unmeasured actions", "each step below did something nukadoko records but cannot measure, so its record has no evidence of what that action did"]
42
+ "unmeasured" => ["Unmeasured actions", "each step below did something nukadoko records but cannot measure, so its record has no evidence of what that action did"],
43
+ "after-record" => ["After a step's record was written",
44
+ "each step below started an action, or has lines of the program's log or events, after its record was written: they are in that step's own files, and not in its record"],
45
+ "unrecorded-log" => ["The program's log", "what of it no step's record holds"],
46
+ "unrecorded-events" => ["The program's execution events", "what of them no step's record holds"]
42
47
  }.freeze
43
48
 
44
49
  module_function
@@ -46,7 +51,7 @@ module Nukadoko
46
51
  #: (Hash[Symbol, untyped]) -> String
47
52
  def render(doc)
48
53
  lines = [MARKER, *title(doc), *table(doc), *reasons(doc), *limits_section(doc), *condition(doc),
49
- *removed(doc), *claims(doc), *findings(doc), *rerun(doc)]
54
+ *removed(doc), *intent(doc), *paths(doc), *flags(doc), *routes(doc), *claims(doc), *findings(doc), *rerun(doc)]
50
55
  "#{lines.join("\n")}\n"
51
56
  end
52
57
 
@@ -124,7 +129,7 @@ module Nukadoko
124
129
  end
125
130
 
126
131
  #: (Hash[Symbol, untyped]) -> Array[Hash[Symbol, untyped]]
127
- def head_scenarios(doc) = doc[:scenarios].reject { _1[:world] }
132
+ def head_scenarios(doc) = doc[:scenarios].reject { _1[:system] }
128
133
 
129
134
  #: (Hash[untyped, untyped]) -> String
130
135
  def conditions_text(conditions)
@@ -172,14 +177,14 @@ module Nukadoko
172
177
 
173
178
  #: (Hash[Symbol, untyped]) -> Array[String]
174
179
  def table(doc)
175
- worlds = doc[:table][:worlds]
180
+ systems = doc[:table][:systems]
176
181
  rows = doc[:table][:rows]
177
182
  base = doc[:table][:base]
178
183
  return [] if rows.empty?
179
184
 
180
185
  heading = lambda do |column|
181
- declared = column[:world] == "head" ? doc[:environments].find { _1[:name] == column[:environment] }&.dig(:conditions) : nil #: Hash[untyped, untyped]?
182
- "#{column[:world]} #{column[:environment]}#{declared ? " (#{conditions_text(declared)})" : ""}"
186
+ declared = column[:system] == "head" ? doc[:environments].find { _1[:name] == column[:environment] }&.dig(:conditions) : nil #: Hash[untyped, untyped]?
187
+ "#{column[:system]} #{column[:environment]}#{declared ? " (#{conditions_text(declared)})" : ""}"
183
188
  end
184
189
  change = lambda do |row|
185
190
  next [] unless base
@@ -193,13 +198,13 @@ module Nukadoko
193
198
  [
194
199
  *base_line(doc),
195
200
  "",
196
- "| verdict | #{"judgment | " if judged}criterion | #{"change | " if base}#{worlds.map { cell_text(heading.(_1)) }.join(" | ")}#{" | seen" if seen} |",
197
- "| --- | #{"--- | " if judged}--- | #{"--- | " if base}#{worlds.map { "---" }.join(" | ")}#{" | ---" if seen} |",
201
+ "| verdict | #{"judgment | " if judged}criterion | #{"change | " if base}#{systems.map { cell_text(heading.(_1)) }.join(" | ")}#{" | seen" if seen} |",
202
+ "| --- | #{"--- | " if judged}--- | #{"--- | " if base}#{systems.map { "---" }.join(" | ")}#{" | ---" if seen} |",
198
203
  *rows.map do |row|
199
204
  written = [*row[:conditions], *row[:expects]]
200
205
  tags = written.empty? ? "" : " #{written.map { cell_code(_1) }.join(" ")}"
201
- cells = worlds.map do |column|
202
- cell = row[:cells].find { _1[:world] == column[:world] && _1[:environment] == column[:environment] }
206
+ cells = systems.map do |column|
207
+ cell = row[:cells].find { _1[:system] == column[:system] && _1[:environment] == column[:environment] }
203
208
  cell ? table_cell(cell) : ""
204
209
  end
205
210
  judgment = judged ? [row.dig(:judgment, :state).to_s] : []
@@ -211,7 +216,7 @@ module Nukadoko
211
216
  # The line above the table under `--base`: what the run compared with,
212
217
  # how many criteria changed, and whether the mapping changed and what
213
218
  # in it, since an unchanged criterion over a changed mapping is what
214
- # the old world was run for.
219
+ # the old system was run for.
215
220
  #: (Hash[Symbol, untyped]) -> Array[String]
216
221
  def base_line(doc)
217
222
  base = doc[:table][:base] or return []
@@ -228,15 +233,15 @@ module Nukadoko
228
233
  end
229
234
  unchanged = rows.select { _1[:change] == "unchanged" && _1[:verdict] != "failed" }
230
235
  ran_old = unchanged.count do |row|
231
- old = row[:cells].select { _1[:world] == "old" }
236
+ old = row[:cells].select { _1[:system] == "old" }
232
237
  old.any? && old.none? { _1[:outcome] == "not-run" }
233
238
  end
234
- old_ran = if ran_old == unchanged.size then "so the old world ran each unchanged criterion under the base's steps."
235
- else "so each unchanged criterion needed the old world, but it ran #{ran_old.zero? ? "none of them" : "only #{ran_old} of the #{unchanged.size}"}; each moved row says why."
239
+ old_ran = if ran_old == unchanged.size then "so the old system ran each unchanged criterion under the base's steps."
240
+ else "so each unchanged criterion needed the old system, but it ran #{ran_old.zero? ? "none of them" : "only #{ran_old} of the #{unchanged.size}"}; each moved row says why."
236
241
  end
237
242
  mapping_line = if doc[:table][:mapping_changed]
238
243
  "The mapping changed (#{parts.empty? ? "the base's could not be read" : parts.join("; ")}), #{old_ran}"
239
- else "The mapping did not change, so no unchanged criterion needed the old world."
244
+ else "The mapping did not change, so no unchanged criterion needed the old system."
240
245
  end
241
246
  compared = base[:commit].start_with?(base[:ref]) ? cell_code(base[:ref]) : "#{cell_code(base[:ref])} (`#{base[:commit][0, 7]}`)"
242
247
  # Judgments are not mapping, so the line names them apart: a reviewer
@@ -337,17 +342,17 @@ module Nukadoko
337
342
  end
338
343
  red.each do |row|
339
344
  lines << "- #{code_span("#{row[:feature]}:#{row[:line]}")} #{cell_text(row[:name])}: #{cell_text(row[:reason])}"
340
- world = { "moved" => "old", "unproven" => "base" }.fetch(row[:verdict], "head")
341
- cell = row[:cells].find { _1[:world] == world && _1[:role] == "required" && !%w[pass not-run].include?(_1[:outcome]) }
345
+ system = { "moved" => "old", "unproven" => "base" }.fetch(row[:verdict], "head")
346
+ cell = row[:cells].find { _1[:system] == system && _1[:role] == "required" && !%w[pass not-run].include?(_1[:outcome]) }
342
347
  next unless cell
343
348
 
344
349
  execution = doc[:scenarios].find do |scenario|
345
350
  at = scenario[:row] || { feature: scenario[:feature], line: scenario[:line] }
346
- at[:feature] == row[:feature] && at[:line] == row[:line] && (scenario[:world] || "head") == world &&
347
- scenario[:environment] == cell[:environment] && scenario[:status] != "passed"
351
+ at[:feature] == row[:feature] && at[:line] == row[:line] && (scenario[:system] || "head") == system &&
352
+ scenario[:environment] == cell[:environment] && (scenario[:status] != "passed" || Underneath.demoting(scenario))
348
353
  end
349
354
  failure = execution && describe_failure(execution)
350
- lines << " - #{world == "head" ? "" : "#{world}: "}#{failure}" if failure
355
+ lines << " - #{system == "head" ? "" : "#{system}: "}#{failure}" if failure
351
356
  end
352
357
  lines
353
358
  end
@@ -364,7 +369,7 @@ module Nukadoko
364
369
  return parts.join("; ")
365
370
  end
366
371
  step = scenario[:steps].find { !%w[passed skipped].include?(_1[:status]) }
367
- return nil unless step
372
+ return underneath_failure(scenario) unless step
368
373
 
369
374
  parts = ["#{cell_text("step \"#{step[:text]}\"")}: #{code_span(first_error_line(step.dig(:error, :message)))}"]
370
375
  exited_producers(step[:used]).each do |name, code, output|
@@ -378,6 +383,20 @@ module Nukadoko
378
383
  parts.join("; ")
379
384
  end
380
385
 
386
+ # Where a pass a failure underneath made broke failed: the step and
387
+ # each failure there that no tag of the scenario accepts.
388
+ #: (Hash[Symbol, untyped]) -> String?
389
+ def underneath_failure(scenario)
390
+ first = Underneath.demoting(scenario)
391
+ return nil unless first
392
+
393
+ at = scenario[:steps][first[:step]]
394
+ unexpected = Underneath.failures(scenario).select { _1[:step] == first[:step] && _1[:expected_by].nil? }
395
+ parts = ["#{cell_text("step \"#{at[:text]}\"")}: #{code_span(unexpected.map { Underneath.text(_1) }.join("; "))}"]
396
+ parts << "record #{code_span(at[:record])}" if at[:record]
397
+ parts.join("; ")
398
+ end
399
+
381
400
  #: (Array[Hash[Symbol, untyped]]?) -> Array[[untyped, Integer, String]]
382
401
  def exited_producers(used)
383
402
  (used || []).filter_map do |entry|
@@ -433,9 +452,11 @@ module Nukadoko
433
452
  by_place = ->(a, b) { [a[:feature].to_s, a[:line].to_i] <=> [b[:feature].to_s, b[:line].to_i] }
434
453
  list.concat(unjudged.uniq { [_1[:feature], _1[:line]] }.map do |scenario|
435
454
  { kind: "no-claim", feature: scenario[:feature], line: scenario[:line], text: scenario[:scenario],
436
- detail: "no claim on a Then line was judged in it", fix: "end the scenario with a Then whose step declares claims" }
455
+ detail: scenario[:judged_by] == "compat" ? "no claim judged it, and a compat step in Then position may have, by throwing or not, where nukadoko cannot see what it checked" : "no claim on a Then line was judged in it", fix: "end the scenario with a Then whose step declares claims" }
437
456
  end.sort(&by_place))
438
457
  list.concat(judgment_limits(rows))
458
+ list.concat(step_limits(head).sort(&by_place))
459
+ (doc[:systems] || []).each { list.concat(system_limits(_1)) }
439
460
  list.sort_by.with_index { |limit, index| [LIMIT_KINDS.index(limit[:kind]), index] }
440
461
  end
441
462
 
@@ -459,9 +480,133 @@ module Nukadoko
459
480
  end
460
481
  end
461
482
 
483
+ # How many unmeasured actions one entry names before it counts the rest.
484
+ UNMEASURED_SHOWN = 3
485
+
486
+ # Per step: what it did that nukadoko saw and could not measure
487
+ # (`observed.unmeasured`), and what reached its files after its record
488
+ # was written. Under `--repeat` the same step with the same gap is one
489
+ # entry, citing its first record.
490
+ #: (Array[Hash[Symbol, untyped]]) -> Array[Hash[Symbol, untyped]]
491
+ def step_limits(head)
492
+ limits = {} #: Hash[Hash[Symbol, untyped], Hash[Symbol, untyped]]
493
+ add = ->(limit) { limits[limit.except(:record)] ||= limit }
494
+ head.each do |scenario|
495
+ scenario[:steps].each do |step|
496
+ at = { feature: scenario[:feature], line: step[:line] || scenario[:line], text: step[:text], environment: scenario[:environment], record: step[:record] }.compact
497
+ unmeasured = step.dig(:observed, :unmeasured) || []
498
+ if unmeasured.any?
499
+ shown = unmeasured.first(UNMEASURED_SHOWN).map { "#{_1[:kind].sub("_", " ")} #{code_span(_1[:detail])}" }
500
+ shown << "and #{unmeasured.size - UNMEASURED_SHOWN} more" if unmeasured.size > UNMEASURED_SHOWN
501
+ fix = unmeasured.any? { _1[:kind] == "child_process" } ? { fix: "start a process with `run_command` to record it" } : {}
502
+ add.({ kind: "unmeasured", **at, detail: shown.join(", "), **fix })
503
+ end
504
+ unjudged = unjudged_failures(step)
505
+ add.({ kind: "unmeasured", **at, detail: unjudged.join(", ") }) if unjudged.any?
506
+ late = late_items(step)
507
+ add.({ kind: "after-record", **at, detail: late.join(", ") }) if late.any?
508
+ end
509
+ end
510
+ limits.values
511
+ end
512
+
513
+ # What of a step's failures underneath could not be seen: its cell
514
+ # was judged without them.
515
+ #: (Hash[Symbol, untyped]) -> Array[String]
516
+ def unjudged_failures(step)
517
+ unseen = step.dig(:underneath, :unseen)
518
+ return [] unless unseen
519
+
520
+ http = if !unseen.key?(:http_lines) then []
521
+ elsif unseen[:http_lines].nil? then ["#{code_span("http.jsonl")} could not be read, so no response status of the step was judged"]
522
+ else ["#{count_of(unseen[:http_lines], "request", "requests")} with no readable line in #{code_span("http.jsonl")}, whose status was not judged"]
523
+ end
524
+ errors = unseen[:page_errors] ? ["#{count_of(unseen[:page_errors], "page error", "page errors")} past the record's cap, not judged"] : []
525
+ http + errors
526
+ end
527
+
528
+ # What reached a step's own files after its record was written: this
529
+ # is the one place that says those lines exist, and names their file.
530
+ #: (Hash[Symbol, untyped]) -> Array[String]
531
+ def late_items(step)
532
+ items = [] #: Array[String]
533
+ started = step[:after_step_end]
534
+ if started
535
+ parts = [*("#{count_of(started[:requests], "request", "requests")} in #{code_span(started[:http] || "http.jsonl")}" if started[:requests].to_i.positive?),
536
+ *(count_of(started[:processes], "process", "processes") if started[:processes].to_i.positive?),
537
+ *("#{count_of(started[:mcp], "MCP call", "MCP calls")} in #{code_span("mcp.jsonl")}" if started[:mcp].to_i.positive?)]
538
+ items << "started after the step ended: #{parts.join(", ")}" if parts.any?
539
+ end
540
+ # A step of a system of more than one service names the service whose
541
+ # log a late line is in.
542
+ named = (step.dig(:observed, :program_log, :services) || {}).size > 1
543
+ lates = [*(step[:program_log_late] || {}).map { |service, late| [late, named ? "service #{service}'s log" : "the program's log"] },
544
+ *(step[:events_late] || {}).map { |service, late| [late, named ? "service #{service}'s events" : "the program's events"] }]
545
+ lates.each do |late, what|
546
+ next unless late
547
+
548
+ lines = count_of(late[:lines], "line", "lines")
549
+ items << (late[:file].nil? ? "#{lines} of #{what} after the step ended, which could not be written" : "#{lines} of #{what} after the step ended, in #{code_span(late[:file])}")
550
+ end
551
+ items
552
+ end
553
+
554
+ # Once per service of each system, what of the program's log and
555
+ # execution events no step's record holds. A system of more than one
556
+ # service names the service on each entry. A head step's late lines
557
+ # are its own entry, so the system names its late count only when it
558
+ # is the pattern a program that buffers its output on a pipe makes,
559
+ # or when the system is the base's or the old one.
560
+ #: (Hash[Symbol, untyped]) -> Array[Hash[Symbol, untyped]]
561
+ def system_limits(system)
562
+ services = system[:services] || []
563
+ services.flat_map { service_limits(system, _1, services.size > 1) }
564
+ end
565
+
566
+ #: (Hash[Symbol, untyped], Hash[Symbol, untyped], bool) -> Array[Hash[Symbol, untyped]]
567
+ def service_limits(system, service, named)
568
+ tree = system[:tree] || "head"
569
+ at = { environment: system[:name], system: tree, **(named ? { service: service[:name] } : {}) }
570
+ limits = [] #: Array[Hash[Symbol, untyped]]
571
+ not_read = "not read, since the program runs at #{service[:url] || "a URL"}, which nukadoko did not start"
572
+ late_log = ->(count) { "#{count_of(count, "line", "lines")} carrying a step's trace id arrived after that step ended (#{code_span(Trace::Log.late_file(service[:name].to_s))})" }
573
+ if service.key?(:program_log) && service[:program_log].nil?
574
+ limits << { kind: "unrecorded-log", **at, detail: not_read }
575
+ elsif (log = service[:program_log]) && log[:stamped].positive?
576
+ if log[:tagged].zero?
577
+ limits << { kind: "unrecorded-log", **at, detail: "carries no trace ids, so each step's slice of it is empty" }
578
+ elsif log[:late].positive? && log[:lines].zero?
579
+ limits << { kind: "unrecorded-log", **at,
580
+ detail: "#{late_log.(log[:late])}, and none in time, which is what a program that buffers its output on a pipe looks like",
581
+ fix: "if that is the cause, set `STDOUT.sync = true` in Ruby, or `PYTHONUNBUFFERED=1` for Python" }
582
+ elsif log[:late].positive? && tree != "head"
583
+ limits << { kind: "unrecorded-log", **at, detail: late_log.(log[:late]) }
584
+ end
585
+ end
586
+ if service.key?(:events) && service[:events].nil?
587
+ limits << { kind: "unrecorded-events", **at, detail: not_read }
588
+ elsif (events = service[:events])
589
+ if events[:lines].zero?
590
+ limits << { kind: "unrecorded-events", **at, detail: "the program wrote no line to the file #{code_span("{events}")} names, so no step's record has an event" }
591
+ else
592
+ kinds = (events[:undefined_kinds] || {}).to_a
593
+ facts = [
594
+ *("#{count_of(events[:late], "line", "lines")} carrying a step's trace id arrived after that step ended (#{code_span(Events.late_file(service[:name].to_s))})" if events[:late].positive? && tree != "head"),
595
+ *("#{count_of(events[:unreadable], "line is", "lines are")} not one readable line of the format" if events[:unreadable].positive?),
596
+ *("#{count_of(kinds.sum { _2 }, "line names", "lines name")} a kind the format does not define (#{kinds.map { code_span(_1.first.to_s) }.join(", ")})" if kinds.any?)
597
+ ]
598
+ if facts.any?
599
+ copy = events[:file].nil? ? "the system's copy of the file could not be written" : "the whole file is in #{code_span(events[:file])}"
600
+ limits << { kind: "unrecorded-events", **at, detail: "#{facts.join("; ")}; #{copy}" }
601
+ end
602
+ end
603
+ end
604
+ limits
605
+ end
606
+
462
607
  #: (Hash[Symbol, untyped], Array[Hash[Symbol, untyped]]) -> Hash[Symbol, untyped]?
463
608
  def control_limit(row, head)
464
- controls = row[:cells].select { _1[:world] == "head" && _1[:role] == "control" }
609
+ controls = row[:cells].select { _1[:system] == "head" && _1[:role] == "control" }
465
610
  return nil if controls.empty?
466
611
 
467
612
  outcomes = controls.map { _1[:outcome] }
@@ -483,7 +628,7 @@ module Nukadoko
483
628
 
484
629
  #: (Hash[Symbol, untyped], bool) -> String
485
630
  def limit_place(limit, named)
486
- return "" unless limit[:feature]
631
+ return limit[:environment] ? "system #{cell_text(limit[:system].nil? || limit[:system] == "head" ? limit[:environment] : "#{limit[:environment]} (#{limit[:system]})")}#{", service #{cell_text(limit[:service])}" if limit[:service]}" : "" unless limit[:feature]
487
632
 
488
633
  environment = named && limit[:environment] ? " (#{cell_text(limit[:environment])})" : ""
489
634
  "#{code_span("#{limit[:feature]}:#{limit[:line]}")}#{limit[:text] ? " #{cell_text(limit[:text])}" : ""}#{environment}"
@@ -553,16 +698,56 @@ module Nukadoko
553
698
  end
554
699
  browsers = doc[:scenarios].filter_map { "#{_1[:browser][:type]} #{_1[:browser][:version]}" if _1[:browser] }.uniq
555
700
  browser = browsers.empty? ? "- browser: none launched" : "- browser: #{browsers.join(", ")}"
556
- ["", commit_line, *environments, browser, selection, *order_line, *proof_line,
701
+ ["", commit_line, *environments, *checkout_lines(doc), browser, selection, *order_line, *proof_line,
557
702
  "- nukadoko: #{doc[:nukadoko]}", "- run: #{doc[:run_id]}",
558
703
  "- started #{doc[:started_at]}, finished #{doc[:finished_at]} (#{duration(elapsed)})"]
559
704
  end
560
705
 
706
+ # A checkout holds code the run's commit does not name: it sits
707
+ # outside the repository. One line per checkout says which commit the
708
+ # systems ran, and, under --base, that head's systems and the base's ran
709
+ # the same one, since a checkout is the same directory in every tree.
710
+ # A dirty checkout fails nothing; the line says so, as the commit
711
+ # line does.
712
+ #: (Hash[Symbol, untyped]) -> Array[String]
713
+ def checkout_lines(doc)
714
+ found = {} #: Hash[Array[untyped], Hash[Symbol, untyped]]
715
+ (doc[:systems] || []).each do |system|
716
+ system[:services].each do |service|
717
+ next unless service[:code] == "checkout" && service[:root]
718
+
719
+ entry = found[[service[:name], service[:root], service[:commit], service[:dirty]]] ||= { **service.slice(:name, :root, :commit, :dirty), trees: [] }
720
+ tree = system[:tree] || "head"
721
+ entry[:trees] << tree unless entry[:trees].include?(tree)
722
+ end
723
+ end
724
+ found.values.map do |entry|
725
+ commit = entry[:commit] #: String?
726
+ at = if commit.nil? then ", commit unknown (no git state was readable)"
727
+ else " at `#{commit[0, 7]}`#{" with uncommitted changes" if entry[:dirty] == true}"
728
+ end
729
+ trees = entry[:trees] #: Array[String]
730
+ shared = trees.size < 2 ? "" : ", the same for #{trees[0..-2].to_a.join(", ")} and #{trees.last}"
731
+ "- service #{entry[:name]}: checkout #{code_span(entry[:root])}#{at}#{shared}"
732
+ end
733
+ end
734
+
561
735
  # The claim as ClaimLine writes it, then where its values were
562
736
  # measured: a value no recorded channel holds is what a constant in
563
737
  # step code looks like, so the line says `measured: none`.
564
738
  #: (Hash[Symbol, untyped], String?) -> String
565
- def claim_line(claim, own) = "#{ClaimLine.line(claim)}#{measured_text(claim, own)}"
739
+ def claim_line(claim, own) = "#{ClaimLine.line(claim)}#{after_system_stop_text(claim)}#{measured_text(claim, own)}"
740
+
741
+ # A claim on a count of execution events the system's stop judged
742
+ # otherwise was judged on lines still on their way: its line says what
743
+ # the whole count came to, since its outcome proves nothing.
744
+ #: (Hash[Symbol, untyped]) -> String
745
+ def after_system_stop_text(claim)
746
+ after = (claim[:each] ? nil : claim[:after_system_stop]) #: Hash[Symbol, untyped]?
747
+ return "" if after.nil? || after[:outcome] == claim[:outcome]
748
+
749
+ "; after the system stopped, #{after[:reason]}, and the cell is broke"
750
+ end
566
751
 
567
752
  #: (Hash[Symbol, untyped], String?) -> String
568
753
  def measured_text(claim, own)
@@ -587,6 +772,7 @@ module Nukadoko
587
772
  def claims(doc)
588
773
  named = doc[:environments].size > 1
589
774
  any_control = false
775
+ any_late = false
590
776
  entries = {}
591
777
  head_scenarios(doc).each do |scenario|
592
778
  scenario[:steps].each do |step|
@@ -599,6 +785,7 @@ module Nukadoko
599
785
  attempts = step[:attempts].to_i > 1 ? ", #{step[:attempts]} readings" : ""
600
786
  observation = step[:observation] == "unobserved" ? ", observed nothing" : ""
601
787
  head = "- #{cell_code(where)} #{cell_text(step[:text])}#{marks.empty? ? "" : " (#{marks.join(", ")})"}#{attempts}#{observation}"
788
+ any_late ||= step[:claims].any? { !after_system_stop_text(_1).empty? }
602
789
  claim_lines = step[:claims].map { " - #{cell_text(claim_line(_1, step[:step_record_id]))}" }
603
790
  key = [head, *claim_lines].join("\n")
604
791
  next if entries.key?(key)
@@ -609,7 +796,7 @@ module Nukadoko
609
796
  end
610
797
  return [] if entries.empty?
611
798
 
612
- lead = "#{CLAIMS_LEAD}#{CONTROL_NOTE if any_control}"
799
+ lead = "#{CLAIMS_LEAD}#{CONTROL_NOTE if any_control}#{LATE_NOTE if any_late}"
613
800
  seen_note = " `seen` links what the Then's pages showed right after the reading its claims were judged on, or, for a Then that read no page, " \
614
801
  "the requests its step sent; a capture taken 100 ms or more after the reading says so. The claims judged the values above, " \
615
802
  "not these files, and the scenario's `final.png` is the page as the scenario ended, not as it was judged."
@@ -617,6 +804,589 @@ module Nukadoko
617
804
  ["", "## Claims", "", lead, "", *entries.values.each_with_index.sort_by { |(place, _), index| [*place, index] }.flat_map { _1.first.last }]
618
805
  end
619
806
 
807
+ FLAG_VERDICTS = %i[held proven failed unproven moved].freeze
808
+
809
+ # How many criteria one pair of sides lists before it counts the rest.
810
+ PATHS_SHOWN = 20
811
+
812
+ # Each kind of a path in words, one and many, in PATH_KINDS order.
813
+ PATH_KIND_WORDS = {
814
+ "error" => %w[error errors], "db" => %w[write writes], "http" => ["outgoing call", "outgoing calls"],
815
+ "job" => %w[job jobs], "request" => %w[request requests], "flag" => %w[flag flags]
816
+ }.freeze
817
+
818
+ # A criterion's results, from the one that says most to the one that
819
+ # says least, with their words in a count line.
820
+ PATH_RESULTS = [%w[changed changed], %w[open open], ["same_so_far", "the same so far"],
821
+ ["marked_only", "different only in bookkeeping or infrastructure writes"], ["same", "the same"]].freeze
822
+
823
+ # Against the base, the groups a listed criterion falls in, in the
824
+ # order they are listed: what nothing in the feature files accounts
825
+ # for first, since a refactor must have none and a reader is sent
826
+ # there; then what a person approves new sentences for; then what may
827
+ # still read the same.
828
+ BASE_GROUPS = ["Unchanged criteria that did something else at head, which nothing in the feature files asks for:",
829
+ "Criteria whose differences the intent declares:",
830
+ "New or changed criteria, whose new sentences account for what they do differently:",
831
+ "Criteria that may differ:"].freeze
832
+ BASE_GROUP_UNDECLARED = "Unchanged criteria that did something else at head, which neither the intent nor the feature files ask for:"
833
+
834
+ # The words of a row's result, as the intent's readers use them.
835
+ INTENT_RESULTS = { "as_intended" => "as intended", "not_happening" => "intended but not happening",
836
+ "not_reached" => "not reached by the scenarios compared" }.freeze
837
+
838
+ #: (untyped) -> String
839
+ def program_code(value) = code_span(program_word(value.to_s))
840
+
841
+ #: (String) -> String
842
+ def kind_words(kind) = Events.own_kind?(kind) ? program_code(kind) : PATH_KIND_WORDS.fetch(kind)[1]
843
+
844
+ # A difference's key, as events/path.rb reads one.
845
+ #: (Hash[Symbol, untyped]) -> Hash[String, untyped]
846
+ def difference_key(difference) = difference.except(:holds, :a, :b, :intent).transform_keys(&:to_s)
847
+
848
+ #: (Hash[String, untyped], String) -> String
849
+ def own_field_text(key, field) = program_code("#{field}=#{JSON.generate(key[field])}")
850
+
851
+ # What one entry of a path is, the program's own words in code spans.
852
+ #: (Hash[String, untyped]) -> String
853
+ def path_entry_text(key)
854
+ return Events.key_fields(key).map { own_field_text(key, _1) }.join(" ") if Events.own_kind?(key["kind"])
855
+
856
+ case key["kind"]
857
+ when "error" then "#{program_code(key["class"])} #{key["handled"] ? "handled" : "unhandled"}"
858
+ when "db" then "#{program_code(key["table"])} #{key["op"]}"
859
+ when "http" then "#{program_code("#{key["method"]} #{key["origin"]}#{key["path"]}")} #{key["status"] || "with no status"}"
860
+ when "job" then "#{program_code(key["name"])} #{key["phase"]}"
861
+ when "request" then "#{program_code("#{key["method"]} #{key["route"] || "(no route)"}")} #{key["status"]}"
862
+ else "#{program_code(key["name"])} #{program_code(key["side"])}"
863
+ end
864
+ end
865
+
866
+ #: (Hash[Symbol, untyped]) -> String
867
+ def range_words(range)
868
+ return "#{range[:at_least]} or more" if range[:at_most].nil?
869
+
870
+ range[:at_most] == range[:at_least] ? range[:at_least].to_s : "#{range[:at_least]} to #{range[:at_most]}"
871
+ end
872
+
873
+ # A flag compares as seen or not, so its range reads as whether the
874
+ # program evaluated it.
875
+ #: (Hash[Symbol, untyped]) -> String
876
+ def seen_words(range)
877
+ return "evaluated" if range[:at_least].positive?
878
+
879
+ range[:at_most]&.zero? ? "not evaluated" : "maybe evaluated"
880
+ end
881
+
882
+ #: (Hash[Symbol, untyped]) -> String
883
+ def difference_text(difference)
884
+ key = difference_key(difference)
885
+ words = Events.kind_rule(key["kind"])[:compare] == "seen" ? method(:seen_words) : method(:range_words)
886
+ holds = difference[:holds].nil? ? "" : " (#{difference[:holds]})"
887
+ entry = path_entry_text(key)
888
+ "#{entry.empty? ? "" : "#{entry} "}#{words.(difference[:a])} → #{words.(difference[:b])}#{holds}#{intent_text(difference)}"
889
+ end
890
+
891
+ # Against the base, the intent row that declares a difference.
892
+ #: (Hash[Symbol, untyped]) -> String
893
+ def intent_text(difference) = difference[:intent].nil? ? "" : " (intent row #{difference[:intent]})"
894
+
895
+ # A writer's own value that changed reads as one difference: the
896
+ # fields that stayed, then the one that changed, old then new, when
897
+ # exactly one entry went, one came, as many as went, and they differ
898
+ # in one field.
899
+ #: (Array[Hash[Symbol, untyped]]) -> Array[String]
900
+ def own_texts(differences)
901
+ if differences.size == 2
902
+ one = differences.fetch(0)
903
+ other = differences.fetch(1)
904
+ gone = one[:b][:at_most] == 0 ? one : other
905
+ came = gone.equal?(one) ? other : one
906
+ exact = ->(range) { range[:at_most] == range[:at_least] }
907
+ gone_key = difference_key(gone)
908
+ came_key = difference_key(came)
909
+ differ = Events.key_fields(gone_key).reject { JSON.generate(gone_key[_1]) == JSON.generate(came_key[_1]) }
910
+ if gone[:b][:at_most] == 0 && came[:a][:at_most] == 0 && exact.(gone[:a]) && exact.(came[:b]) &&
911
+ gone[:a][:at_least] == came[:b][:at_least] && differ.size == 1 && gone[:intent] == came[:intent]
912
+ field = differ.first
913
+ stayed = Events.key_fields(gone_key).reject { _1 == field }.map { "#{own_field_text(gone_key, _1)} " }.join
914
+ value = ->(key) { program_code(JSON.generate(key[field])) }
915
+ return ["#{stayed}#{program_code(field)} #{value.(gone_key)} → #{value.(came_key)}#{gone[:a][:at_least] == 1 ? "" : " #{gone[:a][:at_least]} times"}#{intent_text(gone)}"]
916
+ end
917
+ end
918
+ differences.map { difference_text(_1) }
919
+ end
920
+
921
+ # A route whose status changed, and a flag whose side did, read as one
922
+ # difference, what it is then the old value and the new, when every
923
+ # count on it is exact and as many went as came.
924
+ #: (Array[Hash[Symbol, untyped]], ^(Hash[Symbol, untyped]) -> String, ^(Hash[Symbol, untyped]) -> String, ^(Hash[Symbol, untyped]) -> String) -> Array[String]
925
+ def switch_texts(differences, group, what, value)
926
+ differences.group_by { group.(_1) }.values.flat_map do |list|
927
+ gone = list.select { _1[:b][:at_most] == 0 && _1[:a][:at_most] == _1[:a][:at_least] }
928
+ came = list.select { _1[:a][:at_most] == 0 && _1[:b][:at_most] == _1[:b][:at_least] }
929
+ total = ->(of, side) { of.sum { _1[side][:at_least] } }
930
+ next list.map { difference_text(_1) } if gone.empty? || came.empty? || gone.size + came.size != list.size || total.(gone, :a) != total.(came, :b)
931
+
932
+ ["#{what.(list.first)} #{gone.map { value.(_1) }.join(" and ")} → #{came.map { value.(_1) }.join(" and ")}"]
933
+ end
934
+ end
935
+
936
+ #: (Array[Hash[Symbol, untyped]]) -> Array[String]
937
+ def kind_texts(differences)
938
+ return own_texts(differences) if differences.any? { Events.own_kind?(_1[:kind]) }
939
+
940
+ requests = differences.select { _1[:kind] == "request" }
941
+ flags = differences.select { _1[:kind] == "flag" }
942
+ [*differences.reject { %w[request flag].include?(_1[:kind]) }.map { difference_text(_1) },
943
+ *switch_texts(requests, ->(d) { JSON.generate([d[:method], d[:route]]) }, ->(d) { program_code("#{d[:method]} #{d[:route] || "(no route)"}") }, ->(d) { d[:status].to_s }),
944
+ *switch_texts(flags, ->(d) { d[:name].to_s }, ->(d) { program_code(d[:name]) }, ->(d) { program_code(d[:side]) })]
945
+ end
946
+
947
+ #: (String, String) -> Integer
948
+ def by_kind(left, right) = [Events.kind_rank(left), left] <=> [Events.kind_rank(right), right] || 0
949
+
950
+ # The differences of one step by kind, in rank order.
951
+ #: (Array[Hash[Symbol, untyped]]) -> String
952
+ def differences_text(differences)
953
+ kinds = differences.map { _1[:kind] }.uniq.sort { |a, b| by_kind(a, b) }
954
+ kinds.map { |kind| "#{kind_words(kind)} #{kind_texts(differences.select { _1[:kind] == kind }).join(", ")}" }.join("; ")
955
+ end
956
+
957
+ #: (Array[Hash[Symbol, untyped]]) -> String
958
+ def gap_list(gaps)
959
+ gaps.map do |gap|
960
+ word = code_span(gap[:gap])
961
+ case gap[:gap]
962
+ when "jobs-pending" then "#{word} #{count_of(gap[:job_ids].size, "job", "jobs")}"
963
+ when "request-unreported" then "#{word} #{count_of(gap[:requests], "request", "requests")}"
964
+ when "unreadable" then "#{word} #{count_of(gap[:lines], "line", "lines")}"
965
+ else word
966
+ end
967
+ end.join(", ")
968
+ end
969
+
970
+ # Who leaves an open difference open: the sides that are not whole,
971
+ # or lines on a whole side that may be any line.
972
+ #: (Hash[Symbol, untyped], ^(Symbol) -> String) -> String
973
+ def open_why(gaps, side_name)
974
+ return "neither side is whole (#{side_name.(:a)}: #{gap_list(gaps[:a])}; #{side_name.(:b)}: #{gap_list(gaps[:b])})" if gaps[:a].any? && gaps[:b].any?
975
+ return "#{side_name.(:a)} is not whole (#{gap_list(gaps[:a])})" if gaps[:a].any?
976
+ return "#{side_name.(:b)} is not whole (#{gap_list(gaps[:b])})" if gaps[:b].any?
977
+
978
+ "a line on a side may be any line"
979
+ end
980
+
981
+ #: (Array[Integer?]) -> String
982
+ def line_words(lines)
983
+ known = lines.map { _1.nil? ? "?" : _1.to_s }
984
+ known.size == 1 ? "line #{known[0]}" : "lines #{known[0]} to #{known[-1]}"
985
+ end
986
+
987
+ # How a comparison names its sides, and which kinds it compares.
988
+ #: (Hash[Symbol, untyped], Hash[String, Hash[Symbol, untyped]]) -> Hash[Symbol, untyped]
989
+ def path_reading(comparison, scenarios)
990
+ base = comparison.dig(:between, :kind) == "base"
991
+ { comparison: comparison, scenarios: scenarios,
992
+ side_name: ->(side) { base ? (side == :a ? "the base" : "head") : code_span(comparison[side][:environment]) },
993
+ side_where: ->(side) { base ? (side == :a ? "at the base" : "at head") : "in #{code_span(comparison[side][:environment])}" },
994
+ compared: ->(kind) { base || Events.kind_rule(kind)[:condition_premise] != true } }
995
+ end
996
+
997
+ # What the steps that ran on one side only did there, counted by kind.
998
+ #: (Hash[Symbol, untyped], Hash[Symbol, untyped], Array[Hash[Symbol, untyped]], Symbol) -> Hash[String, Integer]
999
+ def one_side_kinds(reading, criterion, steps, side)
1000
+ scenario = reading[:scenarios][criterion[side][:scenario_record_id]]
1001
+ counts = Hash.new(0) #: Hash[String, Integer]
1002
+ steps.each do |step|
1003
+ found = scenario && (scenario[:steps] || []).find { _1[:line] == step[:line] && _1[:text] == step[:text] }&.dig(:path)
1004
+ next if found.nil?
1005
+
1006
+ path = PathView.path_of(found)
1007
+ [*path[:entries], *path[:requests].map { Events.request_key(_1).merge("count" => 1) }].each do |entry|
1008
+ next unless reading[:compared].(entry["kind"])
1009
+
1010
+ counts[entry["kind"]] += Events.kind_rule(entry["kind"])[:compare] == "seen" ? 1 : entry["count"]
1011
+ end
1012
+ end
1013
+ counts
1014
+ end
1015
+
1016
+ # A criterion's step lines, of the differences `marked` picks out:
1017
+ # those on a marked table, or every other.
1018
+ #: (Hash[Symbol, untyped], Hash[Symbol, untyped], bool) -> Array[String]
1019
+ def step_lines(reading, criterion, marked)
1020
+ (criterion[:steps] || []).flat_map do |step|
1021
+ next [] unless step[:ran] == "both"
1022
+
1023
+ changed = step[:changed].select { !_1[:holds].nil? == marked }
1024
+ open = step[:open].select { !_1[:holds].nil? == marked }
1025
+ parts = [*(differences_text(changed) if changed.any?),
1026
+ *("open, since #{open_why(step[:gaps], reading[:side_name])}: #{differences_text(open)}" if open.any?)]
1027
+ parts.empty? ? [] : [" - line #{step[:line] || "?"} #{code_span(step[:text])}: #{parts.join("; ")}"]
1028
+ end
1029
+ end
1030
+
1031
+ #: (Hash[Symbol, untyped], Hash[Symbol, untyped]) -> Array[String]
1032
+ def criterion_lines(reading, criterion)
1033
+ steps = criterion[:steps] || []
1034
+ lines = step_lines(reading, criterion, false)
1035
+ one_side = steps.reject { _1[:ran] == "both" }
1036
+ return lines if one_side.empty?
1037
+
1038
+ ran = one_side.first[:ran].to_sym
1039
+ other = ran == :a ? :b : :a
1040
+ stopped = criterion[other]
1041
+ where = if stopped[:line].nil? then ", since #{reading[:side_name].(other)} did not run #{one_side.size == 1 ? "it" : "them"}"
1042
+ else ", since #{reading[:side_name].(other)} stopped at line #{stopped[:line]} (#{code_span(cell_word(stopped))})"
1043
+ end
1044
+ counts = one_side_kinds(reading, criterion, one_side, ran)
1045
+ did = counts.keys.sort { |a, b| by_kind(a, b) }.map do |kind|
1046
+ Events.own_kind?(kind) ? "#{counts[kind]} #{program_code(kind)}" : count_of(counts[kind], PATH_KIND_WORDS.fetch(kind)[0], PATH_KIND_WORDS.fetch(kind)[1])
1047
+ end
1048
+ lines << " - #{line_words(one_side.map { _1[:line] })} ran only #{reading[:side_where].(ran)}#{where}: #{did.empty? ? "no execution event" : did.join(", ")}"
1049
+ end
1050
+
1051
+ # A criterion whose open counts read the same on both sides, in one
1052
+ # line: the gap words that leave them open, which both sides share.
1053
+ #: (Hash[Symbol, untyped]) -> String
1054
+ def same_so_far_text(criterion)
1055
+ words = (criterion[:steps] || []).flat_map { _1[:ran] == "both" ? _1[:gaps][:a].map { |gap| gap[:gap] } : [] }.uniq
1056
+ words.empty? ? "the same so far, with lines on both sides that may be any line" : "the same so far, with #{words.map { code_span(_1) }.join(", ")} on both sides"
1057
+ end
1058
+
1059
+ # A criterion ranks by the highest kind among its decided differences
1060
+ # and what its one-side steps did, so what persists outside the
1061
+ # process comes first.
1062
+ #: (Hash[Symbol, untyped], Hash[Symbol, untyped]) -> Integer
1063
+ def path_rank(reading, criterion)
1064
+ steps = criterion[:steps] || []
1065
+ one_side = steps.reject { _1[:ran] == "both" }
1066
+ kinds = [*steps.flat_map { _1[:ran] == "both" ? _1[:changed].select { |d| d[:holds].nil? }.map { |d| d[:kind] } : [] },
1067
+ *(one_side.empty? ? [] : one_side_kinds(reading, criterion, one_side, one_side.first[:ran].to_sym).keys)]
1068
+ [Events::PATH_KIND_ORDER.size + 1, *kinds.map { Events.kind_rank(_1) }].min || 0
1069
+ end
1070
+
1071
+ #: (untyped) -> String
1072
+ def condition_value(value)
1073
+ return "not declared" if value.nil?
1074
+
1075
+ [true, false].include?(value) ? value.to_s : code_span(program_word(value.to_s))
1076
+ end
1077
+
1078
+ #: (Hash[Symbol, untyped]) -> String
1079
+ def path_heading(comparison)
1080
+ between = comparison[:between]
1081
+ return "### base → head" if between[:kind] == "base"
1082
+
1083
+ "### #{code_span(comparison[:a][:environment])} → #{code_span(comparison[:b][:environment])} " \
1084
+ "(#{code_span(between[:name])} #{condition_value(between[:a])} → #{condition_value(between[:b])})"
1085
+ end
1086
+
1087
+ #: (String) -> Integer
1088
+ def result_rank(result) = PATH_RESULTS.index { _1.first == result } || PATH_RESULTS.size
1089
+
1090
+ #: (Hash[Symbol, untyped]) -> Integer
1091
+ def base_group(criterion)
1092
+ return 3 unless criterion[:result] == "changed"
1093
+ return 0 if criterion[:explained_by].nil?
1094
+
1095
+ criterion[:explained_by] == "intent" ? 1 : 2
1096
+ end
1097
+
1098
+ #: (Array[String]) -> String
1099
+ def path_results_said(results)
1100
+ PATH_RESULTS.filter_map { |result, word| (n = results.count(result)).zero? ? nil : "#{n} #{word}" }.join(", ")
1101
+ end
1102
+
1103
+ # Against the base, what the unchanged criteria came to, each once,
1104
+ # as what it came to where it says most.
1105
+ #: (Array[Hash[Symbol, untyped]], Hash[String, untyped]) -> Array[String]
1106
+ def unchanged_line(read, change_of)
1107
+ results = {} #: Hash[String, String]
1108
+ read.flat_map { _1[:criteria] }.each do |criterion|
1109
+ place = "#{criterion[:feature]}:#{criterion[:line]}"
1110
+ next unless change_of[place] == "unchanged"
1111
+
1112
+ was = results[place]
1113
+ results[place] = criterion[:result] if was.nil? || result_rank(criterion[:result]) < result_rank(was)
1114
+ end
1115
+ return [] if results.empty?
1116
+
1117
+ all = results.values
1118
+ if all.all?("same")
1119
+ return [all.size == 1 ? "The 1 unchanged criterion compared did the same at the base and at head." : "Each of the #{all.size} unchanged criteria compared did the same at the base and at head."]
1120
+ end
1121
+
1122
+ ["Of the #{count_of(all.size, "unchanged criterion", "unchanged criteria")} compared, #{path_results_said(all)}."]
1123
+ end
1124
+
1125
+ # The comparisons under one heading: a pair of environments alone, or
1126
+ # every environment's base pair. Each criterion counts once, as what
1127
+ # it came to where it says most, and one listed with the same lines in
1128
+ # several base pairs is listed once, naming their environments.
1129
+ #: (Hash[Symbol, untyped], Array[Hash[Symbol, untyped]]) -> Array[String]
1130
+ def path_section(doc, comparisons)
1131
+ heading = path_heading(comparisons.first)
1132
+ named = ->(environments) { environments.map { code_span(_1) }.join(", ") }
1133
+ place = ->(entry) { "#{entry[:feature]}:#{entry[:line]}" }
1134
+ read = comparisons.select { _1[:unread].nil? }
1135
+ unread = {} #: Hash[String, Array[String]]
1136
+ comparisons.each { (unread[_1[:unread]] ||= []) << _1[:b][:environment] unless _1[:unread].nil? }
1137
+ unread_text = unread.map do |reason, environments|
1138
+ read.empty? && unread.size == 1 ? "Not compared: #{reason}." : "Not compared in #{named.(environments)}: #{reason}."
1139
+ end
1140
+ return ["", heading, "", unread_text.join(" ")] if read.empty?
1141
+
1142
+ results = {} #: Hash[String, String]
1143
+ read.flat_map { _1[:criteria] }.each do |criterion|
1144
+ was = results[place.(criterion)]
1145
+ results[place.(criterion)] = criterion[:result] if was.nil? || result_rank(criterion[:result]) < result_rank(was)
1146
+ end
1147
+ compared = results.empty? ? "No criterion was compared." : "#{count_of(results.size, "criterion", "criteria")} compared: #{path_results_said(results.values)}."
1148
+ reasons = {} #: Hash[String, Array[String]]
1149
+ read.flat_map { _1[:not_compared] }.each do |entry|
1150
+ next if results.key?(place.(entry)) || reasons.values.any? { _1.include?(place.(entry)) }
1151
+
1152
+ (reasons[entry[:reason]] ||= []) << place.(entry)
1153
+ end
1154
+ not_compared = reasons.map do |reason, places|
1155
+ " #{count_of(places.size, "criterion", "criteria")} #{reason}, so #{places.size == 1 ? "its path was" : "their paths were"} not compared."
1156
+ end
1157
+
1158
+ scenarios = doc[:scenarios].to_h { [_1[:scenario_record_id], _1] }
1159
+ row_order = doc[:table][:rows].each_with_index.to_h { |row, index| [place.(row), index] }
1160
+ items = {} #: Hash[String, Hash[Symbol, untyped]]
1161
+ asides = {} #: Hash[String, Hash[Symbol, untyped]]
1162
+ read.each do |comparison|
1163
+ reading = path_reading(comparison, scenarios)
1164
+ comparison[:criteria].each do |criterion|
1165
+ marked = step_lines(reading, criterion, true)
1166
+ if marked.any?
1167
+ aside = asides[JSON.generate([place.(criterion), marked])] ||= { criterion: criterion, rank: 0, lines: marked, environments: [] }
1168
+ aside[:environments] << comparison[:b][:environment]
1169
+ end
1170
+ next if %w[same marked_only].include?(criterion[:result])
1171
+
1172
+ lines = criterion[:result] == "same_so_far" ? [same_so_far_text(criterion)] : criterion_lines(reading, criterion)
1173
+ item = items[JSON.generate([place.(criterion), lines])] ||= { criterion: criterion, rank: path_rank(reading, criterion), lines: lines, environments: [] }
1174
+ item[:environments] << comparison[:b][:environment]
1175
+ end
1176
+ end
1177
+ base = comparisons.first.dig(:between, :kind) == "base"
1178
+ group = ->(item) { base ? base_group(item[:criterion]) : 0 }
1179
+ so_far = ->(item) { item[:criterion][:result] == "same_so_far" ? 1 : 0 }
1180
+ listed = items.values.sort_by.with_index { |item, index| [group.(item), so_far.(item), item[:rank], row_order[place.(item[:criterion])] || 0, index] }
1181
+ lines = ["", heading, "", ["#{compared}#{not_compared.join}", *unread_text].join(" ")]
1182
+ lines.concat(unchanged_line(read, doc[:table][:rows].to_h { [place.(_1), _1[:change]] })) if base
1183
+ lines << "" if listed.any? && !base
1184
+ shown_group = nil
1185
+ listed.first(PATHS_SHOWN).each do |item|
1186
+ criterion = item[:criterion]
1187
+ if base && group.(item) != shown_group
1188
+ shown_group = now = group.(item)
1189
+ lines.push("", now.zero? && doc.key?(:intent) ? BASE_GROUP_UNDECLARED : BASE_GROUPS[now], "")
1190
+ end
1191
+ head = "- #{cell_code(place.(criterion))} #{cell_text(criterion[:name])}#{read.size > 1 ? ", in #{named.(item[:environments])}" : ""}"
1192
+ lines.concat(criterion[:result] == "same_so_far" ? ["#{head}: #{item[:lines][0]}"] : [head, *item[:lines]])
1193
+ end
1194
+ lines << "- and #{listed.size - PATHS_SHOWN} more in run.json's `paths`" if listed.size > PATHS_SHOWN
1195
+ aside = asides.values.sort_by.with_index { |item, index| [row_order[place.(item[:criterion])] || 0, index] }
1196
+ if aside.any?
1197
+ lines.push("", "Bookkeeping and infrastructure writes that differ, which no sentence is about, though another reader of those tables may be:", "")
1198
+ aside.first(PATHS_SHOWN).each do |item|
1199
+ lines.push("- #{cell_code(place.(item[:criterion]))} #{cell_text(item[:criterion][:name])}#{read.size > 1 ? ", in #{named.(item[:environments])}" : ""}", *item[:lines])
1200
+ end
1201
+ lines << "- and #{aside.size - PATHS_SHOWN} more in run.json's `paths`" if aside.size > PATHS_SHOWN
1202
+ end
1203
+ lines
1204
+ end
1205
+
1206
+ # What a row declares, its cells as written.
1207
+ #: (Hash[Symbol, untyped]) -> String
1208
+ def intent_row_text(row)
1209
+ target = row[:target].to_s.empty? ? "every criterion" : code_span(row[:target])
1210
+ change = row[:before].to_s.empty? ? "no change" : "#{code_span(row[:before])} → #{code_span(row[:after])}"
1211
+ "row #{row[:row]} #{target} #{code_span(row[:observation])}: #{change}"
1212
+ end
1213
+
1214
+ # run.json's `intent`, for the person who approves the change: when it
1215
+ # was declared and whether it moved since, and what each row came to.
1216
+ # Each criterion with a difference no row declares is listed under
1217
+ # "Changed paths", so this section counts them and does not repeat
1218
+ # them.
1219
+ #: (Hash[Symbol, untyped]) -> Array[String]
1220
+ def intent(doc)
1221
+ intent = doc[:intent]
1222
+ return [] if intent.nil?
1223
+
1224
+ file = code_span(intent[:file])
1225
+ commit = ->(entry) { "#{code_span(entry[:commit].to_s[0, 12])} (#{cell_text(entry[:subject])})" }
1226
+ declared = if intent[:declared_in].nil? then "#{file} is declared in the working tree alone, in no commit since the base."
1227
+ else "#{file} was declared in #{commit.(intent[:declared_in])}, " \
1228
+ "#{intent[:declared_in][:first] ? "the change's first commit" : "after the change's first commit, so the work it judges began before it"}."
1229
+ end
1230
+ lines = ["", "## Intent", "", declared]
1231
+ lines << "It changed after that in #{intent[:changed_after].map(&commit).join(", ")}: the change of intent is part of what is approved." if intent[:changed_after].any?
1232
+ lines << "The working tree's #{file} differs from the one at HEAD, and the run read the working tree's." if intent[:uncommitted]
1233
+ rows = intent[:rows]
1234
+ said = INTENT_RESULTS.filter_map { |result, words| (n = rows.count { _1[:result] == result }).zero? ? nil : "#{n} #{words}" }
1235
+ lines.push("", rows.empty? ? "It declares no row: nothing the criteria do is meant to change." : "#{count_of(rows.size, "row", "rows")}: #{said.join(", ")}.")
1236
+ places = ->(list) { list.map { cell_code("#{_1[:feature]}:#{_1[:line]}") }.join(", ") }
1237
+ lines << "" if rows.any?
1238
+ rows.each do |row|
1239
+ parts = [*("#{INTENT_RESULTS["not_happening"]} in #{places.(row[:not_happening])}" if row[:not_happening].any?),
1240
+ *("#{INTENT_RESULTS["as_intended"]} in #{places.(row[:as_intended])}" if row[:as_intended].any?),
1241
+ *(INTENT_RESULTS["not_reached"] if row[:result] == "not_reached")]
1242
+ lines << "- #{intent_row_text(row)}; #{parts.join("; ")}"
1243
+ end
1244
+ if intent[:problems].any?
1245
+ lines.push("", "What #{file} holds that could not be read as a row:", "")
1246
+ intent[:problems].each { lines << "- #{_1[:line].nil? ? "the file" : "line #{_1[:line]}"}: #{_1[:message]}" }
1247
+ end
1248
+ undeclared = (doc[:paths] || []).flat_map do |comparison|
1249
+ next [] unless comparison.dig(:between, :kind) == "base"
1250
+
1251
+ comparison[:criteria].select { _1.key?(:explained_by) && _1[:explained_by].nil? }.map { "#{_1[:feature]}:#{_1[:line]}" }
1252
+ end.uniq
1253
+ lines.push("", if undeclared.empty? then "No criterion compared with the base did anything the change does not declare."
1254
+ else "#{count_of(undeclared.size, "criterion", "criteria")} compared with the base did something the change does not declare, listed first under \"Changed paths\"."
1255
+ end)
1256
+ end
1257
+
1258
+ # run.json's `paths`, for the person who decides whether a change may
1259
+ # be turned on for everyone: per pair of sides, each criterion whose
1260
+ # program did something else on one side than on the other, step by
1261
+ # step.
1262
+ #: (Hash[Symbol, untyped]) -> Array[String]
1263
+ def paths(doc)
1264
+ comparisons = doc[:paths] || []
1265
+ return [] if comparisons.empty?
1266
+
1267
+ base = comparisons.select { _1.dig(:between, :kind) == "base" }
1268
+ sections = [base, *comparisons.select { _1.dig(:between, :kind) == "condition" }.map { [_1] }].reject(&:empty?)
1269
+ lead = "Each criterion below did something else on one side than on the other, or may have, step by step, as the program's execution events report it. " \
1270
+ "A count that a side's missing lines could still reach is listed as open, never as a change. " \
1271
+ "A criterion whose open counts read the same on both sides, under the same gaps, is the same so far, and comes last. " \
1272
+ "Between two environments, what they switch is named in the heading, so no flag's side is listed among the changes." \
1273
+ "#{base.any? ? " Against the base, an unchanged criterion that changed comes first, apart from a new or changed one, whose new sentences account for its difference." : ""}"
1274
+ ["", "## Changed paths", "", lead, *sections.flat_map { path_section(doc, _1) }]
1275
+ end
1276
+
1277
+ # At most this many routes are named in one line of "Routes"; the
1278
+ # rest are counted, and run.json's `routes` names every one.
1279
+ ROUTES_NAMED = 20
1280
+
1281
+ # A flag's name, side, and gate are the program's text: a line break
1282
+ # in one would end the list item, so control characters are escaped.
1283
+ #: (String) -> String
1284
+ def program_word(text) = text.to_s.gsub(/[\u0000-\u001f\u007f\u{2028}\u{2029}]/) { format("\\u%04x", _1.ord) }
1285
+
1286
+ #: (Hash[Symbol, untyped]) -> Array[Hash[Symbol, untyped]]
1287
+ def head_systems(doc) = (doc[:systems] || []).select { _1[:tree].nil? || _1[:tree] == "head" }
1288
+
1289
+ # The lists the primary service of `system` read: the steps' requests
1290
+ # and flags are read against them.
1291
+ #: (Hash[Symbol, untyped]) -> Hash[Symbol, untyped]?
1292
+ def primary_lists(system) = (system[:services] || []).find { _1[:name] == system[:primary] }&.dig(:lists)
1293
+
1294
+ # One line per flag and side of run.json's `flags`: its counts, and
1295
+ # the gates its lines named, or that no criterion evaluated a side the
1296
+ # flags list declares. A system whose flags list was not read is named,
1297
+ # so a missing side is never read as one the program does not have.
1298
+ #: (Hash[Symbol, untyped]) -> Array[String]
1299
+ def flags(doc)
1300
+ rows = doc[:flags] || []
1301
+ unread = head_systems(doc).select { primary_lists(_1)&.dig(:flags, :error) }
1302
+ return [] if rows.empty? && unread.empty?
1303
+
1304
+ # The service is named only where the rows name more than one, so a
1305
+ # program of one service, and a run where one service alone
1306
+ # reported a flag, read as they did before.
1307
+ services = rows.filter_map { _1[:service] }.uniq
1308
+ lines = rows.map do |row|
1309
+ whose = services.size > 1 && row[:service] ? "service #{code_span(program_word(row[:service]))} " : ""
1310
+ flag = "- #{whose}#{code_span(program_word(row[:name]))} #{code_span(program_word(row[:side]))}"
1311
+ next "#{flag}, no criterion's steps evaluated it on this side" if row[:criteria].empty?
1312
+
1313
+ counts = FLAG_VERDICTS.select { row[:counts][_1].positive? }.map { "#{row[:counts][_1]} #{_1}" }
1314
+ counts << "#{row[:counts][:controls]} only in controls" if row[:counts][:controls].positive?
1315
+ gates = row[:criteria].flat_map { |criterion| criterion[:cells].flat_map { _1[:gates] } }.uniq.sort { |a, b| Events.by_gate(a, b) }
1316
+ named = gates.compact.map { code_span(program_word(_1)) }
1317
+ tail = [*("gates #{named.join(", ")}" if named.any?), *("only this side was evaluated" if row[:only_side_evaluated]),
1318
+ *("no flags list that was read declares this side" if row[:listed_in]&.empty?)]
1319
+ "#{flag}, #{count_of(row[:criteria].size, "criterion", "criteria")}#{counts.empty? ? "" : ": #{counts.join(", ")}"}#{tail.map { "; #{_1}" }.join}"
1320
+ end
1321
+ lead = ["Each line counts the criteria whose steps evaluated the flag on the side the program's lines named, by their verdict in the table.",
1322
+ "One seen on that side only in controls is counted apart, since its verdict is about another side.",
1323
+ "`gates` lists the gates those lines named, and a line may name none.", ""]
1324
+ ["", "## Flags", "", *(rows.empty? ? [] : lead), *lines,
1325
+ *unread.map { "- System #{code_span(program_word(_1[:name]))} read no flags list (`program-list-unread` under Findings), so a side of a flag its steps never evaluated may not stand here." }]
1326
+ end
1327
+
1328
+ #: (Hash[Symbol, untyped]) -> String
1329
+ def route_name(route) = code_span(program_word(route[:route].nil? ? route[:method] : "#{route[:method]} #{route[:route]}"))
1330
+
1331
+ #: (Array[String]) -> String
1332
+ def named_list(names)
1333
+ shown = names.first(ROUTES_NAMED).join(", ")
1334
+ names.size > ROUTES_NAMED ? "#{shown}, and #{names.size - ROUTES_NAMED} more" : shown
1335
+ end
1336
+
1337
+ # The places of run.json's `routes` no step reached: the listed routes
1338
+ # no head step requested, split from those whose handler a request
1339
+ # reached through another route, then the requests the lists do not
1340
+ # account for. A system whose list was not read is named.
1341
+ #: (Hash[Symbol, untyped]) -> Array[String]
1342
+ def routes(doc)
1343
+ heads = head_systems(doc)
1344
+ unread = heads.select { primary_lists(_1)&.dig(:routes, :error) }
1345
+ routes = doc[:routes] || []
1346
+ return [] if unread.empty? && doc[:routes].nil?
1347
+
1348
+ read = heads.select { |system| (list = primary_lists(system)&.dig(:routes)) && list.key?(:error) && list[:error].nil? }.map { _1[:name] }.uniq
1349
+ listed = routes.select { _1[:listed_in].any? }
1350
+ requested = listed.select { _1[:requests].positive? }
1351
+ through = {} #: Hash[String, Array[Hash[Symbol, untyped]]]
1352
+ routes.each { |route| route[:handlers].each { (through[_1] ||= []) << route } if route[:requests].positive? }
1353
+ gaps = [] #: Array[String]
1354
+ aliases = [] #: Array[String]
1355
+ listed.each do |route|
1356
+ next if route[:requests].positive?
1357
+
1358
+ others = route[:handlers].flat_map { through[_1] || [] }
1359
+ if route[:handlers].any? && route[:handlers].all? { through.key?(_1) }
1360
+ aliases << "#{route_name(route)} through #{others.map { route_name(_1) }.uniq.join(", ")}"
1361
+ else
1362
+ gaps << route_name(route)
1363
+ end
1364
+ end
1365
+ unlisted = routes.filter_map do |route|
1366
+ next nil if route[:route].nil?
1367
+
1368
+ requests = route[:criteria].sum { |c| c[:cells].sum { read.include?(_1[:environment]) && !route[:listed_in].include?(_1[:environment]) ? _1[:requests] : 0 } }
1369
+ requests.zero? ? nil : [route, requests]
1370
+ end
1371
+ unmatched = routes.select { _1[:route].nil? && _1[:requests].positive? }
1372
+ same = listed.all? { |route| read.all? { route[:listed_in].include?(_1) } }
1373
+ lists = if read.size == 1 then "the program's routes list names"
1374
+ elsif same then "the program's routes list names (the same in #{read.size} systems)"
1375
+ else "the routes lists of #{read.size} systems name"
1376
+ end
1377
+ lines = [
1378
+ *(doc[:routes].nil? ? [] : ["Head steps requested #{requested.size} of the #{count_of(listed.size, "route", "routes")} #{lists}.",
1379
+ "A request says which route the program matched, not that a step checked what it answered.", ""]),
1380
+ *("- Never requested (#{gaps.size}): #{named_list(gaps)}" if gaps.any?),
1381
+ *("- Never requested, though a route with its handler was (#{aliases.size}): #{named_list(aliases)}" if aliases.any?),
1382
+ *("- #{count_of(unlisted.sum(&:last), "request", "requests")} to a route its system's list does not name: #{named_list(unlisted.map { "#{route_name(_1)} #{_2}" })}" if unlisted.any?),
1383
+ *("- #{count_of(unmatched.sum { _1[:requests] }, "request", "requests")} the program matched to no route: #{named_list(unmatched.map { "#{route_name(_1)} #{_1[:requests]}" })}" if unmatched.any?),
1384
+ *unread.map { "- System #{code_span(program_word(_1[:name]))} read no routes list (`program-list-unread` under Findings), so none of its routes stands here." }
1385
+ ].compact
1386
+ lines.pop if lines.last == ""
1387
+ ["", "## Routes", "", *lines]
1388
+ end
1389
+
620
1390
  #: (Hash[Symbol, untyped]) -> String
621
1391
  def where_of(finding)
622
1392
  return finding[:line] ? "#{finding[:file]}:#{finding[:line]}" : finding[:file] if finding[:file]
@@ -651,14 +1421,14 @@ module Nukadoko
651
1421
  end
652
1422
 
653
1423
  # A walk is a run that paused after each step for an answer: its
654
- # verdicts were measured, but between steps the world and the browser
1424
+ # verdicts were measured, but between steps the system and the browser
655
1425
  # were open to anything, which no record covers. The longest pause
656
1426
  # says how long that was.
657
1427
  #: (Hash[Symbol, untyped]) -> String
658
1428
  def walk_sentence(walk)
659
1429
  longest = [0, *walk[:pauses].map { ((Time.iso8601(_1[:answered_at]) - Time.iso8601(_1[:paused_at])) * 1000).round }].max
660
1430
  "Walked one step at a time (`nuka run --step`): #{count_of(walk[:pauses].size, "pause", "pauses")}, the longest #{duration(longest)}. " \
661
- "Between steps the world and the browser were open to anything, so this run is not the run a reviewer approves."
1431
+ "Between steps the system and the browser were open to anything, so this run is not the run a reviewer approves."
662
1432
  end
663
1433
  end
664
1434
  end