|
| 1 | +import assert from 'assert' |
| 2 | +import { setup } from 'iso-web/msw' |
| 3 | +import { createPublicClient, http, toHex } from 'viem' |
| 4 | +import { calibration, mainnet } from '../src/chains.ts' |
| 5 | +import { JSONRPC, presets } from '../src/mocks/jsonrpc/index.ts' |
| 6 | +import { findPieceIdsByCid, findPieceIdsByCidCall } from '../src/pdp-verifier/find-piece-ids-by-cid.ts' |
| 7 | +import * as Piece from '../src/piece/piece.ts' |
| 8 | + |
| 9 | +describe('findPieceIdsByCid', () => { |
| 10 | + const server = setup() |
| 11 | + |
| 12 | + before(async () => { |
| 13 | + await server.start() |
| 14 | + }) |
| 15 | + |
| 16 | + after(() => { |
| 17 | + server.stop() |
| 18 | + }) |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + server.resetHandlers() |
| 22 | + }) |
| 23 | + |
| 24 | + describe('findPieceIdsByCidCall', () => { |
| 25 | + const pieceCid = Piece.parse('bafkzcibcd4bdomn3tgwgrh3g532zopskstnbrd2n3sxfqbze7rxt7vqn7veigmy') |
| 26 | + |
| 27 | + it('should create call with calibration chain defaults', () => { |
| 28 | + const call = findPieceIdsByCidCall({ |
| 29 | + chain: calibration, |
| 30 | + dataSetId: 1n, |
| 31 | + pieceCid, |
| 32 | + }) |
| 33 | + |
| 34 | + assert.equal(call.functionName, 'findPieceIdsByCid') |
| 35 | + assert.deepEqual(call.args, [1n, { data: toHex(pieceCid.bytes) }, 0n, 1n]) |
| 36 | + assert.equal(call.address, calibration.contracts.pdp.address) |
| 37 | + assert.equal(call.abi, calibration.contracts.pdp.abi) |
| 38 | + }) |
| 39 | + |
| 40 | + it('should create call with mainnet chain defaults', () => { |
| 41 | + const call = findPieceIdsByCidCall({ |
| 42 | + chain: mainnet, |
| 43 | + dataSetId: 1n, |
| 44 | + pieceCid, |
| 45 | + }) |
| 46 | + |
| 47 | + assert.equal(call.functionName, 'findPieceIdsByCid') |
| 48 | + assert.deepEqual(call.args, [1n, { data: toHex(pieceCid.bytes) }, 0n, 1n]) |
| 49 | + assert.equal(call.address, mainnet.contracts.pdp.address) |
| 50 | + assert.equal(call.abi, mainnet.contracts.pdp.abi) |
| 51 | + }) |
| 52 | + |
| 53 | + it('should use provided startPieceId and limit', () => { |
| 54 | + const call = findPieceIdsByCidCall({ |
| 55 | + chain: calibration, |
| 56 | + dataSetId: 1n, |
| 57 | + pieceCid, |
| 58 | + startPieceId: 10n, |
| 59 | + limit: 5n, |
| 60 | + }) |
| 61 | + |
| 62 | + assert.deepEqual(call.args, [1n, { data: toHex(pieceCid.bytes) }, 10n, 5n]) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should use custom address when provided', () => { |
| 66 | + const customAddress = '0x1234567890123456789012345678901234567890' |
| 67 | + const call = findPieceIdsByCidCall({ |
| 68 | + chain: calibration, |
| 69 | + dataSetId: 1n, |
| 70 | + pieceCid, |
| 71 | + contractAddress: customAddress, |
| 72 | + }) |
| 73 | + |
| 74 | + assert.equal(call.address, customAddress) |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + describe('findPieceIdsByCid (with mocked RPC)', () => { |
| 79 | + it('should find piece IDs by CID', async () => { |
| 80 | + server.use(JSONRPC(presets.basic)) |
| 81 | + |
| 82 | + const client = createPublicClient({ |
| 83 | + chain: calibration, |
| 84 | + transport: http(), |
| 85 | + }) |
| 86 | + |
| 87 | + const pieceCid = Piece.parse('bafkzcibcd4bdomn3tgwgrh3g532zopskstnbrd2n3sxfqbze7rxt7vqn7veigmy') |
| 88 | + const result = await findPieceIdsByCid(client, { dataSetId: 1n, pieceCid }) |
| 89 | + |
| 90 | + assert.ok(Array.isArray(result)) |
| 91 | + assert.equal(result.length, 1) |
| 92 | + assert.equal(result[0], 0n) |
| 93 | + }) |
| 94 | + |
| 95 | + it('should return empty array when piece not found', async () => { |
| 96 | + server.use( |
| 97 | + JSONRPC({ |
| 98 | + ...presets.basic, |
| 99 | + pdpVerifier: { |
| 100 | + ...presets.basic.pdpVerifier, |
| 101 | + findPieceIdsByCid: () => [[]], |
| 102 | + }, |
| 103 | + }) |
| 104 | + ) |
| 105 | + |
| 106 | + const client = createPublicClient({ |
| 107 | + chain: calibration, |
| 108 | + transport: http(), |
| 109 | + }) |
| 110 | + |
| 111 | + const pieceCid = Piece.parse('bafkzcibcd4bdomn3tgwgrh3g532zopskstnbrd2n3sxfqbze7rxt7vqn7veigmy') |
| 112 | + const result = await findPieceIdsByCid(client, { dataSetId: 1n, pieceCid }) |
| 113 | + |
| 114 | + assert.ok(Array.isArray(result)) |
| 115 | + assert.equal(result.length, 0) |
| 116 | + }) |
| 117 | + |
| 118 | + it('should return multiple piece IDs', async () => { |
| 119 | + server.use( |
| 120 | + JSONRPC({ |
| 121 | + ...presets.basic, |
| 122 | + pdpVerifier: { |
| 123 | + ...presets.basic.pdpVerifier, |
| 124 | + findPieceIdsByCid: () => [[42n, 99n]], |
| 125 | + }, |
| 126 | + }) |
| 127 | + ) |
| 128 | + |
| 129 | + const client = createPublicClient({ |
| 130 | + chain: calibration, |
| 131 | + transport: http(), |
| 132 | + }) |
| 133 | + |
| 134 | + const pieceCid = Piece.parse('bafkzcibcd4bdomn3tgwgrh3g532zopskstnbrd2n3sxfqbze7rxt7vqn7veigmy') |
| 135 | + const result = await findPieceIdsByCid(client, { dataSetId: 1n, pieceCid, limit: 10n }) |
| 136 | + |
| 137 | + assert.ok(Array.isArray(result)) |
| 138 | + assert.equal(result.length, 2) |
| 139 | + assert.equal(result[0], 42n) |
| 140 | + assert.equal(result[1], 99n) |
| 141 | + }) |
| 142 | + }) |
| 143 | +}) |
0 commit comments