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
3 changes: 3 additions & 0 deletions packages/app/src/cli/services/historical-logs/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export const queryLimits = {
defaultLimit: 20,
defaultSince: '1h',
maximumLimit: 1000,
maximumLookbackDays: 7,
maximumWindowDays: 1,
maximumFutureSkewMinutes: 1,
}

export function logError(code: string, message: string, requestId?: string | null): AbortError {
Expand Down
32 changes: 25 additions & 7 deletions packages/app/src/cli/services/historical-logs/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,34 @@ test('normalizes whole-second offset inputs to UTC and makes since relative to u
})
})

test('accepts windows at the API limits', () => {
expect(window({since: '1d'})).toEqual({since: '2026-10-05T14:00:00Z', until: '2026-10-06T14:00:00Z'})
expect(window({since: '2026-09-29T14:00:00Z', until: '2026-09-29T15:00:00Z'})).toEqual({
since: '2026-09-29T14:00:00Z',
until: '2026-09-29T15:00:00Z',
})
expect(window({until: '2026-10-06T14:01:00Z'})).toEqual({
since: '2026-10-06T13:01:00Z',
until: '2026-10-06T14:01:00Z',
})
})

test.each([
[{since: '8d'}, {since: '2026-09-28T14:00:00Z', until: '2026-10-06T14:00:00Z'}],
[{since: '8d'}, '--since must be within the last 7 days.'],
[{since: '2026-09-29T13:59:59Z', until: '2026-09-29T14:30:00Z'}, '--since must be within the last 7 days.'],
[{until: '2026-09-28T14:00:00Z'}, '--since must be within the last 7 days.'],
[{since: '2d'}, '--since and --until must be at most 1 day apart (--until is the current time by default).'],
[
{since: '2026-09-01T14:00:00Z', until: '2026-09-09T14:00:00Z'},
{since: '2026-09-01T14:00:00Z', until: '2026-09-09T14:00:00Z'},
{since: '2026-10-04T14:00:00Z', until: '2026-10-05T14:00:01Z'},
'--since and --until must be at most 1 day apart (--until is the current time by default).',
],
[{until: '2026-10-06T14:02:00Z'}, {since: '2026-10-06T13:02:00Z', until: '2026-10-06T14:02:00Z'}],
])('leaves time-policy eligibility to the server for %j', (input, expected) => {
expect(window(input)).toEqual(expected)
})
[{until: '2026-10-06T14:01:01Z'}, '--until must not be more than 1 minute in the future.'],
] satisfies [LogInput, string][])(
'rejects windows outside the API limits for %j with the flag to change',
(input, message) => {
expect(() => window(input)).toThrow(expect.objectContaining({code: 'invalid-argument', message}))
},
)

test.each([
{since: '0h'},
Expand Down
20 changes: 20 additions & 0 deletions packages/app/src/cli/services/historical-logs/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,32 @@ export function resolveLogWindow(window: ReturnType<typeof prepareLogWindow>) {
throw logError('invalid-argument', '--since produces a timestamp outside the supported date range.')
if (start >= end)
throw logError('invalid-argument', '--since must be earlier than --until (the current time by default).')
const dayMilliseconds = 86_400_000
if (start < anchor - queryLimits.maximumLookbackDays * dayMilliseconds)
throw logError(
'invalid-argument',
`--since must be within the last ${plural(queryLimits.maximumLookbackDays, 'day')}.`,
)
if (end - start > queryLimits.maximumWindowDays * dayMilliseconds)
throw logError(
'invalid-argument',
`--since and --until must be at most ${plural(queryLimits.maximumWindowDays, 'day')} apart (--until is the current time by default).`,
)
if (end > anchor + queryLimits.maximumFutureSkewMinutes * 60_000)
throw logError(
'invalid-argument',
`--until must not be more than ${plural(queryLimits.maximumFutureSkewMinutes, 'minute')} in the future.`,
)
return {
since: formatJsonOutputTimestamp(new Date(start)),
until: formatJsonOutputTimestamp(new Date(end)),
}
}

function plural(count: number, unit: string): string {
return `${count} ${count === 1 ? unit : `${unit}s`}`
}

function parseTimestamp(value: string, flag: string): number {
const match = /^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})(Z|[+-]\d{2}:\d{2})$/.exec(value)
const invalid = () =>
Expand Down
26 changes: 20 additions & 6 deletions packages/app/src/cli/services/historical-logs/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1038,14 +1038,28 @@ test('propagates app lookup failures without querying logs', async () => {
})

test.each([
[{since: '8d'}, 'The timestamp range must not exceed 7 days.'],
[{since: '2026-09-01T12:00:00Z', until: '2026-09-01T13:00:00Z'}, 'timestamp.gte must be within the last 30 days.'],
[{until: '2026-10-06T14:02:00Z'}, 'timestamp.lt must not be more than 1 minute in the future.'],
])('surfaces backend time-policy errors for %j without retrying', async (input, message) => {
[{since: '8d'}, '--since must be within the last 7 days.'],
[{since: '2d'}, '--since and --until must be at most 1 day apart'],
[{until: '2026-10-06T14:02:00Z'}, '--until must not be more than 1 minute in the future.'],
])('rejects windows outside the API limits for %j before querying logs', async (input, message) => {
authorize()
const errors = [{message, path: ['app', 'logs'], extensions: {code: 'INVALID_LOG_QUERY'}}]
await expect(retrieveLogs({...scope, ...input})).rejects.toMatchObject({
code: 'invalid-argument',
message: expect.stringContaining(message),
})
expect(shopifyFetch).not.toHaveBeenCalled()
})

// A window that passes the local limits can still fail on the server, for example when the clocks disagree.
test.each([
'timestamp.gte must be within the last 7 days.',
'The timestamp range must not exceed 1 day.',
'timestamp.lt must not be more than 1 minute in the future.',
])('surfaces backend time-policy error %s without retrying', async (message) => {
authorize()
const errors = [{message, path: ['app', 'logs'], extensions: {code: 'BAD_REQUEST'}}]
respond({errors})
const failure = await retrieveLogs({...scope, ...input}).catch((error: unknown) => error)
const failure = await retrieveLogs(scope).catch((error: unknown) => error)
expect(failure).toMatchObject({message, code: 'log-query-failed', details: {errors}})
await withCapturedStandardStreams(async ({stdout, stderr}) => {
await handler(failure)
Expand Down
Loading