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
@@ -0,0 +1,478 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # The states a step declares (`requires`, `gives`, `ends`), read and refused
5
+ # as the TypeScript engine's defineStep refuses them, and the walk that
6
+ # judges one scenario against them: whether a line that requires a state
7
+ # can only be reached with it not held. nuka check reports that walk per
8
+ # feature file, and nuka run refuses a scenario it finds before any of its
9
+ # steps runs, so a scenario never passes one and fails the other.
10
+ #
11
+ # The walk follows the one path on which a line runs at all: every line
12
+ # before it passed. A line no step matches makes every state unknown, and
13
+ # unknown is never reported. It never says a state holds, only that it
14
+ # cannot.
15
+ module Nukadoko
16
+ module Engine
17
+ module States
18
+ Use = Data.define(
19
+ :state, #: String
20
+ :key, #: String?
21
+ )
22
+
23
+ Declared = Data.define(
24
+ :requires, #: Array[Use]
25
+ :gives, #: Array[Use]
26
+ :ends, #: Array[Use]
27
+ )
28
+
29
+ # One requiring line the walk can only reach with the state not held.
30
+ Draft = Data.define(
31
+ :index, #: Integer
32
+ :step_name, #: String
33
+ :use, #: Use
34
+ :holder, #: String?
35
+ :cause, #: Hash[Symbol, untyped]
36
+ :also, #: Array[Integer]
37
+ )
38
+
39
+ # One line of a scenario: the step it binds alone, and the text each
40
+ # capture read, or nil for a line no single step matches.
41
+ Line = Data.define(
42
+ :step, #: singleton(Step)
43
+ :texts, #: Hash[String, String]
44
+ )
45
+
46
+ # What the walk knows of one state just before a line: a fact per
47
+ # holder a line has named, and one for every holder none has. A fact
48
+ # is [:held], [:not_held, the index of the line that ended it or
49
+ # nil], or [:unknown].
50
+ Facts = Data.define(
51
+ :by, #: Hash[String, Array[untyped]]
52
+ :unnamed, #: Array[untyped]
53
+ )
54
+
55
+ LISTS = %i[requires gives ends].freeze
56
+ FORMS = 'a name ("signed-in") or a [name, key] pair (["signed-in", "person"])'
57
+
58
+ # `signed-in for person`, or `book-open`: how nuka steps names an entry.
59
+ #: (Use) -> String
60
+ def self.describe(use) = use.key ? "#{use.state} for #{use.key}" : use.state
61
+
62
+ #: (Use) -> String
63
+ def self.quoted(use) = use.key ? "[#{use.state.to_json}, #{use.key.to_json}]" : use.state.to_json
64
+
65
+ # The three lists read against the step's args keys, or the problem
66
+ # with the first entry that is not a state a line can hold, in the
67
+ # TypeScript engine's words.
68
+ #: (singleton(Step)) -> (Declared | String)
69
+ def self.read(step)
70
+ keys = step.args.fields.map { _1.name.to_s }
71
+ read = {} #: Hash[Symbol, Array[Use]]
72
+ LISTS.each do |list|
73
+ uses = [] #: Array[Use]
74
+ step.public_send(list).each_with_index do |entry, index|
75
+ at = "#{list}[#{index}]"
76
+ use = entry_use(entry)
77
+ return "#{at} is not a state: write #{FORMS}" unless use
78
+ if use.state.empty? || use.state.strip != use.state
79
+ return "#{at} names the state #{use.state.to_json}, and a state name is not empty and has no space at either end"
80
+ end
81
+ if use.key && !keys.include?(use.key)
82
+ declared = keys.empty? ? "args declares no key" : "args declares #{keys.map(&:to_json).join(", ")}"
83
+ return "#{at} names args key #{use.key.to_json}, which args does not declare: #{declared}"
84
+ end
85
+ earlier = uses.index(use)
86
+ return "#{at} repeats #{quoted(use)} from #{list}[#{earlier}]" if earlier
87
+
88
+ uses << use
89
+ end
90
+ read[list] = uses
91
+ end
92
+ gives = read.fetch(:gives)
93
+ ends = read.fetch(:ends)
94
+ gives.each_with_index do |use, index|
95
+ ended = ends.index(use)
96
+ return "gives[#{index}] and ends[#{ended}] both name #{quoted(use)}, and a step leaves a state one way" if ended
97
+ end
98
+ Declared.new(requires: read.fetch(:requires), gives: gives, ends: ends)
99
+ end
100
+
101
+ #: (untyped) -> Use?
102
+ def self.entry_use(entry)
103
+ return Use.new(state: entry.to_s, key: nil) if entry.is_a?(String) || entry.is_a?(Symbol)
104
+ return nil unless entry.is_a?(Array) && entry.size == 2 && entry.all? { _1.is_a?(String) || _1.is_a?(Symbol) }
105
+
106
+ Use.new(state: entry[0].to_s, key: entry[1].to_s)
107
+ end
108
+
109
+ # What every step of a vocabulary says about each state: which name it
110
+ # with a key and which without, and which can give it from a line.
111
+ class Index
112
+ attr_reader :states #: Hash[String, Hash[Symbol, Array[untyped]]]
113
+
114
+ #: (Array[singleton(Step)]) -> void
115
+ def initialize(steps)
116
+ @states = {}
117
+ @parts = {} #: Hash[singleton(Step), Array[String]]
118
+ steps.each do |step|
119
+ declared = step.states
120
+ next unless declared
121
+
122
+ [*declared.requires, *declared.gives, *declared.ends].each do |use|
123
+ info = (@states[use.state] ||= { keyed: [], unkeyed: [], givers: [] })
124
+ shape = use.key ? info[:keyed] : info[:unkeyed]
125
+ shape << step.step_name unless shape.include?(step.step_name)
126
+ end
127
+ pattern = step.patterns.first
128
+ if pattern
129
+ declared.gives.each do |use|
130
+ givers = @states.fetch(use.state)[:givers]
131
+ givers << { name: step.step_name, key: use.key, pattern: pattern } unless givers.any? { _1[:name] == step.step_name }
132
+ end
133
+ end
134
+ @parts[step] = through_parts(step)
135
+ end
136
+ end
137
+
138
+ # The states the step's transitive parts give or end.
139
+ #: (singleton(Step)) -> Array[String]
140
+ def parts_of(step) = @parts[step] || []
141
+
142
+ #: (String) -> bool
143
+ def mismatched?(state)
144
+ info = @states.fetch(state)
145
+ info[:keyed].any? && info[:unkeyed].any?
146
+ end
147
+
148
+ private
149
+
150
+ #: (singleton(Step)) -> Array[String]
151
+ def through_parts(step)
152
+ found = [] #: Array[String]
153
+ seen = [step]
154
+ visit = lambda do |current|
155
+ current.parts.each do |part|
156
+ next if seen.include?(part) || !(part.is_a?(Class) && part < Step)
157
+
158
+ seen << part
159
+ declared = part.states
160
+ found.concat([*declared.gives, *declared.ends].map(&:state)) if declared
161
+ visit.(part)
162
+ end
163
+ end
164
+ visit.(step)
165
+ found.uniq
166
+ end
167
+ end
168
+
169
+ # Each line of the scenario as the walk reads it: the one step it
170
+ # binds and the text each word or string capture read.
171
+ #: (Array[singleton(Step)], Scenario) -> Array[Line?]
172
+ def self.lines(steps, scenario)
173
+ scenario.steps.map do |line|
174
+ matched = [] #: Array[Line]
175
+ steps.each do |step|
176
+ texts = step.patterns.lazy.filter_map { captured(_1, line.text) }.first
177
+ matched << Line.new(step: step, texts: texts) if texts
178
+ end
179
+ matched.size == 1 ? matched.first : nil
180
+ end
181
+ end
182
+
183
+ # A {word} capture reads its text as written, and a {string} its text
184
+ # inside the quotes; another type reads a value only the run knows.
185
+ #: (String, String) -> Hash[String, String]?
186
+ def self.captured(pattern, text)
187
+ values = Pattern.new(pattern).match(text)
188
+ return nil unless values
189
+
190
+ types = pattern.scan(Pattern::NAMED).to_h { |key, type| [key.to_s, type.to_s] }
191
+ values.select { |key, value| %w[word string].include?(types[key]) && value.is_a?(String) }
192
+ end
193
+
194
+ # The holder a line names for `use`: "" for an unkeyed state, the
195
+ # captured text when the key's field is a plain string, or nil when
196
+ # only the run knows it.
197
+ #: (Line, Use) -> String?
198
+ def self.holder_of(line, use)
199
+ return "" unless use.key
200
+
201
+ text = line.texts[use.key.to_s]
202
+ text && line.step.args[use.key.to_s]&.type == :string ? text : nil
203
+ end
204
+
205
+ # Walks the scenario once and returns every requiring line that only
206
+ # one outcome can reach: the state not held. Pure.
207
+ #: (Array[Line?], Index) -> Array[Draft]
208
+ def self.walk(lines, index)
209
+ return [] if index.states.empty?
210
+
211
+ facts = index.states.keys.to_h { [_1, Facts.new(by: {}, unnamed: [:not_held, nil])] }
212
+ given_at = [] #: Array[[Integer, String, String]]
213
+ drafts = [] #: Array[Draft]
214
+ open = {} #: Hash[untyped, Draft]
215
+ undeclared = lambda do |from, to, state|
216
+ lines[from...to].to_a.filter_map do |line|
217
+ next unless line && !line.step.claims
218
+
219
+ declared = line.step.states
220
+ line.step.step_name unless declared && [*declared.requires, *declared.gives, *declared.ends].any? { _1.state == state }
221
+ end.uniq
222
+ end
223
+
224
+ lines.each_with_index do |line, at|
225
+ declared = line&.step&.states
226
+ if line && declared
227
+ declared.requires.each do |use|
228
+ info = index.states.fetch(use.state)
229
+ next if index.mismatched?(use.state)
230
+
231
+ current = facts.fetch(use.state)
232
+ holder = holder_of(line, use)
233
+ unless holder
234
+ nobody = current.unnamed[0] == :not_held && current.by.values.all? { _1[0] == :not_held }
235
+ next unless nobody
236
+
237
+ cause = info[:givers].empty? ? { kind: :no_giver } : { kind: :held_by_none, undeclared: undeclared.(0, at, use.state) }
238
+ drafts << Draft.new(index: at, step_name: line.step.step_name, use: use, holder: nil, cause: cause, also: [])
239
+ next
240
+ end
241
+ fact = current.by[holder] || current.unnamed
242
+ next unless fact[0] == :not_held
243
+
244
+ fold = [use.state, holder, fact[1]]
245
+ if (earlier = open[fold])
246
+ earlier.also << at unless earlier.index == at || earlier.also.include?(at)
247
+ next
248
+ end
249
+ ender = fact[1] && lines[fact[1]]
250
+ cause = if info[:givers].empty? then { kind: :no_giver }
251
+ elsif fact[1] && ender then { kind: :ended, index: fact[1], step_name: ender.step.step_name, undeclared: undeclared.(fact[1] + 1, at, use.state) }
252
+ else
253
+ { kind: :never_given, others: given_at.select { _1[1] == use.state && _1[2] != holder }, undeclared: undeclared.(0, at, use.state) }
254
+ end
255
+ draft = Draft.new(index: at, step_name: line.step.step_name, use: use, holder: use.key ? holder : nil, cause: cause, also: [])
256
+ open[fold] = draft
257
+ drafts << draft
258
+ end
259
+ end
260
+ if line.nil? || declared.nil?
261
+ facts = facts.transform_values { Facts.new(by: {}, unnamed: [:unknown]) }
262
+ next
263
+ end
264
+
265
+ changes = [] #: Array[[String, Symbol, String?]]
266
+ declared.ends.each { changes << [_1.state, :end, holder_of(line, _1)] }
267
+ declared.gives.each { changes << [_1.state, :give, holder_of(line, _1)] }
268
+ named = changes.map(&:first)
269
+ index.parts_of(line.step).each { changes << [_1, :unknown, nil] unless named.include?(_1) }
270
+ changes.each do |change|
271
+ state, op, holder = change
272
+ facts[state] = apply(facts.fetch(state), op, holder, at)
273
+ given_at << [at, state, holder] if op == :give && holder && holder != ""
274
+ end
275
+ end
276
+ drafts
277
+ end
278
+
279
+ #: (Facts, Symbol, String?, Integer) -> Facts
280
+ def self.apply(facts, op, holder, at)
281
+ return Facts.new(by: {}, unnamed: [:unknown]) if op == :unknown
282
+ return Facts.new(by: facts.by.merge(holder => op == :give ? [:held] : [:not_held, at]), unnamed: facts.unnamed) if holder
283
+
284
+ # Exactly one holder changes, and the walk does not know which.
285
+ changing = op == :give ? :not_held : :held
286
+ blur = ->(fact) { fact[0] == changing ? [:unknown] : fact }
287
+ Facts.new(by: facts.by.transform_values(&blur), unnamed: blur.(facts.unnamed))
288
+ end
289
+
290
+ # The scenario's state issues by line index, each the message nuka run
291
+ # puts on the line it refuses.
292
+ #: (Array[singleton(Step)], Scenario) -> Hash[Integer, Array[String]]
293
+ def self.refusals(steps, scenario)
294
+ index = Index.new(steps)
295
+ lines = self.lines(steps, scenario)
296
+ at = ->(i) { "line #{scenario.steps[i].line}" }
297
+ walk(lines, index).each_with_object({}) do |draft, found|
298
+ (found[draft.index] ||= []) << message(draft, at, scenario, draft.also.map(&at))
299
+ end
300
+ end
301
+
302
+ # state-missing over every scenario of one feature file, for nuka
303
+ # check. A Background line reaches the same finding in every
304
+ # scenario, so it is reported once with the folded lines of all of
305
+ # them.
306
+ #: (Array[singleton(Step)], Array[Scenario]) -> Array[Hash[Symbol, untyped]]
307
+ def self.findings(steps, scenarios)
308
+ index = Index.new(steps)
309
+ merged = {} #: Hash[untyped, Hash[Symbol, untyped]]
310
+ scenarios.each do |scenario|
311
+ at = ->(i) { "line #{scenario.steps[i].line}" }
312
+ walk(lines(steps, scenario), index).each do |draft|
313
+ line = scenario.steps[draft.index].line
314
+ key = [scenario.uri, line, message(draft, at, scenario, []), fix(draft, at, index, at.(draft.index))]
315
+ found = (merged[key] ||= { draft: draft, scenario: scenario, at: at, uri: scenario.uri, line: line, folded: {}, scenarios: [] })
316
+ draft.also.each { found[:folded][at.(_1)] = scenario.steps[_1].line }
317
+ found[:scenarios] |= [[scenario.uri, scenario.keyword_line]] if draft.also.any?
318
+ end
319
+ end
320
+ merged.values.map do |found|
321
+ draft = found[:draft]
322
+ at = found[:at]
323
+ labels = found[:folded].sort_by { _2 }.map(&:first)
324
+ last = found[:scenarios].size > 1 ? nil : (labels.last || at.(draft.index))
325
+ { uri: found[:uri], line: found[:line], step: draft.step_name,
326
+ message: message(draft, at, found[:scenario], labels), fix: fix(draft, at, index, last) }
327
+ end
328
+ end
329
+
330
+ # Every state some steps name with a key and others without.
331
+ #: (Array[singleton(Step)]) -> Array[Hash[Symbol, untyped]]
332
+ def self.shape_issues(steps)
333
+ index = Index.new(steps)
334
+ index.states.filter_map do |state, info|
335
+ next unless index.mismatched?(state)
336
+
337
+ named = ->(names) { names.size == 1 ? %(step "#{names.first}") : "steps #{names.map { %("#{_1}") }.join(", ")}" }
338
+ { state: state, step: (info[:unkeyed].size <= info[:keyed].size ? info[:unkeyed] : info[:keyed]).first,
339
+ message: %(State "#{state}" has a holder key in #{named.(info[:keyed])} and none in #{named.(info[:unkeyed])}, so no line that requires it is judged),
340
+ fix: %(Write "#{state}" the same way in every step: ["#{state}", "<key>"] in each requires, gives, and ends entry, or the bare name in all of them.) }
341
+ end
342
+ end
343
+
344
+ #: (Array[String]) -> String
345
+ def self.listed(items) = items.size <= 2 ? items.join(" and ") : "#{items[0...-1].to_a.join(", ")}, and #{items.last}"
346
+
347
+ #: (Draft, ^(Integer) -> String, Scenario, Array[String]) -> String
348
+ def self.message(draft, at, scenario, also)
349
+ use = draft.use
350
+ holder = draft.holder
351
+ cause = draft.cause
352
+ subject = if use.key.nil? then %(Step "#{draft.step_name}" requires "#{use.state}")
353
+ elsif holder.nil? then %(Step "#{draft.step_name}" requires "#{use.state}" for a #{use.key} this line does not name)
354
+ else %(Step "#{draft.step_name}" requires "#{use.state}" for #{use.key} "#{holder}")
355
+ end
356
+ reason = case cause[:kind]
357
+ when :no_giver then "no step gives it, so no line can"
358
+ when :ended then %(#{at.(cause[:index])} "#{scenario.steps[cause[:index]].text}" (step "#{cause[:step_name]}") ends it, with no line between them giving it again)
359
+ when :held_by_none then "by what the earlier lines declare, no #{use.key || "holder"} holds it here"
360
+ else
361
+ if draft.index.zero?
362
+ "no earlier line gives it: #{at.(0)} is the scenario's first line"
363
+ else
364
+ others = cause[:others].map { |given, _, who| "#{at.(given)} gives it to #{who}" }
365
+ "no earlier line gives it#{holder ? " to #{holder}" : ""}#{others.any? ? ": #{listed(others)}" : ""}"
366
+ end
367
+ end
368
+ "#{subject}, and #{reason}#{also_required(also)}. nuka run refuses this scenario before any of its steps runs"
369
+ end
370
+
371
+ #: (Array[String]) -> String
372
+ def self.also_required(labels)
373
+ return "" if labels.empty?
374
+ return "; #{labels.first} requires it too" if labels.size == 1
375
+
376
+ numbers = labels.all? { _1.start_with?("line ") } ? labels.map { _1.delete_prefix("line ") } : nil
377
+ "; #{numbers ? "lines #{listed(numbers)}" : listed(labels)} require it too"
378
+ end
379
+
380
+ #: (Draft, ^(Integer) -> String, Index, String?) -> String
381
+ def self.fix(draft, at, index, last)
382
+ use = draft.use
383
+ cause = draft.cause
384
+ ref = use.key ? %(["#{use.state}", "<key>"]) : %("#{use.state}")
385
+ if cause[:kind] == :no_giver
386
+ given = index.states.select { |_, info| info[:givers].any? }.keys
387
+ known = given.empty? ? "No step gives a state yet." : "The states steps give: #{given.map { %("#{_1}") }.join(", ")}."
388
+ return %(Add #{ref} to the gives of the step that leaves it holding, or correct the name in the requires of step "#{draft.step_name}". #{known})
389
+ end
390
+ givers = index.states.fetch(use.state)[:givers].map do |giver|
391
+ filled = draft.holder && giver[:key] ? { giver[:key] => draft.holder } : {}
392
+ %("#{giver[:name]}" ("#{ExampleLine.of(giver[:pattern], filled)}"))
393
+ end
394
+ runs = givers.size == 1 ? "step #{givers.first}" : "one of steps #{givers.join(", ")}"
395
+ put = if cause[:kind] == :ended
396
+ "Put a line that runs #{runs} between #{at.(cause[:index])} and #{at.(draft.index)}#{last ? ", or move #{at.(cause[:index])} below #{last}" : ""}."
397
+ else
398
+ "Put a line that runs #{runs} before #{at.(draft.index)}."
399
+ end
400
+ names = cause[:undeclared]
401
+ undeclared = if names.empty? then ""
402
+ elsif names.size == 1 then %( Or, if step "#{names.first}" gives "#{use.state}" without saying so, add #{ref} to its gives.)
403
+ else %( Or, if one of steps #{names.map { %("#{_1}") }.join(", ")} gives "#{use.state}" without saying so, add #{ref} to that step's gives.)
404
+ end
405
+ %(#{put}#{undeclared} If the scenario means to show what happens without "#{use.state}", the line needs a step that does not require it.)
406
+ end
407
+ end
408
+
409
+ # A feature line a pattern matches, for a finding that tells an agent
410
+ # which line to add: a capture becomes the value given for its key, an
411
+ # optional group is dropped (or kept when it stands as a word of its
412
+ # own), an alternation keeps its first alternative, and an escape gives
413
+ # its character. It never runs a parameter type.
414
+ module ExampleLine
415
+ #: (String, Hash[String, String]) -> String
416
+ def self.of(pattern, filled)
417
+ line = +""
418
+ i = 0
419
+ while i < pattern.length
420
+ ch = pattern[i].to_s
421
+ if ch == "\\" && i + 1 < pattern.length
422
+ line << pattern[i + 1].to_s
423
+ i += 2
424
+ elsif ch == "{"
425
+ close = pattern.index("}", i + 1)
426
+ return line + pattern[i..].to_s unless close
427
+
428
+ line << capture(pattern[(i + 1)...close].to_s, filled)
429
+ i = close + 1
430
+ elsif ch == "("
431
+ close = skip_optional(pattern, i)
432
+ alone = (i.zero? || pattern[i - 1] == " ") && (close >= pattern.length || pattern[close] == " ")
433
+ line << pattern[(i + 1)...(close - 1)].to_s.gsub(/\\(.)/, '\1') if alone
434
+ i = close
435
+ elsif ch == "/"
436
+ i = skip_alternatives(pattern, i)
437
+ else
438
+ line << ch
439
+ i += 1
440
+ end
441
+ end
442
+ line
443
+ end
444
+
445
+ # {word} matches no space, so a holder a {string} capture read
446
+ # elsewhere ("Aki Tanaka") would make a line the step never matches.
447
+ #: (String, Hash[String, String]) -> String
448
+ def self.capture(token, filled)
449
+ key, type = token.split(":", 2)
450
+ return "{#{token}}" unless key && type && key.match?(/\A[A-Za-z_][\w-]*\z/)
451
+
452
+ given = filled[key]
453
+ fits = given && !(type == "word" && (given.empty? || given.match?(/\s/)))
454
+ value = fits ? given.to_s : "<#{key}>"
455
+ type == "string" ? %("#{value.gsub('"', '\\"')}") : value
456
+ end
457
+
458
+ #: (String, Integer) -> Integer
459
+ def self.skip_optional(pattern, start)
460
+ i = start + 1
461
+ i += pattern[i] == "\\" ? 2 : 1 while i < pattern.length && pattern[i] != ")"
462
+ i + 1
463
+ end
464
+
465
+ #: (String, Integer) -> Integer
466
+ def self.skip_alternatives(pattern, start)
467
+ i = start + 1
468
+ while i < pattern.length && pattern[i] != " " && pattern[i] != "{"
469
+ i = if pattern[i] == "\\" then i + 2
470
+ elsif pattern[i] == "(" then skip_optional(pattern, i)
471
+ else i + 1
472
+ end
473
+ end
474
+ i
475
+ end
476
+ end
477
+ end
478
+ end
data/lib/nukadoko/step.rb CHANGED
@@ -24,6 +24,17 @@ module Nukadoko
24
24
  #: (String) -> String
25
25
  #: () -> String?
26
26
  def pattern(text = nil) = text ? (@pattern = text) : @pattern
27
+
28
+ # Every wording a line may bind the step by: `pattern` first, then
29
+ # each of `patterns`, as the TypeScript engine reads both. Each one
30
+ # must capture the same keys, which nuka check holds them to.
31
+ #: (*String | Array[String]) -> Array[String]
32
+ def patterns(*texts)
33
+ return [*@pattern, *@patterns] if texts.empty?
34
+
35
+ @patterns = texts.flatten.freeze
36
+ end
37
+
27
38
  #: (String) -> String
28
39
  #: () -> String?
29
40
  def description(text = nil) = text ? (@description = text) : @description
@@ -37,6 +48,60 @@ module Nukadoko
37
48
  #: () -> untyped
38
49
  def poll(value = nil) = value.nil? ? @poll : (@poll = value)
39
50
 
51
+ # How long `run` (or `observe`) may take, in milliseconds, before the
52
+ # step fails as timed out. Without it the config's stepTimeout
53
+ # applies, twenty minutes by default; there is no value for no
54
+ # limit, since a step that never returns would take the run with it.
55
+ #: (untyped) -> untyped
56
+ #: () -> untyped
57
+ def timeout(ms = nil) = ms.nil? ? @timeout : (@timeout = ms)
58
+
59
+ # The paths an earlier step's commands changed, by the key a claim
60
+ # reads them as: `files written: { of: RunBuild, change: "added",
61
+ # under: "src/" }`. nukadoko reads the list from that step's record,
62
+ # never from what its code returned.
63
+ #: (Hash[Symbol | String, untyped]) -> Hash[String, untyped]
64
+ #: () -> Hash[String, untyped]
65
+ def files(map = nil)
66
+ return @files || {} unless map
67
+
68
+ @files = map.is_a?(Hash) ? map.transform_keys(&:to_s) : map
69
+ end
70
+
71
+ # The counts of an earlier step's execution events, by the key a
72
+ # claim reads them as: `events writes: { of: PlaceScore, count: {
73
+ # kind: "db", op: %w[insert update delete other], committed: true } }`.
74
+ # nukadoko reads the lines of that step's trace from the system's
75
+ # events file, never from what its code returned.
76
+ #: (Hash[Symbol | String, untyped]) -> Hash[String, untyped]
77
+ #: () -> Hash[String, untyped]
78
+ def events(map = nil)
79
+ return @events || {} unless map
80
+
81
+ @events = map.is_a?(Hash) ? map.transform_keys(&:to_s) : map
82
+ end
83
+
84
+ # The states a line of this step needs held (`requires`), leaves
85
+ # held (`gives`), and leaves not held (`ends`), each a name
86
+ # ("book-open") or a name and the args key that tells one holder from
87
+ # another (["signed-in", :person]). nuka check walks them through each
88
+ # scenario, and nuka run refuses a scenario that can only reach a line
89
+ # without the state it requires.
90
+ #: (*untyped) -> Array[untyped]
91
+ def requires(*entries) = entries.empty? ? (@requires || []) : (@requires = entries.freeze)
92
+ #: (*untyped) -> Array[untyped]
93
+ def gives(*entries) = entries.empty? ? (@gives || []) : (@gives = entries.freeze)
94
+ #: (*untyped) -> Array[untyped]
95
+ def ends(*entries) = entries.empty? ? (@ends || []) : (@ends = entries.freeze)
96
+
97
+ # The three lists read, or nil while one of them is malformed, which
98
+ # Definition reports.
99
+ #: () -> Engine::States::Declared?
100
+ def states
101
+ read = Engine::States.read(self)
102
+ read.is_a?(Engine::States::Declared) ? read : nil
103
+ end
104
+
40
105
  # The steps this one may run through the `call` fixture. A part with no
41
106
  # pattern exists only for calls.
42
107
  #: (Array[singleton(Step)]) -> Array[singleton(Step)]
@@ -72,7 +137,7 @@ module Nukadoko
72
137
  #: () -> String
73
138
  def step_name = File.basename(file, ".rb").tr("_", "-")
74
139
 
75
- # A step that runs code may change the world unless it says it does
140
+ # A step that runs code may change the system unless it says it does
76
141
  # not; a claim step has no action, so it never does.
77
142
  #: () -> bool
78
143
  def mutating? = claims ? false : mutates != false
@@ -135,11 +200,15 @@ module Nukadoko
135
200
 
136
201
  # Each step file is registered for autoload under its class name first, so a
137
202
  # step can name another in `from` or `field` whatever order the files load in.
203
+ # Every other Ruby file under the features directory is cucumber-ruby glue
204
+ # the compat door reads (lib/nukadoko/compat.rb), loaded before the step
205
+ # files in cucumber-ruby's order, and a step file may register glue too.
138
206
  #: (String) -> Array[singleton(Step)]
139
207
  def self.load_steps(dir)
140
208
  files = Dir.glob(File.join(dir, "**/*.rb")).sort.map { File.expand_path(_1) }
141
209
  files.each { Object.autoload(File.basename(_1, ".rb").split("_").map(&:capitalize).join.to_sym, _1) }
142
- files.each { require _1 }
210
+ glue = Compat.files(File.dirname(dir), dir)
211
+ Compat.load(glue - files + files)
143
212
  Step.registry
144
213
  end
145
214
  end
@@ -39,6 +39,52 @@ module Nukadoko
39
39
  # A pageAs call that asks an open context for options it was not made with.
40
40
  class ContextOptionsConflictError < ArgumentError; end
41
41
 
42
+ # A step read `services[name]` for a name the program does not write:
43
+ # nil there would fail later, at a request to a URL with no host, far
44
+ # from the mistake. The message names the services the system has.
45
+ class UnknownServiceError < KeyError #[untyped, untyped]
46
+ attr_reader :service #: String
47
+
48
+ #: (String, Array[String]) -> void
49
+ def initialize(service, known)
50
+ @service = service
51
+ super(if known.empty? then "services.#{service}: this run has no program, so it has no services"
52
+ else "services.#{service}: the program has no service \"#{service}\" (#{known.size == 1 ? "its one service is" : "its services are"} #{known.join(", ")})"
53
+ end)
54
+ end
55
+ end
56
+
57
+ # The `services` fixture: every service of the system by name, the
58
+ # primary among them, as Engine::Program::Address holds it. A step sends
59
+ # to another service at its absolute `url`, and runs that service's
60
+ # command line with run_command in its `root` with its `env`. The same
61
+ # values in every scenario of the system.
62
+ class Services
63
+ include Enumerable #[[String, untyped]]
64
+
65
+ #: (Hash[String, untyped]) -> void
66
+ def initialize(services)
67
+ @services = services.freeze
68
+ end
69
+
70
+ # The service named `name`, a String or a Symbol; a name the program
71
+ # does not write raises UnknownServiceError.
72
+ #: (String | Symbol) -> untyped
73
+ def [](name) = @services.fetch(name.to_s) { raise UnknownServiceError.new(name.to_s, @services.keys) }
74
+
75
+ #: (String | Symbol) -> bool
76
+ def key?(name) = @services.key?(name.to_s)
77
+
78
+ #: () -> Array[String]
79
+ def names = @services.keys
80
+
81
+ #: () { ([String, untyped]) -> void } -> void
82
+ def each(&) = @services.each(&)
83
+
84
+ #: () -> Hash[String, untyped]
85
+ def to_h = @services.dup
86
+ end
87
+
42
88
  module Engine
43
89
  #: () -> String
44
90
  def self.now_iso = Time.now.utc.iso8601(3)
@@ -148,7 +194,7 @@ module Nukadoko
148
194
  class Evidence
149
195
  # The files and directories the engine writes into the same directory.
150
196
  HARNESS = %w[record.json final.png trace.zip requests.zip http.jsonl mcp.jsonl websocket.jsonl
151
- program-log.txt program-log-late.txt events.jsonl events-late.jsonl events-read.jsonl judged].freeze
197
+ events-read.jsonl judged].freeze
152
198
  PROCESS_FILE = /\Aprocess-[1-9][0-9]*\.((stdout|stderr|stdin)\.txt|json)\z/
153
199
  NAMED_FILE = /\A(?:trace\.[a-z0-9][a-z0-9-]{0,63}\.zip|final\.[a-z0-9][a-z0-9-]{0,63}\.png)\z/
154
200
  MAX_ATTACHMENTS = 100
@@ -202,9 +248,12 @@ module Nukadoko
202
248
  %(and must not be ".", "..", or empty. Every attachment and path is written inside this step's own evidence directory.)
203
249
  end
204
250
  key = name.downcase
205
- if (HARNESS + @reserved).any? { _1.downcase == key } || key.match?(PROCESS_FILE) || key.match?(NAMED_FILE)
251
+ if (HARNESS + @reserved).any? { _1.downcase == key } || key.match?(PROCESS_FILE) || key.match?(NAMED_FILE) ||
252
+ Engine::Trace::Log.file?(key) || Engine::Events.events_file?(key)
206
253
  raise ReservedEvidenceNameError, "evidence name #{name.inspect} is reserved for a file the engine writes itself " \
207
- "(record.json, final.png, trace.zip, http.jsonl, the program's log lines, the trace.<name>.zip and " \
254
+ "(record.json, final.png, trace.zip, http.jsonl, the program's log lines in program-log.<service>.txt and program-log-late.<service>.txt, " \
255
+ "the program's execution events in events.<service>.jsonl and events-late.<service>.jsonl, " \
256
+ "the trace.<name>.zip and " \
208
257
  "final.<name>.png files of a pageAs context, and the process-<n> files of a run_command). " \
209
258
  "A name that differs only by case is reserved too. Choose a different name."
210
259
  end