|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { CloudWatchLogsServiceException } from '@aws-sdk/client-cloudwatch-logs' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { mockListCloudWatchLogGroups, mockListCloudWatchLogStreams } = vi.hoisted(() => ({ |
| 8 | + mockListCloudWatchLogGroups: vi.fn(), |
| 9 | + mockListCloudWatchLogStreams: vi.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('@/tools/cloudwatch/listing', () => ({ |
| 13 | + listCloudWatchLogGroups: mockListCloudWatchLogGroups, |
| 14 | + listCloudWatchLogStreams: mockListCloudWatchLogStreams, |
| 15 | +})) |
| 16 | + |
| 17 | +import { createSelectorProtectedValues } from '@/lib/selectors/server/protected-values' |
| 18 | +import { cloudWatchSelectorAttachments } from '@/lib/selectors/server/providers/cloudwatch' |
| 19 | +import type { ExecuteServerSelectorArgs } from '@/lib/selectors/server/types' |
| 20 | + |
| 21 | +function logGroupArgs(signal?: AbortSignal): ExecuteServerSelectorArgs { |
| 22 | + return { |
| 23 | + selectorKey: 'cloudwatch.logGroups', |
| 24 | + context: { |
| 25 | + awsAccessKeyId: 'access-key', |
| 26 | + awsSecretAccessKey: 'secret-key', |
| 27 | + awsRegion: 'us-east-1', |
| 28 | + }, |
| 29 | + request: { kind: 'list' }, |
| 30 | + scope: { kind: 'workspace', workspaceId: 'workspace-1' }, |
| 31 | + workspaceId: 'workspace-1', |
| 32 | + principal: { kind: 'session', userId: 'user-1', sessionId: 'session-1' }, |
| 33 | + requesterUserId: 'user-1', |
| 34 | + references: new Map(), |
| 35 | + protectedValues: createSelectorProtectedValues(), |
| 36 | + signal, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function cloudWatchError(status: number): CloudWatchLogsServiceException { |
| 41 | + return new CloudWatchLogsServiceException({ |
| 42 | + name: 'CloudWatchLogsError', |
| 43 | + $fault: status >= 500 ? 'server' : 'client', |
| 44 | + $metadata: { httpStatusCode: status }, |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +describe('CloudWatch server selector adapter errors', () => { |
| 49 | + beforeEach(() => vi.clearAllMocks()) |
| 50 | + |
| 51 | + it.each([ |
| 52 | + [401, 'SelectorConnectionUnavailableError', 401], |
| 53 | + [403, 'SelectorConnectionUnavailableError', 403], |
| 54 | + [429, 'SelectorOptionsUnavailableError', 429], |
| 55 | + [500, 'SelectorOptionsUnavailableError', 502], |
| 56 | + ] as const)( |
| 57 | + 'maps trusted AWS status %i to the safe selector taxonomy', |
| 58 | + async (status, name, safeStatus) => { |
| 59 | + mockListCloudWatchLogGroups.mockRejectedValueOnce(cloudWatchError(status)) |
| 60 | + |
| 61 | + await expect( |
| 62 | + cloudWatchSelectorAttachments['cloudwatch.logGroups'].execute(logGroupArgs()) |
| 63 | + ).rejects.toMatchObject({ name, status: safeStatus }) |
| 64 | + } |
| 65 | + ) |
| 66 | + |
| 67 | + it('does not trust a status-shaped unknown error', async () => { |
| 68 | + mockListCloudWatchLogGroups.mockRejectedValueOnce({ $metadata: { httpStatusCode: 401 } }) |
| 69 | + |
| 70 | + await expect( |
| 71 | + cloudWatchSelectorAttachments['cloudwatch.logGroups'].execute(logGroupArgs()) |
| 72 | + ).rejects.toMatchObject({ name: 'SelectorOptionsUnavailableError', status: 502 }) |
| 73 | + }) |
| 74 | + |
| 75 | + it('preserves caller cancellation', async () => { |
| 76 | + const controller = new AbortController() |
| 77 | + const abortError = new DOMException('The operation was aborted', 'AbortError') |
| 78 | + controller.abort(abortError) |
| 79 | + mockListCloudWatchLogGroups.mockRejectedValueOnce(abortError) |
| 80 | + |
| 81 | + await expect( |
| 82 | + cloudWatchSelectorAttachments['cloudwatch.logGroups'].execute(logGroupArgs(controller.signal)) |
| 83 | + ).rejects.toBe(abortError) |
| 84 | + }) |
| 85 | + |
| 86 | + it('rejects an invalid region before invoking the AWS listing helper', async () => { |
| 87 | + const args = logGroupArgs() |
| 88 | + args.context.awsRegion = 'not-a-region' |
| 89 | + |
| 90 | + await expect( |
| 91 | + cloudWatchSelectorAttachments['cloudwatch.logGroups'].execute(args) |
| 92 | + ).rejects.toMatchObject({ name: 'SelectorContextUnavailableError' }) |
| 93 | + expect(mockListCloudWatchLogGroups).not.toHaveBeenCalled() |
| 94 | + expect(mockListCloudWatchLogStreams).not.toHaveBeenCalled() |
| 95 | + }) |
| 96 | +}) |
0 commit comments