-
Notifications
You must be signed in to change notification settings - Fork 24
perf(cli): project recent-results fields in checks get #1353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
160 changes: 160 additions & 0 deletions
160
packages/cli/src/commands/__tests__/checks-get-results-projection.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import type { CheckResult } from '../../rest/check-results.js' | ||
|
|
||
| vi.mock('../../rest/api.js', () => ({ | ||
| checks: { get: vi.fn() }, | ||
| checkStatuses: { get: vi.fn() }, | ||
| checkResults: { get: vi.fn(), getAll: vi.fn() }, | ||
| errorGroups: { getByCheckId: vi.fn(), get: vi.fn() }, | ||
| analytics: { get: vi.fn() }, | ||
| })) | ||
|
|
||
| import * as api from '../../rest/api.js' | ||
| import ChecksGet from '../checks/get.js' | ||
|
|
||
| // The fixed, internal projection the recent-results table needs. Exactly the | ||
| // fields formatResults() and resolveResultStatus() read — kept in sync with | ||
| // RECENT_RESULTS_FIELDS in checks/get.ts. | ||
| const EXPECTED_FIELDS = ['id', 'startedAt', 'runLocation', 'hasErrors', 'hasFailures', 'isDegraded', 'responseTime'] | ||
|
|
||
| function makeCheck () { | ||
| return { | ||
| id: 'check-1', | ||
| name: 'My API Check', | ||
| description: null, | ||
| checkType: 'API', | ||
| activated: true, | ||
| muted: false, | ||
| frequency: 10, | ||
| locations: ['eu-west-1'], | ||
| privateLocations: [], | ||
| tags: [], | ||
| groupId: null, | ||
| scriptPath: null, | ||
| request: { url: 'https://example.com', method: 'GET' }, | ||
| created_at: '2026-01-01T00:00:00.000Z', | ||
| updated_at: '2026-01-01T00:00:00.000Z', | ||
| } | ||
| } | ||
|
|
||
| function makeResult (overrides: Partial<CheckResult> = {}): CheckResult { | ||
| return { | ||
| id: 'r1', | ||
| checkId: 'check-1', | ||
| name: 'My API Check', | ||
| hasFailures: false, | ||
| hasErrors: false, | ||
| isDegraded: false, | ||
| overMaxResponseTime: false, | ||
| runLocation: 'eu-west-1', | ||
| startedAt: '2026-05-20T08:00:00.000Z', | ||
| stoppedAt: '2026-05-20T08:00:04.000Z', | ||
| created_at: '2026-05-20T08:00:04.000Z', | ||
| responseTime: 4000, | ||
| checkRunId: 1, | ||
| attempts: 1, | ||
| resultType: 'FINAL', | ||
| sequenceId: 'seq-1', | ||
| ...overrides, | ||
| } | ||
| } | ||
|
|
||
| function createCommandContext (parsed: unknown) { | ||
| const logged: string[] = [] | ||
| return Object.assign(Object.create(ChecksGet.prototype), { | ||
| parse: vi.fn().mockResolvedValue(parsed), | ||
| log: vi.fn((msg?: string) => { | ||
| if (msg) logged.push(msg) | ||
| }), | ||
| style: { outputFormat: undefined, longError: vi.fn() }, | ||
| logged, | ||
| }) | ||
| } | ||
|
|
||
| describe('checks get recent-results projection', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| process.exitCode = undefined | ||
| vi.mocked(api.checks.get).mockResolvedValue({ data: makeCheck() } as any) | ||
| vi.mocked(api.checkStatuses.get).mockResolvedValue({ data: undefined } as any) | ||
| vi.mocked(api.errorGroups.getByCheckId).mockResolvedValue({ data: [] } as any) | ||
| vi.mocked(api.analytics.get).mockResolvedValue({ data: undefined } as any) | ||
| vi.mocked(api.checkResults.getAll).mockResolvedValue({ | ||
| data: { entries: [makeResult()], nextId: null, length: 1 }, | ||
| } as any) | ||
| }) | ||
|
|
||
| it('requests only the narrow projection for default (detail) output', async () => { | ||
| const ctx = createCommandContext({ | ||
| args: { id: 'check-1' }, | ||
| flags: { 'results-limit': 10, 'output': 'detail', 'stats-range': 'last24Hours' }, | ||
| }) | ||
|
|
||
| await ChecksGet.prototype.run.call(ctx as any) | ||
|
|
||
| expect(api.checkResults.getAll).toHaveBeenCalledWith('check-1', { | ||
| limit: 10, | ||
| nextId: undefined, | ||
| fields: EXPECTED_FIELDS, | ||
| }) | ||
| }) | ||
|
|
||
| it('forwards a results cursor unchanged alongside the projection', async () => { | ||
| const ctx = createCommandContext({ | ||
| args: { id: 'check-1' }, | ||
| flags: { 'results-limit': 25, 'results-cursor': 'cursor-abc', 'output': 'detail', 'stats-range': 'last24Hours' }, | ||
| }) | ||
|
|
||
| await ChecksGet.prototype.run.call(ctx as any) | ||
|
|
||
| expect(api.checkResults.getAll).toHaveBeenCalledWith('check-1', { | ||
| limit: 25, | ||
| nextId: 'cursor-abc', | ||
| fields: EXPECTED_FIELDS, | ||
| }) | ||
| }) | ||
|
|
||
| it('requests the narrow projection for markdown output', async () => { | ||
| const ctx = createCommandContext({ | ||
| args: { id: 'check-1' }, | ||
| flags: { 'results-limit': 10, 'output': 'md', 'stats-range': 'last24Hours' }, | ||
| }) | ||
|
|
||
| await ChecksGet.prototype.run.call(ctx as any) | ||
|
|
||
| expect(api.checkResults.getAll).toHaveBeenCalledWith('check-1', { | ||
| limit: 10, | ||
| nextId: undefined, | ||
| fields: EXPECTED_FIELDS, | ||
| }) | ||
| }) | ||
|
|
||
| it('does not project fields for json output (preserves full results entries)', async () => { | ||
| const ctx = createCommandContext({ | ||
| args: { id: 'check-1' }, | ||
| flags: { 'results-limit': 10, 'output': 'json', 'stats-range': 'last24Hours' }, | ||
| }) | ||
|
|
||
| await ChecksGet.prototype.run.call(ctx as any) | ||
|
|
||
| expect(api.checkResults.getAll).toHaveBeenCalledTimes(1) | ||
| const callArgs = vi.mocked(api.checkResults.getAll).mock.calls[0][1] | ||
| expect(callArgs).not.toHaveProperty('fields') | ||
| expect(callArgs).toMatchObject({ limit: 10, nextId: undefined }) | ||
| }) | ||
|
|
||
| it('--result drilldown uses the detail endpoint and never lists with projection', async () => { | ||
| vi.mocked(api.checkResults.get).mockResolvedValue({ | ||
| data: makeResult({ id: 'res-42', resultType: 'FINAL', attempts: 1 }), | ||
| } as any) | ||
| const ctx = createCommandContext({ | ||
| args: { id: 'check-1' }, | ||
| flags: { result: 'res-42', output: 'detail' }, | ||
| }) | ||
|
|
||
| await ChecksGet.prototype.run.call(ctx as any) | ||
|
|
||
| expect(api.checkResults.get).toHaveBeenCalledWith('check-1', 'res-42') | ||
| expect(api.checkResults.getAll).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest' | ||
| import type { AxiosInstance } from 'axios' | ||
| import CheckResults from '../check-results.js' | ||
|
|
||
| function makeAxiosMock (): AxiosInstance { | ||
| return { | ||
| get: vi.fn().mockResolvedValue({ data: { entries: [], nextId: null, length: 0 } }), | ||
| } as unknown as AxiosInstance | ||
| } | ||
|
|
||
| describe('CheckResults.getAll()', () => { | ||
| let api: AxiosInstance | ||
| let checkResults: CheckResults | ||
|
|
||
| beforeEach(() => { | ||
| api = makeAxiosMock() | ||
| checkResults = new CheckResults(api) | ||
| }) | ||
|
|
||
| it('serializes the fields array as a comma-separated query string', async () => { | ||
| await checkResults.getAll('check-1', { limit: 10, fields: ['id', 'startedAt', 'responseTime'] }) | ||
|
|
||
| expect(api.get).toHaveBeenCalledWith('/v2/check-results/check-1', { | ||
| params: { limit: 10, fields: 'id,startedAt,responseTime' }, | ||
| }) | ||
| }) | ||
|
|
||
| it('does not send a fields param when fields is omitted', async () => { | ||
| await checkResults.getAll('check-1', { limit: 5 }) | ||
|
|
||
| const [, config] = vi.mocked(api.get).mock.calls[0] | ||
| expect((config as any).params.fields).toBeUndefined() | ||
| }) | ||
|
|
||
| it('passes other params through untouched', async () => { | ||
| await checkResults.getAll('check-1', { from: 100, to: 200, resultType: 'ALL', nextId: 'cursor' }) | ||
|
|
||
| const [, config] = vi.mocked(api.get).mock.calls[0] | ||
| expect((config as any).params).toMatchObject({ from: 100, to: 200, resultType: 'ALL', nextId: 'cursor' }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.