nukadoko 0.5.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 (203) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +553 -3
  3. data/README.ja.md +22 -6
  4. data/README.md +13 -7
  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 +403 -50
  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 +22 -1
  77. data/lib/nukadoko/judgments.rb +33 -3
  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 +1156 -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 +803 -30
  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/verdict.rb +18 -1
  104. data/lib/nukadoko/version.rb +1 -1
  105. data/lib/nukadoko/vocabulary.rb +166 -23
  106. data/lib/nukadoko/walk.rb +2 -2
  107. data/lib/nukadoko/walk_step.rb +1 -1
  108. data/lib/nukadoko.rb +23 -0
  109. data/llms.txt +42 -35
  110. data/sig/generated/nukadoko/base.rbs +4 -4
  111. data/sig/generated/nukadoko/changed_files.rbs +42 -0
  112. data/sig/generated/nukadoko/check.rbs +147 -10
  113. data/sig/generated/nukadoko/claim_drops.rbs +135 -0
  114. data/sig/generated/nukadoko/claim_line.rbs +11 -0
  115. data/sig/generated/nukadoko/claim_rules.rbs +49 -4
  116. data/sig/generated/nukadoko/claim_sides.rbs +63 -12
  117. data/sig/generated/nukadoko/cli.rbs +80 -7
  118. data/sig/generated/nukadoko/codes.rbs +19 -4
  119. data/sig/generated/nukadoko/command.rbs +68 -1
  120. data/sig/generated/nukadoko/command_help.rbs +18 -0
  121. data/sig/generated/nukadoko/compat.rbs +392 -0
  122. data/sig/generated/nukadoko/criteria.rbs +1 -1
  123. data/sig/generated/nukadoko/definition.rbs +50 -0
  124. data/sig/generated/nukadoko/error_frames.rbs +1 -1
  125. data/sig/generated/nukadoko/events/count.rbs +69 -0
  126. data/sig/generated/nukadoko/events/counted.rbs +70 -0
  127. data/sig/generated/nukadoko/events/format.rbs +163 -0
  128. data/sig/generated/nukadoko/events/lists.rbs +47 -0
  129. data/sig/generated/nukadoko/events/path.rbs +161 -0
  130. data/sig/generated/nukadoko/events/read.rbs +109 -0
  131. data/sig/generated/nukadoko/events/reader.rbs +226 -0
  132. data/sig/generated/nukadoko/events/selection.rbs +100 -0
  133. data/sig/generated/nukadoko/events/tally.rbs +227 -0
  134. data/sig/generated/nukadoko/features.rbs +6 -4
  135. data/sig/generated/nukadoko/fixtures.rbs +67 -11
  136. data/sig/generated/nukadoko/http_recording.rbs +1 -1
  137. data/sig/generated/nukadoko/intent.rbs +132 -0
  138. data/sig/generated/nukadoko/intent_file.rbs +46 -0
  139. data/sig/generated/nukadoko/intent_view.rbs +69 -0
  140. data/sig/generated/nukadoko/judgment_file.rbs +7 -1
  141. data/sig/generated/nukadoko/judgments.rbs +10 -0
  142. data/sig/generated/nukadoko/measure_mcp.rbs +104 -0
  143. data/sig/generated/nukadoko/parts.rbs +2 -2
  144. data/sig/generated/nukadoko/path_view.rbs +105 -0
  145. data/sig/generated/nukadoko/place_rows.rbs +43 -0
  146. data/sig/generated/nukadoko/program.rbs +627 -23
  147. data/sig/generated/nukadoko/program_log.rbs +67 -0
  148. data/sig/generated/nukadoko/program_shape.rbs +147 -0
  149. data/sig/generated/nukadoko/project_fixtures.rbs +16 -3
  150. data/sig/generated/nukadoko/project_program.rbs +2 -1
  151. data/sig/generated/nukadoko/repeated_prefix.rbs +172 -0
  152. data/sig/generated/nukadoko/report_run.rbs +1 -1
  153. data/sig/generated/nukadoko/run.rbs +286 -41
  154. data/sig/generated/nukadoko/run_record.rbs +20 -12
  155. data/sig/generated/nukadoko/scaffold.rbs +3 -0
  156. data/sig/generated/nukadoko/schema.rbs +5 -0
  157. data/sig/generated/nukadoko/secrets.rbs +26 -3
  158. data/sig/generated/nukadoko/states.rbs +214 -0
  159. data/sig/generated/nukadoko/step.rbs +58 -1
  160. data/sig/generated/nukadoko/step_fixtures.rbs +39 -0
  161. data/sig/generated/nukadoko/summary.rbs +256 -2
  162. data/sig/generated/nukadoko/table.rbs +3 -2
  163. data/sig/generated/nukadoko/trace.rbs +108 -33
  164. data/sig/generated/nukadoko/tree_passes.rbs +36 -8
  165. data/sig/generated/nukadoko/underneath.rbs +141 -0
  166. data/sig/generated/nukadoko/verdict.rbs +8 -0
  167. data/sig/generated/nukadoko/vocabulary.rbs +45 -6
  168. data/skills/nukadoko/SKILL.md +18 -18
  169. data/skills/nukadoko/references/checking-steps.md +16 -83
  170. data/skills/nukadoko/references/diagnosing.md +6 -6
  171. data/skills/nukadoko/references/done.md +22 -0
  172. data/skills/nukadoko/references/evidence.md +19 -6
  173. data/skills/nukadoko/references/exploring.md +22 -7
  174. data/skills/nukadoko/references/fixtures.md +12 -32
  175. data/skills/nukadoko/references/judgments.md +18 -0
  176. data/skills/nukadoko/references/maintenance.md +1 -1
  177. data/skills/nukadoko/references/merge-risk.md +1 -1
  178. data/skills/nukadoko/references/parts.md +46 -23
  179. data/skills/nukadoko/references/{perturbed-worlds.md → perturbed-systems.md} +30 -12
  180. data/skills/nukadoko/references/program.md +168 -169
  181. data/skills/nukadoko/references/reviewing.md +20 -20
  182. data/skills/nukadoko/references/ruby/checking-steps.md +106 -0
  183. data/skills/nukadoko/references/ruby/examples/cli.md +212 -0
  184. data/skills/nukadoko/references/ruby/fixtures.md +34 -0
  185. data/skills/nukadoko/references/ruby/program.md +112 -0
  186. data/skills/nukadoko/references/ruby/step-files.md +115 -0
  187. data/skills/nukadoko/references/running.md +34 -34
  188. data/skills/nukadoko/references/scenario-rules.md +7 -6
  189. data/skills/nukadoko/references/states.md +7 -1
  190. data/skills/nukadoko/references/step-files.md +41 -120
  191. data/skills/nukadoko/references/typescript/checking-steps.md +129 -0
  192. data/skills/nukadoko/references/{examples → typescript/examples}/cli.md +1 -1
  193. data/skills/nukadoko/references/{examples → typescript/examples}/http-api.md +6 -6
  194. data/skills/nukadoko/references/{examples → typescript/examples}/workflow.md +1 -1
  195. data/skills/nukadoko/references/typescript/fixtures.md +56 -0
  196. data/skills/nukadoko/references/typescript/program.md +111 -0
  197. data/skills/nukadoko/references/typescript/step-files.md +104 -0
  198. data/skills/nukadoko/references/verdicts.md +10 -10
  199. data/skills/nukadoko/references/writing-steps.md +67 -67
  200. data/skills/nukadoko/references/writing-the-feature.md +1 -1
  201. data/skills/nukadoko-verification/SKILL.md +13 -8
  202. metadata +103 -7
  203. data/skills/nukadoko/references/ruby.md +0 -80
@@ -0,0 +1,364 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "json"
5
+
6
+ # Responsibility: the intent file's format (docs/intent.md): what a change
7
+ # declares it means to do differently, as one Markdown table whose rows
8
+ # are target x observation x before -> after, each cell in the execution
9
+ # events vocabulary. Reading it, writing it back, and the two questions a
10
+ # row answers about one entry of a path: does the row aim at it, and does
11
+ # a difference on it go the way the row says. Pure. Boundary: where the
12
+ # file is and what git says of it are IntentFile's; which criteria a row
13
+ # reached, and what each came to, are IntentView's.
14
+ #
15
+ # Only the table whose header is exactly
16
+ # `| Target | Observation | Before | After |` is read; prose around it is
17
+ # for people. A cell's terms are the events vocabulary: a kind, and
18
+ # `field<op>value` over a line's fields, so a row names what the program
19
+ # reports and never a step, a criterion's text, or an id.
20
+ #
21
+ # A value is JSON when it parses as JSON and is otherwise the bare text:
22
+ # `plan:team` and `"plan:team"` are one value, and `10` is a number. A
23
+ # term ends at a space outside a JSON string or brackets, so a value with
24
+ # spaces is written as JSON, and a `|` in a cell is written `\|`, as
25
+ # Markdown has it.
26
+ #
27
+ # Before and after say how many lines a step's path counts for an
28
+ # observation with no bare field (`0` -> `1`), or which value the bare
29
+ # field had. Both left empty says the observation is not meant to change
30
+ # at all, which is how a refactor declares that decisions stay as they
31
+ # were. A row that cannot be read is a problem named by its line, never
32
+ # left out in silence.
33
+ module Nukadoko
34
+ module Engine
35
+ module Intent
36
+ FILE = "INTENT.md"
37
+ HEADER = %w[Target Observation Before After].freeze
38
+ OPS = %w[>= <= != = > <].freeze
39
+ FIELD = /\A[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z0-9_]+)*\z/
40
+
41
+ module_function
42
+
43
+ # The cells of a Markdown table line: between its outer pipes, split
44
+ # at each `|` that `\` does not escape, each trimmed, `\|` read as `|`.
45
+ #: (String) -> Array[String]?
46
+ def table_cells(line)
47
+ text = line.strip
48
+ return nil if !text.start_with?("|") || !text.end_with?("|") || text.size < 2 || text.end_with?("\\|")
49
+
50
+ cells = [] #: Array[String]
51
+ cell = +""
52
+ index = 1
53
+ while index < text.size - 1
54
+ char = text[index]
55
+ if char == "\\" && text[index + 1] == "|"
56
+ cell << "|"
57
+ index += 1
58
+ elsif char == "|"
59
+ cells << cell.strip
60
+ cell = +""
61
+ else
62
+ cell << char.to_s
63
+ end
64
+ index += 1
65
+ end
66
+ cells << cell.strip
67
+ end
68
+
69
+ # A cell as a table line holds it: `|` escaped.
70
+ #: (String) -> String
71
+ def cell_text(value) = value.gsub("|", "\\|")
72
+
73
+ # The terms of a cell, split at each run of spaces outside a JSON
74
+ # string and outside brackets.
75
+ #: (String) -> Array[String]
76
+ def split_terms(text)
77
+ terms = [] #: Array[String]
78
+ term = +""
79
+ depth = 0
80
+ quoted = false
81
+ chars = text.chars
82
+ index = 0
83
+ while index < chars.size
84
+ char = chars.fetch(index)
85
+ if quoted
86
+ term << char
87
+ if char == "\\"
88
+ term << (chars[index + 1] || "")
89
+ index += 1
90
+ elsif char == '"'
91
+ quoted = false
92
+ end
93
+ index += 1
94
+ next
95
+ end
96
+ if char == '"' then quoted = true
97
+ elsif "[{".include?(char) then depth += 1
98
+ elsif "]}".include?(char) && depth.positive? then depth -= 1
99
+ end
100
+ if char.match?(/\s/) && depth.zero?
101
+ terms << term unless term.empty?
102
+ term = +""
103
+ else
104
+ term << char
105
+ end
106
+ index += 1
107
+ end
108
+ terms << term unless term.empty?
109
+ terms
110
+ end
111
+
112
+ # A value as written: JSON when it parses, the bare text otherwise.
113
+ #: (String) -> untyped
114
+ def value_of(text)
115
+ JSON.parse(text)
116
+ rescue JSON::ParserError
117
+ text
118
+ end
119
+
120
+ # A value as a cell writes it: bare when that reads back as the same
121
+ # value and holds no space, pipe, quote, or bracket, and JSON
122
+ # otherwise.
123
+ #: (untyped) -> String
124
+ def value_text(value)
125
+ return value if value.is_a?(String) && !value.empty? && !value.match?(/[\s|"\[\]{}]/) && value_of(value) == value
126
+
127
+ JSON.generate(value)
128
+ end
129
+
130
+ #: (String) -> Hash[Symbol, untyped]
131
+ def term_of(text)
132
+ return { bare: text } if FIELD.match?(text)
133
+
134
+ (1...text.size).each do |index|
135
+ op = OPS.find { text[index, _1.size] == _1 }
136
+ next if op.nil?
137
+
138
+ field = text[0, index].to_s
139
+ break unless FIELD.match?(field)
140
+
141
+ value = text[(index + op.size)..].to_s
142
+ return { problem: "`#{text}` has no value after `#{op}`" } if value.empty?
143
+
144
+ return { field: field, op: op, value: value_of(value) }
145
+ end
146
+ { problem: "`#{text}` is neither a field nor `field<op>value` with one of #{OPS.map { "`#{_1}`" }.join(", ")}" }
147
+ end
148
+
149
+ # Why a field cannot be read on a kind's lines, or nil.
150
+ #: (String, String) -> String?
151
+ def field_problem(kind, field)
152
+ if Events.own_kind?(kind)
153
+ return field.start_with?(Events::EXTENSION_PREFIX) ? nil : "a #{kind} line is read only at a field of its own, which starts with `#{Events::EXTENSION_PREFIX}`, not at `#{field}`"
154
+ end
155
+
156
+ fields = Events::PATH_KINDS.fetch(kind)[:fields]
157
+ fields.include?(field) ? nil : "a path keeps a `#{kind}` line by #{fields.map { "`#{_1}`" }.join(", ")}, not by `#{field}`"
158
+ end
159
+
160
+ # A term as a path's key reads it: an `http` origin on loopback
161
+ # without its port, as a path keeps it.
162
+ #: (String, Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
163
+ def key_term(kind, term)
164
+ return term unless kind == "http" && term[:field] == "origin" && term[:value].is_a?(String)
165
+
166
+ term.merge(value: Events.path_origin(term[:value]))
167
+ end
168
+
169
+ #: (String) -> Integer?
170
+ def count_of(text) = text.match?(/\A(?:0|[1-9][0-9]*)\z/) ? text.to_i : nil
171
+
172
+ #: (Integer, Integer, Hash[Symbol, String]) -> Hash[Symbol, untyped]
173
+ def row_of(row, line, cells)
174
+ problem = ->(message) { { line: line, message: "row #{row}: #{message}" } }
175
+ tags = [] #: Array[String]
176
+ targets = [] #: Array[String]
177
+ split_terms(cells[:target].to_s).each { _1.match?(/\A@\S+\z/) ? tags << _1 : targets << _1 }
178
+ kind, *observed = split_terms(cells[:observation].to_s)
179
+ return problem.("the observation names no kind of line") if kind.nil?
180
+ unless Events.own_kind?(kind) || Events::PATH_KINDS.key?(kind)
181
+ return problem.("`#{kind}` is not a kind a path keeps: one of #{Events::PATH_KINDS.keys.map { "`#{_1}`" }.join(", ")}, " \
182
+ "or a writer's own kind that starts with `#{Events::EXTENSION_PREFIX}`")
183
+ end
184
+
185
+ conditions = [] #: Array[Hash[Symbol, untyped]]
186
+ targets.each do |text|
187
+ term = term_of(text)
188
+ return problem.(term[:problem]) if term[:problem]
189
+ return problem.("the target term `#{text}` names a field with no value; a target is tags and `field<op>value` terms") if term[:bare]
190
+
191
+ conditions << key_term(kind, term)
192
+ end
193
+ filters = [] #: Array[Hash[Symbol, untyped]]
194
+ projections = [] #: Array[String]
195
+ observed.each do |text|
196
+ term = term_of(text)
197
+ return problem.(term[:problem]) if term[:problem]
198
+
199
+ term[:bare] ? projections << term[:bare] : filters << key_term(kind, term)
200
+ end
201
+ if projections.size > 1
202
+ return problem.("the observation names #{projections.size} fields with no value (#{projections.map { "`#{_1}`" }.join(", ")}); it observes at most one")
203
+ end
204
+
205
+ [*conditions.map { _1[:field] }, *filters.map { _1[:field] }, *projections].each do |field|
206
+ why = field_problem(kind, field)
207
+ return problem.(why) if why
208
+ end
209
+ if kind == "db" && (conditions + filters).any? { _1[:field] == "op" && _1[:op] == "=" && _1[:value] == "read" }
210
+ return problem.("a path keeps no database read, so `op=read` matches nothing")
211
+ end
212
+
213
+ projection = projections.first
214
+ read = { row: row, line: line, cells: cells, tags: tags, conditions: conditions, kind: kind, filters: filters, projection: projection }
215
+ return read.merge(change: nil) if cells[:before].to_s.empty? && cells[:after].to_s.empty?
216
+ return problem.("before and after are both empty, for no change, or both filled") if cells[:before].to_s.empty? || cells[:after].to_s.empty?
217
+
218
+ if projection.nil?
219
+ before = count_of(cells[:before].to_s)
220
+ after = count_of(cells[:after].to_s)
221
+ if before.nil? || after.nil?
222
+ return problem.("an observation with no field to read counts lines, so before and after are whole numbers, not `#{cells[:before]}` and `#{cells[:after]}`")
223
+ end
224
+ return problem.("before and after are both #{before}; leave both empty for no change") if before == after
225
+
226
+ return read.merge(change: { before: before, after: after })
227
+ end
228
+ before = value_of(cells[:before].to_s)
229
+ after = value_of(cells[:after].to_s)
230
+ return problem.("before and after are the same value; leave both empty for no change") if same_value?(before, after)
231
+
232
+ read.merge(change: { before: before, after: after })
233
+ end
234
+
235
+ # Reads the intent file's text: `{ rows:, problems: }`. Pure.
236
+ #: (String) -> Hash[Symbol, Array[Hash[Symbol, untyped]]]
237
+ def parse(text)
238
+ lines = text.split(/\r?\n/, -1)
239
+ start = lines.index { (cells = table_cells(_1)) && cells == HEADER }
240
+ return { rows: [], problems: [{ line: nil, message: "no table has the header `| #{HEADER.join(" | ")} |`" }] } if start.nil?
241
+
242
+ rows = [] #: Array[Hash[Symbol, untyped]]
243
+ problems = [] #: Array[Hash[Symbol, untyped]]
244
+ delimiter = table_cells(lines[start + 1] || "")
245
+ if delimiter.nil? || delimiter.size != HEADER.size || !delimiter.all? { _1.match?(/\A:?-+:?\z/) }
246
+ return { rows: rows, problems: [{ line: start + 2, message: "the header is not followed by a delimiter row such as `| --- | --- | --- | --- |`" }] }
247
+ end
248
+
249
+ row = 0
250
+ ((start + 2)...lines.size).each do |index|
251
+ cells = table_cells(lines.fetch(index))
252
+ break if cells.nil?
253
+
254
+ row += 1
255
+ if cells.size != HEADER.size
256
+ problems << { line: index + 1, message: "row #{row}: has #{cells.size} cells, not #{HEADER.size}" }
257
+ next
258
+ end
259
+ target, observation, before, after = cells
260
+ read = row_of(row, index + 1, { target: target.to_s, observation: observation.to_s, before: before.to_s, after: after.to_s })
261
+ read.key?(:kind) ? rows << read : problems << read
262
+ end
263
+ { rows: rows, problems: problems }
264
+ end
265
+
266
+ # The table a list of rows' cells makes, header first: what `parse`
267
+ # reads back to the same cells.
268
+ #: (Array[Hash[Symbol, String]]) -> String
269
+ def table(rows)
270
+ line = ->(cells) { "| #{cells.map { cell_text(_1) }.join(" | ")} |" }
271
+ [line.(HEADER), line.(HEADER.map { "---" }), *rows.map { line.(_1.values_at(:target, :observation, :before, :after).map(&:to_s)) }].join("\n")
272
+ end
273
+
274
+ # For each writer's own kind a row observes, every field a row reads
275
+ # on it, so a path counts the kind by them.
276
+ #: (Hash[Symbol, untyped]) -> Hash[String, Array[String]]
277
+ def own_fields(intent)
278
+ fields = {} #: Hash[String, Array[String]]
279
+ intent[:rows].each do |row|
280
+ next unless Events.own_kind?(row[:kind])
281
+
282
+ named = fields[row[:kind]] ||= []
283
+ [*row[:conditions].map { _1[:field] }, *row[:filters].map { _1[:field] }, *row[:projection]].each { named << _1 unless named.include?(_1) }
284
+ end
285
+ fields.transform_values(&:sort)
286
+ end
287
+
288
+ # Two values are one when their JSON is, objects' keys in any order.
289
+ #: (untyped, untyped) -> bool
290
+ def same_value?(one, other) = canonical(one) == canonical(other)
291
+
292
+ #: (untyped) -> String
293
+ def canonical(value)
294
+ case value
295
+ when Array then "[#{value.map { canonical(_1) }.join(",")}]"
296
+ when Hash then "{#{value.sort_by { |key, _| key.to_s }.map { |key, inner| "#{JSON.generate(key.to_s)}:#{canonical(inner)}" }.join(",")}}"
297
+ else JSON.generate(value)
298
+ end
299
+ end
300
+
301
+ # The value a key or a line holds at a field: a path's own key holds
302
+ # a dotted field as one name, a line as nested objects. `nil` with
303
+ # `found: false` when it holds none.
304
+ #: (Hash[String, untyped], String) -> [bool, untyped]
305
+ def field_value(record, field)
306
+ return [true, record[field]] if record.key?(field)
307
+
308
+ value = record
309
+ field.split(".").each do |segment|
310
+ return [false, nil] unless value.is_a?(Hash) && value.key?(segment)
311
+
312
+ value = value[segment]
313
+ end
314
+ [true, value]
315
+ end
316
+
317
+ # Whether a term holds on a record. A field the record lacks meets no
318
+ # term, and an order compares two numbers or two strings alone.
319
+ #: (Hash[Symbol, untyped], Hash[String, untyped]) -> bool
320
+ def term_holds?(term, record)
321
+ found, value = field_value(record, term[:field])
322
+ return false unless found
323
+
324
+ expected = term[:value]
325
+ return same_value?(value, expected) if term[:op] == "="
326
+ return !same_value?(value, expected) if term[:op] == "!="
327
+ return false unless (value.is_a?(Numeric) && expected.is_a?(Numeric)) || (value.is_a?(String) && expected.is_a?(String))
328
+
329
+ case term[:op]
330
+ when "<" then value < expected
331
+ when "<=" then value <= expected
332
+ when ">" then value > expected
333
+ else value >= expected
334
+ end
335
+ end
336
+
337
+ # Whether a row aims at an entry of a path: its kind, and every
338
+ # condition and filter holds on it.
339
+ #: (Hash[Symbol, untyped], Hash[String, untyped]) -> bool
340
+ def observes?(row, key) = key["kind"] == row[:kind] && (row[:conditions] + row[:filters]).all? { term_holds?(_1, key) }
341
+
342
+ #: (Hash[Symbol, untyped], Integer) -> bool
343
+ def within?(range, count) = range[:at_least] <= count && (range[:at_most].nil? || count <= range[:at_most])
344
+
345
+ # Whether a difference on an entry the row observes goes the way the
346
+ # row says. For a count: the base side can hold `before` and head
347
+ # `after`. For a value: the entry holds `before` and fewer lines at
348
+ # head, or `after` and more; the caller pairs the two. A row meant not
349
+ # to change declares no difference.
350
+ #: (Hash[Symbol, untyped], Hash[String, untyped], Hash[Symbol, untyped], Hash[Symbol, untyped]) -> Symbol?
351
+ def declares(row, key, one, other)
352
+ change = row[:change]
353
+ return nil if change.nil?
354
+ return within?(one, change[:before]) && within?(other, change[:after]) ? :count : nil if row[:projection].nil?
355
+
356
+ _, value = field_value(key, row[:projection])
357
+ return :gone if same_value?(value, change[:before]) && !other[:at_most].nil? && other[:at_most] < one[:at_least]
358
+ return :came if same_value?(value, change[:after]) && !one[:at_most].nil? && one[:at_most] < other[:at_least]
359
+
360
+ nil
361
+ end
362
+ end
363
+ end
364
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # Responsibility: the intent a change declares (docs/intent.md), read
5
+ # under `nuka run --base`: the file at the repository's top as head's
6
+ # working tree holds it, and what git says of it since the base. Boundary:
7
+ # git and the disk only; the format is Intent's, what each row came to
8
+ # IntentView's.
9
+ #
10
+ # The file is the change's when the change wrote it: a file the base
11
+ # already holds unchanged is an earlier change's intent, merged with it,
12
+ # and says nothing about this one. So a run reads it only when head's
13
+ # text differs from the base's, and a change that removes it declares
14
+ # nothing.
15
+ #
16
+ # The reviewer reads when it was declared: in the change's first commit,
17
+ # before anything it judges was written, or later, and each commit since
18
+ # that changed it again. A change of intent is itself a difference the
19
+ # reviewer approves, so it is named, never folded into the rows.
20
+ module Nukadoko
21
+ module Engine
22
+ module IntentFile
23
+ # git did not answer.
24
+ class GitFailed < StandardError; end
25
+
26
+ module_function
27
+
28
+ # The intent the change at `project` declares against `base_commit`:
29
+ # `{ intent:, record: }`, the record run.json's `intent` before its
30
+ # rows are judged, or nil when it declares none. Raises GitFailed
31
+ # when git does not answer.
32
+ #: (String, String) -> Hash[Symbol, untyped]?
33
+ def read(project, base_commit)
34
+ top = git(project, "rev-parse", "--show-toplevel").strip
35
+ prefix = git(project, "rev-parse", "--show-prefix").strip.delete_suffix("/")
36
+ file = File.join(top, Intent::FILE)
37
+ return nil unless File.file?(file)
38
+
39
+ text = File.read(file, encoding: "UTF-8")
40
+ return nil if text == at_commit(top, base_commit)
41
+
42
+ all = commits_since(top, base_commit)
43
+ touched = commits_since(top, base_commit, Intent::FILE)
44
+ first, *later = touched
45
+ at_head = all.empty? ? at_commit(top, base_commit) : at_commit(top, all.fetch(-1)[:commit])
46
+ { intent: Intent.parse(text),
47
+ record: { file: "#{"../" * (prefix.empty? ? 0 : prefix.split("/").size)}#{Intent::FILE}",
48
+ declared_in: first && first.merge(first: first[:commit] == all.first&.dig(:commit)),
49
+ changed_after: later, uncommitted: text != at_head, problems: [] } }
50
+ end
51
+
52
+ #: (String, *String) -> String
53
+ def git(dir, *argv)
54
+ out, ok = Base.git(dir, *argv, strip: false)
55
+ raise GitFailed, "git #{argv.first} failed in #{dir}" unless ok
56
+
57
+ out
58
+ end
59
+
60
+ # The file at a commit, or nil when the commit holds none.
61
+ #: (String, String) -> String?
62
+ def at_commit(top, commit)
63
+ listed = git(top, "ls-tree", "-z", "--name-only", commit, "--", Intent::FILE).split("\0")
64
+ return nil unless listed.include?(Intent::FILE)
65
+
66
+ git(top, "show", "#{commit}:#{Intent::FILE}").force_encoding("UTF-8")
67
+ end
68
+
69
+ # The commits after `base` up to HEAD, oldest first, each with its
70
+ # subject: all of them, or with `file`, those that changed it.
71
+ #: (String, String, ?String?) -> Array[Hash[Symbol, String]]
72
+ def commits_since(top, base, file = nil)
73
+ out = git(top, "log", "-z", "--reverse", "--format=%H%x09%s", "#{base}..HEAD", *(file ? ["--", file] : []))
74
+ out.force_encoding("UTF-8").split("\0").reject(&:empty?).map do |line|
75
+ commit, subject = line.split("\t", 2)
76
+ { commit: commit.to_s, subject: subject.to_s }
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,190 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # Responsibility: what each row of a change's intent came to against the
5
+ # base pairs of run.json's `paths` (docs/intent.md), and which row declares
6
+ # each difference there. Pure: it reads the comparisons, the scenarios
7
+ # they name, and each criterion's tags. Boundary: reading the intent is
8
+ # Intent's, comparing two paths PathView's; this module adds the account
9
+ # and changes no count.
10
+ #
11
+ # A row aims at a criterion that carries every tag of its target, and,
12
+ # when its target or a bare field reads a line's values, whose steps on
13
+ # either side hold an entry those values meet. A row that counts lines
14
+ # with no such term aims at every criterion its tags name, since `0 -> 1`
15
+ # must be told apart from nothing happening where nothing was there to
16
+ # see. Each criterion it aims at either did what it says or did not: a
17
+ # row meant to change declares a difference there, and one meant not to
18
+ # change finds no difference on what it observes that no other row
19
+ # declares. A row that aimed at no compared criterion was not reached.
20
+ #
21
+ # A value row declares a difference only in pairs, within one step: the
22
+ # lines that held the old value go, and as many that hold the new one
23
+ # come. A value that went somewhere else is no such pair, so neither half
24
+ # is declared.
25
+ #
26
+ # Only a decided difference (`changed`) is declared or not: an `open` one
27
+ # could still read the same. A changed criterion of a base pair whose
28
+ # every difference a row declares, with no step that ran on one side
29
+ # alone, is explained by the intent; a new or changed criterion keeps its
30
+ # own sentences as the account for the rest; anything else is a
31
+ # difference nothing the change declares asked for.
32
+ module Nukadoko
33
+ module Engine
34
+ module IntentView
35
+ module_function
36
+
37
+ #: (Hash[Symbol, untyped]) -> String
38
+ def place_text(place) = "#{place[:feature]}:#{place[:line]}"
39
+
40
+ # A difference's key, as a row reads it.
41
+ #: (Hash[Symbol, untyped]) -> Hash[String, untyped]
42
+ def key_of(difference) = difference.except(:holds, :a, :b, :intent).transform_keys(&:to_s)
43
+
44
+ #: (Hash[Symbol, untyped]?) -> Array[Hash[String, untyped]]
45
+ def keys_of_path(path)
46
+ return [] if path.nil?
47
+
48
+ read = PathView.path_of(path)
49
+ [*read[:entries], *read[:requests].map { Events.request_key(_1) }]
50
+ end
51
+
52
+ # Whether a row aims at a criterion, from its tags and every entry its
53
+ # two sides' steps hold.
54
+ #: (Hash[Symbol, untyped], Array[String], Array[Hash[String, untyped]]) -> bool
55
+ def aims?(row, tags, seen)
56
+ return false unless row[:tags].all? { tags.include?(_1) }
57
+ return true if row[:conditions].empty? && row[:projection].nil?
58
+
59
+ # A count row's filters name what is counted, which may be absent on
60
+ # both sides; its target alone says where it aims.
61
+ reads = row[:projection].nil? ? row.merge(filters: []) : row
62
+ seen.any? { Intent.observes?(reads, _1) }
63
+ end
64
+
65
+ # The first row that declares each difference of one step, by its
66
+ # index among `changed`, in the rows' order: a count at once, a value
67
+ # in pairs.
68
+ #: (Array[Hash[Symbol, untyped]], Array[Hash[Symbol, untyped]], Array[Hash[Symbol, untyped]]) -> Hash[Integer, Integer]
69
+ def declared(rows, aimed, changed)
70
+ found = {} #: Hash[Integer, Integer]
71
+ rows.each do |row|
72
+ next unless aimed.include?(row)
73
+
74
+ gone = [] #: Array[Integer]
75
+ came = [] #: Array[Integer]
76
+ changed.each_with_index do |difference, index|
77
+ next if found.key?(index) || !Intent.observes?(row, key_of(difference))
78
+
79
+ case Intent.declares(row, key_of(difference), difference[:a], difference[:b])
80
+ when :count then found[index] = row[:row]
81
+ when :gone then gone << index
82
+ when :came then came << index
83
+ end
84
+ end
85
+ # How many lines went, and how many came: a decided difference's
86
+ # bounds on the side it shrank toward are known.
87
+ went = gone.sum { changed.fetch(_1)[:a][:at_least] - (changed.fetch(_1)[:b][:at_most] || 0) }
88
+ arrived = came.sum { changed.fetch(_1)[:b][:at_least] - (changed.fetch(_1)[:a][:at_most] || 0) }
89
+ (gone + came).each { found[_1] = row[:row] } if gone.any? && came.any? && went == arrived
90
+ end
91
+ found
92
+ end
93
+
94
+ # Judges each row against the base pairs of `paths`: `{ paths:, rows: }`.
95
+ # `tags_of` gives a criterion's tags, as head's feature file has them.
96
+ #: (Hash[Symbol, untyped], Array[Hash[Symbol, untyped]], Array[Hash[Symbol, untyped]], Array[Hash[Symbol, untyped]], ^(Hash[Symbol, untyped]) -> Array[String]) -> Hash[Symbol, untyped]
97
+ def judge(intent, paths, scenarios, rows, tags_of)
98
+ by_id = scenarios.to_h { [_1[:scenario_record_id], _1] }
99
+ change_of = rows.to_h { [place_text(_1), _1[:change]] }
100
+ happened = intent[:rows].to_h { [_1[:row], []] } #: Hash[Integer, Array[String]]
101
+ missed = intent[:rows].to_h { [_1[:row], []] } #: Hash[Integer, Array[String]]
102
+ judge_one = lambda do |criterion|
103
+ place = place_text(criterion)
104
+ tags = tags_of.(criterion)
105
+ sides = [by_id[criterion[:a][:scenario_record_id]], by_id[criterion[:b][:scenario_record_id]]]
106
+ seen = sides.flat_map { (_1&.dig(:steps) || []).flat_map { |step| keys_of_path(step[:path]) } }
107
+ aimed = intent[:rows].select { aims?(_1, tags, seen) }
108
+ steps = (criterion[:steps] || []).map do |step|
109
+ next step unless step[:ran] == "both"
110
+
111
+ by_row = declared(intent[:rows], aimed, step[:changed])
112
+ step.merge(changed: step[:changed].each_with_index.map { |difference, index| by_row.key?(index) ? difference.merge(intent: by_row[index]) : difference })
113
+ end
114
+ changed = steps.flat_map { _1[:ran] == "both" ? _1[:changed] : [] }
115
+ aimed.each do |row|
116
+ did = if row[:change].nil? then changed.none? { _1[:intent].nil? && Intent.observes?(row, key_of(_1)) }
117
+ else changed.any? { _1[:intent] == row[:row] }
118
+ end
119
+ list = (did ? happened : missed).fetch(row[:row])
120
+ list << place unless list.include?(place)
121
+ end
122
+ next(criterion[:steps].nil? ? criterion : criterion.merge(steps: steps)) unless criterion.key?(:explained_by)
123
+
124
+ unmarked = changed.select { _1[:holds].nil? }
125
+ by_intent = unmarked.any? && unmarked.all? { !_1[:intent].nil? } && steps.all? { _1[:ran] == "both" }
126
+ change = change_of[place]
127
+ explained_by = if by_intent then "intent"
128
+ elsif %w[new changed].include?(change) then "criterion_change"
129
+ end
130
+ criterion.merge(explained_by: explained_by, steps: steps)
131
+ end
132
+ judged = paths.map { _1.dig(:between, :kind) == "base" ? _1.merge(criteria: _1[:criteria].map(&judge_one)) : _1 }
133
+ order = rows.each_with_index.to_h { |row, index| [place_text(row), index] }
134
+ places = lambda do |list|
135
+ list.sort_by.with_index { |text, index| [order[text] || 0, index] }.map do |text|
136
+ at = text.rindex(":").to_i
137
+ { feature: text[0, at], line: text[(at + 1)..].to_i }
138
+ end
139
+ end
140
+ judged_rows = intent[:rows].map do |row|
141
+ missing = missed.fetch(row[:row])
142
+ # A criterion that missed in one environment and did it in another
143
+ # did not do it everywhere it was aimed at.
144
+ did = happened.fetch(row[:row]) - missing
145
+ result = if missing.any? then "not_happening"
146
+ elsif did.any? then "as_intended"
147
+ else "not_reached"
148
+ end
149
+ { row: row[:row], result: result, as_intended: places.(did), not_happening: places.(missing) }
150
+ end
151
+ { paths: judged, rows: judged_rows }
152
+ end
153
+
154
+ # The findings that gate a run whose change declares an intent: each
155
+ # problem reading it, each criterion with a difference nothing the
156
+ # change declares, and each row that did not happen where it aimed. A
157
+ # row not reached gates nothing. `rerun` gives the command that runs
158
+ # one criterion against the same base.
159
+ #: (Hash[Symbol, untyped], Array[Hash[Symbol, untyped]], ^(Hash[Symbol, untyped]) -> String) -> Array[Hash[Symbol, untyped]]
160
+ def findings(intent, paths, rerun)
161
+ file = intent[:file]
162
+ found = intent[:problems].map do |problem|
163
+ { severity: "failed", code: "intent-unread", file: file, line: problem[:line],
164
+ message: "#{file} cannot be read as an intent: #{problem[:message]}", fix: Codes.fix("intent-unread") }.compact
165
+ end
166
+ unexplained = {} #: Hash[String, Hash[Symbol, untyped]]
167
+ paths.each do |comparison|
168
+ next unless comparison.dig(:between, :kind) == "base"
169
+
170
+ comparison[:criteria].each { unexplained[place_text(_1)] = _1 if _1.key?(:explained_by) && _1[:explained_by].nil? }
171
+ end
172
+ unexplained.each_value do |criterion|
173
+ found << { severity: "failed", code: "intent-unintended", file: criterion[:feature], line: criterion[:line],
174
+ message: "Scenario \"#{criterion[:name]}\" did something at head that it did not do at the base, and neither #{file} nor its sentences declare it; " \
175
+ "\"base → head\" in the summary lists what",
176
+ fix: Codes.fix("intent-unintended").to_s.gsub("INTENT.md", file), do: rerun.(criterion) }
177
+ end
178
+ intent[:rows].each do |row|
179
+ next unless row[:result] == "not_happening"
180
+
181
+ first = row[:not_happening].first
182
+ found << { severity: "failed", code: "intent-not-happening", file: file, line: row[:line],
183
+ message: "row #{row[:row]} of #{file} did not happen in #{row[:not_happening].map { place_text(_1) }.join(", ")}, which its target names",
184
+ fix: Codes.fix("intent-not-happening"), do: first && rerun.(first) }.compact
185
+ end
186
+ found
187
+ end
188
+ end
189
+ end
190
+ end
@@ -31,7 +31,8 @@ module Nukadoko
31
31
  raise Error, failure if failure
32
32
 
33
33
  begin
34
- JSON.parse(text)
34
+ # JSON.parse keeps the last of two values for one key, and so does this.
35
+ JSON.parse(text, allow_duplicate_key: true)
35
36
  rescue JSON::ParserError => e
36
37
  # JSON.parse keeps a lone surrogate escape (\ud800) in its string; a
37
38
  # Ruby string has no way to hold one, so the text is refused here.
@@ -113,7 +113,7 @@ module Nukadoko
113
113
  def unjudgeable(row, target, run, values)
114
114
  return "#{run[:run_id]} ran no criterion at #{target}" unless row
115
115
 
116
- required = row[:cells].select { _1[:world] == "head" && _1[:role] == "required" }
116
+ required = row[:cells].select { _1[:system] == "head" && _1[:role] == "required" }
117
117
  if required.empty? || required.any? { _1[:outcome] != "pass" }
118
118
  return "#{target} did not pass in #{run[:run_id]} (#{row[:reason]}), and only a criterion whose required cells passed has a value to judge"
119
119
  end
@@ -138,7 +138,7 @@ module Nukadoko
138
138
 
139
139
  #: (Hash[Symbol, untyped], Hash[Symbol, untyped]) -> Array[String]
140
140
  def seen_of(run, read)
141
- scenario = run[:scenarios].find { |entry| !entry[:world] && entry[:environment] == read[:environment] && entry[:steps].any? { _1[:line] == read[:line] && _1[:text] == read[:step] } }
141
+ scenario = run[:scenarios].find { |entry| !entry[:system] && entry[:environment] == read[:environment] && entry[:steps].any? { _1[:line] == read[:line] && _1[:text] == read[:step] } }
142
142
  step = scenario && scenario[:steps].find { _1[:line] == read[:line] && _1[:text] == read[:step] }
143
143
  (step&.dig(:judged_view, :pages) || []).flat_map { [_1.dig(:screenshot, :file), _1.dig(:aria, :file)].grep(String) }
144
144
  end