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,163 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "pathname"
5
+
6
+ # Which scenarios a run executes: the targets on the command line (feature
7
+ # files, `file:line` selections, directories) narrowed by `--tag`. A target
8
+ # that names nothing runnable is refused before anything runs, with the
9
+ # TypeScript engine's code for the same mistake, so a run never quietly
10
+ # does less than its command line asked for.
11
+ module Nukadoko
12
+ module Engine
13
+ class Refusal < StandardError
14
+ attr_reader :code #: String
15
+ attr_reader :severity #: String
16
+
17
+ # `fix` and `run` are this refusal's own way past it; without either,
18
+ # the code's registry fix and the command's --help stand in.
19
+ #: (String, String, ?severity: String, ?fix: String?, ?run: String?) -> void
20
+ def initialize(code, message, severity: "refused", fix: nil, run: nil)
21
+ super(message)
22
+ @code = code
23
+ @severity = severity
24
+ @fix = fix
25
+ @run = run
26
+ end
27
+
28
+ # A refusal stops before anything runs (exit 2); a selection that kept
29
+ # no scenario ran and found nothing to do (exit 1).
30
+ #: () -> Integer
31
+ def exit_code = severity == "refused" ? 2 : 1
32
+
33
+ #: (?String) -> Hash[Symbol, untyped]
34
+ def to_h(command = "run")
35
+ own = @fix || @run ? { fix: @fix || Codes.fix(code), do: @run }.compact : { fix: Codes.fix(code), do: "nuka #{command} --help" }
36
+ { severity: severity, code: code, message: message, **own }
37
+ end
38
+ end
39
+
40
+ class Selection
41
+ TAG = /\A(not )?(@\S+)\z/
42
+
43
+ # `whole_files` are the files a target named whole and --tag left
44
+ # whole, which a rerun line may name as a file.
45
+ attr_reader :scenarios #: Array[Scenario]
46
+ attr_reader :selection #: String
47
+ attr_reader :tag_selection #: Hash[Symbol, untyped]?
48
+ attr_reader :whole_files #: Array[String]
49
+ attr_reader :problems #: Array[FeatureProblem]
50
+
51
+ #: (String, Array[String], Array[String], ?features_dir: String) -> void
52
+ def initialize(project, targets, tags, features_dir: "features")
53
+ @project = project
54
+ @tags = tags.map { parse_tag(_1) }
55
+ lines = Hash.new { |h, k| h[k] = [] }
56
+ whole = []
57
+ named = targets.empty? ? [features_dir] : targets
58
+ named.each { add(_1, whole, lines) }
59
+ line_target = named.find { _1.match?(/:\d+\z/) }
60
+ if @tags.any? && line_target
61
+ raise Refusal.new("invalid-value", "--tag selects among whole files, and #{JSON.generate(line_target)} already names one scenario",
62
+ fix: "Drop --tag to run that scenario, or drop the :line to select by tag in its file.")
63
+ end
64
+
65
+ files = (whole + lines.keys).uniq.sort
66
+ @problems = []
67
+ all = Dir.chdir(@project) { Engine.load_features(files, problems: @problems) }
68
+ by_line = all.select { whole.include?(_1.uri) || lines[_1.uri].include?(_1.line) }
69
+ lines.each do |file, wanted|
70
+ wanted.each do |line|
71
+ next if by_line.any? { _1.uri == file && _1.line == line }
72
+
73
+ starts = all.select { _1.uri == file }.map(&:line).uniq.sort
74
+ fix = starts.empty? ? "Add a scenario to #{file}; nothing in it can run yet." : "Name a line a scenario or an Examples row starts on: #{starts.join(", ")}."
75
+ raise Refusal.new("no-scenario-at-line", "No scenario found at #{file}:#{line}", fix: fix)
76
+ end
77
+ end
78
+ @scenarios = by_line.select { |s| @tags.all? { |negated, tag| s.tags.include?(tag) != negated } }
79
+ left_out = by_line.size - @scenarios.size
80
+ @whole_files = whole.select { |file| by_line.select { _1.uri == file }.all? { @scenarios.include?(_1) } }
81
+ @tag_selection = { expressions: tags, left_out: left_out } if tags.any?
82
+ @selection = all.size == @scenarios.size ? "full" : "partial"
83
+ return unless @scenarios.empty? && @tags.any?
84
+
85
+ flags = tags.flat_map { ["--tag", Selection.shell_word(_1)] }.join(" ")
86
+ which = by_line.size == 1 ? "the 1 scenario" : "all #{by_line.size} scenarios"
87
+ raise Refusal.new("nothing-selected", "no scenario to run: #{flags} left out #{which} of #{named.join(", ")}, so nothing was checked",
88
+ severity: "failed", fix: "Name a tag a scenario of the targets carries, or run without --tag.")
89
+ end
90
+
91
+ SHELL_SAFE = %r{\A[A-Za-z0-9_./:@%+,-][A-Za-z0-9_./:=@%+,-]*\z}
92
+
93
+ # A word as a POSIX shell reads it back: as it is when it holds no
94
+ # character the shell treats apart, otherwise in single quotes.
95
+ #: (String) -> String
96
+ def self.shell_word(word) = SHELL_SAFE.match?(word) ? word : "'#{word.gsub("'") { %q('\'') }}'"
97
+
98
+ private
99
+
100
+ #: (String) -> [bool, String]
101
+ def parse_tag(expression)
102
+ match = TAG.match(expression)
103
+ unless match
104
+ raise Refusal.new("invalid-value", "--tag #{JSON.generate(expression)} is not an expression nuka run reads: " \
105
+ "give \"@name\" or \"not @name\", and repeat --tag for a scenario that has to meet more than one")
106
+ end
107
+
108
+ [!match[1].nil?, match[2]]
109
+ end
110
+
111
+ #: (String, Array[String], Hash[String, Array[Integer]]) -> void
112
+ def add(target, whole, lines)
113
+ parsed = target.match(/\A(.*?)(?::(\d+))?\z/) #: MatchData
114
+ path = parsed[1].to_s
115
+ line = parsed[2]
116
+ absolute = File.expand_path(path, @project)
117
+ if File.directory?(absolute)
118
+ if line
119
+ raise Refusal.new("line-on-directory", %(":line" has no meaning for a directory: #{target} names a directory, not a feature file),
120
+ fix: "Remove the :line suffix from the directory, or name a feature file.", run: "nuka run #{Selection.shell_word(path)}")
121
+ end
122
+
123
+ found = walk(absolute)
124
+ if found.empty?
125
+ raise Refusal.new("no-feature-files", %(no .feature file was found while scanning "#{path}"; nothing can run), fix: "Add a .feature file under #{path}.")
126
+ end
127
+
128
+ found.each { whole << relative(_1) unless whole.include?(relative(_1)) }
129
+ elsif path.end_with?(".md")
130
+ raise Refusal.new("markdown-target", "#{path} is a Markdown file: Markdown oaths were removed, and nukadoko reads scenarios from .feature files only",
131
+ fix: "Write the scenarios of #{path} as Gherkin in a .feature file, and name that file.")
132
+ elsif !File.file?(absolute)
133
+ raise Refusal.new("target-not-found", "Feature file not found: #{path}")
134
+ elsif line
135
+ lines[relative(absolute)] << Integer(line, 10)
136
+ else
137
+ whole << relative(absolute) unless whole.include?(relative(absolute))
138
+ end
139
+ end
140
+
141
+ # Every .feature file below dir, skipping node_modules and any directory
142
+ # whose name starts with a dot, as step discovery skips them.
143
+ #: (String) -> Array[String]
144
+ def walk(dir)
145
+ Dir.children(dir).sort.flat_map do |name|
146
+ full = File.join(dir, name)
147
+ if File.directory?(full)
148
+ name.start_with?(".") || name == "node_modules" ? [] : walk(full)
149
+ else
150
+ name.end_with?(".feature") ? [full] : []
151
+ end
152
+ end
153
+ end
154
+
155
+ # Two spellings of one file are one file: a target is named by its real
156
+ # path relative to the project, so it is selected once.
157
+ #: (String) -> String
158
+ def relative(absolute)
159
+ Pathname.new(File.realpath(absolute)).relative_path_from(Pathname.new(File.realpath(@project))).to_s
160
+ end
161
+ end
162
+ end
163
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rbs_inline: enabled
2
3
 
3
4
  # The RBS of each step, written from the same declarations check reads, so a
4
5
  # project that runs Steep gets the body checked against its own contract:
@@ -8,13 +9,15 @@ module Nukadoko
8
9
  module Signature
9
10
  FIXTURE_TYPES = { request: "Nukadoko::Engine::Request", scratch: "Nukadoko::Engine::Scratch", call: "Nukadoko::Engine::Caller", baseURL: "String", page: "untyped", context: "untyped" }.freeze
10
11
 
12
+ #: (Array[singleton(Nukadoko::Step)]) -> String
11
13
  def self.of(steps)
12
14
  steps.map { of_step(_1) }.join("\n")
13
15
  end
14
16
 
17
+ #: (singleton(Nukadoko::Step)) -> String
15
18
  def self.of_step(step)
16
19
  params = step.fixtures.map { "#{_1}: #{FIXTURE_TYPES.fetch(_1, "untyped")}" } + ["args: Args"]
17
- returns = step.returns ? step.returns.rbs_record : "Hash[Symbol, untyped]"
20
+ returns = (schema = step.returns) ? schema.rbs_record : "Hash[Symbol, untyped]"
18
21
  [
19
22
  "class #{step.name} < Nukadoko::Step",
20
23
  " class Args",
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # `nuka skill path`: the directory that holds every agent skill this gem
5
+ # ships, one absolute path, as the TypeScript engine prints its own. It is
6
+ # a fact about the installed nukadoko, the one route sure to match the
7
+ # version installed; copying a skill into an agent's own layout is what
8
+ # `gh skill install` and the agent's plugin marketplace already do.
9
+ #
10
+ # The gem ships the skills beside lib/ (bin/stage-shared copies them in
11
+ # before `gem build`). A checkout that has not staged them reads the
12
+ # repository root's, which are the same files.
13
+ module Nukadoko
14
+ module Skill
15
+ module_function
16
+
17
+ #: () -> String
18
+ def dir
19
+ here = __dir__.to_s
20
+ staged = File.expand_path("../../skills", here)
21
+ File.directory?(staged) ? staged : File.expand_path("../../../skills", here)
22
+ end
23
+
24
+ #: (IO, IO) -> Integer
25
+ def print_path(out, err)
26
+ found = dir
27
+ unless File.directory?(found)
28
+ err.puts "nuka skill: skill source not found at #{found} (a packaging bug, not a project issue)"
29
+ return 1
30
+ end
31
+
32
+ out.puts found
33
+ 0
34
+ end
35
+ end
36
+ end
data/lib/nukadoko/step.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rbs_inline: enabled
2
3
 
3
4
  # A step is a class under features/steps/, one per file, named by its file.
4
5
  # Everything except the body is a class-level declaration, so check reads the
@@ -7,32 +8,53 @@
7
8
  module Nukadoko
8
9
  class Step
9
10
  class << self
10
- attr_reader :file
11
+ #: () -> String
12
+ def file = @file
11
13
 
14
+ #: (singleton(Step)) -> void
12
15
  def inherited(subclass)
13
16
  super
14
- subclass.instance_variable_set(:@file, caller_locations(1, 1).first.absolute_path)
17
+ subclass.instance_variable_set(:@file, caller_locations(1, 1)&.first&.absolute_path)
15
18
  Step.registry << subclass
16
19
  end
17
20
 
21
+ #: () -> Array[singleton(Step)]
18
22
  def registry = (@registry ||= [])
19
23
 
24
+ #: (String) -> String
25
+ #: () -> String?
20
26
  def pattern(text = nil) = text ? (@pattern = text) : @pattern
27
+ #: (String) -> String
28
+ #: () -> String?
21
29
  def description(text = nil) = text ? (@description = text) : @description
30
+ #: (bool) -> bool
31
+ #: () -> bool?
22
32
  def mutates(value = nil) = value.nil? ? @mutates : (@mutates = value)
33
+ #: (Array[Hash[Symbol, untyped]]) -> Array[Hash[Symbol, untyped]]
34
+ #: () -> Array[Hash[Symbol, untyped]]?
23
35
  def claims(list = nil) = list ? (@claims = list) : @claims
36
+ #: (untyped) -> untyped
37
+ #: () -> untyped
24
38
  def poll(value = nil) = value.nil? ? @poll : (@poll = value)
25
39
 
26
40
  # The steps this one may run through the `call` fixture. A part with no
27
41
  # pattern exists only for calls.
42
+ #: (Array[singleton(Step)]) -> Array[singleton(Step)]
43
+ #: () -> Array[singleton(Step)]
28
44
  def parts(list = nil) = list ? (@parts = list.freeze) : (@parts || [])
29
45
 
46
+ #: () { () [self: Schema] -> void } -> Schema
47
+ #: () -> Schema
30
48
  def args(&block) = block ? (@args = Schema.build(&block)) : (@args ||= Schema.new)
49
+ #: () { () [self: Schema] -> void } -> Schema
50
+ #: () -> Schema?
31
51
  def returns(&block) = block ? (@returns = Schema.build(&block)) : @returns
32
52
 
33
53
  # Args an earlier step of the scenario returned, by key: one source as
34
54
  # `[Step, :field]`, or several as a list of them, where the nearest
35
55
  # earlier one that ran gives the value.
56
+ #: (Hash[Symbol | String, untyped]) -> Hash[String, Array[untyped]]
57
+ #: () -> Hash[String, Array[untyped]]
36
58
  def from(map = nil)
37
59
  return @from || {} unless map
38
60
 
@@ -41,54 +63,73 @@ module Nukadoko
41
63
 
42
64
  # observe never sees the args a claim compares with, so it cannot pick
43
65
  # what to read by the answer it will be judged against.
44
- def inputs = claims ? args.without(expected_keys(claims)) : args
66
+ #: () -> Schema
67
+ def inputs
68
+ claims = self.claims
69
+ claims ? args.without(Engine::ClaimRules.withheld_keys(claims, from.keys).keys) : args
70
+ end
45
71
 
72
+ #: () -> String
46
73
  def step_name = File.basename(file, ".rb").tr("_", "-")
47
74
 
48
75
  # A step that runs code may change the world unless it says it does
49
76
  # not; a claim step has no action, so it never does.
77
+ #: () -> bool
50
78
  def mutating? = claims ? false : mutates != false
79
+ #: () -> Symbol
51
80
  def body = claims ? :observe : :run
81
+ #: () -> Integer?
52
82
  def line = method_defined?(body) ? instance_method(body).source_location&.last : nil
53
83
 
84
+ #: () -> Array[Symbol]
54
85
  def fixtures
55
86
  return [] unless method_defined?(body)
56
87
 
57
88
  instance_method(body).parameters.filter_map { |kind, name| name if kind == :keyreq && name != :args }
58
89
  end
59
90
 
91
+ # What `run` gave back, checked against `returns`, as problems to
92
+ # report. A Ruby method always gives back its last value, so a step
93
+ # with no returns that ends on a call (an Integer, a String, nil)
94
+ # returns nothing; a Hash with keys would lose them from the record,
95
+ # so that is refused, as the TypeScript engine refuses it.
96
+ #: (untyped) -> [untyped, Array[String]]
97
+ def returned(value)
98
+ schema = returns
99
+ return [value, schema.problems(value)] if schema
100
+ return [{}, []] unless value.is_a?(Hash)
101
+ return [value, []] if value.empty?
102
+
103
+ one = value.size == 1
104
+ [value, ["this step declares no returns, so #{value.keys.join(", ")} would be dropped from the step record; declare returns with #{one ? "that key" : "those keys"}, or stop returning #{one ? "it" : "them"}"]]
105
+ end
106
+
107
+ # The keywords the body is called with: `args` only when it names
108
+ # them, as a TypeScript step that does not destructure `args` never
109
+ # sees them, so `def run(scratch:)` is a whole step.
110
+ #: (Hash[Symbol, untyped], untyped) -> Hash[Symbol, untyped]
111
+ def keywords(fixtures, args)
112
+ params = method_defined?(body) ? instance_method(body).parameters : []
113
+ takes = params.any? { |kind, name| kind == :keyrest || (name == :args && %i[key keyreq].include?(kind)) }
114
+ takes ? fixtures.merge(args: args) : fixtures
115
+ end
116
+
60
117
  # The fixtures the engine builds before the body runs: the step's own,
61
118
  # and those of every part it can reach, since the bag is closed before
62
119
  # any call happens.
120
+ #: (?Array[singleton(Step)]) -> Array[Symbol]
63
121
  def bag(seen = [])
64
122
  return [] if seen.include?(self)
65
123
 
66
124
  reachable = parts.select { _1.is_a?(Class) && _1 < Step }.flat_map { _1.bag([*seen, self]) }
67
125
  (fixtures + reachable - (seen.empty? ? [] : [:call])).uniq
68
126
  end
69
-
70
- private
71
-
72
- def expected_keys(node)
73
- case node
74
- in Array then node.flat_map { expected_keys(_1) }
75
- in Hash then node.flat_map { |k, v| k.to_s == "expected" ? expected_key(v) : expected_keys(v) }
76
- else []
77
- end
78
- end
79
-
80
- def expected_key(value)
81
- case value
82
- in String then [value]
83
- in { from: String => key } then [key]
84
- else []
85
- end
86
- end
87
127
  end
88
128
  end
89
129
 
90
130
  # Each step file is registered for autoload under its class name first, so a
91
131
  # step can name another in `from` or `field` whatever order the files load in.
132
+ #: (String) -> Array[singleton(Step)]
92
133
  def self.load_steps(dir)
93
134
  files = Dir.glob(File.join(dir, "**/*.rb")).sort.map { File.expand_path(_1) }
94
135
  files.each { Object.autoload(File.basename(_1, ".rb").split("_").map(&:capitalize).join.to_sym, _1) }