|
| 1 | +import * as os from 'node:os'; |
| 2 | +import { describe, it, mock } from 'node:test'; |
| 3 | +import { createInfoCommand } from './info_command_factory.js'; |
| 4 | +import { InfoCommand } from './info_command.js'; |
| 5 | +import { EnvironmentInfoProvider } from '../../info/env_info_provider.js'; |
| 6 | +import { CdkInfoProvider } from '../../info/cdk_info_provider.js'; |
| 7 | +import { TestCommandRunner } from '../../test-utils/command_runner.js'; |
| 8 | +import { printer } from '../../printer.js'; |
| 9 | +import assert from 'node:assert'; |
| 10 | +import yargs from 'yargs'; |
| 11 | + |
| 12 | +void describe('createInfoCommand', () => { |
| 13 | + void it('should return an instance of InfoCommand', () => { |
| 14 | + const result = createInfoCommand(); |
| 15 | + assert.ok(result instanceof InfoCommand); |
| 16 | + }); |
| 17 | +}); |
| 18 | + |
| 19 | +void describe('info command run', () => { |
| 20 | + const command = createInfoCommand(); |
| 21 | + const parser = yargs().command(command); |
| 22 | + const commandRunner = new TestCommandRunner(parser); |
| 23 | + |
| 24 | + void it('includes info subcommands in help output', async () => { |
| 25 | + const output = await commandRunner.runCommand(['info', '--help']); |
| 26 | + assert.match( |
| 27 | + output, |
| 28 | + /info\W*Generates information for Amplify troubleshooting/ |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + void it('test info command run', async () => { |
| 33 | + const environmentInfoProviderMock = new EnvironmentInfoProvider(); |
| 34 | + const cdkInfoProviderMock = new CdkInfoProvider(); |
| 35 | + |
| 36 | + const cdkMockValue = ` |
| 37 | + AWS environment variables: |
| 38 | + AWS_STS_REGIONAL_ENDPOINTS = regional |
| 39 | + AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1 |
| 40 | + AWS_SDK_LOAD_CONFIG = 1 |
| 41 | + CDK environment variables: |
| 42 | + CDK_DEBUG = true |
| 43 | + CDK_DISABLE_VERSION_CHECK = true |
| 44 | + `; |
| 45 | + |
| 46 | + const environmentInfoMockValue = ` |
| 47 | + System: |
| 48 | + CPU: fake |
| 49 | + Memory: fake |
| 50 | + OS: fakeOS |
| 51 | + Shell: /fake/path |
| 52 | + Binaries: |
| 53 | + Node: 0.0.0 - /fake/path |
| 54 | + npm: 0.0.0 - /fake/path |
| 55 | + pnpm: 0.0.0 - /fake/path |
| 56 | + Yarn: 0.0.0 - /fake/path |
| 57 | + NPM Packages: |
| 58 | + fake: 0.0.0 |
| 59 | + `; |
| 60 | + |
| 61 | + const infoMock = mock.method( |
| 62 | + environmentInfoProviderMock, |
| 63 | + 'getEnvInfo', |
| 64 | + async () => environmentInfoMockValue |
| 65 | + ); |
| 66 | + |
| 67 | + const cdkInfoMock = mock.method( |
| 68 | + cdkInfoProviderMock, |
| 69 | + 'getCdkInfo', |
| 70 | + async () => cdkMockValue |
| 71 | + ); |
| 72 | + |
| 73 | + const mockPrinter = mock.method(printer, 'print', () => ({})); |
| 74 | + |
| 75 | + const command = new InfoCommand( |
| 76 | + environmentInfoProviderMock, |
| 77 | + cdkInfoProviderMock |
| 78 | + ); |
| 79 | + const parser = yargs().command(command); |
| 80 | + const commandRunner = new TestCommandRunner(parser); |
| 81 | + await commandRunner.runCommand(['info']); |
| 82 | + |
| 83 | + assert.equal(infoMock.mock.callCount(), 1); |
| 84 | + assert.equal(cdkInfoMock.mock.callCount(), 1); |
| 85 | + assert.equal(mockPrinter.mock.callCount(), 1); |
| 86 | + assert.strictEqual( |
| 87 | + mockPrinter.mock.calls[0].arguments[0], |
| 88 | + `${environmentInfoMockValue}${os.EOL}${cdkMockValue}` |
| 89 | + ); |
| 90 | + }); |
| 91 | +}); |
0 commit comments