|
| 1 | +import { describe, expect, test } from 'bun:test'; |
| 2 | + |
| 3 | +// ============================================================================= |
| 4 | +// CLI `address` command — spawn-based smoke tests |
| 5 | +// |
| 6 | +// The address-derivation logic is unit-tested in src/sign.test.ts |
| 7 | +// (`accountFromEnv`); these cover the CLI wiring: key/mnemonic in, address out, |
| 8 | +// clear error + exit 1 when the key material is missing or malformed. |
| 9 | +// ============================================================================= |
| 10 | + |
| 11 | +// The anvil account #0 — its private key and mnemonic both derive this address. |
| 12 | +const TEST_PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'; |
| 13 | +const TEST_MNEMONIC = 'test test test test test test test test test test test junk'; |
| 14 | +const TEST_ADDRESS = '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'; |
| 15 | + |
| 16 | +async function runAddress( |
| 17 | + env: Record<string, string | undefined> |
| 18 | +): Promise<{ exitCode: number; stdout: string; stderr: string }> { |
| 19 | + const { AIRNODE_PRIVATE_KEY: _k, AIRNODE_MNEMONIC: _m, ...base } = process.env; |
| 20 | + const proc = Bun.spawn(['bun', '--env-file', '/dev/null', 'src/cli/index.ts', 'address'], { |
| 21 | + env: { ...base, ...env }, |
| 22 | + stdout: 'pipe', |
| 23 | + stderr: 'pipe', |
| 24 | + }); |
| 25 | + const [exitCode, stdout, stderr] = await Promise.all([ |
| 26 | + proc.exited, |
| 27 | + new Response(proc.stdout).text(), |
| 28 | + new Response(proc.stderr).text(), |
| 29 | + ]); |
| 30 | + return { exitCode, stdout, stderr }; |
| 31 | +} |
| 32 | + |
| 33 | +describe('address CLI', () => { |
| 34 | + test('derives the address from AIRNODE_PRIVATE_KEY', async () => { |
| 35 | + const { exitCode, stdout } = await runAddress({ AIRNODE_PRIVATE_KEY: TEST_PRIVATE_KEY }); |
| 36 | + expect(exitCode).toBe(0); |
| 37 | + expect(stdout).toContain(TEST_ADDRESS); |
| 38 | + }); |
| 39 | + |
| 40 | + test('derives the address from AIRNODE_MNEMONIC', async () => { |
| 41 | + const { exitCode, stdout } = await runAddress({ AIRNODE_MNEMONIC: TEST_MNEMONIC }); |
| 42 | + expect(exitCode).toBe(0); |
| 43 | + expect(stdout).toContain(TEST_ADDRESS); |
| 44 | + }); |
| 45 | + |
| 46 | + test('exits 1 with a clear error when neither variable is set', async () => { |
| 47 | + const { exitCode, stderr } = await runAddress({}); |
| 48 | + expect(exitCode).toBe(1); |
| 49 | + expect(stderr).toContain('AIRNODE_PRIVATE_KEY or AIRNODE_MNEMONIC environment variable is required'); |
| 50 | + }); |
| 51 | + |
| 52 | + test('exits 1 with a clear error on a malformed private key', async () => { |
| 53 | + const { exitCode, stderr } = await runAddress({ AIRNODE_PRIVATE_KEY: '0xnothex' }); |
| 54 | + expect(exitCode).toBe(1); |
| 55 | + expect(stderr).toContain('AIRNODE_PRIVATE_KEY must be a 0x-prefixed 32-byte hex string'); |
| 56 | + }); |
| 57 | +}); |
0 commit comments