# frozen_string_literal: true require "rails_helper" RSpec.describe Streams::QuietHours do def with_env(vars) previous = vars.keys.index_with { |k| ENV[k] } vars.each { |k, v| ENV[k] = v } yield ensure previous.each { |k, v| v.nil? ? ENV.delete(k) : ENV[k] = v } end it "is active inside the Rome window" do with_env( "STREAM_AUTOSCALE_QUIET_HOURS" => "02:00-07:00", "STREAM_AUTOSCALE_QUIET_TZ" => "Europe/Rome" ) do now = Time.find_zone!("Europe/Rome").local(2026, 9, 5, 3, 30) expect(described_class.active?(now: now)).to eq(true) end end it "is inactive outside the window" do with_env( "STREAM_AUTOSCALE_QUIET_HOURS" => "02:00-07:00", "STREAM_AUTOSCALE_QUIET_TZ" => "Europe/Rome" ) do now = Time.find_zone!("Europe/Rome").local(2026, 9, 5, 10, 0) expect(described_class.active?(now: now)).to eq(false) end end it "can be disabled with off" do with_env("STREAM_AUTOSCALE_QUIET_HOURS" => "off") do now = Time.find_zone!("Europe/Rome").local(2026, 9, 5, 3, 0) expect(described_class.active?(now: now)).to eq(false) end end end