diff --git a/apps/sim/lib/sim-search/live/dates.test.ts b/apps/sim/lib/sim-search/live/dates.test.ts index 8bfe1337787..a4dbe36c175 100644 --- a/apps/sim/lib/sim-search/live/dates.test.ts +++ b/apps/sim/lib/sim-search/live/dates.test.ts @@ -134,7 +134,7 @@ describe('generic live search dates', () => { const result = await searchSlack(api, input) expect(api.json).toHaveBeenCalledTimes(1) expect(api.json.mock.calls[0]?.[1].body).toMatchObject({ - modifiers: 'after:2026-09-21 before:2026-09-24', + modifiers: 'after:2026-09-20 before:2026-09-25', after: Date.parse(filters.startDate) / 1000 - 1, before: Date.parse(filters.endDate) / 1000, sort: 'timestamp', @@ -166,3 +166,29 @@ describe('generic live search dates', () => { expect(api.json).toHaveBeenCalledTimes(3) }) }) + +describe('Slack date windows', () => { + const DAY = 86_400_000 + const HOUR = 3_600_000 + /** Slack reads `after:`/`before:` days in the member's zone, which may be UTC-12 to UTC+14. */ + const firstIncludedInstant = (afterDay: string) => Date.parse(afterDay) + DAY + 12 * HOUR + const firstExcludedInstant = (beforeDay: string) => Date.parse(beforeDay) - 14 * HOUR + + it.each([ + ['UTC midnight', '2026-09-22T00:00:00Z', '2026-09-23T00:00:00Z'], + ['midnight ahead of UTC', '2026-09-22T00:00:00+09:00', '2026-09-23T00:00:00+09:00'], + ['midnight behind UTC', '2026-09-22T00:00:00-11:00', '2026-09-23T00:00:00-11:00'], + ])( + 'day modifiers never narrow a window starting at %s in any member zone', + async (_, startDate, endDate) => { + const api = client() + api.json.mockResolvedValue({ ok: true, results: { messages: [] } }) + await searchSlack(api, { ...input, filters: { startDate, endDate } }) + const modifiers = String(api.json.mock.calls[0]?.[1].body.modifiers) + const after = modifiers.match(/after:(\S+)/)?.[1] ?? '' + const before = modifiers.match(/before:(\S+)/)?.[1] ?? '' + expect(firstIncludedInstant(after)).toBeLessThanOrEqual(Date.parse(startDate)) + expect(firstExcludedInstant(before)).toBeGreaterThanOrEqual(Date.parse(endDate)) + } + ) +}) diff --git a/apps/sim/lib/sim-search/live/slack.ts b/apps/sim/lib/sim-search/live/slack.ts index e72f6c0c56d..30886e010ed 100644 --- a/apps/sim/lib/sim-search/live/slack.ts +++ b/apps/sim/lib/sim-search/live/slack.ts @@ -157,6 +157,16 @@ const SLACK_PAGE_SIZE = 20 * against the member's RTS rate limit of about 10 calls a minute. */ const MAX_SLACK_SEARCH_PAGES = 3 +/** + * Slack's `after:`/`before:` day modifiers are exclusive and read in the member's own zone + * (UTC-12 to UTC+14), so each is widened by two days; the exact `after`/`before` timestamps sent + * alongside still bound the results. + */ +const DAY_MODIFIER_MARGIN_MS = 2 * 86_400_000 + +/** The UTC day, as `YYYY-MM-DD`, of an instant shifted by `offsetMs`. */ +const utcDay = (iso: string, offsetMs: number) => + new Date(Date.parse(iso) + offsetMs).toISOString().slice(0, 10) /** * A search across every source reads one page, so Slack shares the result list with the other @@ -221,12 +231,8 @@ async function searchSlackPage( .map(([type]) => type) const dates = nativeDateBounds(input) const dateModifiers = [ - dates.start - ? `after:${new Date(Date.parse(dates.start) - 86400000).toISOString().slice(0, 10)}` - : '', - dates.end - ? `before:${new Date(Date.parse(dates.end) + 86400000).toISOString().slice(0, 10)}` - : '', + dates.start ? `after:${utcDay(dates.start, -DAY_MODIFIER_MARGIN_MS)}` : '', + dates.end ? `before:${utcDay(dates.end, DAY_MODIFIER_MARGIN_MS)}` : '', ] .filter(Boolean) .join(' ')