discourse/plugins/discourse-ai/spec/system/ai_usage_spec.rb
Keegan George e6e538ee8e
FIX: AI usage doesn't refresh custom date ranges correctly (#34816)
## 🔍 Overview 
Fixes custom date range functionality in AI usage admin page where:
  - Users experienced visual glitches when selecting dates
  - Date picker would show incorrect dates after refresh
  - Users had to refresh multiple times to get correct date range

  **Changes:**
- Added fromDate/toDate getters with fallback logic for preview
functionality
  - Updated DateTimeInputRange to use preview-aware getters
  - Clear temporary dates after refresh to prevent stale state
  - Added system test to prevent regression

  The date picker now immediately shows selected dates (preview) and
  maintains correct values after refresh, eliminating user confusion.

This addresses the exact issue reported in [the meta
topic](https://meta.discourse.org/t/discourse-ai-admin-usage-page-custom-date-range-issues/378959)
and provides a clean solution with test coverage to prevent regression.
2025-09-16 10:16:01 -07:00

38 lines
1.2 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe "AI Usage Admin Page", type: :system do
fab!(:admin)
before do
enable_current_plugin
sign_in(admin)
end
context "when using custom date range functionality" do
it "allows selecting custom date range without JavaScript errors" do
visit "/admin/plugins/discourse-ai/ai-usage"
expect(page).to have_css(".ai-usage")
# Click custom date button to show date picker
find(".ai-usage__period-buttons .btn-default:last-child").click
expect(page).to have_css(".ai-usage__custom-date-pickers")
# Set dates
date_inputs = all(".ai-usage__custom-date-pickers input[type='date']")
date_inputs[0].set("2025-07-01")
date_inputs[1].set("2025-07-31")
# Verify dates are set correctly (preview functionality)
expect(date_inputs[0].value).to eq("2025-07-01")
expect(date_inputs[1].value).to eq("2025-07-31")
# Click refresh - this used to cause visual glitches and date reversion
find(".ai-usage__custom-date-pickers .btn", text: I18n.t("js.refresh")).click
# Wait for any potential async operations
sleep(1)
expect(page).to have_css(".ai-usage__summary")
end
end
end