A platform for community discussion. Free, open, simple.
FEATURE: time-window condition (#41952)
Joffrey JAFFEUX8e7f778102a8
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/timezone-control.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/timezone-control.gjsnew file mode 100644index 00000000000..4bd24d96a5b--- /dev/null+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/timezone-control.gjs@@ -0,0 +1,35 @@+import Component from "@glimmer/component";+import { hash } from "@ember/helper";+import TimezoneInput from "discourse/select-kit/components/timezone-input";+import { propertySelectNoneKey } from "../../../lib/workflows/property-engine";+import ExpressionWrapper from "./expression-wrapper";++export default class TimezoneControl extends Component {+ get none() {+ return (+ this.args.schema?.control_options?.none ||+ propertySelectNoneKey(this.args.nodeDefinition, this.args.fieldName)+ );+ }++ get clearable() {+ return !this.args.schema?.required;+ }++ <template>+ <ExpressionWrapper+ @field={{@field}}+ @schema={{@schema}}+ @supportsExpression={{@supportsExpression}}+ @placeholder={{@placeholder}}+ @dynamicValueHint={{@dynamicValueHint}}+ @session={{@session}}+ >+ <TimezoneInput+ @value={{@field.value}}+ @onChange={{@field.set}}+ @options={{hash none=this.none clearable=this.clearable}}+ />+ </ExpressionWrapper>+ </template>+}diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/field-control-registry.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/field-control-registry.jsindex d06005db618..0827fe7069d 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/field-control-registry.js+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/field-control-registry.js@@ -19,6 +19,7 @@ import NoticeControl from "../../components/workflows/configurators/notice-contrimport QueryParams from "../../components/workflows/configurators/query-params";import SelectControl from "../../components/workflows/configurators/select-control";import TagsControl from "../../components/workflows/configurators/tags-control";+import TimezoneControl from "../../components/workflows/configurators/timezone-control";import UrlPreview from "../../components/workflows/configurators/url-preview";import UserControl from "../../components/workflows/configurators/user-control";import UserOrGroupControl from "../../components/workflows/configurators/user-or-group-control";@@ -70,6 +71,8 @@ const FIELD_CONTROL_REGISTRY = {icon: { kind: "field", type: "icon", renderer: IconControl },checkbox: { kind: "field", type: "checkbox", renderer: DefaultInputControl },textarea: { kind: "field", type: "textarea", renderer: DefaultInputControl },+ time: { kind: "field", type: "input-time", renderer: DefaultInputControl },+ timezone: { kind: "field", type: "custom", renderer: TimezoneControl },default: {kind: "field",diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.ymlindex f9d0d669d8f..ea37b5c655f 100644--- a/plugins/discourse-workflows/config/locales/client.en.yml+++ b/plugins/discourse-workflows/config/locales/client.en.yml@@ -148,7 +148,7 @@ en:error_workflow_none: "None"no_error_workflows_available: "No workflows with an Error trigger node are available"timezone: "Timezone"- timezone_description: "Schedule triggers use this timezone. If unset, they use the site timezone."+ timezone_description: "Schedule triggers and time conditions use this timezone. If unset, they use the site timezone."danger_zone: "Danger zone"delete_description: "Permanently delete this workflow and all its executions. This action cannot be undone."versions:@@ -223,6 +223,7 @@ en:"trigger:manual": "Manual trigger""condition:if": "If""condition:filter": "Filter"+ "condition:time_window": "Time window""action:topic_tags": "Topic tags""action:code": "Code""action:topic": "Topic"@@ -272,6 +273,7 @@ en:"trigger:manual": "Fires when triggered manually by an admin""condition:if": "Branch the workflow based on a condition""condition:filter": "Continue only if conditions are met"+ "condition:time_window": "Branch the workflow based on the current day and time""action:topic_tags": "Add or remove tags on a topic""action:code": "Run custom JavaScript code""action:topic": "Create, retrieve, list, close, archive, or set custom fields on topics"@@ -800,6 +802,20 @@ en:limit_wait_time: "Limit wait time"timeout_amount: "Timeout after"timeout_unit: "Timeout unit"+ time_window:+ day_mode: "Days"+ day_mode_all: "All days"+ day_mode_weekdays: "Weekdays only"+ day_mode_weekends: "Weekends only"+ day_mode_custom: "Specific days"+ days: "Days of week"+ use_time_range: "Limit to a time range"+ start_time: "Start time"+ end_time: "End time"+ end_time_description: "The window ends just before this time. Choose an end earlier than the start to span midnight (e.g. 22:00 to 06:00)."+ timezone: "Timezone"+ timezone_description: "Defaults to the workflow timezone."+ timezone_none: "Workflow default"loop_over_items:batch_size: "Batch size"batch_size_description: "Number of items to process per iteration"diff --git a/plugins/discourse-workflows/config/locales/server.en.yml b/plugins/discourse-workflows/config/locales/server.en.ymlindex 79a84d40669..9efa957133c 100644--- a/plugins/discourse-workflows/config/locales/server.en.yml+++ b/plugins/discourse-workflows/config/locales/server.en.yml@@ -103,6 +103,14 @@ en:invalid_unit: "Invalid wait unit: %{unit}."invalid_timeout_unit: "Invalid timeout unit: %{unit}."timeout_amount_must_be_positive: "Timeout amount must be greater than 0."+ time_window:+ invalid_day_mode: "Unknown day mode: %{mode}."+ days_required: "At least one day must be selected."+ invalid_days: "Days must be values between 0 (Sunday) and 6 (Saturday)."+ invalid_time_range: "Start and end times must use the HH:MM format."+ start_end_equal: "Start and end times cannot be the same."+ invalid_time: "'%{field}' is not a valid HH:MM time: '%{value}'."+ invalid_timezone: "Invalid timezone: %{timezone}."modal:no_target_user: "No target user is available for the modal."user_not_found: "User '%{username}' not found."diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor.rbindex 77ec1f808e9..1964a75940d 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/executor.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor.rb@@ -560,6 +560,7 @@ module DiscourseWorkflowsresolver: resolver,vars: preloaded_vars,workflow: @workflow,+ workflow_version: @options.workflow_version,execution_id: @store.execution&.id,resume_token: @context.resume_token,node_id: node.id.to_s,diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rbindex b87e14dd200..955cb94cbef 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb@@ -112,6 +112,7 @@ module DiscourseWorkflowsresolver: nil,vars: nil,workflow: nil,+ workflow_version: nil,execution_id: nil,resume_token: nil,node_id: nil,@@ -145,6 +146,7 @@ module DiscourseWorkflows@resolver = resolver@vars = vars@workflow = workflow+ @workflow_version = workflow_version@execution_id = execution_id@resume_token = resume_token@node_id = node_id@@ -241,6 +243,13 @@ module DiscourseWorkflowsinput_items(input_index)end+ def get_timezone+ DiscourseWorkflows::WorkflowTimezone.for(+ workflow: @workflow,+ workflow_version: @workflow_version,+ )+ end+def continue_on_failon_error = @node_settings["onError"]return %w[continueRegularOutput continueErrorOutput].include?(on_error) if on_error.present?diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/time_window/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/time_window/v1.rbnew file mode 100644index 00000000000..b0ae26770e8--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/time_window/v1.rb@@ -0,0 +1,242 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module TimeWindow+ class V1 < NodeType+ WEEKDAY_OPTIONS = [1, 2, 3, 4, 5, 6, 0].freeze+ WEEKDAYS = (1..5).to_a.freeze+ WEEKENDS = [0, 6].freeze+ DAY_MODES = %w[all weekdays weekends custom].freeze+ TIME_FORMAT = /\A(\d{1,2}):(\d{2})\z/++ description(+ name: "condition:time_window",+ version: "1.0",+ defaults: {+ icon: "business-time",+ color: "blue",+ },+ outputs: [+ { key: "true", label_key: "discourse_workflows.branch.true" },+ { key: "false", label_key: "discourse_workflows.branch.false" },+ ],+ capabilities: {+ run_scope: "per_item",+ },+ output_contracts: [{ mode: :passthrough }, { mode: :passthrough }],+ properties: {+ day_mode: {+ type: :options,+ options: DAY_MODES,+ default: "all",+ no_data_expression: true,+ },+ days: {+ type: :multi_options,+ default: [1, 2, 3, 4, 5],+ options: WEEKDAY_OPTIONS,+ display_options: {+ show: {+ day_mode: %w[custom],+ },+ },+ control_options: {+ option_format: :weekday,+ },+ },+ use_time_range: {+ type: :boolean,+ default: false,+ },+ start_time: {+ type: :string,+ default: "09:00",+ display_options: {+ show: {+ use_time_range: [true],+ },+ },+ ui: {+ control: :time,+ },+ },+ end_time: {+ type: :string,+ default: "17:00",+ display_options: {+ show: {+ use_time_range: [true],+ },+ },+ ui: {+ control: :time,+ },+ },+ timezone: {+ type: :string,+ ui: {+ control: :timezone,+ },+ control_options: {+ none: "discourse_workflows.time_window.timezone_none",+ },+ },+ },+ )++ def self.validate_configuration(configuration, errors)+ config = (configuration || {}).deep_stringify_keys++ day_mode = config["day_mode"].presence || "all"+ if DAY_MODES.exclude?(day_mode.to_s)+ errors.add(+ :base,+ I18n.t("discourse_workflows.errors.time_window.invalid_day_mode", mode: day_mode),+ )+ end++ if day_mode.to_s == "custom"+ days = Array.wrap(config["days"])+ if days.empty?+ errors.add(:base, I18n.t("discourse_workflows.errors.time_window.days_required"))+ elsif days.any? { |day| !valid_wday?(day) }+ errors.add(:base, I18n.t("discourse_workflows.errors.time_window.invalid_days"))+ end+ end++ if ActiveModel::Type::Boolean.new.cast(config["use_time_range"])+ start_value = config["start_time"]+ end_value = config["end_time"]+ static_values = [start_value, end_value].reject { |value| expression?(value) }++ if static_values.any? { |value| parse_minutes(value).nil? }+ errors.add(:base, I18n.t("discourse_workflows.errors.time_window.invalid_time_range"))+ elsif static_values.size == 2 && parse_minutes(start_value) == parse_minutes(end_value)+ errors.add(:base, I18n.t("discourse_workflows.errors.time_window.start_end_equal"))+ end+ end++ timezone = config["timezone"].presence+ if timezone && !expression?(timezone) && !WorkflowTimezone.valid?(timezone)+ errors.add(+ :base,+ I18n.t("discourse_workflows.errors.time_window.invalid_timezone", timezone: timezone),+ )+ end+ end++ def self.expression?(value)+ value.is_a?(String) && value.start_with?("=")+ end++ def self.valid_wday?(value)+ (0..6).cover?(Integer(value))+ rescue ArgumentError, TypeError+ false+ end++ def self.parse_minutes(value)+ match = TIME_FORMAT.match(value.to_s.strip)+ return if match.nil?++ hour = match[1].to_i+ minute = match[2].to_i+ return if hour > 23 || minute > 59++ hour * 60 + minute+ end++ def execute(exec_ctx)+ now = Time.zone.now++ exec_ctx+ .input_items+ .each_with_index+ .partition { |_item, item_index| within_window?(exec_ctx, item_index, now) }+ .map { |items| items.map(&:first) }+ end++ private++ def within_window?(exec_ctx, item_index, now)+ local_now = now.in_time_zone(resolve_timezone(exec_ctx, item_index))+ exec_ctx.log.kv("local_time", local_now.strftime("%A %H:%M (%Z)")) if item_index.zero?+ day_match?(exec_ctx, item_index, local_now) &&+ time_match?(exec_ctx, item_index, local_now)+ end++ def day_match?(exec_ctx, item_index, local_now)+ mode = exec_ctx.get_node_parameter("day_mode", item_index, default: "all").to_s++ case mode+ when "all"+ true+ when "weekdays"+ WEEKDAYS.include?(local_now.wday)+ when "weekends"+ WEEKENDS.include?(local_now.wday)+ when "custom"+ Array+ .wrap(exec_ctx.get_node_parameter("days", item_index, default: []))+ .map(&:to_i)+ .include?(local_now.wday)+ else+ raise_node_error!(+ I18n.t("discourse_workflows.errors.time_window.invalid_day_mode", mode: mode),+ item_index: item_index,+ )+ end+ end++ def time_match?(exec_ctx, item_index, local_now)+ return true if !exec_ctx.get_node_parameter("use_time_range", item_index, default: false)++ start_minutes = minutes_of_day!(exec_ctx, "start_time", item_index)+ end_minutes = minutes_of_day!(exec_ctx, "end_time", item_index)+ current_minutes = local_now.hour * 60 + local_now.min++ if start_minutes < end_minutes+ current_minutes >= start_minutes && current_minutes < end_minutes+ elsif start_minutes > end_minutes+ current_minutes >= start_minutes || current_minutes < end_minutes+ else+ false+ end+ end++ def minutes_of_day!(exec_ctx, field, item_index)+ value = exec_ctx.get_node_parameter(field, item_index, default: nil)+ minutes = self.class.parse_minutes(value)++ if minutes.nil?+ raise_node_error!(+ I18n.t(+ "discourse_workflows.errors.time_window.invalid_time",+ field: field,+ value: value.to_s,+ ),+ item_index: item_index,+ )+ end++ minutes+ end++ def resolve_timezone(exec_ctx, item_index)+ timezone = exec_ctx.get_node_parameter("timezone", item_index, default: nil).to_s.presence+ return exec_ctx.get_timezone if timezone.nil?++ if !WorkflowTimezone.valid?(timezone)+ raise_node_error!(+ I18n.t("discourse_workflows.errors.time_window.invalid_timezone", timezone: timezone),+ item_index: item_index,+ )+ end++ timezone+ end+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb b/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rbindex 5ac61d2de6e..915bcdfdf62 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb@@ -111,6 +111,8 @@ module DiscourseWorkflowsselecttagstextarea+ time+ timezoneurl_previewuseruser_seen_trigger_optionsdiff --git a/plugins/discourse-workflows/plugin.rb b/plugins/discourse-workflows/plugin.rbindex 9d046ef9ef4..66d3b3b1809 100644--- a/plugins/discourse-workflows/plugin.rb+++ b/plugins/discourse-workflows/plugin.rb@@ -41,6 +41,7 @@ register_svg_icon "reply"register_svg_icon "triangle-exclamation"register_svg_icon "flag"register_svg_icon "clock"+register_svg_icon "business-time"register_svg_icon "dollar-sign"register_svg_icon "comments"register_svg_icon "pause"diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/time_window_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/time_window_spec.rbnew file mode 100644index 00000000000..a3e848f3006--- /dev/null+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/time_window_spec.rb@@ -0,0 +1,401 @@+# frozen_string_literal: true++RSpec.describe DiscourseWorkflows::Nodes::TimeWindow::V1 do+ WEDNESDAY_UTC = Time.utc(2026, 7, 22, 10, 0)+ SATURDAY_UTC = Time.utc(2026, 7, 25, 10, 0)++ def wrap_items(*jsons)+ jsons.map { |json| { "json" => json } }+ end++ describe "#execute" do+ context "with day modes" do+ it "routes every item to the true branch with the all mode" do+ freeze_time SATURDAY_UTC++ items = wrap_items({ "id" => 1 }, { "id" => 2 })+ result = execute_node_output(configuration: { "day_mode" => "all" }, input_items: items)++ expect(result[0]).to eq(items)+ expect(result[1]).to be_empty+ end++ it "defaults to the all mode when day_mode is missing" do+ freeze_time SATURDAY_UTC++ items = wrap_items({ "id" => 1 })+ result = execute_node_output(configuration: {}, input_items: items)++ expect(result[0]).to eq(items)+ end++ it "matches weekdays only" do+ config = { "day_mode" => "weekdays" }+ items = wrap_items({ "id" => 1 })++ freeze_time WEDNESDAY_UTC+ result = execute_node_output(configuration: config, input_items: items)+ expect(result[0]).to eq(items)++ freeze_time SATURDAY_UTC+ result = execute_node_output(configuration: config, input_items: items)+ expect(result[1]).to eq(items)+ end++ it "matches weekends only" do+ config = { "day_mode" => "weekends" }+ items = wrap_items({ "id" => 1 })++ freeze_time SATURDAY_UTC+ result = execute_node_output(configuration: config, input_items: items)+ expect(result[0]).to eq(items)++ freeze_time WEDNESDAY_UTC+ result = execute_node_output(configuration: config, input_items: items)+ expect(result[1]).to eq(items)+ end++ it "matches specific days in custom mode" do+ config = { "day_mode" => "custom", "days" => [3] }+ items = wrap_items({ "id" => 1 })++ freeze_time WEDNESDAY_UTC+ result = execute_node_output(configuration: config, input_items: items)+ expect(result[0]).to eq(items)++ freeze_time SATURDAY_UTC+ result = execute_node_output(configuration: config, input_items: items)+ expect(result[1]).to eq(items)+ end++ it "accepts custom days stored as strings" do+ freeze_time WEDNESDAY_UTC++ items = wrap_items({ "id" => 1 })+ config = { "day_mode" => "custom", "days" => %w[3] }+ result = execute_node_output(configuration: config, input_items: items)++ expect(result[0]).to eq(items)+ end++ it "routes everything to the false branch when custom days are empty" do+ freeze_time WEDNESDAY_UTC++ items = wrap_items({ "id" => 1 })+ config = { "day_mode" => "custom", "days" => [] }+ result = execute_node_output(configuration: config, input_items: items)++ expect(result[0]).to be_empty+ expect(result[1]).to eq(items)+ end++ it "raises on an unknown day mode" do+ freeze_time WEDNESDAY_UTC++ expect {+ execute_node_output(+ configuration: {+ "day_mode" => "banana",+ },+ input_items: wrap_items({ "id" => 1 }),+ )+ }.to raise_error(DiscourseWorkflows::NodeError, /banana/)+ end+ end++ context "with a time range" do+ it "ignores times when use_time_range is off" do+ freeze_time Time.utc(2026, 7, 22, 3, 0)++ items = wrap_items({ "id" => 1 })+ config = { "use_time_range" => false, "start_time" => "09:00", "end_time" => "17:00" }+ result = execute_node_output(configuration: config, input_items: items)++ expect(result[0]).to eq(items)+ end++ it "is start-inclusive and end-exclusive" do+ config = { "use_time_range" => true, "start_time" => "09:00", "end_time" => "17:00" }+ items = wrap_items({ "id" => 1 })++ { [8, 59] => 1, [9, 0] => 0, [16, 59] => 0, [17, 0] => 1 }.each do |(hour, minute), branch|+ freeze_time Time.utc(2026, 7, 22, hour, minute)+ result = execute_node_output(configuration: config, input_items: items)+ expect(result[branch]).to eq(items), "expected #{hour}:#{minute} on branch #{branch}"+ end+ end++ it "wraps past midnight when the end is before the start" do+ config = { "use_time_range" => true, "start_time" => "22:00", "end_time" => "06:00" }+ items = wrap_items({ "id" => 1 })++ {+ [23, 0] => 0,+ [3, 0] => 0,+ [22, 0] => 0,+ [12, 0] => 1,+ [6, 0] => 1,+ }.each do |(hour, minute), branch|+ freeze_time Time.utc(2026, 7, 22, hour, minute)+ result = execute_node_output(configuration: config, input_items: items)+ expect(result[branch]).to eq(items), "expected #{hour}:#{minute} on branch #{branch}"+ end+ end++ it "treats a midnight end as until the end of the day" do+ config = { "use_time_range" => true, "start_time" => "08:00", "end_time" => "00:00" }+ items = wrap_items({ "id" => 1 })++ freeze_time Time.utc(2026, 7, 22, 23, 59)+ result = execute_node_output(configuration: config, input_items: items)+ expect(result[0]).to eq(items)++ freeze_time Time.utc(2026, 7, 22, 7, 0)+ result = execute_node_output(configuration: config, input_items: items)+ expect(result[1]).to eq(items)+ end++ it "matches nothing when start and end are equal" do+ freeze_time Time.utc(2026, 7, 22, 9, 0)++ items = wrap_items({ "id" => 1 })+ config = { "use_time_range" => true, "start_time" => "09:00", "end_time" => "09:00" }+ result = execute_node_output(configuration: config, input_items: items)++ expect(result[1]).to eq(items)+ end+ end++ context "with timezones" do+ it "evaluates the window in the configured timezone" do+ # Wednesday 22:00 UTC is Thursday 07:00 in Tokyo.+ freeze_time Time.utc(2026, 7, 22, 22, 0)++ items = wrap_items({ "id" => 1 })+ config = {+ "use_time_range" => true,+ "start_time" => "06:00",+ "end_time" => "08:00",+ "timezone" => "Asia/Tokyo",+ }++ result = execute_node_output(configuration: config, input_items: items)+ expect(result[0]).to eq(items)++ result =+ execute_node_output(configuration: config.merge("timezone" => "UTC"), input_items: items)+ expect(result[1]).to eq(items)+ end++ it "flips the day of week with the timezone" do+ # Saturday 02:00 UTC is still Friday in Los Angeles.+ freeze_time Time.utc(2026, 7, 25, 2, 0)++ items = wrap_items({ "id" => 1 })+ config = { "day_mode" => "weekdays", "timezone" => "America/Los_Angeles" }++ result = execute_node_output(configuration: config, input_items: items)+ expect(result[0]).to eq(items)+ end++ it "falls back to the workflow timezone" do+ freeze_time Time.utc(2026, 7, 25, 2, 0)++ workflow =+ Fabricate(+ :discourse_workflows_workflow,+ settings: {+ "timezone" => "America/Los_Angeles",+ },+ )+ items = wrap_items({ "id" => 1 })+ config = { "day_mode" => "weekdays" }++ result = execute_node_output(configuration: config, input_items: items, workflow: workflow)+ expect(result[0]).to eq(items)+ end++ it "prefers the node timezone over the workflow timezone" do+ freeze_time Time.utc(2026, 7, 25, 2, 0)++ workflow =+ Fabricate(+ :discourse_workflows_workflow,+ settings: {+ "timezone" => "America/Los_Angeles",+ },+ )+ items = wrap_items({ "id" => 1 })+ config = { "day_mode" => "weekdays", "timezone" => "UTC" }++ result = execute_node_output(configuration: config, input_items: items, workflow: workflow)+ expect(result[1]).to eq(items)+ end++ it "falls back to the site timezone without a workflow" do+ freeze_time Time.utc(2026, 7, 25, 2, 0)++ items = wrap_items({ "id" => 1 })+ result =+ execute_node_output(configuration: { "day_mode" => "weekdays" }, input_items: items)++ expect(result[1]).to eq(items)+ end++ it "raises on an invalid timezone" do+ freeze_time WEDNESDAY_UTC++ expect {+ execute_node_output(+ configuration: {+ "timezone" => "Mars/Olympus",+ },+ input_items: wrap_items({ "id" => 1 }),+ )+ }.to raise_error(DiscourseWorkflows::NodeError, %r{Mars/Olympus})+ end+ end++ context "with expressions" do+ it "resolves the time range per item" do+ freeze_time Time.utc(2026, 7, 22, 12, 0)++ items =+ wrap_items(+ { "id" => 1, "start" => "08:00", "end" => "18:00" },+ { "id" => 2, "start" => "13:00", "end" => "14:00" },+ )+ config = {+ "use_time_range" => true,+ "start_time" => "={{ $json.start }}",+ "end_time" => "={{ $json.end }}",+ }++ result = execute_node_output(configuration: config, input_items: items)++ expect(result[0].map { |item| item["json"]["id"] }).to eq([1])+ expect(result[1].map { |item| item["json"]["id"] }).to eq([2])+ end++ it "raises when an expression resolves to an invalid time" do+ freeze_time WEDNESDAY_UTC++ config = {+ "use_time_range" => true,+ "start_time" => "={{ $json.start }}",+ "end_time" => "18:00",+ }++ expect {+ execute_node_output(+ configuration: config,+ input_items: wrap_items({ "start" => "banana" }),+ )+ }.to raise_error(DiscourseWorkflows::NodeError, /banana/)+ end++ it "raises when an expression resolves to an invalid timezone" do+ freeze_time WEDNESDAY_UTC++ expect {+ execute_node_output(+ configuration: {+ "timezone" => "={{ $json.tz }}",+ },+ input_items: wrap_items({ "tz" => "Nowhere/Land" }),+ )+ }.to raise_error(DiscourseWorkflows::NodeError, %r{Nowhere/Land})+ end+ end+ end++ describe ".validate_configuration" do+ def validate(config)+ errors = ActiveModel::Errors.new(Object.new)+ described_class.validate_configuration(config, errors)+ errors+ end++ it "accepts a valid configuration" do+ errors =+ validate(+ "day_mode" => "custom",+ "days" => [1, 2],+ "use_time_range" => true,+ "start_time" => "09:00",+ "end_time" => "17:30",+ "timezone" => "Europe/Paris",+ )++ expect(errors).to be_empty+ end++ it "rejects an unknown day mode" do+ errors = validate("day_mode" => "banana")++ expect(errors[:base]).to include(+ I18n.t("discourse_workflows.errors.time_window.invalid_day_mode", mode: "banana"),+ )+ end++ it "requires days in custom mode" do+ errors = validate("day_mode" => "custom", "days" => [])++ expect(errors[:base]).to include(+ I18n.t("discourse_workflows.errors.time_window.days_required"),+ )+ end++ it "rejects out-of-range days" do+ errors = validate("day_mode" => "custom", "days" => [1, 9])++ expect(errors[:base]).to include(+ I18n.t("discourse_workflows.errors.time_window.invalid_days"),+ )+ end++ it "rejects malformed times" do+ errors = validate("use_time_range" => true, "start_time" => "25:99", "end_time" => "17:00")++ expect(errors[:base]).to include(+ I18n.t("discourse_workflows.errors.time_window.invalid_time_range"),+ )+ end++ it "requires times when the time range is enabled" do+ errors = validate("use_time_range" => true)++ expect(errors[:base]).to include(+ I18n.t("discourse_workflows.errors.time_window.invalid_time_range"),+ )+ end++ it "rejects equal start and end times" do+ errors = validate("use_time_range" => true, "start_time" => "09:00", "end_time" => "09:00")++ expect(errors[:base]).to include(+ I18n.t("discourse_workflows.errors.time_window.start_end_equal"),+ )+ end++ it "rejects an invalid timezone" do+ errors = validate("timezone" => "Mars/Olympus")++ expect(errors[:base]).to include(+ I18n.t("discourse_workflows.errors.time_window.invalid_timezone", timezone: "Mars/Olympus"),+ )+ end++ it "skips expression values" do+ errors =+ validate(+ "use_time_range" => true,+ "start_time" => "={{ $json.start }}",+ "end_time" => "={{ $json.end }}",+ "timezone" => "={{ $json.tz }}",+ )++ expect(errors).to be_empty+ end+ end+end