story_player 0.1.1
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 +7 -0
- data/CHANGELOG.md +14 -0
- data/CODE_OF_CONDUCT.md +26 -0
- data/CONTRIBUTING.md +23 -0
- data/GOVERNANCE.md +25 -0
- data/LICENSE +21 -0
- data/README.md +208 -0
- data/SECURITY.md +25 -0
- data/docs/host-integration.md +75 -0
- data/exe/story-player +67 -0
- data/lib/story_player/annotations.rb +54 -0
- data/lib/story_player/assets/accessibility_path.js +168 -0
- data/lib/story_player/assets/annotations.css +229 -0
- data/lib/story_player/assets/annotations.js +471 -0
- data/lib/story_player/assets/element_picker.js +209 -0
- data/lib/story_player/assets/offline_player.html +191 -0
- data/lib/story_player/assets/stage.css +65 -0
- data/lib/story_player/assets/stage.js +195 -0
- data/lib/story_player/bridge.rb +97 -0
- data/lib/story_player/capybara.rb +23 -0
- data/lib/story_player/history.rb +51 -0
- data/lib/story_player/integration.rb +40 -0
- data/lib/story_player/minitest.rb +45 -0
- data/lib/story_player/note_store.rb +126 -0
- data/lib/story_player/playback.rb +138 -0
- data/lib/story_player/playlist.rb +101 -0
- data/lib/story_player/polyrun.rb +23 -0
- data/lib/story_player/recording.rb +132 -0
- data/lib/story_player/review.rb +59 -0
- data/lib/story_player/rspec.rb +39 -0
- data/lib/story_player/run.rb +118 -0
- data/lib/story_player/session.rb +73 -0
- data/lib/story_player/version.rb +5 -0
- data/lib/story_player.rb +58 -0
- data/story_player.gemspec +50 -0
- metadata +174 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "story_player/integration"
|
|
4
|
+
require "minitest/test"
|
|
5
|
+
|
|
6
|
+
module StoryPlayer
|
|
7
|
+
module Minitest
|
|
8
|
+
include Integration
|
|
9
|
+
|
|
10
|
+
def self.included(base)
|
|
11
|
+
base.prepend(Lifecycle)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
module Lifecycle
|
|
15
|
+
def before_teardown
|
|
16
|
+
outcome = if skipped?
|
|
17
|
+
:skipped
|
|
18
|
+
else
|
|
19
|
+
passed? ? :passed : :failed
|
|
20
|
+
end
|
|
21
|
+
story_player_complete!(outcome)
|
|
22
|
+
super
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def run
|
|
26
|
+
return super unless StoryPlayer.mode
|
|
27
|
+
|
|
28
|
+
result = nil
|
|
29
|
+
with_demo_stage(title: "#{self.class}##{name}", page: -> { demo_stage_page }, **demo_stage_options) do
|
|
30
|
+
result = super
|
|
31
|
+
result.passed?
|
|
32
|
+
end
|
|
33
|
+
result
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def demo_stage_page
|
|
38
|
+
page
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def demo_stage_options
|
|
42
|
+
{}
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "json"
|
|
6
|
+
require "securerandom"
|
|
7
|
+
require "tempfile"
|
|
8
|
+
require "time"
|
|
9
|
+
require "yaml"
|
|
10
|
+
|
|
11
|
+
module StoryPlayer
|
|
12
|
+
class NoteStore
|
|
13
|
+
ANCHOR_STRATEGIES = %w[identifier semantic position].freeze
|
|
14
|
+
ANCHOR_HINTS = %w[name native duplicate identifier hidden].freeze
|
|
15
|
+
|
|
16
|
+
def initialize(root:, title:)
|
|
17
|
+
@title = title.to_s
|
|
18
|
+
slug = @title.gsub(/[^a-z0-9-]/i, "-")[0, 60]
|
|
19
|
+
@directory = File.join(root, "#{slug}-#{digest(@title)}")
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def list(page:, surface: nil, origin: nil)
|
|
23
|
+
base = directory(page, surface, origin)
|
|
24
|
+
Dir.glob("*.md", base:).reverse_each.filter_map do |name|
|
|
25
|
+
file = File.join(base, name)
|
|
26
|
+
header, comment = File.read(file).delete_prefix("---\n").split("\n---\n", 2)
|
|
27
|
+
next unless comment
|
|
28
|
+
|
|
29
|
+
fields = header.start_with?("{") ? JSON.parse(header) : YAML.safe_load(header)
|
|
30
|
+
next unless fields.is_a?(Hash) && fields["created_at"].is_a?(String)
|
|
31
|
+
|
|
32
|
+
fields.slice("created_at", "element").merge("comment" => comment.delete_prefix("\n").delete_suffix("\n"))
|
|
33
|
+
rescue JSON::ParserError, Psych::Exception, Errno::ENOENT, Errno::EISDIR, ArgumentError
|
|
34
|
+
next
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def save(payload, page:, surface: nil, origin: nil)
|
|
39
|
+
comment = payload["comment"]
|
|
40
|
+
raise ArgumentError, "Invalid comment" unless valid_text?(comment, 10_000)
|
|
41
|
+
|
|
42
|
+
now = Time.now.utc
|
|
43
|
+
metadata = {
|
|
44
|
+
"page" => page, "surface" => surface, "origin" => origin,
|
|
45
|
+
"created_at" => now.iso8601(6), "demo" => @title
|
|
46
|
+
}.compact
|
|
47
|
+
metadata["element"] = element_reference(payload["element"]) unless payload["element"].nil?
|
|
48
|
+
|
|
49
|
+
destination = directory(page, surface, origin)
|
|
50
|
+
FileUtils.mkdir_p(destination, mode: 0o700)
|
|
51
|
+
filename = "#{now.strftime("%Y%m%dT%H%M%S%6NZ")}-#{SecureRandom.hex(6)}.md"
|
|
52
|
+
Tempfile.create(".annotation", destination) do |file|
|
|
53
|
+
file.write("---\n#{JSON.generate(metadata)}\n---\n\n#{comment}\n")
|
|
54
|
+
file.close
|
|
55
|
+
File.rename(file.path, File.join(destination, filename))
|
|
56
|
+
end
|
|
57
|
+
true
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def directory(page, surface, origin)
|
|
63
|
+
File.join(@directory, digest([origin, [page, surface].join("\n")].compact.join("\n")))
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def digest(value)
|
|
67
|
+
Digest::SHA256.hexdigest(value)[0, 16]
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def valid_text?(value, limit)
|
|
71
|
+
value.is_a?(String) && value.length <= limit && !value.strip.empty?
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def element_reference(element)
|
|
75
|
+
raise ArgumentError, "Invalid element" unless element.is_a?(Hash)
|
|
76
|
+
|
|
77
|
+
reference = element.slice("label", "selector")
|
|
78
|
+
unless valid_text?(reference["label"], 160) && valid_text?(reference["selector"], 2048)
|
|
79
|
+
raise ArgumentError, "Invalid element"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
reference["accessibility"] = accessibility_reference(element["accessibility"]) if element.key?("accessibility")
|
|
83
|
+
|
|
84
|
+
%w[x y].each do |axis|
|
|
85
|
+
next unless element.key?(axis)
|
|
86
|
+
|
|
87
|
+
value = element[axis]
|
|
88
|
+
raise ArgumentError, "Invalid position" unless value.is_a?(Numeric) && value.between?(0, 1)
|
|
89
|
+
|
|
90
|
+
reference[axis] = value.to_f
|
|
91
|
+
end
|
|
92
|
+
reference
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def accessibility_reference(anchor)
|
|
96
|
+
raise ArgumentError, "Invalid accessibility path" unless anchor.is_a?(Hash)
|
|
97
|
+
|
|
98
|
+
path = anchor["path"]
|
|
99
|
+
hints = anchor["hints"]
|
|
100
|
+
strategy = anchor["strategy"]
|
|
101
|
+
unless path.is_a?(Array) && path.length.between?(1, 8) &&
|
|
102
|
+
hints.is_a?(Array) && hints.length <= ANCHOR_HINTS.length && (hints - ANCHOR_HINTS).empty? &&
|
|
103
|
+
ANCHOR_STRATEGIES.include?(strategy)
|
|
104
|
+
raise ArgumentError, "Invalid accessibility path"
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
reference = {"path" => path.map { |segment| accessibility_segment(segment) }, "strategy" => strategy, "hints" => hints.uniq}
|
|
108
|
+
if strategy == "position"
|
|
109
|
+
key = anchor["key"]
|
|
110
|
+
raise ArgumentError, "Invalid position key" unless key.is_a?(String) && key.match?(/\A[a-f0-9]{24}\z/)
|
|
111
|
+
|
|
112
|
+
reference["key"] = key
|
|
113
|
+
end
|
|
114
|
+
reference
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def accessibility_segment(segment)
|
|
118
|
+
unless segment.is_a?(Hash) && segment["role"].is_a?(String) && segment["role"].match?(/\A[a-z][a-z0-9-]{0,63}\z/) &&
|
|
119
|
+
segment["name"].is_a?(String) && segment["name"].length <= 160
|
|
120
|
+
raise ArgumentError, "Invalid accessibility path segment"
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
segment.slice("role", "name")
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module StoryPlayer
|
|
4
|
+
module Playback
|
|
5
|
+
SPEEDS = [0.25, 0.5, 1, 1.5, 2].freeze
|
|
6
|
+
PLAYBACK_SETTINGS = %w[pause speed].freeze
|
|
7
|
+
PAUSED = "Paused. You can use this page."
|
|
8
|
+
FINAL = "Final step. Explore the result, or finish the demo."
|
|
9
|
+
RECOVERING = "Restoring the demo step…"
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def browser_message(_location, operation, data)
|
|
14
|
+
return @view if operation == "state"
|
|
15
|
+
view = @view
|
|
16
|
+
return false unless view && data.is_a?(Hash) && data["gate"] == view[:gate]
|
|
17
|
+
return false if view[:mode] == "recovering"
|
|
18
|
+
|
|
19
|
+
value = data["value"]
|
|
20
|
+
accepted = if @mode == :review
|
|
21
|
+
review_command?(operation, value)
|
|
22
|
+
else
|
|
23
|
+
playback_command?(operation, value)
|
|
24
|
+
end
|
|
25
|
+
return false unless accepted
|
|
26
|
+
|
|
27
|
+
@commands.push([data["gate"], operation, value], true)
|
|
28
|
+
true
|
|
29
|
+
rescue ThreadError, ClosedQueueError
|
|
30
|
+
false
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def playback_command?(operation, value)
|
|
34
|
+
case operation
|
|
35
|
+
when "seek" then value.is_a?(Integer)
|
|
36
|
+
when "speed" then SPEEDS.include?(value)
|
|
37
|
+
else %w[play pause stop back forward restore finish].include?(operation)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def wait_for_viewer
|
|
42
|
+
idle_deadline = now + @idle_timeout
|
|
43
|
+
advance_at = now + @step_delay / @speed
|
|
44
|
+
loop do
|
|
45
|
+
raise Error, "The browser closed while waiting for the demo" if @commands.closed?
|
|
46
|
+
|
|
47
|
+
remaining = idle_deadline - now
|
|
48
|
+
raise Error, "The demo paused for too long" unless remaining.positive?
|
|
49
|
+
|
|
50
|
+
playing = @mode == :playing
|
|
51
|
+
timeout = playing ? [advance_at - now, remaining, 0.2].min : remaining
|
|
52
|
+
gate, command, value = @commands.pop(timeout: [timeout, 0].max)
|
|
53
|
+
if playing && @page.url != @checkpoints.last.url
|
|
54
|
+
@mode = :paused
|
|
55
|
+
publish(PAUSED)
|
|
56
|
+
end
|
|
57
|
+
if command && (gate == @gate || PLAYBACK_SETTINGS.include?(command))
|
|
58
|
+
return if handle(command, value)
|
|
59
|
+
|
|
60
|
+
idle_deadline = now + @idle_timeout
|
|
61
|
+
advance_at = now + @step_delay / @speed
|
|
62
|
+
elsif @mode == :playing && now >= advance_at
|
|
63
|
+
return if recover(@checkpoints.length - 1, :playing)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def handle(command, value)
|
|
69
|
+
last = @checkpoints.length - 1
|
|
70
|
+
case command
|
|
71
|
+
when "play" then return recover(last, :playing) unless @final
|
|
72
|
+
when "finish" then return @final && recover(last, :paused)
|
|
73
|
+
when "forward"
|
|
74
|
+
return recover(last, :paused) if @cursor == last
|
|
75
|
+
|
|
76
|
+
recover(@cursor + 1, :paused)
|
|
77
|
+
when "back" then recover([@cursor - 1, 0].max, :paused)
|
|
78
|
+
when "stop" then recover(0, :paused)
|
|
79
|
+
when "restore" then recover(@cursor, :paused)
|
|
80
|
+
when "seek" then recover(value, :paused) if value.between?(0, last)
|
|
81
|
+
when "pause"
|
|
82
|
+
@mode = :paused
|
|
83
|
+
publish(PAUSED)
|
|
84
|
+
when "speed"
|
|
85
|
+
@speed = value
|
|
86
|
+
publish(playback_status)
|
|
87
|
+
end
|
|
88
|
+
false
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def recover(index, mode)
|
|
92
|
+
@mode = :recovering
|
|
93
|
+
publish(RECOVERING)
|
|
94
|
+
restore_checkpoint(@checkpoints.fetch(index))
|
|
95
|
+
@cursor = index
|
|
96
|
+
@mode = mode
|
|
97
|
+
publish(playback_status)
|
|
98
|
+
true
|
|
99
|
+
rescue => error
|
|
100
|
+
raise if @commands.closed?
|
|
101
|
+
|
|
102
|
+
warn "Story Player recovery failed (#{error.class})"
|
|
103
|
+
@mode = :paused
|
|
104
|
+
publish("Could not restore this step. Try again, or return to the demo page.")
|
|
105
|
+
false
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def restore_checkpoint(checkpoint)
|
|
109
|
+
@page.goto(checkpoint.url, waitUntil: "domcontentloaded") if @page.url != checkpoint.url
|
|
110
|
+
checkpoint.restore&.call
|
|
111
|
+
if @page.url != checkpoint.url || !@bridge.evaluate("([x, y]) => { window.scrollTo(x, y); return true; }", arg: checkpoint.scroll)
|
|
112
|
+
raise Error, "The checkpoint page did not finish restoring"
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def playback_status
|
|
117
|
+
return FINAL if @final && @cursor == @checkpoints.length - 1
|
|
118
|
+
|
|
119
|
+
(@mode == :playing) ? "Playing" : PAUSED
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def publish(status)
|
|
123
|
+
@revision += 1
|
|
124
|
+
count = @checkpoints.length
|
|
125
|
+
@view = {
|
|
126
|
+
title: @title, label: @cursor && @checkpoints.fetch(@cursor).label, cursor: @cursor || -1, count:,
|
|
127
|
+
total: @final ? count : [count + 1, @expected_total.to_i].max, final: @final,
|
|
128
|
+
mode: @mode.to_s, speed: @speed, status:, gate: @gate, revision: @revision,
|
|
129
|
+
outcome: @outcome, playlist: @playlist, history: !!@history
|
|
130
|
+
}.freeze
|
|
131
|
+
@bridge.evaluate('(view) => window[Symbol.for("demoStage.player")]?.render(view)', arg: @view)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def now
|
|
135
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "json"
|
|
5
|
+
require "tmpdir"
|
|
6
|
+
require "yaml"
|
|
7
|
+
|
|
8
|
+
module StoryPlayer
|
|
9
|
+
class Playlist
|
|
10
|
+
OUTCOMES = %w[passed failed skipped].freeze
|
|
11
|
+
Story = Data.define(:title, :label, :command, :env)
|
|
12
|
+
|
|
13
|
+
class Selection
|
|
14
|
+
def self.current
|
|
15
|
+
return unless ENV["DEMO_STAGE_PLAYLIST"] && ENV["DEMO_STAGE_SELECTION"]
|
|
16
|
+
|
|
17
|
+
new(JSON.parse(ENV.fetch("DEMO_STAGE_PLAYLIST")), ENV.fetch("DEMO_STAGE_SELECTION"))
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
attr_reader :view
|
|
21
|
+
|
|
22
|
+
def initialize(view, filename)
|
|
23
|
+
@view = view
|
|
24
|
+
@filename = filename
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def finish(outcome, index)
|
|
28
|
+
File.write(@filename, JSON.generate(outcome: outcome, index: index))
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.load(path)
|
|
33
|
+
new(YAML.safe_load_file(path))
|
|
34
|
+
rescue Psych::Exception, SystemCallError => error
|
|
35
|
+
raise Error, "Could not read playlist: #{error.message}"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def initialize(stories, directory: Dir.pwd)
|
|
39
|
+
raise Error, "A playlist needs at least one story" unless stories.is_a?(Array) && !stories.empty?
|
|
40
|
+
|
|
41
|
+
@stories = stories.map { |story| parse(story) }
|
|
42
|
+
@directory = directory
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def run
|
|
46
|
+
outcomes = Array.new(@stories.length)
|
|
47
|
+
titles = @stories.map(&:label)
|
|
48
|
+
index = 0
|
|
49
|
+
Dir.mktmpdir("demo-stage-playlist-") do |directory|
|
|
50
|
+
while index
|
|
51
|
+
story = @stories.fetch(index)
|
|
52
|
+
response = File.join(directory, "selection.json")
|
|
53
|
+
FileUtils.rm_f(response)
|
|
54
|
+
env = story.env.merge(
|
|
55
|
+
"DEMO_STAGE_MODE" => "live", "DEMO_STAGE_TITLE" => story.title, "HEADFUL" => "1",
|
|
56
|
+
"DEMO_STAGE_PLAYLIST" => JSON.generate(titles:, outcomes:, current: index),
|
|
57
|
+
"DEMO_STAGE_SELECTION" => response
|
|
58
|
+
)
|
|
59
|
+
success = system(env, [story.command.first, story.command.first], *story.command.drop(1), chdir: @directory)
|
|
60
|
+
return 128 + $?.termsig if $?.signaled?
|
|
61
|
+
|
|
62
|
+
selection = read_selection(response)
|
|
63
|
+
outcomes[index] = success ? (selection&.fetch("outcome") || "passed") : "failed"
|
|
64
|
+
$stdout.puts "#{story.label} · #{outcomes[index].capitalize}"
|
|
65
|
+
index = selection ? selection["index"] : ((index + 1) if index + 1 < titles.length)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
outcomes.include?("failed") ? 1 : 0
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
def parse(story)
|
|
74
|
+
raise Error, "Each playlist entry needs a title and command" unless story.is_a?(Hash)
|
|
75
|
+
|
|
76
|
+
title = story["title"]
|
|
77
|
+
label = story.fetch("label", title)
|
|
78
|
+
command = story["command"]
|
|
79
|
+
env = story.fetch("env", {})
|
|
80
|
+
unless title.is_a?(String) && !title.empty? && label.is_a?(String) && command.is_a?(Array) && !command.empty? &&
|
|
81
|
+
command.all? { |argument| argument.is_a?(String) } && !command.first.empty? &&
|
|
82
|
+
env.is_a?(Hash) && env.all? { |key, value| key.is_a?(String) && value.is_a?(String) }
|
|
83
|
+
raise Error, "Each story needs a title, a command argument list, and optional string environment values"
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
Story.new(title, label, command, env)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def read_selection(path)
|
|
90
|
+
return unless File.file?(path)
|
|
91
|
+
|
|
92
|
+
response = JSON.parse(File.read(path))
|
|
93
|
+
return unless response.is_a?(Hash) && OUTCOMES.include?(response["outcome"])
|
|
94
|
+
|
|
95
|
+
index = response["index"]
|
|
96
|
+
response if index.nil? || (index.is_a?(Integer) && index.between?(0, @stories.length - 1))
|
|
97
|
+
rescue JSON::ParserError
|
|
98
|
+
nil
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "story_player/integration"
|
|
4
|
+
|
|
5
|
+
module StoryPlayer
|
|
6
|
+
module Polyrun
|
|
7
|
+
def self.install!(group, title:, page: nil, **options)
|
|
8
|
+
unless group.respond_to?(:around)
|
|
9
|
+
raise Error, "StoryPlayer::Polyrun requires Polyrun Quick around hooks (Polyrun >= 3)"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
group.around do |example|
|
|
13
|
+
extend Integration
|
|
14
|
+
|
|
15
|
+
source = page ? -> { instance_exec(&page) } : -> { self.page }
|
|
16
|
+
with_demo_stage(title:, page: source, **options) do
|
|
17
|
+
example.call
|
|
18
|
+
true
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "json"
|
|
6
|
+
require "tempfile"
|
|
7
|
+
require "tmpdir"
|
|
8
|
+
require "time"
|
|
9
|
+
|
|
10
|
+
module StoryPlayer
|
|
11
|
+
class Recording
|
|
12
|
+
def self.latest(root, title: nil)
|
|
13
|
+
pointer = File.join(root, title ? key(title) : "latest")
|
|
14
|
+
name = begin
|
|
15
|
+
File.read(pointer).strip if File.file?(pointer)
|
|
16
|
+
rescue Errno::ENOENT, Errno::EISDIR
|
|
17
|
+
nil
|
|
18
|
+
end
|
|
19
|
+
if name&.start_with?("recording-") && !name.include?("\0") && File.basename(name) == name
|
|
20
|
+
path = File.join(root, name)
|
|
21
|
+
return path if File.file?(File.join(path, "index.html"))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
entry = history(root, title: title && title.to_s).first
|
|
25
|
+
return File.join(root, entry.fetch("id")) if entry
|
|
26
|
+
|
|
27
|
+
raise Error, "No demo recording is available"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.history(root, title: nil)
|
|
31
|
+
entries = Dir.glob("recording-*", base: root, sort: false).filter_map do |id|
|
|
32
|
+
path = File.join(root, id, "manifest.json")
|
|
33
|
+
source = File.read(path, encoding: Encoding::UTF_8)
|
|
34
|
+
next unless source.valid_encoding?
|
|
35
|
+
|
|
36
|
+
manifest = JSON.parse(source)
|
|
37
|
+
next unless manifest.is_a?(Hash) && manifest["chapter"].is_a?(String)
|
|
38
|
+
next if title && manifest["chapter"] != title
|
|
39
|
+
|
|
40
|
+
next unless File.file?(File.join(root, id, "index.html"))
|
|
41
|
+
|
|
42
|
+
{
|
|
43
|
+
"id" => id, "chapter" => manifest.fetch("chapter"),
|
|
44
|
+
"outcome" => manifest["outcome"].to_s.empty? ? "unknown" : manifest["outcome"].to_s,
|
|
45
|
+
"recordedAt" => manifest["recordedAt"].is_a?(String) ? manifest["recordedAt"] : File.mtime(path).utc.iso8601
|
|
46
|
+
}
|
|
47
|
+
rescue JSON::ParserError, Errno::ENOENT, Errno::ENOTDIR, Errno::EISDIR
|
|
48
|
+
next
|
|
49
|
+
end
|
|
50
|
+
entries.sort_by { |entry| [entry.fetch("recordedAt"), entry.fetch("id")] }.reverse
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def self.key(title)
|
|
54
|
+
"chapter-#{Digest::SHA256.hexdigest(title.to_s)[0, 20]}"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def initialize(page:, title:, root:)
|
|
58
|
+
@page = page
|
|
59
|
+
@browser = StoryPlayer.playwright_page(page, required: false)
|
|
60
|
+
@title = title.to_s
|
|
61
|
+
@root = File.expand_path(root)
|
|
62
|
+
@frames = []
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def checkpoint(label, wait_for_load: true, timeout: nil, **)
|
|
66
|
+
prepare_directory
|
|
67
|
+
name = format("%04d.png", @frames.length + 1)
|
|
68
|
+
path = File.join(@directory, name)
|
|
69
|
+
if @browser
|
|
70
|
+
@browser.wait_for_load_state(state: "load", timeout:) if wait_for_load
|
|
71
|
+
@browser.screenshot(path:, timeout:, style: "#demo-stage-player, #demo-stage-annotations { visibility: hidden !important }")
|
|
72
|
+
elsif @page.respond_to?(:save_screenshot)
|
|
73
|
+
@page.save_screenshot(path)
|
|
74
|
+
else
|
|
75
|
+
@page.screenshot(path:)
|
|
76
|
+
end
|
|
77
|
+
raise Error, "Story Player could not save a screenshot" unless File.size?(path)
|
|
78
|
+
|
|
79
|
+
@frames << {"file" => name, "label" => label.to_s}
|
|
80
|
+
rescue
|
|
81
|
+
FileUtils.rm_f(path) if path
|
|
82
|
+
raise
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def finish(outcome: :passed)
|
|
86
|
+
return @directory if @complete
|
|
87
|
+
prepare_directory
|
|
88
|
+
|
|
89
|
+
manifest = {
|
|
90
|
+
"chapter" => @title, "frames" => @frames, "startPaused" => true, "defaultSpeed" => 1,
|
|
91
|
+
"outcome" => outcome.to_s, "recordedAt" => Time.now.utc.iso8601(6)
|
|
92
|
+
}
|
|
93
|
+
serialized = JSON.generate(manifest)
|
|
94
|
+
template = File.read(File.join(ASSETS, "offline_player.html"))
|
|
95
|
+
File.write(File.join(@directory, "index.html"), template.sub("__DEMO_MANIFEST__") { serialized.gsub("<", "\\u003c") })
|
|
96
|
+
write_manifest(serialized)
|
|
97
|
+
@complete = true
|
|
98
|
+
point(self.class.key(@title))
|
|
99
|
+
point("latest")
|
|
100
|
+
@directory
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def close
|
|
104
|
+
FileUtils.remove_entry(@directory) if @directory && !@complete
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
private
|
|
108
|
+
|
|
109
|
+
def prepare_directory
|
|
110
|
+
return if @directory
|
|
111
|
+
|
|
112
|
+
FileUtils.mkdir_p(@root, mode: 0o700)
|
|
113
|
+
@directory = Dir.mktmpdir("recording-", @root)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def write_manifest(manifest)
|
|
117
|
+
Tempfile.create(".manifest-", @directory) do |file|
|
|
118
|
+
file.puts(manifest)
|
|
119
|
+
file.close
|
|
120
|
+
File.rename(file.path, File.join(@directory, "manifest.json"))
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def point(name)
|
|
125
|
+
Tempfile.create(".pointer-", @root) do |file|
|
|
126
|
+
file.puts(File.basename(@directory))
|
|
127
|
+
file.close
|
|
128
|
+
File.rename(file.path, File.join(@root, name))
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module StoryPlayer
|
|
4
|
+
module Review
|
|
5
|
+
def reviewable?
|
|
6
|
+
!@page.closed? && @bridge.trusted_page?
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def review(outcome, playlist:, history:)
|
|
10
|
+
return if @page.closed?
|
|
11
|
+
|
|
12
|
+
@outcome = outcome.to_s
|
|
13
|
+
@playlist = playlist
|
|
14
|
+
@history = history
|
|
15
|
+
@gate += 1
|
|
16
|
+
@mode = :review
|
|
17
|
+
start
|
|
18
|
+
publish(review_message)
|
|
19
|
+
|
|
20
|
+
until @commands.closed?
|
|
21
|
+
gate, command, value = @commands.pop(timeout: @idle_timeout)
|
|
22
|
+
break unless command
|
|
23
|
+
next unless gate == @gate
|
|
24
|
+
|
|
25
|
+
case command
|
|
26
|
+
when "close-story" then break
|
|
27
|
+
when "select-story" then return value
|
|
28
|
+
when "history" then show_history
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
nil
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def review_message
|
|
37
|
+
if @outcome == "failed"
|
|
38
|
+
"Failure details appear on exit."
|
|
39
|
+
else
|
|
40
|
+
"Inspect the page or leave a note."
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def review_command?(operation, value)
|
|
45
|
+
case operation
|
|
46
|
+
when "close-story" then true
|
|
47
|
+
when "history" then !!@history
|
|
48
|
+
when "select-story" then @playlist && value.is_a?(Integer) && value.between?(0, @playlist.fetch("titles").length - 1)
|
|
49
|
+
else false
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def show_history
|
|
54
|
+
@history.call
|
|
55
|
+
rescue
|
|
56
|
+
publish("Could not open run history. Try again.")
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "story_player/integration"
|
|
4
|
+
require "rspec/core"
|
|
5
|
+
|
|
6
|
+
module StoryPlayer
|
|
7
|
+
module RSpec
|
|
8
|
+
def self.install!(filter = {demo: true}, automatic: false, page: nil, title: nil, options: nil, mode: -> { StoryPlayer.mode })
|
|
9
|
+
::RSpec.configure do |config|
|
|
10
|
+
config.include Integration, filter
|
|
11
|
+
|
|
12
|
+
config.prepend_after(:each, filter) do |example|
|
|
13
|
+
outcome = if example.exception
|
|
14
|
+
:failed
|
|
15
|
+
elsif example.skipped? || example.pending?
|
|
16
|
+
:skipped
|
|
17
|
+
else
|
|
18
|
+
:passed
|
|
19
|
+
end
|
|
20
|
+
story_player_complete!(outcome)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
config.around(filter) do |example|
|
|
24
|
+
if (active_mode = mode.call)
|
|
25
|
+
name = title ? title.call(example) : example.full_description
|
|
26
|
+
settings = options ? options.call(example) : {}
|
|
27
|
+
source = page ? -> { instance_exec(&page) } : -> { self.page }
|
|
28
|
+
with_demo_stage(title: name, page: source, automatic:, mode: active_mode, **settings) do
|
|
29
|
+
example.run
|
|
30
|
+
example.exception.nil?
|
|
31
|
+
end
|
|
32
|
+
else
|
|
33
|
+
example.run
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|