ruby_raider 4.0.0 → 4.1.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 (78) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ffmpeg/build.sh +208 -0
  3. data/.github/ffmpeg/check.sh +72 -0
  4. data/.github/ffmpeg/frames.rb +34 -0
  5. data/.github/ffmpeg/probe.sh +17 -0
  6. data/.github/workflows/ffmpeg.yml +160 -0
  7. data/README.md +226 -33
  8. data/RELEASE.md +19 -0
  9. data/lib/commands/flow_commands.rb +104 -0
  10. data/lib/generators/templates/common/read_me.tt +6 -7
  11. data/lib/generators/templates/flows/flow_helper.tt +42 -0
  12. data/lib/generators/templates/flows/run.tt +332 -0
  13. data/lib/generators/templates/helpers/video_helper.tt +1 -2
  14. data/lib/ruby_raider/ffmpeg.rb +38 -11
  15. data/lib/ruby_raider.rb +16 -0
  16. data/lib/utilities/flows/next_run.rb +61 -0
  17. data/lib/utilities/flows/notifier.rb +76 -0
  18. data/lib/utilities/flows/os/command_runner.rb +73 -0
  19. data/lib/utilities/flows/os/cron.rb +137 -0
  20. data/lib/utilities/flows/os/launchd.rb +164 -0
  21. data/lib/utilities/flows/os/linux.rb +63 -0
  22. data/lib/utilities/flows/os/systemd.rb +178 -0
  23. data/lib/utilities/flows/os/task_scheduler.rb +179 -0
  24. data/lib/utilities/flows/project.rb +170 -0
  25. data/lib/utilities/flows/run_log.rb +61 -0
  26. data/lib/utilities/flows/runner.rb +206 -0
  27. data/lib/utilities/flows/schedule.rb +221 -0
  28. data/lib/utilities/flows/schedule_store.rb +135 -0
  29. data/lib/utilities/flows/scheduler.rb +187 -0
  30. data/lib/version +1 -1
  31. data/sig/commands/flow_commands.rbs +13 -0
  32. data/sig/ruby_raider/ffmpeg.rbs +1 -0
  33. data/sig/ruby_raider.rbs +2 -0
  34. data/sig/utilities/flows/next_run.rbs +15 -0
  35. data/sig/utilities/flows/notifier.rbs +29 -0
  36. data/sig/utilities/flows/os/command_runner.rbs +27 -0
  37. data/sig/utilities/flows/os/cron.rbs +34 -0
  38. data/sig/utilities/flows/os/launchd.rbs +45 -0
  39. data/sig/utilities/flows/os/linux.rbs +27 -0
  40. data/sig/utilities/flows/os/systemd.rbs +43 -0
  41. data/sig/utilities/flows/os/task_scheduler.rbs +53 -0
  42. data/sig/utilities/flows/project.rbs +46 -0
  43. data/sig/utilities/flows/run_log.rbs +22 -0
  44. data/sig/utilities/flows/runner.rbs +35 -0
  45. data/sig/utilities/flows/schedule.rbs +61 -0
  46. data/sig/utilities/flows/schedule_store.rbs +34 -0
  47. data/sig/utilities/flows/scheduler.rbs +42 -0
  48. data/spec/commands/flow_commands_spec.rb +128 -0
  49. data/spec/generators/flow_helper_spec.rb +40 -0
  50. data/spec/generators/video_helper_spec.rb +21 -4
  51. data/spec/integration/content/gemfile_content_spec.rb +2 -2
  52. data/spec/integration/content/readme_content_spec.rb +7 -2
  53. data/spec/integration/end_to_end_spec.rb +83 -8
  54. data/spec/integration/flows/flow_project.rb +62 -0
  55. data/spec/integration/flows/run_command_spec.rb +91 -0
  56. data/spec/integration/flows/run_lifecycle_spec.rb +383 -0
  57. data/spec/utilities/ffmpeg_spec.rb +80 -7
  58. data/spec/utilities/flows/next_run_spec.rb +119 -0
  59. data/spec/utilities/flows/notifier_spec.rb +121 -0
  60. data/spec/utilities/flows/os/command_runner_spec.rb +56 -0
  61. data/spec/utilities/flows/os/cron_spec.rb +287 -0
  62. data/spec/utilities/flows/os/launchd_spec.rb +293 -0
  63. data/spec/utilities/flows/os/linux_spec.rb +149 -0
  64. data/spec/utilities/flows/os/systemd_spec.rb +329 -0
  65. data/spec/utilities/flows/os/task_scheduler_spec.rb +505 -0
  66. data/spec/utilities/flows/project_spec.rb +178 -0
  67. data/spec/utilities/flows/run_log_spec.rb +47 -0
  68. data/spec/utilities/flows/runner_spec.rb +313 -0
  69. data/spec/utilities/flows/schedule_spec.rb +233 -0
  70. data/spec/utilities/flows/schedule_store_spec.rb +137 -0
  71. data/spec/utilities/flows/scheduler_spec.rb +329 -0
  72. data/spec/utilities/video_command_spec.rb +13 -0
  73. metadata +58 -7
  74. data/docs/superpowers/plans/2026-10-25-failure-video-replay.md +0 -400
  75. data/docs/superpowers/plans/2026-10-26-parity-cleanup.md +0 -233
  76. data/docs/superpowers/plans/2026-10-28-failure-card.md +0 -295
  77. data/docs/superpowers/specs/2026-10-25-failure-video-replay-design.md +0 -214
  78. data/docs/superpowers/specs/2026-10-28-failure-card-design.md +0 -210
@@ -321,9 +321,9 @@ describe 'End-to-End Project Generation and Execution' do
321
321
  JSON.parse(File.read(File.join(project, 'raider-results', 'run.json')))['results']
322
322
  end
323
323
 
324
- # Failure video replay against real browsers. Needs an ffmpeg with libvpx (RAIDER_FFMPEG_PATH), because the
325
- # RaiderHQ ffmpeg release has no checksums yet. Skipped when it, or the browser, is missing. Firefox can be
326
- # pointed at a specific binary with FIREFOX_BINARY.
324
+ # Failure video replay against real browsers. Needs an ffmpeg with libvpx (RAIDER_FFMPEG_PATH), so most examples
325
+ # never download the release binary; RAIDER_E2E_DOWNLOAD=1 adds one that does. Skipped when it, or the browser,
326
+ # is missing. Firefox can be pointed at a specific binary with FIREFOX_BINARY.
327
327
  context 'Failure video replay', :slow, :video do
328
328
  let(:ffmpeg_path) { ENV.fetch('RAIDER_FFMPEG_PATH', '') }
329
329
  let(:cache_dir) { Dir.mktmpdir('raider-cache') }
@@ -421,17 +421,92 @@ describe 'End-to-End Project Generation and Execution' do
421
421
  it 'runs unchanged with one [video] line and no download when no ffmpeg is usable', :aggregate_failures do
422
422
  off = run_in_project(project, command, env: video_env('VIDEO' => 'off'))
423
423
  FileUtils.rm_rf(File.join(project, 'raider-results'))
424
- result = run_in_project(project, command, env: video_env('RAIDER_FFMPEG_PATH' => nil))
424
+ # A file where the cache dir should be: the download cannot even start, so nothing is fetched.
425
+ blocker = File.join(cache_dir, 'not-a-dir')
426
+ File.write(blocker, '')
427
+ result = run_in_project(project, command, env: video_env('RAIDER_FFMPEG_PATH' => nil, 'RAIDER_CACHE_DIR' => blocker))
425
428
 
426
429
  expect(result[:exit_code]).to eq(off[:exit_code]), "STDOUT: #{result[:stdout]}\nSTDERR: #{result[:stderr]}"
427
430
  video_lines = result[:stderr].lines.grep(/\[video\]/)
428
431
  expect(video_lines.size).to eq(1), "STDERR: #{result[:stderr]}"
429
- # Decided at the first driver/recorder call, before any capture or fetch: the placeholder
430
- # manifest has no checksum, so nothing is captured or downloaded.
431
- expect(video_lines.first).to include('no ffmpeg available for this platform yet')
432
+ expect(video_lines.first).to include('ffmpeg download failed')
432
433
  expect(video_files(project)).to be_empty
433
- expect(Dir.glob(File.join(cache_dir, '**', '*'))).to be_empty
434
+ expect(Dir.glob(File.join(cache_dir, '**', '*'))).to eq([blocker])
434
435
  end
436
+
437
+ # Downloads the pinned ffmpeg-7.1 release asset for this platform into an empty cache.
438
+ it 'downloads the pinned ffmpeg into an empty cache and attaches the video', :aggregate_failures do
439
+ skip 'set RAIDER_E2E_DOWNLOAD=1 to download the ffmpeg-7.1 release asset' unless ENV['RAIDER_E2E_DOWNLOAD'] == '1'
440
+
441
+ result = run_in_project(project, command, env: video_env('RAIDER_FFMPEG_PATH' => nil))
442
+ expect(result[:exit_code]).to eq(1), "STDOUT: #{result[:stdout]}\nSTDERR: #{result[:stderr]}"
443
+ expect(result[:stderr]).not_to include('[video]')
444
+ expect(Dir.glob(File.join(cache_dir, 'ffmpeg', '7.1', 'ffmpeg{,.exe}')).size).to eq(1)
445
+ expect_video_for_failure_only(project, 'fails after driving the page', result)
446
+ end
447
+ end
448
+ end
449
+
450
+ # raider flow init and raider flow run on a generated Selenium project, headless, against a local page
451
+ context 'Flows', :slow do
452
+ let(:project) { generate_video_project('rspec', 'selenium') }
453
+
454
+ def write_flows(project)
455
+ page = File.join(File.expand_path(project), 'flow_page.html')
456
+ File.write(page, <<~HTML)
457
+ <!doctype html><title>Timesheet</title>
458
+ <label for="hours">Hours</label><input id="hours">
459
+ <button onclick="document.getElementById('done').textContent = 'Saved ' + document.getElementById('hours').value">Save</button>
460
+ <p id="done"></p>
461
+ HTML
462
+ { 'timesheet' => "driver.find_element(label: 'Hours').send_keys(flow_input('hours', '8'))\n " \
463
+ "driver.find_element(by_role('button', name: 'Save')).click\n " \
464
+ "driver.find_element(text: \"Saved \#{flow_input('hours', '8')}\")",
465
+ 'export' => "driver.find_element(id: 'export').click" }.each do |name, steps|
466
+ File.write(File.join(project, "flows/#{name}_flow.rb"), <<~RUBY)
467
+ # frozen_string_literal: true
468
+
469
+ require_relative 'flow_helper'
470
+
471
+ RSpec.describe '#{name}' do
472
+ it 'runs' do
473
+ driver.navigate.to 'file://#{page}'
474
+ #{steps}
475
+ end
476
+ end
477
+ RUBY
478
+ end
479
+ end
480
+
481
+ def runs(project, name)
482
+ File.readlines(File.join(project, ".raider/flows/#{name}/runs.jsonl")).map { |line| JSON.parse(line) }
483
+ end
484
+
485
+ it 'runs a flow headless into flow-results and records completed and failed runs', :aggregate_failures do
486
+ skip 'Google Chrome is not installed' unless browser_installed?('chrome')
487
+ FileUtils.rm_rf([File.join(project, 'flows'), File.join(project, '.raider/flows'), File.join(project, 'flow-results')])
488
+
489
+ init = run_in_project(project, 'bundle exec raider flow init')
490
+ expect(init[:exit_code]).to eq(0), init[:stdout] + init[:stderr]
491
+ write_flows(project)
492
+ env = { 'VIDEO' => 'off', 'TRACE' => 'off', 'RAIDER_TOKEN' => nil, 'RAIDER_ENDPOINT' => nil }
493
+ completed = run_in_project(project, 'bundle exec raider flow run timesheet --input hours=7', env:)
494
+ failed = run_in_project(project, 'bundle exec raider flow run export', env: env.merge('RAIDER_TIMEOUT' => '1'))
495
+
496
+ expect(completed[:exit_code]).to eq(0), completed[:stdout] + completed[:stderr]
497
+ expect(completed[:stdout]).to match(%r{Flow timesheet completed in .* s\nResults: flow-results/timesheet/\d{8}T\d{6}Z})
498
+ expect(runs(project, 'timesheet').last).to include('status' => 'completed', 'trigger' => 'manual')
499
+ expect(failed[:exit_code]).to eq(1), failed[:stdout] + failed[:stderr]
500
+ expect(runs(project, 'export').last).to include('status' => 'failed', 'failed_step' => { 'title' => 'runs', 'line' => 8 })
501
+ expect(File).to exist(File.join(project, runs(project, 'export').last['results_dir'], 'run.json'))
502
+ expect(File).not_to exist(File.join(project, 'raider-results'))
503
+ end
504
+
505
+ it 'passes its own RuboCop with the flow files' do
506
+ FileUtils.rm_rf(File.join(project, 'flows'))
507
+ run_in_project(project, 'bundle exec raider flow init')
508
+
509
+ expect_project_rubocop_clean(project)
435
510
  end
436
511
  end
437
512
 
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'erb'
4
+ require 'fileutils'
5
+
6
+ # A project with the flow files `raider flow init` writes and a stand-in helpers/spec_helper.rb:
7
+ # no browser and no raider_reporter. FAKE_DRIVER_FAIL makes the driver fail in before(:each);
8
+ # FAKE_STEPS writes a run.json whose desktop step map names step 2 of flows/recorded_flow.rb.
9
+ module FlowProject
10
+ TEMPLATES = File.expand_path('../../../lib/generators/templates/flows', __dir__)
11
+
12
+ STUB_SPEC_HELPER = <<~RUBY
13
+ # frozen_string_literal: true
14
+
15
+ require 'json'
16
+ require 'rspec'
17
+
18
+ module RaiderReporter
19
+ Config = Struct.new(:output_dir, :clean_output_dir)
20
+ def self.configuration = (@configuration ||= Config.new(output_dir: 'raider-results', clean_output_dir: true))
21
+ def self.configure = yield(configuration)
22
+ end
23
+
24
+ # What the real reporter would write to and clean, for the specs to read
25
+ at_exit { File.write('reporter.json', JSON.generate(RaiderReporter.configuration.to_h)) }
26
+
27
+ module DriverHelper
28
+ def driver
29
+ raise 'the driver could not start' if ENV['FAKE_DRIVER_FAIL']
30
+
31
+ @driver ||= Object.new
32
+ end
33
+ end
34
+
35
+ RSpec.configure do |config|
36
+ config.include(DriverHelper)
37
+ config.before(:each) { driver }
38
+ config.after(:each) { File.write('driver_quit', '') }
39
+ # The real reporter writes run.json when it closes; the desktop sidecar names the failed step there
40
+ config.after(:suite) do
41
+ dir = RaiderReporter.configuration.output_dir
42
+ steps = { 'items' => [{ 'id' => 's1', 'title' => 'open' }, { 'id' => 's2', 'title' => 'click "Export"' }], 'failed' => 1 }
43
+ results = [{ 'file_path' => 'flows/recorded_flow.rb', 'status' => 'failed', 'steps' => steps }]
44
+ File.write(File.join(dir, 'run.json'), JSON.generate('results' => results)) if dir && ENV['FAKE_STEPS']
45
+ end
46
+ end
47
+ RUBY
48
+
49
+ module_function
50
+
51
+ def build(dir, flows)
52
+ FileUtils.mkdir_p(File.join(dir, 'flows/support'))
53
+ FileUtils.mkdir_p(File.join(dir, 'helpers'))
54
+ File.write(File.join(dir, 'flows/flow_helper.rb'), render('flow_helper.tt'))
55
+ File.write(File.join(dir, 'flows/support/run.rb'), render('run.tt'))
56
+ File.write(File.join(dir, 'helpers/spec_helper.rb'), STUB_SPEC_HELPER)
57
+ File.write(File.join(dir, '.rspec'), "-I .\n--require helpers/spec_helper\n")
58
+ flows.each { |name, source| File.write(File.join(dir, "flows/#{name}_flow.rb"), source) }
59
+ end
60
+
61
+ def render(name) = ERB.new(File.read(File.join(TEMPLATES, name)), trim_mode: '-').result
62
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'open3'
5
+ require 'tmpdir'
6
+ require_relative 'flow_project'
7
+
8
+ # `raider flow run` and `raider flow list` against a project with its own Gemfile, from another folder.
9
+ RSpec.describe 'raider flow run and list' do
10
+ raider = File.expand_path('../../../bin/raider', __dir__)
11
+
12
+ flows = {
13
+ 'ok' => <<~RUBY,
14
+ require_relative 'flow_helper'
15
+
16
+ RSpec.describe 'ok' do
17
+ it('runs every step') { puts "who=\#{flow_input('who', 'nobody')} headless=\#{ENV.fetch('HEADLESS', 'unset')}" }
18
+ end
19
+ RUBY
20
+ 'broken' => <<~RUBY
21
+ require_relative 'flow_helper'
22
+
23
+ RSpec.describe 'broken' do
24
+ it 'downloads the report' do
25
+ raise 'no such element'
26
+ end
27
+ end
28
+ RUBY
29
+ }
30
+
31
+ # Built once: bundling the project is the slow part
32
+ dir = nil
33
+
34
+ before(:context) do # rubocop:disable RSpec/BeforeAfterAll
35
+ dir = File.realpath(Dir.mktmpdir('flow-command'))
36
+ FlowProject.build(dir, flows)
37
+ File.write(File.join(dir, 'Gemfile'), "source 'https://rubygems.org'\ngem 'rspec'\n")
38
+ _out, err, status = Bundler.with_unbundled_env do
39
+ Open3.capture3('bundle', 'install', '--prefer-local', '--quiet', chdir: dir)
40
+ end
41
+ raise "bundle install failed: #{err}" unless status.success?
42
+ end
43
+
44
+ after(:context) { FileUtils.rm_rf(dir) } # rubocop:disable RSpec/BeforeAfterAll
45
+
46
+ define_method(:raider_flow) do |*args|
47
+ out, err, status = Open3.capture3(RbConfig.ruby, raider, 'flow', *args, '--project', dir, chdir: Dir.tmpdir)
48
+ [out, err, status.exitstatus]
49
+ end
50
+
51
+ it 'runs a completed flow, passing inputs and headless through, and exits 0' do
52
+ out, _err, code = raider_flow('run', 'ok', '--input', 'who=ada')
53
+
54
+ expect(code).to eq(0)
55
+ expect(out).to include('who=ada headless=true', 'Flow ok completed in ')
56
+ expect(out).to match(%r{^Results: flow-results/ok/\d{8}T\d{6}Z(-\d+)?$})
57
+ end
58
+
59
+ it 'exits 1 and names the failed line' do
60
+ out, _err, code = raider_flow('run', 'broken')
61
+
62
+ expect(code).to eq(1)
63
+ expect(out).to include('Flow broken failed at line 5 (downloads the report)')
64
+ end
65
+
66
+ it 'exits 2 for a flow that does not exist' do
67
+ _out, err, code = raider_flow('run', 'missing')
68
+
69
+ expect(code).to eq(2)
70
+ expect(err).to include('No flow missing: flows/missing_flow.rb not found')
71
+ end
72
+
73
+ it 'lists every flow with its last result as JSON for the desktop' do
74
+ raider_flow('run', 'ok')
75
+ out, _err, code = raider_flow('list', '--json')
76
+ listed = JSON.parse(out)
77
+
78
+ expect(code).to eq(0)
79
+ expect(listed.map { |flow| flow['name'] }).to eq(%w[broken ok])
80
+ expect(listed.last).to include('file' => 'flows/ok_flow.rb')
81
+ expect(listed.last['last'].keys).to eq(%w[status finished_at failed_step results_dir])
82
+ expect(listed.last['last']['status']).to eq('completed')
83
+ end
84
+
85
+ it 'lists flows as text' do
86
+ raider_flow('run', 'ok')
87
+ out, = raider_flow('list')
88
+
89
+ expect(out).to match(/^ok\s+completed\s+\d{4}-\d\d-\d\dT/)
90
+ end
91
+ end
@@ -0,0 +1,383 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'json'
5
+ require 'open3'
6
+ require 'time'
7
+ require 'timeout'
8
+ require 'tmpdir'
9
+ require_relative 'flow_project'
10
+
11
+ # Runs flows/support/run.rb and flows/flow_helper.rb, as `raider flow init` writes them, under a real
12
+ # rspec process (see FlowProject: no browser, no raider_reporter).
13
+ RSpec.describe 'flow run lifecycle' do
14
+ flows = {
15
+ 'ok' => <<~RUBY,
16
+ require_relative 'flow_helper'
17
+
18
+ RSpec.describe 'ok' do
19
+ it 'runs every step' do
20
+ driver
21
+ puts "headless=\#{ENV.fetch('HEADLESS', 'unset')} who=\#{flow_input('who', 'nobody')}"
22
+ puts "secret loaded=\#{ENV['RAIDER_SECRET_TOKEN'] == 'hunter2'}"
23
+ end
24
+ end
25
+ RUBY
26
+ 'broken' => <<~RUBY,
27
+ require_relative 'flow_helper'
28
+
29
+ RSpec.describe 'broken' do
30
+ it 'downloads the report' do
31
+ driver
32
+ driver
33
+ raise 'no such element: { id: "export" } (waited 10s)'
34
+ end
35
+ end
36
+ RUBY
37
+ 'recorded' => <<~RUBY,
38
+ require_relative 'flow_helper'
39
+
40
+ RSpec.describe 'recorded' do
41
+ it 'exports' do
42
+ raise 'no such element'
43
+ end
44
+ end
45
+ RUBY
46
+ 'leaky' => <<~RUBY,
47
+ require_relative 'flow_helper'
48
+
49
+ RSpec.describe 'leaky' do
50
+ it "types \#{ENV.fetch('RAIDER_SECRET_TOKEN', 'x')} somewhere" do
51
+ raise 'failed'
52
+ end
53
+ end
54
+ RUBY
55
+ 'env' => <<~RUBY,
56
+ require_relative 'flow_helper'
57
+
58
+ RSpec.describe('env') { it('runs') { puts 'ran' } }
59
+ RUBY
60
+ 'long' => <<~RUBY,
61
+ require_relative 'flow_helper'
62
+
63
+ RSpec.describe 'long' do
64
+ it 'waits' do
65
+ File.write('ready', '')
66
+ sleep 20
67
+ end
68
+ end
69
+ RUBY
70
+ 'unloadable' => <<~RUBY
71
+ require_relative 'flow_helper'
72
+
73
+ UndefinedConstant.call
74
+ RUBY
75
+ }
76
+
77
+ let(:dir) { File.realpath(Dir.mktmpdir('flow-lifecycle')) }
78
+
79
+ before { FlowProject.build(dir, flows) }
80
+
81
+ after { FileUtils.rm_rf(dir) }
82
+
83
+ # Unsets the variables the test process may carry that would change a run
84
+ def clean_env
85
+ %w[HEADLESS RAIDER_FLOW_NAME RAIDER_FLOW_HEADED RAIDER_FLOW_ATTEMPT RAIDER_FLOW_TRIGGER RAIDER_FLOW_RUN_ID
86
+ RAIDER_SECRET_TOKEN RAIDER_INPUT_WHO FAKE_DRIVER_FAIL FAKE_STEPS].to_h { |key| [key, nil] }
87
+ end
88
+
89
+ def run_flow(name, env = {})
90
+ out, err, status = Open3.capture3(clean_env.merge(env), RbConfig.ruby, '-S', 'rspec', "flows/#{name}_flow.rb", chdir: dir)
91
+ [out + err, status.exitstatus]
92
+ end
93
+
94
+ def log(name)
95
+ file = File.join(dir, ".raider/flows/#{name}/runs.jsonl")
96
+ File.exist?(file) ? File.readlines(file).map { |line| JSON.parse(line) } : []
97
+ end
98
+
99
+ def mode(relative) = File.stat(File.join(dir, relative)).mode & 0o777
100
+
101
+ def write_env_file(content, permissions)
102
+ FileUtils.mkdir_p(File.join(dir, '.raider/flows'))
103
+ File.write(File.join(dir, '.raider/flows/env'), content)
104
+ File.chmod(permissions, File.join(dir, '.raider/flows/env'))
105
+ end
106
+
107
+ it 'records a completed run with its results folder, headless by default', :aggregate_failures do
108
+ output, code = run_flow('ok', 'RAIDER_INPUT_WHO' => 'ada')
109
+ line = log('ok').last
110
+
111
+ expect(code).to eq(0)
112
+ expect(output).to include('headless=true who=ada')
113
+ expect(line).to include('status' => 'completed', 'attempt' => 1, 'trigger' => 'manual')
114
+ expect(line['results_dir']).to match(%r{\Aflow-results/ok/\d{8}T\d{6}Z\z})
115
+ expect(File).to be_directory(File.join(dir, line['results_dir']))
116
+ expect(Time.iso8601(line['finished_at'])).to be >= Time.iso8601(line['started_at'])
117
+ end
118
+
119
+ it 'keeps the run log and lock private' do
120
+ run_flow('ok')
121
+
122
+ expect([mode('.raider/flows'), mode('.raider/flows/ok'), mode('.raider/flows/ok/runs.jsonl'), mode('.raider/flows/ok.lock')])
123
+ .to eq([0o700, 0o700, 0o600, 0o600])
124
+ end
125
+
126
+ it 'opens the browser when RAIDER_FLOW_HEADED=1' do
127
+ output, = run_flow('ok', 'RAIDER_FLOW_HEADED' => '1')
128
+
129
+ expect(output).to include('headless=unset')
130
+ end
131
+
132
+ it 'takes the attempt, trigger and run id from the wrapper' do
133
+ run_flow('ok', 'RAIDER_FLOW_ATTEMPT' => '2', 'RAIDER_FLOW_TRIGGER' => 'schedule', 'RAIDER_FLOW_RUN_ID' => 'abcdef0123456789')
134
+
135
+ expect(log('ok').last).to include('attempt' => 2, 'trigger' => 'schedule', 'run_id' => 'abcdef0123456789')
136
+ end
137
+
138
+ it 'falls back to manual for an unknown trigger and drops a malformed run id' do
139
+ run_flow('ok', 'RAIDER_FLOW_TRIGGER' => 'cron; rm -rf /', 'RAIDER_FLOW_RUN_ID' => '../x')
140
+
141
+ expect(log('ok').last).to include('trigger' => 'manual').and(satisfy { |line| !line.key?('run_id') })
142
+ end
143
+
144
+ it 'records a failed step with its line in the flow file' do
145
+ _output, code = run_flow('broken')
146
+ line = log('broken').last
147
+
148
+ expect(code).to eq(1)
149
+ expect(line).to include('status' => 'failed', 'failed_step' => { 'title' => 'downloads the report', 'line' => 7 })
150
+ end
151
+
152
+ it 'names the recorded step when the report has the desktop step map' do
153
+ run_flow('recorded', 'FAKE_STEPS' => '1')
154
+
155
+ expect(log('recorded').last['failed_step']).to eq('number' => 2, 'title' => 'click "Export"', 'line' => 5)
156
+ end
157
+
158
+ it 'records a setup error when the driver fails before the first step' do
159
+ _output, code = run_flow('ok', 'FAKE_DRIVER_FAIL' => '1')
160
+
161
+ expect(code).to eq(1)
162
+ expect(log('ok').last).to include('status' => 'setup_error').and(satisfy { |line| !line.key?('failed_step') })
163
+ end
164
+
165
+ it 'records a setup error when the flow file does not load' do
166
+ _output, code = run_flow('unloadable')
167
+
168
+ expect(code).not_to eq(0)
169
+ expect(log('unloadable').last['status']).to eq('setup_error')
170
+ end
171
+
172
+ it 'refuses a RAIDER_FLOW_NAME that is not the flow being run' do
173
+ output, code = run_flow('ok', 'RAIDER_FLOW_NAME' => 'other')
174
+
175
+ expect(code).not_to eq(0)
176
+ expect(output).to include('RAIDER_FLOW_NAME is "other" but the flow is "ok"')
177
+ expect(log('ok')).to be_empty
178
+ end
179
+
180
+ it 'reports into the run folder and never cleans raider-results' do
181
+ run_flow('ok')
182
+ reporter = JSON.parse(File.read(File.join(dir, 'reporter.json')))
183
+
184
+ expect(reporter).to eq('output_dir' => File.join(dir, log('ok').last['results_dir']), 'clean_output_dir' => false)
185
+ end
186
+
187
+ it 'keeps a refused flow away from raider-results too' do
188
+ run_flow('ok', 'RAIDER_FLOW_NAME' => 'other')
189
+ reporter = JSON.parse(File.read(File.join(dir, 'reporter.json')))
190
+
191
+ expect(reporter['clean_output_dir']).to be(false)
192
+ expect(reporter['output_dir']).to start_with(File.realpath(Dir.tmpdir)).or start_with(Dir.tmpdir)
193
+ end
194
+
195
+ it 'drops a HEADLESS left in the shell for RAIDER_FLOW_HEADED=1' do
196
+ output, = run_flow('ok', 'RAIDER_FLOW_HEADED' => '1', 'HEADLESS' => 'true')
197
+
198
+ expect(output).to include('headless=unset')
199
+ end
200
+
201
+ it 'exits 3 with one busy line when another run holds the lock, and never waits' do
202
+ FileUtils.mkdir_p(File.join(dir, '.raider/flows'))
203
+ File.open(File.join(dir, '.raider/flows/ok.lock'), File::RDWR | File::CREAT, 0o600) do |held|
204
+ held.flock(File::LOCK_EX)
205
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
206
+
207
+ output, code = run_flow('ok')
208
+
209
+ expect(code).to eq(3)
210
+ expect(output).to include('ok is already running')
211
+ expect(Process.clock_gettime(Process::CLOCK_MONOTONIC) - started).to be < 30
212
+ end
213
+ expect(log('ok').map { |line| line['status'] }).to eq(['busy'])
214
+ expect(log('ok').last['results_dir']).to be_nil
215
+ end
216
+
217
+ it 'ends the step on the first Ctrl-C, ignores the second, quits the driver and records the run' do
218
+ pid = Process.spawn(clean_env, RbConfig.ruby, '-S', 'rspec', 'flows/long_flow.rb', chdir: dir, %i[out err] => File::NULL)
219
+ Timeout.timeout(30) { sleep 0.05 until File.exist?(File.join(dir, 'ready')) }
220
+ 2.times do
221
+ Process.kill('INT', pid)
222
+ sleep 0.2
223
+ end
224
+ Timeout.timeout(15) { Process.wait(pid) }
225
+
226
+ expect(log('long').map { |line| line['status'] }).to eq(['setup_error'])
227
+ expect(File).to exist(File.join(dir, 'driver_quit'))
228
+ end
229
+
230
+ it 'releases the lock when the run ends' do
231
+ run_flow('ok')
232
+ _output, code = run_flow('ok')
233
+
234
+ expect(code).to eq(0)
235
+ expect(log('ok').map { |line| line['status'] }).to eq(%w[completed completed])
236
+ end
237
+
238
+ context 'with the flow env file' do
239
+ it 'loads a 0600 file without printing its values' do
240
+ write_env_file("# secrets\nRAIDER_SECRET_TOKEN=\"hunter2\"\n\nbad line\n", 0o600)
241
+
242
+ output, = run_flow('ok')
243
+
244
+ expect(output).to include('secret loaded=true', '.raider/flows/env line 4 ignored: expected KEY=value')
245
+ expect(output).not_to include('hunter2')
246
+ expect(File.read(File.join(dir, '.raider/flows/ok/runs.jsonl'))).not_to include('hunter2')
247
+ end
248
+
249
+ it 'ignores a 0644 file with a warning' do
250
+ write_env_file("RAIDER_SECRET_TOKEN=hunter2\n", 0o644)
251
+
252
+ output, code = run_flow('ok')
253
+
254
+ expect(code).to eq(0)
255
+ expect(output).to include('ignored .raider/flows/env: permissions must be 600', 'secret loaded=false')
256
+ end
257
+
258
+ it 'ignores a link to a file elsewhere' do
259
+ target = File.join(dir, 'elsewhere')
260
+ File.write(target, "RAIDER_SECRET_TOKEN=hunter2\n")
261
+ File.chmod(0o600, target)
262
+ FileUtils.mkdir_p(File.join(dir, '.raider/flows'))
263
+ File.symlink(target, File.join(dir, '.raider/flows/env'))
264
+
265
+ output, = run_flow('ok')
266
+
267
+ expect(output).to include('ignored .raider/flows/env: it must be a regular file you own', 'secret loaded=false')
268
+ end
269
+
270
+ it 'does not override a variable already set' do
271
+ write_env_file("RAIDER_SECRET_TOKEN=hunter2\n", 0o600)
272
+
273
+ output, = run_flow('ok', 'RAIDER_SECRET_TOKEN' => 'from-shell')
274
+
275
+ expect(output).to include('secret loaded=false')
276
+ end
277
+
278
+ it 'keeps secret values out of the failed step title' do
279
+ write_env_file("RAIDER_SECRET_TOKEN=hunter2\n", 0o600)
280
+
281
+ run_flow('leaky')
282
+
283
+ expect(log('leaky').last['failed_step']['title']).to eq('types [secret] somewhere')
284
+ expect(File.read(File.join(dir, '.raider/flows/leaky/runs.jsonl'))).not_to include('hunter2')
285
+ end
286
+ end
287
+
288
+ context 'when something fails after the lock is taken' do
289
+ it 'still records the run when flow-results is not a folder' do
290
+ File.write(File.join(dir, 'flow-results'), '')
291
+
292
+ _output, code = run_flow('ok')
293
+
294
+ expect(code).not_to eq(0)
295
+ expect(log('ok').last).to include('status' => 'setup_error', 'results_dir' => nil)
296
+ end
297
+
298
+ it 'skips an env line that is not UTF-8 and runs the flow' do
299
+ write_env_file("RAIDER_SECRET_TOKEN=hunter2\nBAD=\xFF\n".b, 0o600)
300
+
301
+ output, code = run_flow('ok')
302
+
303
+ expect(code).to eq(0)
304
+ expect(output).to include('.raider/flows/env line 2 ignored', 'secret loaded=true')
305
+ end
306
+ end
307
+
308
+ it 'refuses a flow named env, whose log folder would be the secrets file' do
309
+ output, code = run_flow('env')
310
+
311
+ expect(code).not_to eq(0)
312
+ expect(output).to include('invalid flow name "env"')
313
+ expect(File).not_to exist(File.join(dir, '.raider/flows/env'))
314
+ end
315
+
316
+ it 'leaves no lock, record or results for rspec --dry-run' do
317
+ Open3.capture3(clean_env, RbConfig.ruby, '-S', 'rspec', '--dry-run', 'flows/ok_flow.rb', chdir: dir)
318
+
319
+ expect([log('ok'), File.exist?(File.join(dir, 'flow-results'))]).to eq([[], false])
320
+ end
321
+
322
+ it 'never lets the env file set RAIDER_FLOW_* and clamps the attempt to 1' do
323
+ write_env_file("RAIDER_FLOW_TRIGGER=schedule\n", 0o600)
324
+
325
+ output, = run_flow('ok', 'RAIDER_FLOW_ATTEMPT' => '0')
326
+
327
+ expect(output).to include('.raider/flows/env line 1 ignored')
328
+ expect(log('ok').last).to include('trigger' => 'manual', 'attempt' => 1)
329
+ end
330
+
331
+ it 'ignores a FIFO env file without waiting for a writer' do
332
+ FileUtils.mkdir_p(File.join(dir, '.raider/flows'))
333
+ File.mkfifo(File.join(dir, '.raider/flows/env'), 0o600)
334
+
335
+ output, code = Timeout.timeout(60) { run_flow('ok') }
336
+
337
+ expect(code).to eq(0)
338
+ expect(output).to include('ignored .raider/flows/env: it must be a regular file you own')
339
+ end
340
+
341
+ it 'redacts the longest secret first' do
342
+ write_env_file("RAIDER_SECRET_A=hunt\nRAIDER_SECRET_TOKEN=hunter2\n", 0o600)
343
+
344
+ run_flow('leaky')
345
+
346
+ expect(log('leaky').last['failed_step']['title']).to eq('types [secret] somewhere')
347
+ end
348
+
349
+ it 'redacts the value in use when the shell already set the key' do
350
+ write_env_file("RAIDER_SECRET_TOKEN=hunter2\n", 0o600)
351
+
352
+ run_flow('leaky', 'RAIDER_SECRET_TOKEN' => 'shell-secret')
353
+
354
+ expect(log('leaky').last['failed_step']['title']).to eq('types [secret] somewhere')
355
+ end
356
+
357
+ it 'never prunes through a linked flow-results folder' do
358
+ elsewhere = File.join(dir, 'elsewhere')
359
+ Array.new(25) { |i| FileUtils.mkdir_p(File.join(elsewhere, format('20200101T0000%02dZ', i))) }
360
+ FileUtils.mkdir_p(File.join(dir, 'flow-results'))
361
+ File.symlink(elsewhere, File.join(dir, 'flow-results/ok'))
362
+
363
+ run_flow('ok')
364
+
365
+ expect(Dir.children(elsewhere).grep(/\A2020/).size).to eq(25)
366
+ end
367
+
368
+ it 'keeps the last 50 log lines and the last 20 results folders, and only stamp-named folders' do
369
+ FileUtils.mkdir_p(File.join(dir, '.raider/flows/ok'))
370
+ File.write(File.join(dir, '.raider/flows/ok/runs.jsonl'), Array.new(60) { |i| "#{JSON.generate('n' => i)}\n" }.join)
371
+ old = Array.new(25) { |i| format('flow-results/ok/20200101T0000%02dZ', i) }
372
+ (old + ['flow-results/ok/keep-me']).each { |folder| FileUtils.mkdir_p(File.join(dir, folder)) }
373
+
374
+ run_flow('ok')
375
+
376
+ lines = log('ok')
377
+ stamped = Dir.children(File.join(dir, 'flow-results/ok')).grep(/\A\d{8}T/)
378
+ expect([lines.size, lines.first['n'], lines.last['status']]).to eq([50, 11, 'completed'])
379
+ expect(stamped.size).to eq(20)
380
+ expect(stamped).to include(File.basename(lines.last['results_dir']))
381
+ expect(File).to be_directory(File.join(dir, 'flow-results/ok/keep-me'))
382
+ end
383
+ end