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