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,167 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "json"
5
+ require "rbconfig"
6
+ require "securerandom"
7
+
8
+ # One run as a cucumber-messages stream, for `nuka report --format
9
+ # messages`, in the shape the TypeScript engine writes it
10
+ # (src/report/messages/): one line per envelope, in the protocol's order.
11
+ # It is compat fidelity only, so a migrated team's formatter, JUnit step,
12
+ # or HTML report keeps working: step record internals never appear in the
13
+ # protocol's closed messages. A claim step's claims ride as an attachment
14
+ # of their own media type, and what the verdict table says of an
15
+ # execution as a test case attachment, since the protocol has no field
16
+ # for either.
17
+ #
18
+ # The stream leaves out every control execution: a JUnit-reading CI gates
19
+ # on it, and a control that failed is the proof a green run rests on,
20
+ # which as a test case would read as a failure.
21
+ #
22
+ # Each execution is its own test case, attempt 0: `--repeat` executions
23
+ # all count, and a retry's shape would let a reader fold a red first
24
+ # execution into a green last one. A run over more than one environment
25
+ # gives each (pickle, environment) a pickle of its own, named with the
26
+ # environment, since the protocol has no field for where a case ran.
27
+ module Nukadoko
28
+ module Engine
29
+ module ReportMessages
30
+ CLAIMS_MEDIA_TYPE = "application/vnd.nukadoko.claims+json"
31
+ VERDICT_MEDIA_TYPE = "application/vnd.nukadoko.verdict+json"
32
+ CLAIM_BROKEN_EXCEPTION = "nukadoko.ClaimBroken"
33
+ STATUS = { "passed" => "PASSED", "failed" => "FAILED", "skipped" => "SKIPPED", "undefined" => "UNDEFINED", "ambiguous" => "AMBIGUOUS" }.freeze
34
+ CLAIM_FIELDS = %w[readings readings_left_out settled pages page_requests judged_view].freeze
35
+
36
+ module_function
37
+
38
+ # The stream's text, and the problems that left part of the run out.
39
+ #: (ReportRun) -> String
40
+ def render(run)
41
+ lines = [] #: Array[String]
42
+ append = ->(envelope) { lines << "#{JSON.generate(envelope)}\n" }
43
+ document = run.document
44
+ gating = run.scenarios.reject { _1.verdict&.dig(:role) == "control" }
45
+ append.({ meta: meta(document[:nukadoko].to_s) })
46
+ copies = environment_pickles(gating)
47
+ run.features.each do |feature|
48
+ uri = feature.document["uri"] || feature.path
49
+ append.({ source: { uri: uri, data: feature.source, mediaType: "text/x.cucumber.gherkin+plain" } })
50
+ append.({ gherkinDocument: { **feature.document, "uri" => uri } })
51
+ selected = gating.select { _1.feature.equal?(feature) }.map(&:pickle)
52
+ feature.pickles.select { |pickle| selected.any? { _1.equal?(pickle) } }.each do |pickle|
53
+ (copies[pickle.object_id]&.values || [pickle]).each { append.({ pickle: { **_1, "uri" => uri } }) }
54
+ end
55
+ end
56
+ append.({ testRunStarted: { timestamp: timestamp(document[:started_at]) } })
57
+ gating.each do |scenario|
58
+ pickle = copies.dig(scenario.pickle.object_id, scenario.json[:environment]) || scenario.pickle
59
+ test_case(scenario, pickle, append)
60
+ end
61
+ append.({ testRunFinished: { success: document[:verdict] == "passed", timestamp: timestamp(document[:finished_at]) } })
62
+ lines.join
63
+ end
64
+
65
+ #: (String) -> Hash[Symbol, untyped]
66
+ def meta(version)
67
+ os = RbConfig::CONFIG["host_os"].to_s
68
+ cpu = RbConfig::CONFIG["host_cpu"].to_s
69
+ spec = Gem.loaded_specs["cucumber-messages"] #: untyped
70
+ { protocolVersion: spec&.version.to_s,
71
+ # The version that ran the scenarios, as run.json records it.
72
+ implementation: { name: "nukadoko", version: version },
73
+ runtime: { name: "ruby", version: RUBY_VERSION },
74
+ os: { name: os.include?("darwin") ? "darwin" : os.match?(/mswin|mingw/) ? "win32" : os.include?("linux") ? "linux" : os },
75
+ cpu: { name: { "x86_64" => "x64", "aarch64" => "arm64" }.fetch(cpu, cpu) } }
76
+ end
77
+
78
+ #: (Array[ReportRun::Scenario]) -> Hash[Integer, Hash[String, Hash[String, untyped]]]
79
+ def environment_pickles(scenarios)
80
+ return {} if scenarios.map { _1.json[:environment] }.uniq.size <= 1
81
+
82
+ copies = {} #: Hash[Integer, Hash[String, Hash[String, untyped]]]
83
+ scenarios.each do |scenario|
84
+ per = (copies[scenario.pickle.object_id] ||= {})
85
+ environment = scenario.json[:environment]
86
+ next if per.key?(environment)
87
+
88
+ pickle = scenario.pickle
89
+ per[environment] = { **pickle, "id" => SecureRandom.uuid, "name" => "#{pickle["name"]} (#{environment})",
90
+ "steps" => pickle["steps"].map { { **_1, "id" => SecureRandom.uuid } } }
91
+ end
92
+ copies
93
+ end
94
+
95
+ #: (untyped) -> Integer
96
+ def ms_of(time) = (Time.iso8601(time.to_s).to_r * 1000).floor
97
+
98
+ #: (untyped) -> Hash[Symbol, Integer]
99
+ def timestamp(time) = stamp(ms_of(time))
100
+
101
+ #: (Integer) -> Hash[Symbol, Integer]
102
+ def stamp(ms) = { seconds: ms.div(1000), nanos: (ms % 1000) * 1_000_000 }
103
+
104
+ #: (ReportRun::Scenario, Hash[String, untyped], ^(Hash[Symbol, untyped]) -> void) -> void
105
+ def test_case(scenario, pickle, append)
106
+ json = scenario.json
107
+ start = ms_of(json[:started_at])
108
+ stop = ms_of(json[:finished_at])
109
+ case_id = SecureRandom.uuid
110
+ started_id = SecureRandom.uuid
111
+ previous = start
112
+ steps = json[:steps].each_with_index.map do |step, index|
113
+ id = step[:step_record_id]
114
+ record = id && scenario.step_records[id]
115
+ unreadable = id && scenario.step_records.key?(id) && record.nil?
116
+ # A step with no record of its own is zero-width at the end of
117
+ # the step before it.
118
+ from, to = record ? [ms_of(record["started_at"]), ms_of(record["finished_at"])] : [previous, previous]
119
+ previous = to
120
+ step_id = SecureRandom.uuid
121
+ message = step.dig(:error, :message)
122
+ result = if unreadable
123
+ result(from, to, "UNKNOWN", "step record #{id} could not be read; run.json records this step as #{step[:status]}")
124
+ else
125
+ broken = record && record["status"] == "failed" && record.dig("error", "kind") == "claim_broken"
126
+ result(from, to, STATUS.fetch(step[:status].to_s, "UNKNOWN"), message, broken)
127
+ end
128
+ { step: { id: step_id, **(pickle["steps"][index] ? { pickleStepId: pickle["steps"][index]["id"] } : {}) }, from: from, to: to, result: result,
129
+ attachments: record ? claims_attachment(record, started_id, step_id) : [] }
130
+ end
131
+ append.({ testCase: { id: case_id, pickleId: pickle["id"], testSteps: steps.map { _1[:step] } } })
132
+ append.({ testCaseStarted: { attempt: 0, id: started_id, testCaseId: case_id, timestamp: stamp(start) } })
133
+ steps.each do |step|
134
+ append.({ testStepStarted: { testCaseStartedId: started_id, testStepId: step[:step][:id], timestamp: stamp(step[:from]) } })
135
+ step[:attachments].each { append.({ attachment: _1 }) }
136
+ append.({ testStepFinished: { testCaseStartedId: started_id, testStepId: step[:step][:id], timestamp: stamp(step[:to]), testStepResult: step[:result] } })
137
+ end
138
+ if (verdict = scenario.verdict)
139
+ append.({ attachment: { body: JSON.generate({ verdict: verdict[:verdict], reason: verdict[:reason] }), contentEncoding: "IDENTITY",
140
+ mediaType: VERDICT_MEDIA_TYPE, fileName: "verdict.json", testCaseStartedId: started_id } })
141
+ end
142
+ append.({ testCaseFinished: { testCaseStartedId: started_id, timestamp: stamp(stop), willBeRetried: false } })
143
+ end
144
+
145
+ # `exception` is set for a broken claim only: its type is a fact
146
+ # nukadoko decided, and any other failure carries a message alone.
147
+ #: (Integer, Integer, String, String?, ?bool) -> Hash[Symbol, untyped]
148
+ def result(from, to, status, message, broken = false)
149
+ span = [to - from, 0].max
150
+ { status: status, duration: stamp(span), **(message ? { message: message } : {}),
151
+ **(broken && message ? { exception: { type: CLAIM_BROKEN_EXCEPTION, message: message } } : {}) }
152
+ end
153
+
154
+ # The readings go with the claims: a value that walked toward the
155
+ # expected one across a poll is visible only in them.
156
+ #: (Hash[String, untyped], String, String) -> Array[Hash[Symbol, untyped]]
157
+ def claims_attachment(record, started_id, step_id)
158
+ return [] unless record.key?("claims")
159
+
160
+ body = { claims: record["claims"], **{ observation: record["observation"], attempts: record["attempts"] }.compact,
161
+ **CLAIM_FIELDS.to_h { [_1.to_sym, record[_1]] }.compact }
162
+ [{ body: JSON.generate(body), contentEncoding: "IDENTITY", mediaType: CLAIMS_MEDIA_TYPE, fileName: "claims.json",
163
+ testCaseStartedId: started_id, testStepId: step_id }]
164
+ end
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,169 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "json"
5
+ require "gherkin"
6
+
7
+ # One run directory read back into what the report writers take, as the
8
+ # TypeScript engine's src/report/load-run.ts reads it: each feature the run
9
+ # parsed, from sources.json, and each head execution of run.json with its
10
+ # step records, its pickle, and what the verdict table says of it. It reads
11
+ # only the run directory, so a directory copied off a CI machine renders
12
+ # the same as one in the project that ran it.
13
+ #
14
+ # A run directory copied off a machine is untrusted input: an edited
15
+ # record can name any path, so every path a record names is resolved,
16
+ # symlinks included, and refused unless it stays inside the run directory.
17
+ # A refused path is a problem the report names, never a file it reads.
18
+ #
19
+ # The pickles are parsed again from sources.json, so their ids differ from
20
+ # the run's; no output keeps a pickle id across renders.
21
+ module Nukadoko
22
+ module Engine
23
+ class ReportRun
24
+ Feature = Data.define(
25
+ :path, #: String
26
+ :source, #: String
27
+ :document, #: Hash[String, untyped]
28
+ :pickles, #: Array[Hash[String, untyped]]
29
+ )
30
+
31
+ Scenario = Data.define(
32
+ :json, #: Hash[Symbol, untyped]
33
+ :step_records, #: Hash[String, Hash[String, untyped]?]
34
+ :feature, #: Feature
35
+ :pickle, #: Hash[String, untyped]
36
+ :verdict, #: Hash[Symbol, untyped]?
37
+ )
38
+
39
+ attr_reader :dir #: String
40
+ attr_reader :document #: Hash[Symbol, untyped]
41
+ attr_reader :project_name #: String?
42
+ attr_reader :features #: Array[Feature]
43
+ attr_reader :scenarios #: Array[Scenario]
44
+ attr_reader :problems #: Array[String]
45
+
46
+ # The run in `dir`, or a Refusal that says why it cannot be read.
47
+ #: (String, ?project_root: String) -> void
48
+ def initialize(dir, project_root: Dir.pwd)
49
+ @dir = dir
50
+ @project_root = project_root
51
+ @root = File.realpath(dir)
52
+ run_json = File.join(dir, "run.json")
53
+ raise Refusal.new("run-not-found", "#{dir} holds no run.json that nuka run wrote") unless File.file?(run_json)
54
+ raise Refusal.new("run-invalid", "#{run_json} resolves outside the run directory, so it is not read") unless inside?(run_json)
55
+
56
+ @document = begin
57
+ JSON.parse(File.read(run_json, encoding: "UTF-8"), symbolize_names: true)
58
+ rescue JSON::ParserError => e
59
+ raise Refusal.new("run-invalid", "#{run_json} does not parse as JSON: #{e.message.lines.first.to_s.chomp}")
60
+ end
61
+ @problems = []
62
+ @project_name = nil
63
+ @features = read_features
64
+ @scenarios = read_scenarios
65
+ end
66
+
67
+ # Where a path inside the run directory is on disk; raises for one
68
+ # that resolves outside it.
69
+ #: (String) -> String
70
+ def resolve(relative)
71
+ file = File.join(@dir, relative)
72
+ raise ArgumentError, "#{relative} resolves outside the run directory, so it is not read" unless inside?(file)
73
+
74
+ file
75
+ end
76
+
77
+ # Where a path a record names is on disk: one under `runs/<run id>/`
78
+ # is under the run directory wherever the directory now is, and any
79
+ # other is under the project. Raises for one that resolves outside
80
+ # the run directory.
81
+ #: (String) -> String
82
+ def resolve_recorded(recorded)
83
+ segments = recorded.split(%r{[\\/]})
84
+ at = (0...(segments.size - 1)).find { segments[_1] == "runs" && segments[_1 + 1] == @document[:run_id] }
85
+ file = at ? File.join(@dir, *segments[(at + 2)..].to_a) : File.expand_path(recorded, @project_root)
86
+ raise ArgumentError, "#{recorded} resolves outside the run directory, so it is not read" unless inside?(file)
87
+
88
+ file
89
+ end
90
+
91
+ private
92
+
93
+ #: (String) -> bool
94
+ def inside?(file)
95
+ real = File.exist?(file) ? File.realpath(file) : File.expand_path(file)
96
+ real == @root || real.start_with?("#{@root}/")
97
+ end
98
+
99
+ #: () -> Array[Feature]
100
+ def read_features
101
+ sources = File.join(@dir, "sources.json")
102
+ id = @document[:run_id]
103
+ unless inside?(sources)
104
+ raise Refusal.new("run-sources-invalid", "#{@dir} holds a sources.json that resolves outside the run directory, so the scenarios of run #{id} cannot be parsed")
105
+ end
106
+ raise Refusal.new("run-sources-missing", "#{@dir} holds no sources.json, so the scenarios of run #{id} cannot be parsed") unless File.file?(sources)
107
+
108
+ parsed = begin
109
+ JSON.parse(File.read(sources, encoding: "UTF-8"))
110
+ rescue JSON::ParserError => e
111
+ raise Refusal.new("run-sources-invalid", "#{@dir} holds a sources.json that does not parse as JSON: #{e.message.lines.first.to_s.chomp}, so the scenarios of run #{id} cannot be parsed")
112
+ end
113
+ list = (parsed["features"] if parsed.is_a?(Hash)) #: untyped
114
+ unless list.is_a?(Array) && list.all? { _1.is_a?(Hash) && _1["path"].is_a?(String) && _1["source"].is_a?(String) }
115
+ raise Refusal.new("run-sources-invalid", "#{@dir} holds a sources.json that lacks a field, so the scenarios of run #{id} cannot be parsed")
116
+ end
117
+
118
+ @project_name = parsed["project"].is_a?(String) ? parsed["project"] : nil
119
+ list.filter_map do |entry|
120
+ gherkin = Gherkin #: untyped
121
+ envelopes = gherkin.from_source(entry["path"], entry["source"], include_source: false, include_gherkin_document: true, include_pickles: true).to_a
122
+ error = envelopes.find(&:parse_error)
123
+ next @problems << "#{entry["path"]} in sources.json does not parse: #{error.parse_error.message}" if error
124
+
125
+ document = JSON.parse(envelopes.find(&:gherkin_document).gherkin_document.to_json)
126
+ Feature.new(path: entry["path"], source: entry["source"], document: document,
127
+ pickles: envelopes.select(&:pickle).map { JSON.parse(_1.pickle.to_json) })
128
+ end
129
+ end
130
+
131
+ # An execution of the base or the old world is a cell of the table,
132
+ # not a test result, so the formats render head's executions alone.
133
+ #: () -> Array[Scenario]
134
+ def read_scenarios
135
+ pickles = @features.flat_map { |feature| feature.pickles.map { [[feature.path, _1.dig("location", "line")], [feature, _1]] } }.to_h
136
+ @document[:scenarios].filter_map do |scenario|
137
+ next if scenario[:world]
138
+
139
+ feature, pickle = pickles[[scenario[:feature], scenario[:line]]]
140
+ unless feature && pickle
141
+ next @problems << "#{scenario[:feature]}:#{scenario[:line]} (#{scenario[:scenario_record_id]}) matches no scenario in sources.json"
142
+ end
143
+
144
+ records = {} #: Hash[String, Hash[String, untyped]?]
145
+ scenario[:steps].each do |step|
146
+ id = step[:step_record_id]
147
+ next if id.nil? || step[:record].nil? || records.key?(id)
148
+
149
+ records[id] = begin
150
+ JSON.parse(File.read(resolve(File.join(step[:record], "record.json")), encoding: "UTF-8"))
151
+ rescue StandardError => e
152
+ @problems << (e.is_a?(ArgumentError) ? "#{scenario[:scenario_record_id]}: #{e.message}" : "#{step[:record]}/record.json of #{scenario[:scenario_record_id]} could not be read")
153
+ nil
154
+ end
155
+ end
156
+ Scenario.new(json: scenario, step_records: records, feature: feature, pickle: pickle, verdict: verdict_of(scenario))
157
+ end
158
+ end
159
+
160
+ # What the verdict table says of an execution's row and cell.
161
+ #: (Hash[Symbol, untyped]) -> Hash[Symbol, untyped]?
162
+ def verdict_of(scenario)
163
+ row = @document.dig(:table, :rows)&.find { _1[:feature] == scenario[:feature] && _1[:line] == scenario[:line] }
164
+ cell = row && row[:cells].find { _1[:world] == "head" && _1[:environment] == scenario[:environment] }
165
+ row && cell ? { verdict: row[:verdict], reason: row[:reason], role: cell[:role] } : nil
166
+ end
167
+ end
168
+ end
169
+ end