studio-engine 0.143.2 → 0.144.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.
@@ -0,0 +1,236 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Studio::SystemTestCase: the one browser-test base every host app inherits.
4
+ #
5
+ # A host's test/application_system_test_case.rb becomes:
6
+ #
7
+ # require "test_helper"
8
+ # require "studio/system_test_case"
9
+ #
10
+ # class ApplicationSystemTestCase < Studio::SystemTestCase
11
+ # driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1000]
12
+ # end
13
+ #
14
+ # It owns the harness bugs that were fixed app by app, so a fix ships once, in
15
+ # the gem, to every app. Detail and the customization API: docs/SYSTEM_TESTS.md.
16
+ #
17
+ # lib/studio.rb does NOT require this file. It needs Capybara and
18
+ # selenium-webdriver, which a host bundles in its test group and the gem does
19
+ # not declare. The host requires it from its system-test base only.
20
+ require "action_dispatch/system_test_case"
21
+ require "capybara/selenium/driver"
22
+ require "uri"
23
+
24
+ module Studio
25
+ class SystemTestCase < ActionDispatch::SystemTestCase
26
+ # The harness hooks this base runs, by name. A subclass opts out of one by
27
+ # name, with a reason (skip_harness_hook); every other hook still runs.
28
+ HOOKS = {
29
+ reset_window: "setup: resize the window to desktop_size. The browser outlives " \
30
+ "the test, so a resize leaks into the next test (commercial-welding " \
31
+ "went red under one seed).",
32
+ restore_script_timeout: "around evaluate_async_script: give back the script " \
33
+ "timeout. Capybara 3.40.0 sets it to default_max_wait_time " \
34
+ "(2s) and never restores it (voicebox ContrastSystemTest).",
35
+ clear_emulation: "teardown: clear the DevTools device-metrics and emulated-media " \
36
+ "overrides (phone_viewport!, a dark-mode or reduced-motion test), " \
37
+ "so neither leaks into the next test."
38
+ }.freeze
39
+
40
+ # Selenium's stock read budget is Net::HTTP's 60s. A cold Chrome launch on a
41
+ # slow CI runner can pass it, and the run dies with Net::ReadTimeout in
42
+ # whichever test the seed put first (hub PR #2494).
43
+ DEFAULT_READ_TIMEOUT = 180
44
+ DEFAULT_DESKTOP_SIZE = [1400, 1000].freeze
45
+
46
+ # Thread-local switch the restore_script_timeout hook reads. The prepend on
47
+ # Capybara's driver is global; this key makes the opt-out per test class.
48
+ SCRIPT_TIMEOUT_KEY = :studio_system_test_restore_script_timeout
49
+
50
+ # The window every test starts in. Set it through driven_by's screen_size,
51
+ # so the browser's launch size and the reset size are one number.
52
+ class_attribute :desktop_size, instance_accessor: false, default: DEFAULT_DESKTOP_SIZE
53
+ # The phone viewport phone_viewport! and assert_viewport_width default to.
54
+ class_attribute :phone_width, instance_accessor: false, default: 390
55
+ class_attribute :phone_height, instance_accessor: false, default: 844
56
+ # { hook name => reason } for the hooks this class opted out of.
57
+ class_attribute :skipped_harness_hooks, instance_accessor: false, default: {}.freeze
58
+
59
+ class << self
60
+ # Rails' driven_by, with two additions. screen_size also becomes
61
+ # desktop_size, the size reset_window restores. A Selenium driver gets a
62
+ # 180s read timeout unless options: names its own :timeout.
63
+ def driven_by(driver = :selenium, using: :headless_chrome, screen_size: DEFAULT_DESKTOP_SIZE,
64
+ options: {}, &capabilities)
65
+ self.desktop_size = Array(screen_size).dup.freeze
66
+ options = { timeout: DEFAULT_READ_TIMEOUT }.merge(options) if driver.to_sym == :selenium
67
+ super(driver, using: using, screen_size: screen_size, options: options, &capabilities)
68
+ end
69
+
70
+ # Opt this class (and its subclasses) out of one harness hook. The name
71
+ # must be in HOOKS and the reason must say why; both are logged once, so a
72
+ # skipped fix is never silent.
73
+ def skip_harness_hook(name, reason:)
74
+ name = name.to_sym
75
+ unless HOOKS.key?(name)
76
+ raise ArgumentError, "unknown harness hook #{name.inspect}; the hooks are #{HOOKS.keys.map(&:inspect).join(', ')}"
77
+ end
78
+ raise ArgumentError, "skip_harness_hook #{name.inspect} needs a reason:" if reason.to_s.strip.empty?
79
+
80
+ self.skipped_harness_hooks = skipped_harness_hooks.merge(name => reason.to_s.strip).freeze
81
+ warn "[Studio::SystemTestCase] #{self.name || inspect} skips the #{name} hook: #{reason.to_s.strip}"
82
+ end
83
+
84
+ def harness_hook?(name)
85
+ !skipped_harness_hooks.key?(name.to_sym)
86
+ end
87
+
88
+ # Read by the driver prepend below. nil (no Studio test running on this
89
+ # thread) means on: the restore is a pure bug fix.
90
+ def restore_script_timeout?
91
+ Thread.current[SCRIPT_TIMEOUT_KEY] != false
92
+ end
93
+ end
94
+
95
+ # Capybara 3.40.0's evaluate_async_script sets the browser's script timeout
96
+ # to Capybara.default_max_wait_time (2s) and never sets it back. The browser
97
+ # outlives the test, so every later execute_script in the run has 2s, not the
98
+ # driver's 30s. This gives back the timeout it borrowed (voicebox PR #35).
99
+ module RestoresScriptTimeout
100
+ def evaluate_async_script(script, *args)
101
+ return super unless Studio::SystemTestCase.restore_script_timeout?
102
+
103
+ timeouts = browser.manage.timeouts
104
+ previous = timeouts.script_timeout
105
+ super
106
+ ensure
107
+ timeouts.script_timeout = previous if previous
108
+ end
109
+ end
110
+ Capybara::Selenium::Driver.prepend(RestoresScriptTimeout)
111
+
112
+ # Capybara 3.40.0's Chrome and Edge reset! end with
113
+ # execute_cdp("Storage.clearDataForOrigin", origin: "*", ...)
114
+ # Chrome 158 refuses "*" ("invalid argument: * is not a valid URL"), so every
115
+ # system test's teardown errors. This names the real origins instead: each
116
+ # http(s) origin the driver visited since the last reset, the page it is on
117
+ # at reset, and Capybara.app_host. Cookies are still cleared for every
118
+ # origin, as "*" did, through Network.clearBrowserCookies. A pure bug fix,
119
+ # so no opt-out (task engine-reset-fix-chrome-158).
120
+ module ClearsStorageByOrigin
121
+ ANY_ORIGIN = "*"
122
+ CLEAR_COMMAND = "Storage.clearDataForOrigin"
123
+
124
+ # The origin Chrome keys storage by ("http://127.0.0.1:4567"), or nil for
125
+ # a URL with none (about:blank, data:, a bare path).
126
+ def self.origin_of(url)
127
+ uri = URI.parse(url.to_s)
128
+ return unless %w[http https].include?(uri.scheme) && uri.host
129
+
130
+ port = uri.port == uri.default_port ? "" : ":#{uri.port}"
131
+ "#{uri.scheme}://#{uri.host}#{port}"
132
+ rescue URI::InvalidURIError
133
+ nil
134
+ end
135
+
136
+ def visit(url)
137
+ studio_note_origin { url }
138
+ super
139
+ end
140
+
141
+ def reset!
142
+ studio_note_origin { @browser.current_url } if @browser
143
+ super
144
+ ensure
145
+ @studio_storage_origins = nil
146
+ end
147
+
148
+ def studio_storage_origins
149
+ origins = (@studio_storage_origins || []).dup
150
+ app_origin = ClearsStorageByOrigin.origin_of(Capybara.app_host)
151
+ origins << app_origin if app_origin
152
+ origins.uniq
153
+ end
154
+
155
+ private
156
+
157
+ def execute_cdp(cmd, params = {})
158
+ return super unless cmd == CLEAR_COMMAND && params[:origin] == ANY_ORIGIN
159
+
160
+ super("Network.clearBrowserCookies") if params[:storageTypes].to_s.split(",").include?("cookies")
161
+ studio_storage_origins.each { |origin| super(cmd, params.merge(origin: origin)) }
162
+ nil
163
+ end
164
+
165
+ # Best effort: a read that fails (an open alert at reset) notes nothing,
166
+ # and never stops the reset.
167
+ def studio_note_origin
168
+ origin = ClearsStorageByOrigin.origin_of(yield)
169
+ (@studio_storage_origins ||= []) << origin if origin
170
+ rescue StandardError
171
+ nil
172
+ end
173
+ end
174
+ Capybara::Selenium::Driver::ChromeDriver.prepend(ClearsStorageByOrigin)
175
+ Capybara::Selenium::Driver::EdgeDriver.prepend(ClearsStorageByOrigin)
176
+
177
+ driven_by :selenium, using: :headless_chrome, screen_size: DEFAULT_DESKTOP_SIZE
178
+
179
+ # The base's setup runs before any setup a subclass declares, so an app's
180
+ # own setup always starts in the reset window.
181
+ setup do
182
+ Thread.current[SCRIPT_TIMEOUT_KEY] = self.class.harness_hook?(:restore_script_timeout)
183
+ reset_window if self.class.harness_hook?(:reset_window) && browser_window?
184
+ end
185
+
186
+ # Teardown callbacks run in reverse: a subclass's teardown runs first, then
187
+ # this one.
188
+ teardown do
189
+ clear_emulation if self.class.harness_hook?(:clear_emulation) && devtools?
190
+ ensure
191
+ Thread.current[SCRIPT_TIMEOUT_KEY] = nil
192
+ end
193
+
194
+ def reset_window
195
+ page.driver.browser.manage.window.resize_to(*self.class.desktop_size)
196
+ end
197
+
198
+ # A real phone-width viewport. Headless Chrome will not size its window
199
+ # under 500px, so resize_to(390, ...) gives a 500px page and a "390px" check
200
+ # passes at the wrong width. DevTools device-metrics emulation sets the
201
+ # viewport itself. After each visit, before you measure, call
202
+ # assert_viewport_width. The clear_emulation hook clears it in teardown.
203
+ def phone_viewport!(width = self.class.phone_width, height = self.class.phone_height)
204
+ page.driver.browser.execute_cdp("Emulation.setDeviceMetricsOverride",
205
+ width: width, height: height, deviceScaleFactor: 1, mobile: true)
206
+ end
207
+
208
+ # Reads clientWidth, not innerWidth: under mobile emulation innerWidth grows
209
+ # to fit a page that overflows, so a check against it passes vacuously.
210
+ # Phone widths only: a desktop Linux Chrome takes 15px of clientWidth for
211
+ # its scrollbar.
212
+ def assert_viewport_width(width = self.class.phone_width)
213
+ assert_equal width, evaluate_script("document.documentElement.clientWidth"), "the viewport is #{width}px"
214
+ end
215
+
216
+ # Clears both DevTools overrides a test may set: the phone viewport and the
217
+ # emulated media (prefers-color-scheme, prefers-reduced-motion, print).
218
+ def clear_emulation
219
+ page.driver.browser.execute_cdp("Emulation.clearDeviceMetricsOverride")
220
+ page.driver.browser.execute_cdp("Emulation.setEmulatedMedia", media: "", features: [])
221
+ end
222
+
223
+ private
224
+
225
+ # A real browser window: Selenium, not rack_test. A subclass driven by
226
+ # rack_test skips the browser hooks without opting out.
227
+ def browser_window?
228
+ page.driver.respond_to?(:browser) && page.driver.browser.respond_to?(:manage)
229
+ end
230
+
231
+ # A Chromium browser that takes DevTools commands.
232
+ def devtools?
233
+ browser_window? && page.driver.browser.respond_to?(:execute_cdp)
234
+ end
235
+ end
236
+ end
@@ -1,3 +1,3 @@
1
1
  module Studio
2
- VERSION = "0.143.2"
2
+ VERSION = "0.144.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: studio-engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.143.2
4
+ version: 0.144.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex McRitchie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-11 00:00:00.000000000 Z
11
+ date: 2026-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -434,6 +434,7 @@ files:
434
434
  - app/assets/stylesheets/studio/sticky_table_header.css
435
435
  - app/assets/tailwind/studio_engine/engine-motion.css
436
436
  - app/assets/tailwind/studio_engine/engine.css
437
+ - app/assets/tailwind/studio_engine/host-variant-25559-c64c1299.css
437
438
  - app/assets/tailwind/studio_engine/host.css
438
439
  - app/assets/tailwind/studio_engine/theme.css
439
440
  - app/components/previews/studio/badge_component_preview.rb
@@ -1022,6 +1023,7 @@ files:
1022
1023
  - lib/studio/survey.rb
1023
1024
  - lib/studio/survey/breakdown.rb
1024
1025
  - lib/studio/survey/export.rb
1026
+ - lib/studio/system_test_case.rb
1025
1027
  - lib/studio/tailwind_entry.rb
1026
1028
  - lib/studio/theme_resolver.rb
1027
1029
  - lib/studio/tricks.rb