From c0a08d16aeb89a3fd1303c0223be861ae56271ee Mon Sep 17 00:00:00 2001 From: John Costa Date: Sun, 8 Mar 2026 21:44:19 -0700 Subject: [PATCH 1/2] Replace database assertions with page-based checks in court report spec Remove ActiveRecord precondition/postcondition checks and the wait_for_report_attachment polling helper from the court report generation test. Use Capybara's built-in wait/retry to assert button state, download icon, and spinner visibility instead. This eliminates race conditions on CI where the DB check could execute before the browser action completed. --- spec/support/case_court_report_helpers.rb | 13 ------------- spec/system/case_court_reports/index_spec.rb | 16 ++++++---------- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/spec/support/case_court_report_helpers.rb b/spec/support/case_court_report_helpers.rb index 60ce101df1..40b4f4d65d 100644 --- a/spec/support/case_court_report_helpers.rb +++ b/spec/support/case_court_report_helpers.rb @@ -21,17 +21,4 @@ def open_case_select2_dropdown # Wait for the dropdown to appear expect(page).to have_css(".select2-dropdown", visible: :visible) end - - # Polls the database until the casa_case has an ActiveStorage court_report attached. - # This is used after clicking 'Generate Report' to wait for the background job to complete. - def wait_for_report_attachment(casa_case, timeout: 5) - casa_case.reload # Ensure we have the latest record - - Timeout.timeout(timeout) do - until casa_case.court_reports.attached? - sleep 0.2 - casa_case.reload - end - end - end end diff --git a/spec/system/case_court_reports/index_spec.rb b/spec/system/case_court_reports/index_spec.rb index c7ef0507eb..4fbbf4cb93 100644 --- a/spec/system/case_court_reports/index_spec.rb +++ b/spec/system/case_court_reports/index_spec.rb @@ -148,13 +148,10 @@ end # NOTE: select by option VALUE (stable), stub `window.open` to capture the download URL, - # wait for the ActiveStorage attachment, and assert button state + opened URL. + # wait for the button to re-enable (page-level signal), and assert UI state + opened URL. it "generates and attaches a report on success", :aggregate_failures, :js do # rubocop:disable RSpec/ExampleLength transition_case = casa_cases.detect(&:in_transition_age?) - # Precondition: no report attached - expect(transition_case.court_reports.attached?).to be false - # Stub window.open so we can capture the download URL in the browser page.execute_script(<<~JS) window.__last_opened_url = null; @@ -171,13 +168,12 @@ # Button should be disabled while processing expect(page).to have_selector("#btnGenerateReport[disabled]") - # Wait for the controller to attach the file (allow longer timeout on CI) - wait_for_report_attachment(transition_case, timeout: 10) - transition_case.reload + # Wait for the button to re-enable (report generated successfully) + expect(page).not_to have_selector("#btnGenerateReport[disabled]", wait: 10) - # Postcondition: report attached and button re-enabled - expect(transition_case.court_reports.attached?).to be true - expect(page).not_to have_selector("#btnGenerateReport[disabled]", visible: :all) + # Verify the UI reflects a successful generation + expect(page).to have_selector("#btnGenerateReport .lni-download", visible: :visible) + expect(page).to have_selector("#spinner", visible: :hidden) # Verify the browser attempted to open the generated .docx link opened_url = page.evaluate_script("window.__last_opened_url") From d4bd23ce55303e3a2c7bff689a182ff600e840ec Mon Sep 17 00:00:00 2001 From: John Costa Date: Sun, 8 Mar 2026 21:47:47 -0700 Subject: [PATCH 2/2] Update test description to reflect page-based assertions The test no longer verifies ActiveStorage attachment directly, so rename from "generates and attaches" to "generates a report and opens the download link" to match what is actually asserted. --- spec/system/case_court_reports/index_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/system/case_court_reports/index_spec.rb b/spec/system/case_court_reports/index_spec.rb index 4fbbf4cb93..3ee997293a 100644 --- a/spec/system/case_court_reports/index_spec.rb +++ b/spec/system/case_court_reports/index_spec.rb @@ -149,7 +149,7 @@ # NOTE: select by option VALUE (stable), stub `window.open` to capture the download URL, # wait for the button to re-enable (page-level signal), and assert UI state + opened URL. - it "generates and attaches a report on success", :aggregate_failures, :js do # rubocop:disable RSpec/ExampleLength + it "generates a report and opens the download link on success", :aggregate_failures, :js do # rubocop:disable RSpec/ExampleLength transition_case = casa_cases.detect(&:in_transition_age?) # Stub window.open so we can capture the download URL in the browser