Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(web): order weekly schedule preview by date (#44718)

Juhyuk choi9e20db95c860
diff --git a/web/app/components/workflow/nodes/trigger-schedule/utils/__tests__/weekly-execution-preview.spec.ts b/web/app/components/workflow/nodes/trigger-schedule/utils/__tests__/weekly-execution-preview.spec.ts
new file mode 100644
index 00000000000..25e395d9e56
--- /dev/null
+++ b/web/app/components/workflow/nodes/trigger-schedule/utils/__tests__/weekly-execution-preview.spec.ts
@@ -0,0 +1,138 @@
+import type { ScheduleTriggerNodeType } from '../../types'
+import { BlockEnum } from '../../../../types'
+import {
+ getFormattedExecutionTimes,
+ getNextExecutionTime,
+ getNextExecutionTimes,
+} from '../execution-time-calculator'
+
+const weekdays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
+
+const createWeeklyData = (
+ selectedDays: string[],
+ timezone = 'Asia/Seoul',
+): ScheduleTriggerNodeType => ({
+ type: BlockEnum.TriggerSchedule,
+ title: 'schedule',
+ desc: '',
+ mode: 'visual',
+ frequency: 'weekly',
+ timezone,
+ visual_config: { time: '9:00 AM', weekdays: selectedDays },
+})
+
+const wallTime = (date: Date) => [
+ date.getFullYear(),
+ date.getMonth() + 1,
+ date.getDate(),
+ date.getHours(),
+ date.getMinutes(),
+]
+
+describe('weekly schedule execution preview', () => {
+ afterEach(() => vi.useRealTimers())
+
+ it('shows Monday as the next run after Sunday morning', () => {
+ vi.useFakeTimers()
+ vi.setSystemTime(new Date('2026-10-25T01:00:00Z'))
+
+ const data = createWeeklyData(['sun', 'mon'])
+ expect(getNextExecutionTime(data)).toBe('Monday, October 26, 2026, 9:00 AM')
+ expect(getNextExecutionTimes(data, 9).map(wallTime)).toEqual([
+ [2026, 10, 26, 9, 0],
+ [2026, 11, 1, 9, 0],
+ [2026, 11, 2, 9, 0],
+ [2026, 11, 8, 9, 0],
+ [2026, 11, 9, 9, 0],
+ [2026, 11, 15, 9, 0],
+ [2026, 11, 16, 9, 0],
+ [2026, 11, 22, 9, 0],
+ [2026, 11, 23, 9, 0],
+ ])
+ expect(getFormattedExecutionTimes(data, 5)[4]).toContain('Monday, November 9, 2026, 9:00 AM')
+ })
+
+ it.each([
+ { time: '12:00 AM', cron: '0 0' },
+ { time: '12:00 PM', cron: '0 12' },
+ { time: '11:59 PM', cron: '59 23' },
+ ])('keeps the next five runs in order at $time', ({ time, cron }) => {
+ vi.useFakeTimers()
+ vi.setSystemTime(new Date('2026-10-25T01:00:00Z'))
+
+ const data: ScheduleTriggerNodeType = {
+ ...createWeeklyData(['sun', 'mon']),
+ visual_config: { time, weekdays: ['sun', 'mon'] },
+ }
+ const cronData: ScheduleTriggerNodeType = {
+ ...data,
+ mode: 'cron',
+ cron_expression: `${cron} * * 0,1`,
+ }
+
+ expect(getNextExecutionTimes(data, 5).map(wallTime)).toEqual(
+ getNextExecutionTimes(cronData, 5).map(wallTime),
+ )
+ })
+
+ it.each([
+ '2026-10-24T23:59:00Z', // Sunday before 9 AM in Seoul
+ '2026-10-25T00:00:00Z', // Sunday at 9 AM in Seoul
+ '2026-10-25T01:00:00Z', // Sunday after 9 AM in Seoul
+ '2026-12-31T15:30:00Z', // New Year in Seoul
+ ])('matches the cron schedule for every weekday set at %s', (now) => {
+ vi.useFakeTimers()
+ vi.setSystemTime(new Date(now))
+
+ for (let mask = 1; mask < 1 << weekdays.length; mask++) {
+ const selectedDays = weekdays.filter((_, index) => mask & (1 << index))
+ const cronData: ScheduleTriggerNodeType = {
+ ...createWeeklyData(selectedDays),
+ mode: 'cron',
+ cron_expression: `0 9 * * ${selectedDays.map((day) => weekdays.indexOf(day)).join(',')}`,
+ }
+
+ for (const count of [1, 5]) {
+ expect(getNextExecutionTimes(createWeeklyData(selectedDays), count).map(wallTime)).toEqual(
+ getNextExecutionTimes(cronData, count).map(wallTime),
+ )
+ }
+ }
+ })
+
+ it.each([
+ { timezone: 'America/New_York', now: '2026-10-25T14:00:00Z' }, // DST ends next Sunday
+ { timezone: 'Pacific/Kiritimati', now: '2026-12-31T11:00:00Z' }, // New Year
+ ])('matches cron across a calendar change in $timezone', ({ timezone, now }) => {
+ vi.useFakeTimers()
+ vi.setSystemTime(new Date(now))
+
+ for (let mask = 1; mask < 1 << weekdays.length; mask++) {
+ const selectedDays = weekdays.filter((_, index) => mask & (1 << index))
+ const cronData: ScheduleTriggerNodeType = {
+ ...createWeeklyData(selectedDays, timezone),
+ mode: 'cron',
+ cron_expression: `0 9 * * ${selectedDays.map((day) => weekdays.indexOf(day)).join(',')}`,
+ }
+
+ expect(
+ getNextExecutionTimes(createWeeklyData(selectedDays, timezone), 5).map(wallTime),
+ ).toEqual(getNextExecutionTimes(cronData, 5).map(wallTime))
+ }
+ })
+
+ it('returns unique runs in date order for repeated and reversed weekdays', () => {
+ vi.useFakeTimers()
+ vi.setSystemTime(new Date('2026-10-25T01:00:00Z'))
+
+ expect(getNextExecutionTimes(createWeeklyData(['mon', 'sun', 'mon']), 3).map(wallTime)).toEqual(
+ [
+ [2026, 10, 26, 9, 0],
+ [2026, 11, 1, 9, 0],
+ [2026, 11, 2, 9, 0],
+ ],
+ )
+ expect(getNextExecutionTimes(createWeeklyData([]), 5)).toEqual([])
+ expect(getNextExecutionTimes(createWeeklyData(['sun']), 0)).toEqual([])
+ })
+})
diff --git a/web/app/components/workflow/nodes/trigger-schedule/utils/execution-time-calculator.ts b/web/app/components/workflow/nodes/trigger-schedule/utils/execution-time-calculator.ts
index 03e3649784c..9bb611b9aa4 100644
--- a/web/app/components/workflow/nodes/trigger-schedule/utils/execution-time-calculator.ts
+++ b/web/app/components/workflow/nodes/trigger-schedule/utils/execution-time-calculator.ts
@@ -147,8 +147,14 @@ export const getNextExecutionTimes = (data: ScheduleTriggerNodeType, count: numb
times.push(execution)
}
} else if (data.frequency === 'weekly') {
- const selectedDays = data.visual_config?.weekdays || ['sun']
const dayMap = { sun: 0, mon: 1, tue: 2, wed: 3, thu: 4, fri: 5, sat: 6 }
+ const selectedDays = new Set(
+ (data.visual_config?.weekdays || ['sun'])
+ .map((day) => dayMap[day as keyof typeof dayMap])
+ .filter((day): day is number => day !== undefined),
+ )
+
+ if (selectedDays.size === 0) return times
const [time, period] = defaultTime.split(' ')
const [hour, minute] = time!.split(':')
@@ -159,46 +165,16 @@ export const getNextExecutionTimes = (data: ScheduleTriggerNodeType, count: numb
// Get current time completely in user timezone
const userCurrentTime = getUserTimezoneCurrentTime(timezone)
- let executionCount = 0
- let weekOffset = 0
-
- while (executionCount < count) {
- let hasValidDays = false
-
- for (const selectedDay of selectedDays) {
- if (executionCount >= count) break
-
- const targetDay = dayMap[selectedDay as keyof typeof dayMap]
- if (targetDay === undefined) continue
-
- hasValidDays = true
-
- const currentDayOfWeek = userToday.getDay()
- const daysUntilTarget = (targetDay - currentDayOfWeek + 7) % 7
-
- // Check if today's configured time has already passed
- const todayAtTargetTime = new Date(userToday)
- todayAtTargetTime.setHours(displayHour, Number.parseInt(minute!), 0, 0)
-
- let adjustedDays = daysUntilTarget
- if (daysUntilTarget === 0 && todayAtTargetTime <= userCurrentTime) adjustedDays = 7
-
- const execution = new Date(userToday)
- execution.setDate(userToday.getDate() + adjustedDays + weekOffset * 7)
- execution.setHours(displayHour, Number.parseInt(minute!), 0, 0)
+ let dayOffset = 0
+ while (times.length < count) {
+ const execution = new Date(userToday)
+ execution.setDate(userToday.getDate() + dayOffset)
+ execution.setHours(displayHour, Number.parseInt(minute!), 0, 0)
- // Only add if execution time is in the future
- if (execution > userCurrentTime) {
- times.push(execution)
- executionCount++
- }
- }
+ if (selectedDays.has(execution.getDay()) && execution > userCurrentTime) times.push(execution)
- if (!hasValidDays) break
- weekOffset++
+ dayOffset++
}
-
- times.sort((a, b) => a.getTime() - b.getTime())
} else if (data.frequency === 'monthly') {
const getSelectedDays = (): (number | 'last')[] => {
if (data.visual_config?.monthly_days && data.visual_config.monthly_days.length > 0)