nukadoko 0.1.0 → 0.3.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +417 -0
- data/README.ja.md +62 -7
- data/README.md +19 -5
- data/exe/nuka +8 -0
- data/lib/nukadoko/aria_snapshot.rb +1008 -0
- data/lib/nukadoko/aria_tree.rb +150 -0
- data/lib/nukadoko/base.rb +294 -0
- data/lib/nukadoko/check.rb +669 -22
- data/lib/nukadoko/claim_check.rb +177 -0
- data/lib/nukadoko/claim_check_run.rb +293 -0
- data/lib/nukadoko/claim_line.rb +381 -0
- data/lib/nukadoko/claim_rules.rb +519 -0
- data/lib/nukadoko/claim_sides.rb +469 -0
- data/lib/nukadoko/claims.rb +576 -37
- data/lib/nukadoko/cli.rb +814 -22
- data/lib/nukadoko/codes.json +947 -0
- data/lib/nukadoko/codes.rb +19 -0
- data/lib/nukadoko/command.rb +214 -12
- data/lib/nukadoko/criteria.rb +172 -0
- data/lib/nukadoko/definition.rb +33 -41
- data/lib/nukadoko/env_check.rb +88 -0
- data/lib/nukadoko/features.rb +80 -10
- data/lib/nukadoko/fixtures.rb +329 -11
- data/lib/nukadoko/http_recording.rb +68 -0
- data/lib/nukadoko/init.rb +129 -0
- data/lib/nukadoko/js_json.rb +274 -0
- data/lib/nukadoko/js_pattern.rb +531 -0
- data/lib/nukadoko/judge.rb +221 -0
- data/lib/nukadoko/judgment_file.rb +78 -0
- data/lib/nukadoko/judgments.rb +256 -0
- data/lib/nukadoko/literal.rb +129 -0
- data/lib/nukadoko/measured.rb +105 -0
- data/lib/nukadoko/order.rb +84 -0
- data/lib/nukadoko/parts.rb +43 -22
- data/lib/nukadoko/pattern.rb +73 -3
- data/lib/nukadoko/poll.rb +21 -8
- data/lib/nukadoko/program.rb +105 -17
- data/lib/nukadoko/project_files.rb +17 -0
- data/lib/nukadoko/project_fixtures.rb +17 -2
- data/lib/nukadoko/project_program.rb +119 -0
- data/lib/nukadoko/report_allure.rb +751 -0
- data/lib/nukadoko/report_messages.rb +167 -0
- data/lib/nukadoko/report_run.rb +169 -0
- data/lib/nukadoko/run.rb +898 -91
- data/lib/nukadoko/run_record.rb +535 -0
- data/lib/nukadoko/scaffold.rb +1015 -0
- data/lib/nukadoko/schema.rb +120 -21
- data/lib/nukadoko/secrets.rb +375 -0
- data/lib/nukadoko/selection.rb +163 -0
- data/lib/nukadoko/signature.rb +4 -1
- data/lib/nukadoko/skill.rb +36 -0
- data/lib/nukadoko/spaces.rb +28 -0
- data/lib/nukadoko/step.rb +68 -21
- data/lib/nukadoko/summary.rb +662 -0
- data/lib/nukadoko/table.rb +34 -2
- data/lib/nukadoko/text_fit.rb +179 -0
- data/lib/nukadoko/trace.rb +244 -0
- data/lib/nukadoko/trace_actions.rb +116 -0
- data/lib/nukadoko/tree_passes.rb +122 -0
- data/lib/nukadoko/verdict.rb +128 -0
- data/lib/nukadoko/version.rb +2 -1
- data/lib/nukadoko/vocabulary.rb +6 -0
- data/lib/nukadoko/walk.rb +305 -0
- data/lib/nukadoko/walk_files.rb +473 -0
- data/lib/nukadoko/walk_step.rb +149 -0
- data/lib/nukadoko.rb +60 -2
- data/llms.txt +297 -0
- data/sig/generated/nukadoko/aria_snapshot.rbs +331 -0
- data/sig/generated/nukadoko/aria_tree.rbs +142 -0
- data/sig/generated/nukadoko/base.rbs +142 -0
- data/sig/generated/nukadoko/check.rbs +255 -0
- data/sig/generated/nukadoko/claim_check.rbs +118 -0
- data/sig/generated/nukadoko/claim_check_run.rbs +88 -0
- data/sig/generated/nukadoko/claim_line.rbs +158 -0
- data/sig/generated/nukadoko/claim_rules.rbs +172 -0
- data/sig/generated/nukadoko/claim_sides.rbs +207 -0
- data/sig/generated/nukadoko/claims.rbs +208 -0
- data/sig/generated/nukadoko/cli.rbs +182 -0
- data/sig/generated/nukadoko/codes.rbs +16 -0
- data/sig/generated/nukadoko/command.rbs +109 -0
- data/sig/generated/nukadoko/criteria.rbs +83 -0
- data/sig/generated/nukadoko/definition.rbs +30 -0
- data/sig/generated/nukadoko/env_check.rbs +34 -0
- data/sig/generated/nukadoko/features.rbs +101 -0
- data/sig/generated/nukadoko/fixtures.rbs +175 -0
- data/sig/generated/nukadoko/http_recording.rbs +29 -0
- data/sig/generated/nukadoko/init.rbs +64 -0
- data/sig/generated/nukadoko/js_json.rbs +109 -0
- data/sig/generated/nukadoko/js_pattern.rbs +194 -0
- data/sig/generated/nukadoko/judge.rbs +64 -0
- data/sig/generated/nukadoko/judgment_file.rbs +39 -0
- data/sig/generated/nukadoko/judgments.rbs +120 -0
- data/sig/generated/nukadoko/literal.rbs +71 -0
- data/sig/generated/nukadoko/measured.rbs +42 -0
- data/sig/generated/nukadoko/order.rbs +54 -0
- data/sig/generated/nukadoko/parts.rbs +35 -0
- data/sig/generated/nukadoko/pattern.rbs +62 -0
- data/sig/generated/nukadoko/poll.rbs +41 -0
- data/sig/generated/nukadoko/program.rbs +101 -0
- data/sig/generated/nukadoko/project_files.rbs +19 -0
- data/sig/generated/nukadoko/project_fixtures.rbs +57 -0
- data/sig/generated/nukadoko/project_program.rbs +55 -0
- data/sig/generated/nukadoko/report_allure.rbs +268 -0
- data/sig/generated/nukadoko/report_messages.rbs +68 -0
- data/sig/generated/nukadoko/report_run.rbs +102 -0
- data/sig/generated/nukadoko/run.rbs +264 -0
- data/sig/generated/nukadoko/run_record.rbs +175 -0
- data/sig/generated/nukadoko/scaffold.rbs +328 -0
- data/sig/generated/nukadoko/schema.rbs +155 -0
- data/sig/generated/nukadoko/secrets.rbs +172 -0
- data/sig/generated/nukadoko/selection.rbs +73 -0
- data/sig/generated/nukadoko/signature.rbs +17 -0
- data/sig/generated/nukadoko/skill.rbs +20 -0
- data/sig/generated/nukadoko/spaces.rbs +31 -0
- data/sig/generated/nukadoko/step.rbs +115 -0
- data/sig/generated/nukadoko/summary.rbs +204 -0
- data/sig/generated/nukadoko/table.rbs +43 -0
- data/sig/generated/nukadoko/text_fit.rbs +70 -0
- data/sig/generated/nukadoko/trace.rbs +127 -0
- data/sig/generated/nukadoko/trace_actions.rbs +40 -0
- data/sig/generated/nukadoko/tree_passes.rbs +48 -0
- data/sig/generated/nukadoko/verdict.rbs +56 -0
- data/sig/generated/nukadoko/version.rbs +5 -0
- data/sig/generated/nukadoko/vocabulary.rbs +25 -0
- data/sig/generated/nukadoko/walk.rbs +139 -0
- data/sig/generated/nukadoko/walk_files.rbs +189 -0
- data/sig/generated/nukadoko/walk_step.rbs +50 -0
- data/sig/generated/nukadoko.rbs +26 -0
- data/sig/nukadoko.rbs +5 -48
- data/skills/nukadoko/SKILL.md +120 -0
- data/skills/nukadoko/references/checking-steps.md +200 -0
- data/skills/nukadoko/references/diagnosing.md +200 -0
- data/skills/nukadoko/references/done.md +99 -0
- data/skills/nukadoko/references/evidence.md +124 -0
- data/skills/nukadoko/references/examples/cli.md +230 -0
- data/skills/nukadoko/references/examples/http-api.md +342 -0
- data/skills/nukadoko/references/examples/workflow.md +173 -0
- data/skills/nukadoko/references/exploring.md +184 -0
- data/skills/nukadoko/references/fixtures.md +198 -0
- data/skills/nukadoko/references/from-prose.md +51 -0
- data/skills/nukadoko/references/judgments.md +95 -0
- data/skills/nukadoko/references/maintenance.md +97 -0
- data/skills/nukadoko/references/merge-risk.md +74 -0
- data/skills/nukadoko/references/parts.md +152 -0
- data/skills/nukadoko/references/perturbed-worlds.md +109 -0
- data/skills/nukadoko/references/program.md +200 -0
- data/skills/nukadoko/references/reviewing.md +200 -0
- data/skills/nukadoko/references/running.md +200 -0
- data/skills/nukadoko/references/scenario-rules.md +188 -0
- data/skills/nukadoko/references/states.md +66 -0
- data/skills/nukadoko/references/step-files.md +200 -0
- data/skills/nukadoko/references/verdicts.md +200 -0
- data/skills/nukadoko/references/writing-steps.md +200 -0
- data/skills/nukadoko/references/writing-the-feature.md +165 -0
- data/skills/nukadoko/scripts/claude-code-verdict-hook.mjs +99 -0
- data/skills/nukadoko/scripts/merge-risk.jq +56 -0
- data/skills/nukadoko-verification/SKILL.md +263 -0
- metadata +133 -1
data/lib/nukadoko/features.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
2
3
|
|
|
3
4
|
require "gherkin"
|
|
4
5
|
|
|
@@ -8,18 +9,38 @@ require "gherkin"
|
|
|
8
9
|
module Nukadoko
|
|
9
10
|
module Engine
|
|
10
11
|
# `keyword_line` is the Scenario keyword's own line, the same for every
|
|
11
|
-
# row of an outline
|
|
12
|
-
|
|
12
|
+
# row of an outline, and `outline_name` the name as written, before a
|
|
13
|
+
# row fills it. A step from a Background has `background` set.
|
|
14
|
+
Scenario = Data.define(:uri, :line, :keyword_line, :name, :tags, :steps, :tag_lines, :columns, :outline_name, :serial)
|
|
13
15
|
PickleStep = Data.define(:text, :line, :kind, :table, :doc_string, :background)
|
|
16
|
+
# What makes a file, or a part of one, run nothing: a file that does
|
|
17
|
+
# not parse, or an outline with no row to run. `line` is nil for a file.
|
|
18
|
+
FeatureProblem = Data.define(:code, :uri, :line, :message)
|
|
19
|
+
|
|
20
|
+
# A directory is every .feature file under it; a list is those files.
|
|
21
|
+
# What runs nothing goes into `problems`, for nuka check to report.
|
|
22
|
+
#: (String | Array[String], ?problems: Array[FeatureProblem]) -> Array[Scenario]
|
|
23
|
+
def self.load_features(dir_or_paths, problems: [])
|
|
24
|
+
paths = dir_or_paths.is_a?(Array) ? dir_or_paths : Dir.glob(File.join(dir_or_paths, "**/*.feature")).sort
|
|
25
|
+
return [] if paths.empty?
|
|
14
26
|
|
|
15
|
-
def self.load_features(dir)
|
|
16
|
-
paths = Dir.glob(File.join(dir, "**/*.feature")).sort
|
|
17
27
|
lines = {}
|
|
18
28
|
background = {}
|
|
29
|
+
headers = {}
|
|
30
|
+
names = {}
|
|
31
|
+
serial = {}
|
|
19
32
|
scenarios = []
|
|
33
|
+
parse_errors = Hash.new { |h, k| h[k] = [] }
|
|
20
34
|
Gherkin.from_paths(paths, include_source: false, include_gherkin_document: true, include_pickles: true).each do |envelope|
|
|
21
|
-
if (
|
|
22
|
-
|
|
35
|
+
if (error = envelope.parse_error)
|
|
36
|
+
parse_errors[error.source.uri] << error.message
|
|
37
|
+
elsif (doc = envelope.gherkin_document)
|
|
38
|
+
if doc.feature
|
|
39
|
+
collect_lines(doc.feature, lines, background, headers, names)
|
|
40
|
+
serial[doc.uri] = doc.feature.tags.any? { _1.name == SERIAL_TAG }
|
|
41
|
+
problems.concat(serial_tags_on_scenarios(doc))
|
|
42
|
+
problems.concat(outlines_that_do_not_run(doc))
|
|
43
|
+
end
|
|
23
44
|
elsif (pickle = envelope.pickle)
|
|
24
45
|
steps = pickle.steps.map do |step|
|
|
25
46
|
table = step.argument&.data_table&.rows&.map { |row| row.cells.map(&:value) }
|
|
@@ -27,23 +48,72 @@ module Nukadoko
|
|
|
27
48
|
background.key?(step.ast_node_ids.first))
|
|
28
49
|
end
|
|
29
50
|
scenarios << Scenario.new(pickle.uri, lines.fetch(pickle.ast_node_ids.last), lines.fetch(pickle.ast_node_ids.first),
|
|
30
|
-
pickle.name, pickle.tags.map(&:name), steps
|
|
51
|
+
pickle.name, pickle.tags.map(&:name), steps,
|
|
52
|
+
pickle.tags.to_h { [_1.name, lines[_1.ast_node_id]] }, headers.fetch(pickle.ast_node_ids.last, []),
|
|
53
|
+
names.fetch(pickle.ast_node_ids.first, pickle.name), serial.fetch(pickle.uri, false))
|
|
31
54
|
end
|
|
32
55
|
end
|
|
56
|
+
# One finding per file, every error in the parser's words and order.
|
|
57
|
+
parse_errors.each { |uri, messages| problems << FeatureProblem.new("feature-parse-error", uri, nil, "Parser errors:\n#{messages.join("\n")}") }
|
|
33
58
|
scenarios
|
|
34
59
|
end
|
|
35
60
|
|
|
36
|
-
|
|
61
|
+
# The tag on a Feature line that runs the file while no other file runs.
|
|
62
|
+
SERIAL_TAG = "@nukadoko:serial"
|
|
63
|
+
|
|
64
|
+
# nuka run --concurrency reads the serial tag on the Feature line only,
|
|
65
|
+
# so on a scenario it would read as a rule in force while doing nothing.
|
|
66
|
+
#: (untyped) -> Array[FeatureProblem]
|
|
67
|
+
def self.serial_tags_on_scenarios(doc)
|
|
68
|
+
scenarios = doc.feature.children.flat_map { |child| child.rule ? child.rule.children.filter_map(&:scenario) : [child.scenario].compact }
|
|
69
|
+
scenarios.select { |scenario| scenario.tags.any? { _1.name == SERIAL_TAG } }.map do |scenario|
|
|
70
|
+
FeatureProblem.new("serial-tag-on-scenario", doc.uri, scenario.location.line,
|
|
71
|
+
%(@nukadoko:serial on scenario "#{scenario.name}" has no effect: nuka run --concurrency reads @nukadoko:serial only from the Feature line. Move the tag there.))
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# An outline with no Examples runs its placeholder text as written, and
|
|
76
|
+
# an Examples table with no rows leaves the outline out of the run.
|
|
77
|
+
#: (untyped) -> Array[FeatureProblem]
|
|
78
|
+
def self.outlines_that_do_not_run(doc)
|
|
79
|
+
outline = Gherkin::Dialect.for(doc.feature.language).scenario_outline_keywords
|
|
80
|
+
scenarios = doc.feature.children.flat_map { |child| child.rule ? child.rule.children.filter_map(&:scenario) : [child.scenario].compact }
|
|
81
|
+
scenarios.flat_map do |scenario|
|
|
82
|
+
if scenario.examples.empty?
|
|
83
|
+
next [] unless outline.include?(scenario.keyword)
|
|
84
|
+
|
|
85
|
+
next [FeatureProblem.new("outline-does-not-run", doc.uri, scenario.location.line,
|
|
86
|
+
%(Scenario outline "#{scenario.name}" has no Examples table, so the placeholder text runs as written. Add one Examples row for each boundary that must run.))]
|
|
87
|
+
end
|
|
88
|
+
scenario.examples.select { _1.table_body.empty? }.map do |block|
|
|
89
|
+
FeatureProblem.new("outline-does-not-run", doc.uri, block.location.line,
|
|
90
|
+
%(Scenario "#{scenario.name}" has an Examples table with no rows, so the scenario is left out of the run. Add a row, or remove the table.))
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
#: (untyped, Hash[String, Integer], Hash[String, bool], Hash[String, Array[String]], Hash[String, String]) -> void
|
|
96
|
+
def self.collect_lines(node, lines, background, headers, names)
|
|
97
|
+
node.tags&.each { lines[_1.id] = _1.location.line }
|
|
37
98
|
node.children.each do |child|
|
|
38
|
-
next collect_lines(child.rule, lines, background) if child.rule
|
|
99
|
+
next collect_lines(child.rule, lines, background, headers, names) if child.rule
|
|
39
100
|
|
|
40
101
|
scenario = child.scenario || child.background
|
|
41
102
|
next unless scenario
|
|
42
103
|
|
|
104
|
+
names[scenario.id] = scenario.name if child.scenario
|
|
43
105
|
lines[scenario.id] = scenario.location.line if child.scenario
|
|
44
106
|
scenario.steps.each { |step| lines[step.id] = step.location.line }
|
|
45
107
|
child.background&.steps&.each { |step| background[step.id] = true }
|
|
46
|
-
child.scenario&.
|
|
108
|
+
child.scenario&.tags&.each { lines[_1.id] = _1.location.line }
|
|
109
|
+
child.scenario&.examples&.each do |examples|
|
|
110
|
+
examples.tags.each { lines[_1.id] = _1.location.line }
|
|
111
|
+
columns = examples.table_header&.cells&.map(&:value) || []
|
|
112
|
+
examples.table_body.each do |row|
|
|
113
|
+
lines[row.id] = row.location.line
|
|
114
|
+
headers[row.id] = columns
|
|
115
|
+
end
|
|
116
|
+
end
|
|
47
117
|
end
|
|
48
118
|
end
|
|
49
119
|
end
|
data/lib/nukadoko/fixtures.rb
CHANGED
|
@@ -1,26 +1,58 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
2
3
|
|
|
3
4
|
require "net/http"
|
|
4
5
|
require "json"
|
|
6
|
+
require "fileutils"
|
|
7
|
+
require "time"
|
|
8
|
+
require "timeout"
|
|
5
9
|
|
|
6
10
|
# Fixtures are built by the engine, not the step, so what they record is
|
|
7
11
|
# measured: every request a step sends goes through Request and lands in the
|
|
8
12
|
# step's http.jsonl as it was sent and answered.
|
|
9
13
|
module Nukadoko
|
|
10
14
|
module Engine
|
|
11
|
-
Response = Data.define(
|
|
15
|
+
Response = Data.define(
|
|
16
|
+
:status, #: Integer
|
|
17
|
+
:headers, #: Hash[String, Array[String]]
|
|
18
|
+
:body, #: String
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
class Response
|
|
22
|
+
#: () -> untyped
|
|
12
23
|
def json = JSON.parse(body)
|
|
24
|
+
#: () -> bool
|
|
13
25
|
def ok? = status.between?(200, 299)
|
|
14
26
|
end
|
|
15
27
|
|
|
16
28
|
class Request
|
|
17
|
-
attr_reader :log
|
|
29
|
+
attr_reader :log #: Array[Hash[Symbol, untyped]]
|
|
18
30
|
|
|
31
|
+
#: (String) -> void
|
|
19
32
|
def initialize(base_url)
|
|
20
33
|
@base = URI(base_url)
|
|
21
34
|
@log = []
|
|
22
35
|
end
|
|
23
36
|
|
|
37
|
+
BODY_TEXT = 16 * 1024
|
|
38
|
+
|
|
39
|
+
#: (Float) -> Integer
|
|
40
|
+
def self.since(started) = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000).round
|
|
41
|
+
|
|
42
|
+
# A body as http.jsonl keeps it: its text, at most 16 KiB, and its
|
|
43
|
+
# size; none when there was none.
|
|
44
|
+
#: (Symbol, String?) -> Hash[Symbol, untyped]
|
|
45
|
+
def self.body(name, bytes)
|
|
46
|
+
return {} if bytes.nil? || bytes.empty?
|
|
47
|
+
|
|
48
|
+
raw = bytes.b
|
|
49
|
+
text = raw.byteslice(0, BODY_TEXT).to_s.force_encoding("UTF-8")
|
|
50
|
+
text = text.scrub("") if raw.bytesize > BODY_TEXT
|
|
51
|
+
return { name => { bytes: raw.bytesize, binary: true } } unless text.valid_encoding?
|
|
52
|
+
|
|
53
|
+
{ name => { text: text, bytes: raw.bytesize, **(raw.bytesize > BODY_TEXT ? { truncated: true } : {}) } }
|
|
54
|
+
end
|
|
55
|
+
|
|
24
56
|
%w[get post put patch delete].each do |verb|
|
|
25
57
|
# `data:` is sent as JSON; `body:` is sent as it is, for a request
|
|
26
58
|
# whose body is meant to be wrong.
|
|
@@ -33,10 +65,22 @@ module Nukadoko
|
|
|
33
65
|
req.body = JSON.generate(data)
|
|
34
66
|
end
|
|
35
67
|
req.body = body if body
|
|
68
|
+
# The world's origin gets the step's traceparent, so the program's
|
|
69
|
+
# log says which step a request came from.
|
|
70
|
+
traceparent = req["traceparent"] ? nil : Trace.current&.stamp(uri)
|
|
71
|
+
req["traceparent"] = traceparent if traceparent
|
|
36
72
|
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
73
|
+
entry = { method: verb.upcase, url: uri.to_s, **(path.to_s == uri.to_s ? {} : { declared_url: path.to_s }) }
|
|
74
|
+
stamp = traceparent ? { traceparent: traceparent, traceparent_sent_to: [Trace.origin(uri)] } : {}
|
|
75
|
+
begin
|
|
76
|
+
res = HttpRecording.unrecorded { Net::HTTP.start(uri.host.to_s, uri.port) { _1.request(req) } }
|
|
77
|
+
rescue StandardError => e
|
|
78
|
+
@log << entry.merge(status: 0, duration_ms: Request.since(started), via: "request", **stamp, **Request.body(:request_body, req.body),
|
|
79
|
+
error: e.message.lines.first.to_s.chomp)
|
|
80
|
+
::Kernel.raise e
|
|
81
|
+
end
|
|
82
|
+
@log << entry.merge(status: res.code.to_i, duration_ms: Request.since(started), via: "request", **stamp,
|
|
83
|
+
**Request.body(:request_body, req.body), **Request.body(:response_body, res.body))
|
|
40
84
|
Response.new(res.code.to_i, res.to_hash, res.body.to_s)
|
|
41
85
|
end
|
|
42
86
|
end
|
|
@@ -46,31 +90,305 @@ module Nukadoko
|
|
|
46
90
|
# TypeScript engine. Requests the page sends are recorded by the engine's
|
|
47
91
|
# own listener, and each step gets a trace chunk.
|
|
48
92
|
class Browser
|
|
49
|
-
|
|
93
|
+
# `secrets` gives the run's loaded secrets when a screenshot is taken,
|
|
94
|
+
# since an environment's env files are read after the browser is made.
|
|
95
|
+
#: (String?, ?Hash[Symbol, untyped], ?secrets: ^() -> Secrets) -> void
|
|
96
|
+
def initialize(base_url, launch_options = {}, secrets: -> { Secrets.new })
|
|
50
97
|
@base_url = base_url
|
|
51
98
|
@launch_options = launch_options
|
|
99
|
+
@secrets = secrets
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Opaque, so a masked secret leaves no trace of its glyphs.
|
|
103
|
+
MASK_COLOR = "#000000"
|
|
104
|
+
|
|
105
|
+
# Locators for every element of `page`, in every frame, that shows a
|
|
106
|
+
# loaded secret: as text, or as the value of an input or textarea, which
|
|
107
|
+
# is not text `get_by_text` reads, and a step may have typed the secret
|
|
108
|
+
# there. The text redaction of a record cannot reach pixels, and
|
|
109
|
+
# marking or rewriting the elements would change the page the next step
|
|
110
|
+
# reads. Only the value as written is found, not an encoded spelling;
|
|
111
|
+
# `get_by_text` ignores case, which costs pixels, never a leak.
|
|
112
|
+
#: (untyped) -> Array[untyped]
|
|
113
|
+
def secret_masks(page)
|
|
114
|
+
values = @secrets.call.shown_values.select { _1.length >= Secrets::MIN_LENGTH }
|
|
115
|
+
return [] if values.empty?
|
|
116
|
+
|
|
117
|
+
page.frames.flat_map do |frame|
|
|
118
|
+
fields = frame.locator("input, textarea")
|
|
119
|
+
held = fields.evaluate_all("(elements) => elements.map((element) => String(element.value ?? ''))") #: Array[String]
|
|
120
|
+
values.map { frame.get_by_text(_1) } + held.each_index.select { |index| values.any? { held[index].include?(_1) } }.map { fields.nth(_1) }
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# A screenshot of `page` at `path` with what shows a secret covered,
|
|
125
|
+
# true once written. No screenshot rather than one that shows a
|
|
126
|
+
# secret: masks not found by `deadline` (a monotonic time) give none,
|
|
127
|
+
# and the image is written only once it arrived. `bounded` holds the
|
|
128
|
+
# screenshot to the deadline too, as a pause and a judged view do.
|
|
129
|
+
#: (untyped, String, Float, ?caret: String?, ?bounded: bool) -> bool
|
|
130
|
+
def masked_shot(page, path, deadline, caret: nil, bounded: true)
|
|
131
|
+
masks = Timeout.timeout([deadline - monotonic, 0.001].max) { secret_masks(page) }
|
|
132
|
+
left = ((deadline - monotonic) * 1000).floor
|
|
133
|
+
return false if bounded && left <= 0
|
|
134
|
+
|
|
135
|
+
image = page.screenshot(mask: masks, maskColor: MASK_COLOR, **(bounded ? { timeout: left } : {}), **(caret ? { caret: caret } : {}))
|
|
136
|
+
File.binwrite(path, image)
|
|
137
|
+
true
|
|
138
|
+
rescue StandardError, Timeout::Error
|
|
139
|
+
false
|
|
52
140
|
end
|
|
53
141
|
|
|
142
|
+
#: (String, Array[Hash[Symbol, untyped]]) -> [untyped, ^() -> void]
|
|
54
143
|
def page_for(step_dir, log)
|
|
55
144
|
require "playwright"
|
|
56
145
|
@execution ||= Playwright.create(playwright_cli_executable_path: ENV.fetch("PLAYWRIGHT_CLI", "npx playwright"))
|
|
57
146
|
@browser ||= @execution.playwright.chromium.launch(**@launch_options)
|
|
58
|
-
@context ||= @browser.new_context(baseURL: @base_url).tap
|
|
59
|
-
|
|
147
|
+
@context ||= @browser.new_context(baseURL: @base_url).tap do |context|
|
|
148
|
+
context.tracing.start(screenshots: true, snapshots: true)
|
|
149
|
+
watch_requests(context)
|
|
150
|
+
end
|
|
151
|
+
@page ||= @context.new_page.tap { stamp(_1) }
|
|
60
152
|
@context.tracing.start_chunk
|
|
61
|
-
|
|
153
|
+
@boundary = Trace.current
|
|
154
|
+
listener = ->(req) { log << { method: req.method, url: req.url, via: "page" } }
|
|
62
155
|
@page.on("request", listener)
|
|
63
|
-
|
|
156
|
+
finish = lambda do
|
|
157
|
+
@page.off("request", listener)
|
|
158
|
+
@boundary = nil
|
|
159
|
+
@context.tracing.stop_chunk(path: File.join(step_dir, "trace.zip"))
|
|
160
|
+
end
|
|
161
|
+
[@page, finish]
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
STAMPED_TYPES = %w[Document Fetch XHR].freeze
|
|
165
|
+
ORDERED_KINDS = %w[document fetch xhr].freeze
|
|
166
|
+
|
|
167
|
+
# The step that runs now, as a page request names it: set for every
|
|
168
|
+
# step, a page or not, so a request a page sends during a step is
|
|
169
|
+
# that step's.
|
|
170
|
+
attr_writer :step #: Hash[Symbol, untyped]?
|
|
171
|
+
|
|
172
|
+
# Which requests to the world the page had finished receiving the
|
|
173
|
+
# response of, and which were in flight, around a claim step's
|
|
174
|
+
# reading: an order of the events this process received, never a
|
|
175
|
+
# claim that the page drew from a response. Only a document, a
|
|
176
|
+
# fetch, and an XMLHttpRequest count; an EventSource stays open, so
|
|
177
|
+
# it would be in flight on every reading.
|
|
178
|
+
#: (untyped) -> void
|
|
179
|
+
def watch_requests(context)
|
|
180
|
+
@requests = []
|
|
181
|
+
@by_request = {}
|
|
182
|
+
context.on("request", lambda do |request|
|
|
183
|
+
next unless ORDERED_KINDS.include?(request.resource_type) && Trace.stamps?(@base_url, request.url)
|
|
184
|
+
|
|
185
|
+
entry = { request: request, method: request.method, url: request.url, sent: monotonic, sent_at: Time.now.utc.iso8601(3), step: @step }
|
|
186
|
+
@requests << entry
|
|
187
|
+
@by_request[request.object_id] = entry
|
|
188
|
+
end)
|
|
189
|
+
context.on("requestfinished", lambda do |request|
|
|
190
|
+
entry = @by_request[request.object_id] or next
|
|
191
|
+
entry.merge!(finished: monotonic, finished_at: Time.now.utc.iso8601(3))
|
|
192
|
+
end)
|
|
193
|
+
context.on("requestfailed", lambda do |request|
|
|
194
|
+
entry = @by_request[request.object_id] or next
|
|
195
|
+
entry.merge!(ended: monotonic, failed_at: Time.now.utc.iso8601(3))
|
|
196
|
+
end)
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# Runs one reading of a claim step and keeps when it began and ended.
|
|
200
|
+
#: [T] () { () -> T } -> T
|
|
201
|
+
def reading
|
|
202
|
+
started = monotonic
|
|
203
|
+
value = yield
|
|
204
|
+
@reading = [started, monotonic]
|
|
205
|
+
value
|
|
206
|
+
ensure
|
|
207
|
+
@reading ||= [started, monotonic] if started
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
#: () -> nil
|
|
211
|
+
def begin_step = (@reading = nil)
|
|
212
|
+
|
|
213
|
+
# A claim step's `pages`, `page_requests`, and `judged_view`, after
|
|
214
|
+
# the reading its claims were judged on, when a page was open.
|
|
215
|
+
#: (String, String, ?aria_in_claims: bool) -> Hash[Symbol, untyped]
|
|
216
|
+
def judged(dir, step_record_id, aria_in_claims: false)
|
|
217
|
+
return {} unless @page && @reading
|
|
218
|
+
|
|
219
|
+
started, ended = @reading
|
|
220
|
+
listed = []
|
|
221
|
+
index = lambda do |entry|
|
|
222
|
+
at = listed.index(entry) || (listed << entry).size - 1
|
|
223
|
+
at
|
|
224
|
+
end
|
|
225
|
+
before = @requests.select { _1[:finished] && _1[:finished] < started }.max_by { _1[:finished] }
|
|
226
|
+
open = @requests.select { _1[:sent] <= ended && !(_1[:finished] && _1[:finished] < started) && !(_1[:ended] && _1[:ended] < started) }
|
|
227
|
+
page = { as: nil, page: 1, url: @page.url, last_response: before && index.(before), in_flight: open.map { index.(_1) } }
|
|
228
|
+
# What the pages had received is of requests to a world, so a run
|
|
229
|
+
# with none keeps the view alone.
|
|
230
|
+
received = @base_url ? { pages: [page], page_requests: listed.map { request_record(_1, step_record_id) } } : {}
|
|
231
|
+
{ **received, judged_view: view(dir, ended, aria_in_claims) }
|
|
232
|
+
rescue StandardError
|
|
233
|
+
{}
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
#: (Hash[Symbol, untyped], String) -> Hash[Symbol, untyped]
|
|
237
|
+
def request_record(entry, step_record_id)
|
|
238
|
+
# @type var trace: String?
|
|
239
|
+
trace = begin
|
|
240
|
+
entry[:request].header_value("traceparent") if entry[:finished] || entry[:failed_at]
|
|
241
|
+
rescue StandardError
|
|
242
|
+
nil
|
|
243
|
+
end
|
|
244
|
+
ids = trace&.match(/\A00-(\h{32})-(\h{16})-\h{2}\z/)
|
|
245
|
+
during = if entry[:step].nil? then "pause"
|
|
246
|
+
elsif entry[:step][:step_record_id] == step_record_id then "this-step"
|
|
247
|
+
else "earlier-step"
|
|
248
|
+
end
|
|
249
|
+
{ method: entry[:method], url: entry[:url],
|
|
250
|
+
**(entry[:finished] || entry[:failed_at] ? { trace_id: ids && ids[1], span_id: ids && ids[2] } : {}),
|
|
251
|
+
sent_at: entry[:sent_at], finished_at: entry[:finished_at], **(entry[:failed_at] ? { failed_at: entry[:failed_at] } : {}),
|
|
252
|
+
sent_during: during, **(during == "earlier-step" ? { step: entry[:step] } : {}) }
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# What the page showed right after the judged reading: its title, a
|
|
256
|
+
# viewport screenshot, and its ARIA snapshot, each with how long after
|
|
257
|
+
# the reading it was taken. The claims judged what observe returned,
|
|
258
|
+
# not these files.
|
|
259
|
+
# In a trace group of its own, so the step's actions leave these calls out.
|
|
260
|
+
#: (String, Float, bool) -> Hash[Symbol, untyped]
|
|
261
|
+
def view(dir, ended, aria_in_claims)
|
|
262
|
+
@context.tracing.group(TraceActions::JUDGED_VIEW_GROUP)
|
|
263
|
+
view_files(dir, ended, aria_in_claims)
|
|
264
|
+
ensure
|
|
265
|
+
@context.tracing.group_end rescue nil
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
# A step whose claims judge a tree nukadoko took keeps that tree in its
|
|
269
|
+
# claims, so the view takes no second one.
|
|
270
|
+
#: (String, Float, bool) -> Hash[Symbol, untyped]
|
|
271
|
+
def view_files(dir, ended, aria_in_claims)
|
|
272
|
+
FileUtils.mkdir_p(File.join(dir, "judged"))
|
|
273
|
+
title = (@page.title rescue nil)
|
|
274
|
+
shot = if masked_shot(@page, File.join(dir, "judged", "page.png"), monotonic + VIEW_TIMEOUT_MS / 1000.0, caret: "initial")
|
|
275
|
+
{ file: "judged/page.png", after_reading_ms: ((monotonic - ended) * 1000).round }
|
|
276
|
+
end
|
|
277
|
+
aria = aria_in_claims ? nil : begin
|
|
278
|
+
tree = @page.aria_snapshot(mode: "ai")
|
|
279
|
+
File.write(File.join(dir, "judged", "page.aria.yml"), tree.end_with?("\n") ? tree : "#{tree}\n")
|
|
280
|
+
{ file: "judged/page.aria.yml", after_reading_ms: ((monotonic - ended) * 1000).round }
|
|
281
|
+
rescue StandardError
|
|
282
|
+
nil
|
|
283
|
+
end
|
|
284
|
+
{ pages: [{ as: nil, page: 1, url: @page.url, title: title, screenshot: shot, aria: aria }], **(aria_in_claims ? { aria_in_claims: true } : {}) }
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
#: () -> Float
|
|
288
|
+
def monotonic = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
# A page's documents, fetches, and XMLHttpRequests to the world get
|
|
292
|
+
# the running step's traceparent through the Chrome DevTools
|
|
293
|
+
# Protocol's Fetch domain, on a session of the page's own: a header set
|
|
294
|
+
# on the whole context would reach other origins too, and a route
|
|
295
|
+
# turns the browser's cache off. Images, scripts, and stylesheets are
|
|
296
|
+
# left out, since each paused request costs a round trip. A session
|
|
297
|
+
# that cannot be opened leaves the page's requests unstamped.
|
|
298
|
+
#: (untyped) -> void
|
|
299
|
+
def stamp(page)
|
|
300
|
+
session = @context.new_cdp_session(page)
|
|
301
|
+
session.on("Fetch.requestPaused", lambda do |event|
|
|
302
|
+
request = event["request"]
|
|
303
|
+
headers = nil
|
|
304
|
+
unless request["headers"].keys.any? { _1.downcase == "traceparent" }
|
|
305
|
+
traceparent = @boundary&.stamp(request["url"])
|
|
306
|
+
headers = [*request["headers"].map { |name, value| { name: name, value: value } }, { name: "traceparent", value: traceparent }] if traceparent
|
|
307
|
+
end
|
|
308
|
+
# Answered off the thread that delivers the event, which also
|
|
309
|
+
# reads the answer.
|
|
310
|
+
Thread.new do
|
|
311
|
+
session.send_message("Fetch.continueRequest", params: { requestId: event["requestId"], **(headers ? { headers: headers } : {}) })
|
|
312
|
+
rescue StandardError
|
|
313
|
+
nil
|
|
314
|
+
end
|
|
315
|
+
end)
|
|
316
|
+
world = URI(@base_url)
|
|
317
|
+
patterns = ["#{Trace.origin(world).gsub(/[\\*?]/) { "\\#{_1}" }}/*"]
|
|
318
|
+
patterns << "#{world.scheme}://*.localhost:#{world.port}/*" if world.host == "localhost"
|
|
319
|
+
session.send_message("Fetch.enable", params: { patterns: patterns.product(STAMPED_TYPES).map { |url, type| { urlPattern: url, resourceType: type, requestStage: "Request" } } })
|
|
320
|
+
rescue StandardError
|
|
321
|
+
nil
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
# How long a pause waits for each capture of the page's view. A page
|
|
325
|
+
# whose main thread is stuck answers none of them, and without a
|
|
326
|
+
# deadline the pause would never reach the person.
|
|
327
|
+
VIEW_TIMEOUT_MS = 5_000
|
|
328
|
+
|
|
329
|
+
# What `nuka run --step` shows at a pause, taken while the run waits:
|
|
330
|
+
# a viewport screenshot and an ARIA snapshot of the open page, named
|
|
331
|
+
# under `recorded_as` as run.json names them. From now until
|
|
332
|
+
# `release`, the requests the page sends go to the pause's own
|
|
333
|
+
# http.jsonl, never to the step's record. `[]` when no page is open.
|
|
334
|
+
#: (String, String) -> Array[Hash[Symbol, untyped]]
|
|
335
|
+
def hold(dir, recorded_as)
|
|
336
|
+
return [] unless @page
|
|
337
|
+
|
|
338
|
+
log = File.join(dir, "http.jsonl")
|
|
339
|
+
@held = lambda do |req|
|
|
340
|
+
File.open(log, "a") { _1.puts JSON.generate({ method: req.method, url: req.url, via: "page" }) }
|
|
341
|
+
rescue StandardError
|
|
342
|
+
nil
|
|
343
|
+
end
|
|
344
|
+
@page.on("request", @held)
|
|
345
|
+
# The caret shows, as the person would see the field they type in.
|
|
346
|
+
shot = masked_shot(@page, File.join(dir, "page.png"), monotonic + VIEW_TIMEOUT_MS / 1000.0, caret: "initial") ? "#{recorded_as}/page.png" : nil
|
|
347
|
+
aria = begin
|
|
348
|
+
tree = @page.aria_snapshot(mode: "ai", timeout: VIEW_TIMEOUT_MS)
|
|
349
|
+
File.write(File.join(dir, "page.aria.yml"), tree.end_with?("\n") ? tree : "#{tree}\n")
|
|
350
|
+
"#{recorded_as}/page.aria.yml"
|
|
351
|
+
rescue StandardError
|
|
352
|
+
nil
|
|
353
|
+
end
|
|
354
|
+
title = (@page.title rescue nil)
|
|
355
|
+
[{ as: nil, page: 1, url: @page.url, title: title, screenshot: shot, aria: aria }]
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
# The pause is answered: the page's requests are the next step's again.
|
|
359
|
+
#: () -> void
|
|
360
|
+
def release
|
|
361
|
+
@page.off("request", @held) if @page && @held
|
|
362
|
+
@held = nil
|
|
64
363
|
end
|
|
65
364
|
|
|
66
365
|
# Each scenario gets a browser context of its own, so cookies and storage
|
|
67
366
|
# never carry from one scenario to the next.
|
|
68
|
-
|
|
367
|
+
# The browser the scenario opened a page in, as run.json names it, or
|
|
368
|
+
# nil when it opened none.
|
|
369
|
+
#: () -> Hash[Symbol, untyped]?
|
|
370
|
+
def scenario_browser
|
|
371
|
+
@context && @browser ? { type: "chromium", version: @browser.version } : nil
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
# Closes the scenario's context, and returns the screenshot of its
|
|
375
|
+
# page as the scenario ended, `final.png` in `dir`, when it opened
|
|
376
|
+
# one. A page a failed step left unusable has none, and that costs
|
|
377
|
+
# the screenshot only.
|
|
378
|
+
#: (?String?) -> Array[Hash[Symbol, untyped]]
|
|
379
|
+
def end_scenario(dir = nil)
|
|
380
|
+
shots = [] #: Array[Hash[Symbol, untyped]]
|
|
381
|
+
if dir && @page
|
|
382
|
+
FileUtils.mkdir_p(dir)
|
|
383
|
+
shots << { file: "final.png", at: Time.now.utc.iso8601(3) } if masked_shot(@page, File.join(dir, "final.png"), monotonic + VIEW_TIMEOUT_MS / 1000.0, bounded: false)
|
|
384
|
+
end
|
|
69
385
|
@context&.tracing&.stop
|
|
70
386
|
@context&.close
|
|
71
387
|
@context = @page = nil
|
|
388
|
+
shots
|
|
72
389
|
end
|
|
73
390
|
|
|
391
|
+
#: () -> void
|
|
74
392
|
def close
|
|
75
393
|
@browser&.close
|
|
76
394
|
@execution&.stop
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
require "net/http"
|
|
5
|
+
|
|
6
|
+
# Requests a step sends through Net::HTTP itself, without the `request`
|
|
7
|
+
# fixture, measured as the TypeScript engine measures a step's own `fetch`:
|
|
8
|
+
# each lands in the step's http.jsonl with `via: "net/http"`, and one to
|
|
9
|
+
# the world's origin carries the step's traceparent. It records only while
|
|
10
|
+
# a step runs, on the thread that runs it, so a fixture's setup, the
|
|
11
|
+
# engine's own readiness check, and the `request` fixture (which records
|
|
12
|
+
# its requests itself) go through Net::HTTP untouched.
|
|
13
|
+
module Nukadoko
|
|
14
|
+
module Engine
|
|
15
|
+
module HttpRecording
|
|
16
|
+
#: [T] (Array[untyped]) { () -> T } -> T
|
|
17
|
+
def self.collect(log)
|
|
18
|
+
Thread.current[:nukadoko_http] = log
|
|
19
|
+
yield
|
|
20
|
+
ensure
|
|
21
|
+
Thread.current[:nukadoko_http] = nil
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Net::HTTP as the engine's own code calls it, recording nothing.
|
|
25
|
+
#: [T] () { () -> T } -> T
|
|
26
|
+
def self.unrecorded
|
|
27
|
+
was = Thread.current[:nukadoko_http_off]
|
|
28
|
+
Thread.current[:nukadoko_http_off] = true
|
|
29
|
+
yield
|
|
30
|
+
ensure
|
|
31
|
+
Thread.current[:nukadoko_http_off] = was
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Prepended to Net::HTTP. `request` calls itself once more when the
|
|
35
|
+
# connection was not started, so only the outermost call records.
|
|
36
|
+
# @rbs module-self Net::HTTP
|
|
37
|
+
module Patch
|
|
38
|
+
#: (Net::HTTPGenericRequest, ?String?) ?{ (Net::HTTPResponse) -> void } -> Net::HTTPResponse
|
|
39
|
+
def request(req, body = nil, &block)
|
|
40
|
+
log = Thread.current[:nukadoko_http]
|
|
41
|
+
return super if log.nil? || Thread.current[:nukadoko_http_off] || Thread.current[:nukadoko_http_inside]
|
|
42
|
+
|
|
43
|
+
Thread.current[:nukadoko_http_inside] = true
|
|
44
|
+
url = "#{use_ssl? ? "https" : "http"}://#{address}#{":#{port}" unless port == (use_ssl? ? 443 : 80)}#{req.path}"
|
|
45
|
+
traceparent = req["traceparent"] ? nil : Trace.current&.stamp(url)
|
|
46
|
+
req["traceparent"] = traceparent if traceparent
|
|
47
|
+
stamp = traceparent ? { traceparent: traceparent, traceparent_sent_to: [Trace.origin(URI(url))] } : {}
|
|
48
|
+
sent = body || req.body
|
|
49
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
50
|
+
begin
|
|
51
|
+
res = super
|
|
52
|
+
rescue StandardError => e
|
|
53
|
+
log << { method: req.method, url: url, status: 0, duration_ms: Request.since(started), via: "net/http", **stamp,
|
|
54
|
+
**Request.body(:request_body, sent), error: e.message.lines.first.to_s.chomp }
|
|
55
|
+
raise
|
|
56
|
+
end
|
|
57
|
+
log << { method: req.method, url: url, status: res.code.to_i, duration_ms: Request.since(started), via: "net/http", **stamp,
|
|
58
|
+
**Request.body(:request_body, sent), **(res.body.is_a?(String) ? Request.body(:response_body, res.body) : {}) }
|
|
59
|
+
res
|
|
60
|
+
ensure
|
|
61
|
+
Thread.current[:nukadoko_http_inside] = false if log && !Thread.current[:nukadoko_http_off]
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
Net::HTTP.prepend(Patch)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|