|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const mocks = vi.hoisted(() => ({ |
| 7 | + createCloudWatchLogsClient: vi.fn(), |
| 8 | + destroy: vi.fn(), |
| 9 | + send: vi.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('@/lib/internal/cloudwatch/client', async (importOriginal) => ({ |
| 13 | + ...(await importOriginal<typeof import('@/lib/internal/cloudwatch/client')>()), |
| 14 | + createCloudWatchLogsClient: mocks.createCloudWatchLogsClient, |
| 15 | +})) |
| 16 | + |
| 17 | +import { listCloudWatchLogGroups, listCloudWatchLogStreams } from '@/tools/cloudwatch/listing' |
| 18 | + |
| 19 | +const CREDENTIALS = { |
| 20 | + region: 'us-east-1', |
| 21 | + accessKeyId: 'access-key', |
| 22 | + secretAccessKey: 'secret-key', |
| 23 | +} |
| 24 | + |
| 25 | +function mockTwentyPages( |
| 26 | + collection: 'logGroups' | 'logStreams', |
| 27 | + name: 'logGroupName' | 'logStreamName', |
| 28 | + finalToken: string |
| 29 | +) { |
| 30 | + let page = 0 |
| 31 | + mocks.send.mockImplementation(async () => { |
| 32 | + page += 1 |
| 33 | + return { |
| 34 | + [collection]: [{ [name]: `item-${page}` }], |
| 35 | + nextToken: page === 20 ? finalToken : `page-${page + 1}`, |
| 36 | + } |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +describe('CloudWatch shared listings', () => { |
| 41 | + beforeEach(() => { |
| 42 | + vi.clearAllMocks() |
| 43 | + mocks.createCloudWatchLogsClient.mockReturnValue({ |
| 44 | + send: mocks.send, |
| 45 | + destroy: mocks.destroy, |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + it('continues log groups from an opaque token and exposes page twenty continuation', async () => { |
| 50 | + mockTwentyPages('logGroups', 'logGroupName', 'opaque::group/after-20+=') |
| 51 | + |
| 52 | + const result = await listCloudWatchLogGroups({ |
| 53 | + credentials: CREDENTIALS, |
| 54 | + prefix: '/aws/lambda/', |
| 55 | + nextToken: 'opaque::group/start+=', |
| 56 | + suppressTruncationLog: true, |
| 57 | + }) |
| 58 | + |
| 59 | + expect(result).toMatchObject({ |
| 60 | + pages: 20, |
| 61 | + truncated: true, |
| 62 | + nextToken: 'opaque::group/after-20+=', |
| 63 | + }) |
| 64 | + expect(result.items).toHaveLength(20) |
| 65 | + expect(mocks.send).toHaveBeenCalledTimes(20) |
| 66 | + expect(mocks.send.mock.calls[0]?.[0].input).toMatchObject({ |
| 67 | + logGroupNamePrefix: '/aws/lambda/', |
| 68 | + limit: 50, |
| 69 | + nextToken: 'opaque::group/start+=', |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + it('continues streams within their group and prefix and exposes page twenty continuation', async () => { |
| 74 | + mockTwentyPages('logStreams', 'logStreamName', 'opaque::stream/after-20+=') |
| 75 | + |
| 76 | + const result = await listCloudWatchLogStreams({ |
| 77 | + credentials: CREDENTIALS, |
| 78 | + logGroupName: '/aws/lambda/example', |
| 79 | + prefix: '2026/09/', |
| 80 | + nextToken: 'opaque::stream/start+=', |
| 81 | + suppressTruncationLog: true, |
| 82 | + }) |
| 83 | + |
| 84 | + expect(result).toMatchObject({ |
| 85 | + pages: 20, |
| 86 | + truncated: true, |
| 87 | + nextToken: 'opaque::stream/after-20+=', |
| 88 | + }) |
| 89 | + expect(result.items).toHaveLength(20) |
| 90 | + expect(mocks.send).toHaveBeenCalledTimes(20) |
| 91 | + expect(mocks.send.mock.calls[0]?.[0].input).toMatchObject({ |
| 92 | + logGroupName: '/aws/lambda/example', |
| 93 | + logStreamNamePrefix: '2026/09/', |
| 94 | + orderBy: 'LogStreamName', |
| 95 | + limit: 50, |
| 96 | + nextToken: 'opaque::stream/start+=', |
| 97 | + }) |
| 98 | + }) |
| 99 | +}) |
0 commit comments