|
| 1 | +import ipfsToGatewayUrl, { IPFS_GATEWAY_BASE, getIpfsPath, isIpfsUrl } from './ipfs'; |
| 2 | + |
| 3 | +// CID taken from a real mainnet NFT whose on-chain data URI is ipfs:// |
| 4 | +const CID_V1 = 'bafybeiceg2gltyhlkukwetn26k7t2zdvthg4u4c6uj23rpni2adzgvo5si'; |
| 5 | +const CID_V0 = 'QmPK1s3pNYLi9ERiq3BDxKa4XosgWwFRQUydHUtz4YgpqB'; |
| 6 | + |
| 7 | +describe('isIpfsUrl', () => { |
| 8 | + it('matches the ipfs scheme case-insensitively', () => { |
| 9 | + expect(isIpfsUrl(`ipfs://${CID_V1}/020.png`)).toBe(true); |
| 10 | + expect(isIpfsUrl(`IPFS://${CID_V1}`)).toBe(true); |
| 11 | + }); |
| 12 | + |
| 13 | + it('does not match other schemes', () => { |
| 14 | + expect(isIpfsUrl(`https://ipfs.io/ipfs/${CID_V1}`)).toBe(false); |
| 15 | + expect(isIpfsUrl('')).toBe(false); |
| 16 | + }); |
| 17 | +}); |
| 18 | + |
| 19 | +describe('getIpfsPath', () => { |
| 20 | + it('returns the CID and path', () => { |
| 21 | + expect(getIpfsPath(`ipfs://${CID_V1}/020.png`)).toBe(`${CID_V1}/020.png`); |
| 22 | + expect(getIpfsPath(`ipfs://${CID_V1}`)).toBe(CID_V1); |
| 23 | + }); |
| 24 | + |
| 25 | + it('strips the redundant ipfs/ prefix some minting tools produce', () => { |
| 26 | + expect(getIpfsPath(`ipfs://ipfs/${CID_V1}/020.png`)).toBe(`${CID_V1}/020.png`); |
| 27 | + }); |
| 28 | + |
| 29 | + it('preserves the case of CIDv0 base58 hashes', () => { |
| 30 | + expect(getIpfsPath(`ipfs://${CID_V0}/image.png`)).toBe(`${CID_V0}/image.png`); |
| 31 | + }); |
| 32 | + |
| 33 | + it('returns undefined for non-ipfs URLs and a bare scheme', () => { |
| 34 | + expect(getIpfsPath(`https://example.com/${CID_V1}`)).toBeUndefined(); |
| 35 | + expect(getIpfsPath('ipfs://')).toBeUndefined(); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe('ipfsToGatewayUrl', () => { |
| 40 | + it('translates ipfs:// URIs to the HTTPS gateway', () => { |
| 41 | + expect(ipfsToGatewayUrl(`ipfs://${CID_V1}/020.png`)).toBe(`${IPFS_GATEWAY_BASE}${CID_V1}/020.png`); |
| 42 | + expect(ipfsToGatewayUrl(`ipfs://ipfs/${CID_V1}`)).toBe(`${IPFS_GATEWAY_BASE}${CID_V1}`); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns non-ipfs URLs unchanged', () => { |
| 46 | + const url = 'https://example.com/image.png?size=large'; |
| 47 | + expect(ipfsToGatewayUrl(url)).toBe(url); |
| 48 | + }); |
| 49 | + |
| 50 | + it('returns an unusable bare scheme unchanged so validation rejects it', () => { |
| 51 | + expect(ipfsToGatewayUrl('ipfs://')).toBe('ipfs://'); |
| 52 | + }); |
| 53 | +}); |
0 commit comments