Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion apps/sim/lib/sim-search/live/dates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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))
}
)
})
18 changes: 12 additions & 6 deletions apps/sim/lib/sim-search/live/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(' ')
Expand Down
Loading