Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-test-coverage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@satoshai/abi-cli": patch
---

Improve test coverage and fix typecheck to include test files
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dev": "tsup --watch",
"test": "vitest run",
"test:watch": "vitest",
"typecheck": "tsc --noEmit",
"typecheck": "tsc --noEmit -p tsconfig.check.json",
"lint": "eslint src",
"clean": "rm -rf dist",
"changeset": "changeset",
Expand Down
79 changes: 54 additions & 25 deletions tests/unit/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { parseContractId } from '../../src/fetcher.js';
import { generateTypescript, generateJson } from '../../src/codegen.js';
import { fetchCommand } from '../../src/commands/fetch.js';
import { sampleAbi } from './fixtures.js';

describe('CLI integration', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const runFetch = (args: Record<string, any>) =>
fetchCommand.run!({ args, rawArgs: [], cmd: fetchCommand } as never);

describe('fetchCommand', () => {
const originalFetch = globalThis.fetch;

beforeEach(() => {
Expand All @@ -14,46 +17,72 @@ describe('CLI integration', () => {
globalThis.fetch = originalFetch;
});

it('end-to-end: parse contract, fetch ABI, generate TypeScript', async () => {
it('writes TypeScript to stdout', async () => {
vi.mocked(globalThis.fetch).mockResolvedValueOnce({
ok: true,
json: async () => sampleAbi,
} as Response);

const contractId = 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait';
const { address, name } = parseContractId(contractId);
const chunks: string[] = [];
const writeSpy = vi.spyOn(process.stdout, 'write').mockImplementation((chunk) => {
chunks.push(String(chunk));
return true;
});
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});

expect(address).toBe('SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9');
expect(name).toBe('nft-trait');
await runFetch({
contract: 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait',
network: 'mainnet',
format: 'ts',
stdout: true,
});

const response = await globalThis.fetch(
`https://api.hiro.so/v2/contracts/interface/${address}/${name}`,
);
const abi = await (response as Response).json();
const output = chunks.join('');
expect(output).toContain('export const abi =');
expect(output).toContain('as const;');
expect(output).toContain('"transfer"');

const tsOutput = generateTypescript(contractId, abi);
expect(tsOutput).toContain('export const abi =');
expect(tsOutput).toContain('as const;');
expect(tsOutput).toContain('"transfer"');
writeSpy.mockRestore();
errorSpy.mockRestore();
});

it('end-to-end: parse contract, fetch ABI, generate JSON', async () => {
it('writes JSON to stdout', async () => {
vi.mocked(globalThis.fetch).mockResolvedValueOnce({
ok: true,
json: async () => sampleAbi,
} as Response);

const contractId = 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait';
const { address, name } = parseContractId(contractId);
const chunks: string[] = [];
const writeSpy = vi.spyOn(process.stdout, 'write').mockImplementation((chunk) => {
chunks.push(String(chunk));
return true;
});
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});

const response = await globalThis.fetch(
`https://api.hiro.so/v2/contracts/interface/${address}/${name}`,
);
const abi = await (response as Response).json();
await runFetch({
contract: 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait',
network: 'mainnet',
format: 'json',
stdout: true,
});

const jsonOutput = generateJson(abi);
const parsed = JSON.parse(jsonOutput);
const output = chunks.join('');
const parsed = JSON.parse(output);
expect(parsed.functions).toBeDefined();
expect(parsed.functions.length).toBe(4);

writeSpy.mockRestore();
errorSpy.mockRestore();
});

it('throws on invalid format', async () => {
await expect(
runFetch({
contract: 'SP2P.nft-trait',
network: 'mainnet',
format: 'yaml',
stdout: true,
}),
).rejects.toThrow('Invalid format "yaml"');
});
});
45 changes: 45 additions & 0 deletions tests/unit/fetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,49 @@ describe('fetchContractAbi', () => {
'https://api.testnet.hiro.so/v2/contracts/interface/ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM/my-contract',
);
});

it('uses the correct URL for devnet', async () => {
vi.mocked(globalThis.fetch).mockResolvedValueOnce({
ok: true,
json: async () => sampleAbi,
} as Response);

await fetchContractAbi('devnet', 'ST123', 'my-contract');

expect(globalThis.fetch).toHaveBeenCalledWith(
'http://localhost:3999/v2/contracts/interface/ST123/my-contract',
);
});

it('uses the correct URL for custom network', async () => {
vi.mocked(globalThis.fetch).mockResolvedValueOnce({
ok: true,
json: async () => sampleAbi,
} as Response);

await fetchContractAbi('https://custom.example.com', 'SP123', 'my-contract');

expect(globalThis.fetch).toHaveBeenCalledWith(
'https://custom.example.com/v2/contracts/interface/SP123/my-contract',
);
});

it('throws on network error', async () => {
vi.mocked(globalThis.fetch).mockRejectedValueOnce(new TypeError('fetch failed'));

await expect(
fetchContractAbi('mainnet', 'SP123', 'broken'),
).rejects.toThrow('Network error fetching ABI for SP123.broken on mainnet');
});

it('throws on invalid JSON response', async () => {
vi.mocked(globalThis.fetch).mockResolvedValueOnce({
ok: true,
json: async () => { throw new SyntaxError('Unexpected token'); },
} as unknown as Response);

await expect(
fetchContractAbi('mainnet', 'SP123', 'broken'),
).rejects.toThrow('Invalid JSON response for SP123.broken on mainnet');
});
});
8 changes: 8 additions & 0 deletions tsconfig.check.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"rootDir": "."
},
"include": ["src/**/*", "tests/**/*"],
"exclude": ["node_modules", "dist"]
}