|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +import type { CheckSession, CompletedCheckSession } from '../../rest/check-sessions.js' |
| 4 | + |
| 5 | +vi.mock('../../rest/api.js', () => ({ |
| 6 | + checkSessions: { |
| 7 | + trigger: vi.fn(), |
| 8 | + pollUntilComplete: vi.fn(), |
| 9 | + }, |
| 10 | +})) |
| 11 | + |
| 12 | +import * as api from '../../rest/api.js' |
| 13 | +import { |
| 14 | + CheckSessionWaitTimeoutError, |
| 15 | + NoMatchingChecksError, |
| 16 | +} from '../../rest/check-sessions.js' |
| 17 | +import ChecksRun from '../checks/run.js' |
| 18 | + |
| 19 | +const pendingSessions: CheckSession[] = [ |
| 20 | + { |
| 21 | + checkSessionId: '8166fa86-c9b4-4162-8541-d380c6c212d8', |
| 22 | + checkSessionLink: 'https://app.checklyhq.com/check-sessions/8166fa86-c9b4-4162-8541-d380c6c212d8', |
| 23 | + checkId: 'a4cd4ad9-4815-4a9e-92d2-0a7c562ee69a', |
| 24 | + checkType: 'API', |
| 25 | + name: 'Production API', |
| 26 | + status: 'PROGRESS', |
| 27 | + startedAt: '2026-07-21T12:00:00.000Z', |
| 28 | + stoppedAt: null, |
| 29 | + timeElapsed: 0, |
| 30 | + runLocations: ['us-east-1'], |
| 31 | + runSource: 'TRIGGER_API', |
| 32 | + }, |
| 33 | + { |
| 34 | + checkSessionId: '16e22c0d-22a5-4d03-93c5-91bc38ac3c85', |
| 35 | + checkSessionLink: 'https://app.checklyhq.com/check-sessions/16e22c0d-22a5-4d03-93c5-91bc38ac3c85', |
| 36 | + checkId: '22336fd1-c407-4bf7-a793-91c2246fd08f', |
| 37 | + checkType: 'BROWSER', |
| 38 | + name: 'Checkout journey', |
| 39 | + status: 'PROGRESS', |
| 40 | + startedAt: '2026-07-21T12:00:00.000Z', |
| 41 | + stoppedAt: null, |
| 42 | + timeElapsed: 0, |
| 43 | + runLocations: ['eu-west-1'], |
| 44 | + runSource: 'TRIGGER_API', |
| 45 | + }, |
| 46 | +] |
| 47 | + |
| 48 | +const completedSessions: CompletedCheckSession[] = pendingSessions.map((session, index) => ({ |
| 49 | + ...session, |
| 50 | + status: index === 0 ? 'PASSED' : 'DEGRADED', |
| 51 | + stoppedAt: '2026-07-21T12:00:01.000Z', |
| 52 | + timeElapsed: 1_000, |
| 53 | + results: [], |
| 54 | +})) |
| 55 | + |
| 56 | +function defaultFlags (overrides: Record<string, unknown> = {}) { |
| 57 | + return { |
| 58 | + 'tags': undefined, |
| 59 | + 'check-id': undefined, |
| 60 | + 'refresh-cache': false, |
| 61 | + 'timeout': 600, |
| 62 | + 'detach': false, |
| 63 | + 'fail-on-no-matching': true, |
| 64 | + 'output': 'table', |
| 65 | + ...overrides, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +function createCommandContext (flags: Record<string, unknown>) { |
| 70 | + const logged: string[] = [] |
| 71 | + return { |
| 72 | + parse: vi.fn().mockResolvedValue({ flags }), |
| 73 | + error: vi.fn((message: string) => { |
| 74 | + throw new Error(message) |
| 75 | + }), |
| 76 | + log: vi.fn((message?: string) => { |
| 77 | + if (message) logged.push(message) |
| 78 | + }), |
| 79 | + style: { |
| 80 | + outputFormat: undefined, |
| 81 | + actionStart: vi.fn(), |
| 82 | + actionStatus: vi.fn(), |
| 83 | + actionSuccess: vi.fn(), |
| 84 | + actionFailure: vi.fn(), |
| 85 | + longError: vi.fn(), |
| 86 | + }, |
| 87 | + logged, |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +describe('checks run command', () => { |
| 92 | + beforeEach(() => { |
| 93 | + vi.clearAllMocks() |
| 94 | + process.exitCode = undefined |
| 95 | + vi.mocked(api.checkSessions.trigger).mockResolvedValue({ sessions: pendingSessions }) |
| 96 | + vi.mocked(api.checkSessions.pollUntilComplete) |
| 97 | + .mockImplementation(id => Promise.resolve(completedSessions.find(session => session.checkSessionId === id)!)) |
| 98 | + }) |
| 99 | + |
| 100 | + it('maps selectors to the public check sessions API and waits by default', async () => { |
| 101 | + const ctx = createCommandContext(defaultFlags({ |
| 102 | + 'tags': ['production,api', 'critical'], |
| 103 | + 'check-id': [pendingSessions[0].checkId], |
| 104 | + 'refresh-cache': true, |
| 105 | + 'timeout': 120, |
| 106 | + })) |
| 107 | + |
| 108 | + await ChecksRun.prototype.run.call(ctx as any) |
| 109 | + |
| 110 | + expect(api.checkSessions.trigger).toHaveBeenCalledWith({ |
| 111 | + target: { |
| 112 | + matchTags: [['production', 'api'], ['critical']], |
| 113 | + checkId: [pendingSessions[0].checkId], |
| 114 | + }, |
| 115 | + refreshCache: true, |
| 116 | + }) |
| 117 | + expect(api.checkSessions.pollUntilComplete).toHaveBeenCalledTimes(2) |
| 118 | + expect(api.checkSessions.pollUntilComplete).toHaveBeenCalledWith( |
| 119 | + pendingSessions[0].checkSessionId, |
| 120 | + expect.any(Number), |
| 121 | + ) |
| 122 | + expect(ctx.style.actionStart).toHaveBeenCalledWith('Waiting for check sessions to complete...') |
| 123 | + expect(ctx.style.actionSuccess).toHaveBeenCalled() |
| 124 | + expect(ctx.logged[0]).toContain('2 check sessions completed') |
| 125 | + expect(process.exitCode).toBeUndefined() |
| 126 | + }) |
| 127 | + |
| 128 | + it('omits the target when no selectors are provided', async () => { |
| 129 | + const ctx = createCommandContext(defaultFlags({ detach: true })) |
| 130 | + |
| 131 | + await ChecksRun.prototype.run.call(ctx as any) |
| 132 | + |
| 133 | + expect(api.checkSessions.trigger).toHaveBeenCalledWith({ refreshCache: false }) |
| 134 | + }) |
| 135 | + |
| 136 | + it('detaches without requesting completion', async () => { |
| 137 | + const ctx = createCommandContext(defaultFlags({ detach: true })) |
| 138 | + |
| 139 | + await ChecksRun.prototype.run.call(ctx as any) |
| 140 | + |
| 141 | + expect(api.checkSessions.pollUntilComplete).not.toHaveBeenCalled() |
| 142 | + expect(ctx.style.actionStart).not.toHaveBeenCalled() |
| 143 | + expect(ctx.logged[0]).toContain('2 check sessions started.') |
| 144 | + }) |
| 145 | + |
| 146 | + it('prints a stable JSON envelope without spinner output', async () => { |
| 147 | + const ctx = createCommandContext(defaultFlags({ output: 'json' })) |
| 148 | + |
| 149 | + await ChecksRun.prototype.run.call(ctx as any) |
| 150 | + |
| 151 | + expect(JSON.parse(ctx.logged[0])).toEqual({ sessions: completedSessions }) |
| 152 | + expect(ctx.style.actionStart).not.toHaveBeenCalled() |
| 153 | + }) |
| 154 | + |
| 155 | + it.each(['FAILED', 'TIMED_OUT', 'CANCELLED'] as const)('exits 1 when a session is %s', async status => { |
| 156 | + vi.mocked(api.checkSessions.pollUntilComplete).mockResolvedValue({ |
| 157 | + ...completedSessions[0], |
| 158 | + status, |
| 159 | + }) |
| 160 | + vi.mocked(api.checkSessions.trigger).mockResolvedValue({ sessions: [pendingSessions[0]] }) |
| 161 | + const ctx = createCommandContext(defaultFlags()) |
| 162 | + |
| 163 | + await ChecksRun.prototype.run.call(ctx as any) |
| 164 | + |
| 165 | + expect(process.exitCode).toBe(1) |
| 166 | + }) |
| 167 | + |
| 168 | + it('fails by default when no checks match', async () => { |
| 169 | + vi.mocked(api.checkSessions.trigger).mockRejectedValue(new NoMatchingChecksError()) |
| 170 | + const ctx = createCommandContext(defaultFlags()) |
| 171 | + |
| 172 | + await ChecksRun.prototype.run.call(ctx as any) |
| 173 | + |
| 174 | + expect(ctx.logged).toContain('No matching checks were found.') |
| 175 | + expect(process.exitCode).toBe(1) |
| 176 | + }) |
| 177 | + |
| 178 | + it('can succeed when no checks match', async () => { |
| 179 | + vi.mocked(api.checkSessions.trigger).mockRejectedValue(new NoMatchingChecksError()) |
| 180 | + const ctx = createCommandContext(defaultFlags({ 'fail-on-no-matching': false })) |
| 181 | + |
| 182 | + await ChecksRun.prototype.run.call(ctx as any) |
| 183 | + |
| 184 | + expect(process.exitCode).toBeUndefined() |
| 185 | + }) |
| 186 | + |
| 187 | + it('prints an empty JSON envelope when no checks match without failing', async () => { |
| 188 | + vi.mocked(api.checkSessions.trigger).mockRejectedValue(new NoMatchingChecksError()) |
| 189 | + const ctx = createCommandContext(defaultFlags({ |
| 190 | + 'fail-on-no-matching': false, |
| 191 | + 'output': 'json', |
| 192 | + })) |
| 193 | + |
| 194 | + await ChecksRun.prototype.run.call(ctx as any) |
| 195 | + |
| 196 | + expect(ctx.logged).toHaveLength(1) |
| 197 | + expect(JSON.parse(ctx.logged[0])).toEqual({ sessions: [] }) |
| 198 | + expect(process.exitCode).toBeUndefined() |
| 199 | + }) |
| 200 | + |
| 201 | + it('reports an overall completion timeout and leaves sessions running', async () => { |
| 202 | + vi.mocked(api.checkSessions.trigger).mockResolvedValue({ sessions: [pendingSessions[0]] }) |
| 203 | + vi.mocked(api.checkSessions.pollUntilComplete) |
| 204 | + .mockRejectedValue(new CheckSessionWaitTimeoutError(pendingSessions[0].checkSessionId)) |
| 205 | + const ctx = createCommandContext(defaultFlags()) |
| 206 | + |
| 207 | + await ChecksRun.prototype.run.call(ctx as any) |
| 208 | + |
| 209 | + expect(ctx.style.actionFailure).toHaveBeenCalled() |
| 210 | + expect(ctx.style.longError).toHaveBeenCalledWith( |
| 211 | + 'Timed out waiting for check sessions.', |
| 212 | + expect.stringContaining('--detach'), |
| 213 | + ) |
| 214 | + expect(process.exitCode).toBe(1) |
| 215 | + }) |
| 216 | + |
| 217 | + it('rejects a non-positive timeout before scheduling', async () => { |
| 218 | + const ctx = createCommandContext(defaultFlags({ timeout: 0 })) |
| 219 | + |
| 220 | + await expect(ChecksRun.prototype.run.call(ctx as any)) |
| 221 | + .rejects |
| 222 | + .toThrow('--timeout must be an integer greater than 0.') |
| 223 | + expect(api.checkSessions.trigger).not.toHaveBeenCalled() |
| 224 | + }) |
| 225 | + |
| 226 | + it('has intentional command metadata', () => { |
| 227 | + expect(ChecksRun.readOnly).toBe(false) |
| 228 | + expect(ChecksRun.destructive).toBe(false) |
| 229 | + expect(ChecksRun.idempotent).toBe(false) |
| 230 | + }) |
| 231 | +}) |
0 commit comments