UNPKG

@api3/contracts

Version:

Contracts through which API3 services are delivered

212 lines 8.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const zod_1 = require("zod"); const types_1 = require("./types"); describe('chainSchema', () => { const validChain = { alias: 'ethereum', blockExplorerUrl: 'https://etherscan.io/', decimals: 18, id: '1', name: 'Ethereum', providers: [ { alias: 'default', rpcUrl: 'https://cloudflare-eth.com', }, ], symbol: 'ETH', testnet: false, verificationApi: { type: 'etherscan', }, }; it('should accept a valid chain', () => { expect(() => types_1.chainSchema.parse(validChain)).not.toThrow(); }); it('should accept a symbol of 6 characters or less', () => { const validChainWithSymbol = { ...validChain, symbol: 'ABCDEF', }; expect(() => types_1.chainSchema.parse(validChainWithSymbol)).not.toThrow(); }); it('should reject an empty symbol', () => { const invalidChain = { ...validChain, symbol: '', }; expect(() => types_1.chainSchema.parse(invalidChain)).toThrow(zod_1.z.ZodError); }); it('should reject a symbol longer than 6 characters', () => { const invalidChain = { ...validChain, symbol: 'ABCDEFG', }; expect(() => types_1.chainSchema.parse(invalidChain)).toThrow(zod_1.z.ZodError); }); }); describe('chainAlias', () => { it('should accept valid chain aliases', () => { expect(() => types_1.chainAlias.parse('ethereum')).not.toThrow(); expect(() => types_1.chainAlias.parse('bsc')).not.toThrow(); expect(() => types_1.chainAlias.parse('mantle')).not.toThrow(); }); it('should reject chain aliases not in the CHAINS array', () => { const resultInvalidFormat = types_1.chainAlias.safeParse('Mantle'); expect(resultInvalidFormat.error).toBeInstanceOf(zod_1.ZodError); expect(resultInvalidFormat.error?.issues).toStrictEqual([ { origin: 'string', code: 'invalid_format', format: 'regex', pattern: '/^[\\da-z-]+$/', path: [], message: 'Invalid string: must match pattern /^[\\da-z-]+$/', }, { code: 'custom', path: [], message: 'Invalid chain alias: Mantle' }, ]); const resultInvalidChainAlias = types_1.chainAlias.safeParse('ethereum-mainnet'); expect(resultInvalidChainAlias.error).toBeInstanceOf(zod_1.ZodError); expect(resultInvalidChainAlias.error?.issues).toStrictEqual([ { code: 'custom', path: [], message: 'Invalid chain alias: ethereum-mainnet' }, ]); }); }); describe('dappSchema', () => { it('accepts valid chain IDs and throws on invalid ones', () => { const validChainsDapp = { aliases: { 'valid-chains': { chains: ['ethereum', 'bsc', 'polygon'], title: 'Valid Chains DApp', }, }, homepageUrl: 'https://example.com', }; expect(() => types_1.dappSchema.parse(validChainsDapp)).not.toThrow(); const invalidChainsDapp = { aliases: { 'invalid-chains': { chains: ['ethereum', 'invalid-chain', 'polygon'], title: 'Invalid Chains DApp', }, }, homepageUrl: 'https://example.com', }; expect(() => types_1.dappSchema.parse(invalidChainsDapp)).toThrow(new zod_1.ZodError([ { code: 'custom', path: ['aliases', 'invalid-chains', 'chains', 1], message: 'Invalid chain alias: invalid-chain', }, ])); }); }); describe('chainProviderSchema', () => { it('should accept a valid provider with rpcUrl', () => { const validProvider = { alias: 'default', rpcUrl: 'https://dummy-rpc.com', }; expect(() => types_1.chainProviderSchema.parse(validProvider)).not.toThrow(); }); it('should accept a valid provider with homepageUrl', () => { const validProvider = { alias: 'paid-provider', homepageUrl: 'https://dummy-homepage.com', }; expect(() => types_1.chainProviderSchema.parse(validProvider)).not.toThrow(); }); it('should reject an invalid homepageUrl', () => { const invalidProvider = { alias: 'paid-provider', homepageUrl: 'dummy-homepage.com', }; expect(() => types_1.chainProviderSchema.parse(invalidProvider)).toThrow(new zod_1.ZodError([ { code: 'invalid_format', format: 'url', path: ['homepageUrl'], message: 'Invalid URL', }, ])); }); it('should reject an invalid rpcUrl', () => { const invalidProvider = { alias: 'paid-provider', rpcUrl: 'dummy-rpc.com', }; expect(() => types_1.chainProviderSchema.parse(invalidProvider)).toThrow(new zod_1.ZodError([ { code: 'invalid_format', format: 'url', path: ['rpcUrl'], message: 'Invalid URL', }, ])); }); it('should reject an invalid provider if either rpcUrl or homepageUrl is missing', () => { const invalidProvider = { alias: 'default', }; expect(() => types_1.chainProviderSchema.parse(invalidProvider)).toThrow(new zod_1.ZodError([ { code: 'custom', path: [], message: 'rpcUrl or homepageUrl is required', }, ])); }); }); describe('chainProvidersSchema', () => { it('should accept valid providers', () => { const validProviders = [ { alias: 'default', rpcUrl: 'https://dummy-rpc.com' }, { alias: 'public', rpcUrl: 'https://public-rpc.com' }, ]; expect(() => types_1.chainProvidersSchema.parse(validProviders)).not.toThrow(); }); it('should reject if no provider with alias "default" is present', () => { const invalidProviders = [{ alias: 'public', rpcUrl: 'https://public-rpc.com' }]; expect(() => types_1.chainProvidersSchema.parse(invalidProviders)).toThrow(new zod_1.ZodError([ { code: 'custom', path: [], message: "a provider with alias 'default' is required", }, ])); }); it('should reject if there are duplicate aliases', () => { const invalidProviders = [ { alias: 'default', rpcUrl: 'https://dummy-rpc.com' }, { alias: 'default', rpcUrl: 'https://another-rpc.com' }, ]; expect(() => types_1.chainProvidersSchema.parse(invalidProviders)).toThrow(new zod_1.ZodError([ { code: 'custom', path: [], message: "cannot contain duplicate 'alias' values", }, ])); }); it('should reject if "default" or "public" provider does not have rpcUrl', () => { const invalidProviders = [ { alias: 'default', homepageUrl: 'https://dummy-homepage.com' }, { alias: 'public', homepageUrl: 'https://public-homepage.com' }, ]; expect(() => types_1.chainProvidersSchema.parse(invalidProviders)).toThrow(new zod_1.ZodError([ { code: 'custom', path: [0, 'rpcUrl'], message: "providers with alias 'default' or 'public' must also have an 'rpcUrl'", }, { code: 'custom', path: [1, 'rpcUrl'], message: "providers with alias 'default' or 'public' must also have an 'rpcUrl'", }, ])); }); }); //# sourceMappingURL=types.test.js.map