|
1 | 1 | import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
2 | 2 | import { syncCommand } from '../../src/commands/sync.js'; |
| 3 | +import { generateTypescript } from '../../src/codegen.js'; |
3 | 4 | import { sampleAbi } from './fixtures.js'; |
4 | 5 | import { writeFile, mkdir, readFile } from 'node:fs/promises'; |
5 | 6 | import { resolve, join } from 'node:path'; |
@@ -36,12 +37,14 @@ describe('syncCommand', () => { |
36 | 37 | errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
37 | 38 | vi.mocked(writeFile).mockResolvedValue(); |
38 | 39 | vi.mocked(mkdir).mockResolvedValue(undefined); |
| 40 | + process.exitCode = undefined; |
39 | 41 | }); |
40 | 42 |
|
41 | 43 | afterEach(() => { |
42 | 44 | globalThis.fetch = originalFetch; |
43 | 45 | errorSpy.mockRestore(); |
44 | 46 | vi.restoreAllMocks(); |
| 47 | + process.exitCode = undefined; |
45 | 48 | }); |
46 | 49 |
|
47 | 50 | it('syncs all contracts from config', async () => { |
@@ -259,4 +262,106 @@ describe('syncCommand', () => { |
259 | 262 | 'utf-8', |
260 | 263 | ); |
261 | 264 | }); |
| 265 | + |
| 266 | + describe('--check', () => { |
| 267 | + it('exits 0 when all local files match', async () => { |
| 268 | + const contractId = 'SP1.token-a'; |
| 269 | + const expected = generateTypescript(contractId, sampleAbi); |
| 270 | + mockConfig({ |
| 271 | + outDir: './abis', |
| 272 | + contracts: [{ id: contractId }], |
| 273 | + }); |
| 274 | + mockFetchSuccess(); |
| 275 | + vi.mocked(readFile).mockResolvedValueOnce(expected); |
| 276 | + |
| 277 | + await runSync({ config: '/tmp/abi.config.json', check: true }); |
| 278 | + |
| 279 | + expect(process.exitCode).toBeUndefined(); |
| 280 | + expect(writeFile).not.toHaveBeenCalled(); |
| 281 | + }); |
| 282 | + |
| 283 | + it('sets exitCode 1 when a file is stale', async () => { |
| 284 | + mockConfig({ |
| 285 | + outDir: './abis', |
| 286 | + contracts: [{ id: 'SP1.token-a' }], |
| 287 | + }); |
| 288 | + mockFetchSuccess(); |
| 289 | + vi.mocked(readFile).mockResolvedValueOnce('old content'); |
| 290 | + |
| 291 | + await runSync({ config: '/tmp/abi.config.json', check: true }); |
| 292 | + |
| 293 | + expect(process.exitCode).toBe(1); |
| 294 | + expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('Stale:')); |
| 295 | + expect(writeFile).not.toHaveBeenCalled(); |
| 296 | + }); |
| 297 | + |
| 298 | + it('sets exitCode 1 when a file is missing', async () => { |
| 299 | + mockConfig({ |
| 300 | + outDir: './abis', |
| 301 | + contracts: [{ id: 'SP1.token-a' }], |
| 302 | + }); |
| 303 | + mockFetchSuccess(); |
| 304 | + vi.mocked(readFile).mockRejectedValueOnce( |
| 305 | + Object.assign(new Error('ENOENT'), { code: 'ENOENT' }), |
| 306 | + ); |
| 307 | + |
| 308 | + await runSync({ config: '/tmp/abi.config.json', check: true }); |
| 309 | + |
| 310 | + expect(process.exitCode).toBe(1); |
| 311 | + expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('Missing:')); |
| 312 | + expect(writeFile).not.toHaveBeenCalled(); |
| 313 | + }); |
| 314 | + |
| 315 | + it('reports mixed pass/fail across multiple contracts', async () => { |
| 316 | + const contractA = 'SP1.token-a'; |
| 317 | + const expectedA = generateTypescript(contractA, sampleAbi); |
| 318 | + mockConfig({ |
| 319 | + outDir: './abis', |
| 320 | + contracts: [{ id: contractA }, { id: 'SP2.token-b' }], |
| 321 | + }); |
| 322 | + mockFetchSuccess(2); |
| 323 | + vi.mocked(readFile).mockResolvedValueOnce(expectedA); |
| 324 | + vi.mocked(readFile).mockResolvedValueOnce('old content'); |
| 325 | + |
| 326 | + await runSync({ config: '/tmp/abi.config.json', check: true }); |
| 327 | + |
| 328 | + expect(process.exitCode).toBe(1); |
| 329 | + expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('Stale:')); |
| 330 | + expect(writeFile).not.toHaveBeenCalled(); |
| 331 | + }); |
| 332 | + |
| 333 | + it('respects name alias for filename resolution', async () => { |
| 334 | + const contractId = 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.amm-pool-v2-01'; |
| 335 | + const expected = generateTypescript(contractId, sampleAbi); |
| 336 | + mockConfig({ |
| 337 | + outDir: './abis', |
| 338 | + contracts: [{ id: contractId, name: 'amm-pool' }], |
| 339 | + }); |
| 340 | + mockFetchSuccess(); |
| 341 | + vi.mocked(readFile).mockResolvedValueOnce(expected); |
| 342 | + |
| 343 | + await runSync({ config: '/tmp/abi.config.json', check: true }); |
| 344 | + |
| 345 | + expect(readFile).toHaveBeenCalledWith( |
| 346 | + join(resolve('./abis'), 'amm-pool.ts'), |
| 347 | + 'utf-8', |
| 348 | + ); |
| 349 | + expect(process.exitCode).toBeUndefined(); |
| 350 | + }); |
| 351 | + |
| 352 | + it('does not generate barrel file', async () => { |
| 353 | + const contractId = 'SP1.token-a'; |
| 354 | + const expected = generateTypescript(contractId, sampleAbi); |
| 355 | + mockConfig({ |
| 356 | + outDir: './abis', |
| 357 | + contracts: [{ id: contractId }], |
| 358 | + }); |
| 359 | + mockFetchSuccess(); |
| 360 | + vi.mocked(readFile).mockResolvedValueOnce(expected); |
| 361 | + |
| 362 | + await runSync({ config: '/tmp/abi.config.json', check: true }); |
| 363 | + |
| 364 | + expect(writeFile).not.toHaveBeenCalled(); |
| 365 | + }); |
| 366 | + }); |
262 | 367 | }); |
0 commit comments