A platform for community discussion. Free, open, simple.
DEV: Fix flaky DST handling tests in discourse-calendar (#36848)
Régis Hanold5b0251f0053
diff --git a/plugins/discourse-calendar/spec/system/post_event_spec.rb b/plugins/discourse-calendar/spec/system/post_event_spec.rbindex e4eeb7d24f7..d58dd9b6db4 100644--- a/plugins/discourse-calendar/spec/system/post_event_spec.rb+++ b/plugins/discourse-calendar/spec/system/post_event_spec.rb@@ -18,34 +18,28 @@ describe "Post event", type: :system doendcontext "with location" do+ let(:location) { "123 Main St, Brisbane, Australia http://example.com" }+it "can save a location" do- post =- PostCreator.create(- admin,- title: "My test meetup event",- raw: "[event start='2222-02-22 14:22']\n[/event]",- )+ title = "My test meetup event"+ raw = "[event start='2222-02-22 14:22']\n[/event]"+ post = PostCreator.create(admin, title:, raw:)visit(post.topic.url)post_event_page.edit- post_event_form_page.fill_location("123 Main St, Brisbane, Australia http://example.com")+ post_event_form_page.fill_location(location)post_event_form_page.submit- expect(post_event_page).to have_location(- "123 Main St, Brisbane, Australia http://example.com",- )+ expect(post_event_page).to have_location(location)expect(page).to have_css(".event-location a[href='http://example.com']")endendcontext "with description" doit "can save a description" do- post =- PostCreator.create(- admin,- title: "My test meetup event",- raw: "[event start='2222-02-22 14:22']\n[/event]",- )+ title = "My descriptive meetup event"+ raw = "[event start='2222-02-22 14:22']\n[/event]"+ post = PostCreator.create(admin, title:, raw:)visit(post.topic.url)post_event_page.edit@@ -60,36 +54,33 @@ describe "Post event", type: :system doexpect(page).to have_css(".event-description a[href='http://example.com']")end- # this is a flake cause strftim is calculated on server and client may have a- # slightly different time- xit "correctly builds a multiline description", timezone: "Europe/Paris" do- visit("/new-topic")+ it "correctly builds a multiline description" do+ timezone = "Europe/Paris"+ admin.user_option.update!(timezone:)++ time = Time.new(2025, 6, 15, 14, 30, 0, "+02:00")+ freeze_time(time)+ page.driver.with_playwright_page { |pw_page| pw_page.clock.install(time:) }- time = Time.now.strftime("%Y-%m-%d %H:%M")+ visit("/new-topic")+ find(".toolbar-menu__options-trigger").click+ click_button(I18n.t("js.discourse_post_event.builder_modal.attach"))+ post_event_form_page.fill_description("foo\nbar").fill_timezone(timezone).submit- EXPECTED_BBCODE = <<~EVENT- [event start="#{time}" status="public" timezone="Europe/Paris"]+ expect(composer).to have_value <<~EVENT.strip+ [event start="2025-06-15 14:30" status="public" timezone="#{timezone}"]foobar[/event]EVENT-- find(".toolbar-menu__options-trigger").click- click_button(I18n.t("js.discourse_post_event.builder_modal.attach"))- post_event_form_page.fill_description("foo\nbar").fill_timezone("Europe/Paris").submit-- expect(composer).to have_value(EXPECTED_BBCODE.strip)endendcontext "with max attendees" doit "updates the going button label from Full after toggling" do- post =- PostCreator.create(- admin,- title: "Max attendees event",- raw: "[event status='public' start='2222-02-22 14:22' max-attendees='1']\n[/event]",- )+ title = "My test meetup event"+ raw = "[event status='public' start='2222-02-22 14:22' max-attendees='1']\n[/event]"+ post = PostCreator.create(admin, title:, raw:)visit(post.topic.url)@@ -136,13 +127,12 @@ describe "Post event", type: :system dopw_page.clock.install(time: Time.new(2025, 6, 5, 22, 0, 0))end- post =- PostCreator.create(- admin,- title: "My test meetup event",- raw:- "[event showLocalTime='true' start='2025-09-07 17:30' start='2025-09-07 18:30' timezone='Europe/Paris']\n[/event]",- )+ title = "My FR test meetup event"+ raw = <<~MD+ [event showLocalTime='true' start='2025-09-07 17:30' timezone='Europe/Paris']+ [/event]+ MD+ post = PostCreator.create(admin, title:, raw:)visit(post.topic.url)@@ -151,12 +141,9 @@ describe "Post event", type: :system doendit "shows correct date" do- post =- PostCreator.create(- admin,- title: "My test meetup event",- raw: "[event timezone='Japan' showLocalTime='true' start='2222-02-22 14:22']\n[/event]",- )+ title = "My JP test meetup event"+ raw = "[event timezone='Japan' showLocalTime='true' start='2222-02-22 14:22']\n[/event]"+ post = PostCreator.create(admin, title:, raw:)visit(post.topic.url)@@ -165,12 +152,9 @@ describe "Post event", type: :system doendit "safely renders event name" do- post =- PostCreator.create(- admin,- title: "My test meetup event",- raw: "[event name=':cat: <script>alert(1);</script>' start='2222-02-22 00:00']\n[/event]",- )+ title = "My XSS test meetup event"+ raw = "[event name=':cat: <script>alert(1);</script>' start='2222-02-22 00:00']\n[/event]"+ post = PostCreator.create(admin, title:, raw:)visit(post.topic.url)@@ -206,12 +190,10 @@ describe "Post event", type: :system doendit "does not show participants button when event is standalone" do- post =- PostCreator.create(- admin,- title: "My test meetup event",- raw: "[event name='cool-event' status='standalone' start='2222-02-22 00:00' ]\n[/event]",- )+ title = "My standalone event"+ raw = "[event name='standalone-event' status='standalone' start='2222-02-22 00:00']\n[/event]"+ post = PostCreator.create(admin, title:, raw:)+visit(post.topic.url)post_event_page.open_more_menu@@ -219,12 +201,9 @@ describe "Post event", type: :system doendit "does not show 'send pm' button to the user who created the event" do- post =- PostCreator.create(- admin,- title: "My test meetup event",- raw: "[event name='cool-event' status='public' start='2222-02-22 00:00' ]\n[/event]",- )+ title = "My test meetup event"+ raw = "[event name='cool-event' status='public' start='2222-02-22 00:00']\n[/event]"+ post = PostCreator.create(admin, title:, raw:)visit(post.topic.url)post_event_page.going.open_more_menu@@ -233,13 +212,12 @@ describe "Post event", type: :system doendit "shows '-' for expired recurring events instead of dates" do- post =- PostCreator.create!(- admin,- title: "An expired recurring event",- raw:- "[event start='2024-01-01 10:00' recurrenceUntil='2025-07-31' recurrence='every_week']\n[/event]",- )+ title = "An expired recurring event"+ raw = <<~MD+ [event start='2024-01-01 10:00' recurrenceUntil='2025-07-31' recurrence='every_week']+ [/event]+ MD+ post = PostCreator.create!(admin, title:, raw:)visit(post.topic.url)@@ -250,117 +228,97 @@ describe "Post event", type: :system doendcontext "with DST handling for recurring events" do+ fab!(:eu_viewer) do+ user = Fabricate(:user)+ user.user_option.update!(timezone: "Europe/Paris")+ user+ end++ fab!(:us_viewer) do+ user = Fabricate(:user)+ user.user_option.update!(timezone: "America/New_York")+ user+ end++ def verify_event_time(viewer:, time:, expected_month: nil, expected_day: nil, expected_time:)+ freeze_time(time) do+ title = "Weekly recurring event #{SecureRandom.hex(4)}"+ raw = <<~MD+ [event start='2025-10-15 11:00' timezone='America/New_York' recurrence='every_week']+ [/event]+ MD+ post = PostCreator.create!(admin, title:, raw:)++ event = DiscoursePostEvent::Event.find_by(post:)+ event.set_next_date++ sign_in(viewer)++ # Install Playwright clock **after** sign_in (which may navigate) but before+ # visiting the topic, so moment() calls use the frozen time+ page.driver.with_playwright_page { |pw_page| pw_page.clock.install(time:) }++ visit(post.topic.url)++ expect(page).to have_css(".event-date .month", text: expected_month) if expected_month+ expect(page).to have_css(".event-date .day", text: expected_day) if expected_day+ expect(page).to have_css(".discourse-local-date", text: expected_time)+ end+ end+context "when viewer is in Europe/Paris timezone", timezone: "Europe/Paris" do- fab!(:viewer) do- user = Fabricate(:user)- user.user_option.update!(timezone: "Europe/Paris")- user+ it "shows correct time before any DST transition" do+ # Oct 14, 2025 - Both Europe and US are in summer time+ # 11:00 AM EDT = 15:00 UTC = 17:00 CEST = 5:00 PM+ verify_event_time(+ viewer: eu_viewer,+ time: Time.new(2025, 10, 14, 10, 0, 0, "+02:00"),+ expected_month: "OCT",+ expected_day: "15",+ expected_time: "5:00 PM",+ )+ end++ it "shows correct time after Europe DST ends but before US DST ends" do+ # Oct 28, 2025 - Europe CET (UTC+1), US still EDT (UTC-4)+ # 11:00 AM EDT = 15:00 UTC = 16:00 CET = 4:00 PM+ verify_event_time(+ viewer: eu_viewer,+ time: Time.new(2025, 10, 28, 10, 0, 0, "+01:00"),+ expected_month: "OCT",+ expected_day: "29",+ expected_time: "4:00 PM",+ )end- it "maintains wall clock time (11:00 AM) in event timezone across all DST transitions" do- # Before any DST- freeze_time(Time.new(2025, 10, 14, 10, 0, 0, "+02:00")) do- post =- PostCreator.create!(- admin,- title: "Weekly recurring event across DST",- raw:- "[event start='2025-10-15 11:00' timezone='America/New_York' recurrence='every_week']\n[/event]",- )-- event = DiscoursePostEvent::Event.find_by(post: post)- event.set_next_date- sign_in(viewer)- visit(post.topic.url)-- expect(page).to have_css(".event-date .month", text: "OCT")- expect(page).to have_css(".event-date .day", text: "15")- expect(page).to have_css(".discourse-local-date", text: "5:00 PM")- end-- # Test 2: After Europe DST- freeze_time(Time.new(2025, 10, 28, 10, 0, 0, "+01:00")) do- post =- PostCreator.create!(- admin,- title: "Weekly recurring event 2",- raw:- "[event start='2025-10-15 11:00' timezone='America/New_York' recurrence='every_week']\n[/event]",- )-- event = DiscoursePostEvent::Event.find_by(post: post)- event.set_next_date- sign_in(viewer)- visit(post.topic.url)-- expect(page).to have_css(".event-date .month", text: "OCT")- expect(page).to have_css(".event-date .day", text: "29")- # This is the period where the time CHANGES for European viewers- expect(page).to have_css(".discourse-local-date", text: "4:00 PM")- end-- # Test 3: After both DST transitions- freeze_time(Time.new(2025, 11, 4, 10, 0, 0, "+01:00")) do- post =- PostCreator.create!(- admin,- title: "Weekly recurring event 3",- raw:- "[event start='2025-10-15 11:00' timezone='America/New_York' recurrence='every_week']\n[/event]",- )-- event = DiscoursePostEvent::Event.find_by(post: post)- event.set_next_date- sign_in(viewer)- visit(post.topic.url)-- expect(page).to have_css(".event-date .month", text: "NOV")- expect(page).to have_css(".event-date .day", text: "5")- expect(page).to have_css(".discourse-local-date", text: "5:00 PM")- end+ it "shows correct time after both DST transitions" do+ # Nov 4, 2025 - Europe CET (UTC+1), US EST (UTC-5)+ # 11:00 AM EST = 16:00 UTC = 17:00 CET = 5:00 PM+ verify_event_time(+ viewer: eu_viewer,+ time: Time.new(2025, 11, 4, 10, 0, 0, "+01:00"),+ expected_month: "NOV",+ expected_day: "5",+ expected_time: "5:00 PM",+ )endendcontext "when viewer is in America/New_York timezone", timezone: "America/New_York" do- it "event stays at wall clock time (11:00 AM) in its own timezone throughout DST" do- us_viewer = Fabricate(:user)- us_viewer.user_option.update!(timezone: "America/New_York")-- # Before US DST ends- freeze_time(Time.new(2025, 10, 28, 10, 0, 0, "-04:00")) do- post =- PostCreator.create!(- admin,- title: "Weekly recurring event 4",- raw:- "[event start='2025-10-15 11:00' timezone='America/New_York' recurrence='every_week']\n[/event]",- )-- event = DiscoursePostEvent::Event.find_by(post: post)- event.set_next_date- sign_in(us_viewer)- visit(post.topic.url)-- expect(page).to have_css(".discourse-local-date", text: "11:00 AM")- end-- # After US DST ends- freeze_time(Time.new(2025, 11, 4, 10, 0, 0, "-05:00")) do- post =- PostCreator.create!(- admin,- title: "Weekly recurring event 5",- raw:- "[event start='2025-10-15 11:00' timezone='America/New_York' recurrence='every_week']\n[/event]",- )-- event = DiscoursePostEvent::Event.find_by(post: post)- event.set_next_date- sign_in(us_viewer)- visit(post.topic.url)-- expect(page).to have_css(".discourse-local-date", text: "11:00 AM")- end+ it "shows 11:00 AM before US DST ends" do+ verify_event_time(+ viewer: us_viewer,+ time: Time.new(2025, 10, 28, 10, 0, 0, "-04:00"),+ expected_time: "11:00 AM",+ )+ end++ it "shows 11:00 AM after US DST ends" do+ verify_event_time(+ viewer: us_viewer,+ time: Time.new(2025, 11, 4, 10, 0, 0, "-05:00"),+ expected_time: "11:00 AM",+ )endendend