Skip to content

Commit 1bbadb1

Browse files
authored
Merge pull request #581 from api3dao/test-the-cli-command
Add tests for the CLI command
2 parents 1d3ef45 + d65251b commit 1bbadb1

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

.changeset/chatty-buses-drop.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

src/__snapshots__/cli.test.ts.snap

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`cli tests should match help output 1`] = `
4+
"cli.js [command]
5+
6+
Commands:
7+
cli.js print-api3readerproxyv1-address Prints the dApp-specific
8+
Api3ReaderProxyV1 address
9+
cli.js compute-dapp-id Computes the dApp ID for a given dApp
10+
alias and chain ID
11+
12+
Options:
13+
--version Show version number [boolean]
14+
--help Show help [boolean]"
15+
`;
16+
17+
exports[`cli tests should succeed print-api3readerproxyv1-address on mantle 1`] = `
18+
"dApp alias: lendle
19+
chain: Mantle
20+
dAPI name: ETH/USD
21+
• Please confirm that https://market.api3.org/mantle/eth-usd points to an active feed.
22+
• Your proxy is at https://mantlescan.xyz/address/0x776E79D916e49BBDb8FEe0F43fF148C2Ed3bE125
23+
Please confirm that there is a contract deployed at this address before using it."
24+
`;
25+
26+
exports[`cli tests should succeed print-api3readerproxyv1-address on sonic 1`] = `
27+
"dApp alias: mach-finance
28+
chain: Sonic
29+
dAPI name: S/USD
30+
• Please confirm that https://market.api3.org/sonic/s-usd points to an active feed.
31+
• Your proxy is at https://sonicscan.org/address/0x2551A2a96988829D2a55c3b02b88E138023D1cE8
32+
Please confirm that there is a contract deployed at this address before using it."
33+
`;

src/cli.test.ts

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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

Comments
 (0)