|
| 1 | +import { execSync } from 'node:child_process'; |
| 2 | + |
| 3 | +import { goSync } from '@api3/promise-utils'; |
| 4 | + |
| 5 | +const CLI_EXECUTABLE = 'dist/src/cli.js'; |
| 6 | + |
| 7 | +type CommandArg = [string, string | number | boolean]; |
| 8 | + |
| 9 | +describe('cli tests', () => { |
| 10 | + const execCommand = (command: string, ...args: CommandArg[]) => { |
| 11 | + const formattedCommand = [command, ...args.flatMap(([c, a]) => [c, String(a)])].join(' '); |
| 12 | + const goExecSync = goSync(() => |
| 13 | + execSync(`node ${CLI_EXECUTABLE} ${formattedCommand}`, { |
| 14 | + stdio: ['pipe', 'pipe', 'pipe'], |
| 15 | + }) |
| 16 | + ); |
| 17 | + if (!goExecSync.success) { |
| 18 | + // rethrow the output of the CLI |
| 19 | + throw new Error((goExecSync.error as any).reason.stderr.toString().trim()); |
| 20 | + } |
| 21 | + |
| 22 | + const stdout = goExecSync.data?.toString().trim() || ''; |
| 23 | + return stdout; |
| 24 | + }; |
| 25 | + |
| 26 | + it('should throw an error for an unknown chain id', () => { |
| 27 | + expect(() => { |
| 28 | + execCommand('compute-dapp-id', ['--dapp-alias', 'lendle'], ['--chain-id', '0']); |
| 29 | + }).toThrow('⚠️ Chain with ID 0 is not known'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should throw an error for an unknown dApp alias', () => { |
| 33 | + expect(() => { |
| 34 | + execCommand('compute-dapp-id', ['--dapp-alias', 'unsupported-dapp'], ['--chain-id', '5000']); |
| 35 | + }).toThrow('⚠️ Could not find any record for alias "unsupported-dapp"'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should throw an error for unsupported chain ID', () => { |
| 39 | + expect(() => { |
| 40 | + execCommand('compute-dapp-id', ['--dapp-alias', 'lendle'], ['--chain-id', '1']); |
| 41 | + }).toThrow('⚠️ dApp alias "lendle" is not available on chain "Ethereum"'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return an invalid output if strict is false', () => { |
| 45 | + const output = execCommand( |
| 46 | + 'compute-dapp-id', |
| 47 | + ['--dapp-alias', 'unsupported-dapp'], |
| 48 | + ['--chain-id', '0'], |
| 49 | + ['--strict', false] |
| 50 | + ); |
| 51 | + expect(output).toMatch( |
| 52 | + 'dApp alias: unsupported-dapp\nchain: 0\n\n• dApp ID: 113044575011858809962820051290270246401920929513853405225169263442003318378526' |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should compute dApp ID for lendle on mantle', () => { |
| 57 | + const output = execCommand('compute-dapp-id', ['--dapp-alias', 'lendle'], ['--chain-id', '5000']); |
| 58 | + expect(output).toMatch( |
| 59 | + 'dApp alias: lendle\nchain: Mantle\n\n• dApp ID: 3006187377348428698321862179080566497381498372321749245241868771911713393091' |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should compute dApp ID for mach-finance on sonic', () => { |
| 64 | + const output = execCommand('compute-dapp-id', ['--dapp-alias', 'mach-finance'], ['--chain-id', '146']); |
| 65 | + expect(output).toMatch( |
| 66 | + 'dApp alias: mach-finance\nchain: Sonic\n\n• dApp ID: 103191103032841018751746810000516216875988320776131204933373404128958541332502' |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should match help output', () => { |
| 71 | + const output = execCommand('help'); |
| 72 | + expect(output).toMatchSnapshot(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should throw an error for an unknown chain id while checking Api3ReaderProxyV1 address', () => { |
| 76 | + expect(() => { |
| 77 | + execCommand( |
| 78 | + 'print-api3readerproxyv1-address', |
| 79 | + ['--dapp-alias', 'unsupported-dapp'], |
| 80 | + ['--chain-id', '0'], |
| 81 | + ['--dapi-name', 'ETH/USD'] |
| 82 | + ); |
| 83 | + }).toThrow('Chain with ID 0 is not known'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should throw an error for an unknown dApp alias while checking Api3ReaderProxyV1 address', () => { |
| 87 | + expect(() => { |
| 88 | + execCommand( |
| 89 | + 'print-api3readerproxyv1-address', |
| 90 | + ['--dapp-alias', 'unsupported-dapp'], |
| 91 | + ['--chain-id', '5000'], |
| 92 | + ['--dapi-name', 'ETH/USD'] |
| 93 | + ); |
| 94 | + }).toThrow('⚠️ Could not find any record for alias "unsupported-dapp"'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should throw an error for an unsupported dApi name', () => { |
| 98 | + expect(() => { |
| 99 | + execCommand( |
| 100 | + 'print-api3readerproxyv1-address', |
| 101 | + ['--dapp-alias', 'lendle'], |
| 102 | + ['--chain-id', '5000'], |
| 103 | + ['--dapi-name', 'UNSUPPORTED/USD'] |
| 104 | + ); |
| 105 | + }).toThrow('⚠️ Attempted to read the feed and failed'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should succeed print-api3readerproxyv1-address on mantle', () => { |
| 109 | + const output = execCommand( |
| 110 | + 'print-api3readerproxyv1-address', |
| 111 | + ['--dapp-alias', 'lendle'], |
| 112 | + ['--chain-id', '5000'], |
| 113 | + ['--dapi-name', 'ETH/USD'] |
| 114 | + ); |
| 115 | + expect(output).toMatchSnapshot(); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should succeed print-api3readerproxyv1-address on sonic', () => { |
| 119 | + const output = execCommand( |
| 120 | + 'print-api3readerproxyv1-address', |
| 121 | + ['--dapp-alias', 'mach-finance'], |
| 122 | + ['--chain-id', '146'], |
| 123 | + ['--dapi-name', 'S/USD'] |
| 124 | + ); |
| 125 | + expect(output).toMatchSnapshot(); |
| 126 | + }); |
| 127 | +}); |
0 commit comments