|
| 1 | +import { |
| 2 | + createFileChunkIpldNode, |
| 3 | + PBNode, |
| 4 | + cidToString, |
| 5 | + cidOfNode, |
| 6 | + createChunkedFileIpldNode, |
| 7 | + processFileToIPLDFormat, |
| 8 | + stringToCid, |
| 9 | + decodeNode, |
| 10 | + NODE_METADATA_SIZE, |
| 11 | +} from '@autonomys/auto-dag-data' |
| 12 | +import { dsnFetcher } from '../src/services/dsnFetcher.js' |
| 13 | +import { jest } from '@jest/globals' |
| 14 | +import { randomBytes } from 'crypto' |
| 15 | +import { MemoryBlockstore } from 'blockstore-core' |
| 16 | + |
| 17 | +describe('Partial downloads', () => { |
| 18 | + beforeEach(() => { |
| 19 | + jest.clearAllMocks() |
| 20 | + }) |
| 21 | + afterEach(() => { |
| 22 | + jest.clearAllMocks() |
| 23 | + }) |
| 24 | + |
| 25 | + it('should return the correct chunk', async () => { |
| 26 | + const nodeMapByCID: Record<string, PBNode> = {} |
| 27 | + const chunks = ['a', 'b', 'c', 'd', 'e'] |
| 28 | + |
| 29 | + // Create the tree of nodes |
| 30 | + const nodes = chunks.map((chunk) => |
| 31 | + createFileChunkIpldNode(Buffer.from(chunk, 'utf-8')), |
| 32 | + ) |
| 33 | + const headNode = createChunkedFileIpldNode( |
| 34 | + nodes.map((node) => cidOfNode(node)), |
| 35 | + 5n, |
| 36 | + 1, |
| 37 | + ) |
| 38 | + |
| 39 | + // Create a map of the nodes by CID |
| 40 | + nodes.forEach((node) => { |
| 41 | + const cid = cidToString(cidOfNode(node)) |
| 42 | + nodeMapByCID[cid] = node |
| 43 | + }) |
| 44 | + nodeMapByCID[cidToString(cidOfNode(headNode))] = headNode |
| 45 | + |
| 46 | + const cid = cidToString(cidOfNode(headNode)) |
| 47 | + |
| 48 | + jest.spyOn(dsnFetcher, 'fetchNode').mockImplementation(async (cid) => { |
| 49 | + return nodeMapByCID[cid] |
| 50 | + }) |
| 51 | + |
| 52 | + // First node (head) should return null |
| 53 | + const chunk = await dsnFetcher.getPartial(cid, 0) |
| 54 | + expect(chunk).toBeInstanceOf(Buffer) |
| 55 | + expect(chunk?.length).toBe(0) |
| 56 | + |
| 57 | + // Chunks 1-5 should return the correct chunk |
| 58 | + let i = 0 |
| 59 | + while (i < chunks.length) { |
| 60 | + // i + 1 for offsetting the head node |
| 61 | + const chunk = await dsnFetcher.getPartial(cid, i + 1) |
| 62 | + expect(chunk).toBeDefined() |
| 63 | + expect(chunk?.toString('utf-8')).toBe(chunks[i]) |
| 64 | + i++ |
| 65 | + } |
| 66 | + |
| 67 | + // Chunk 6 should return empty buffer because it's past the end of the file |
| 68 | + const chunk6 = await dsnFetcher.getPartial(cid, 6) |
| 69 | + expect(chunk6).toBeNull() |
| 70 | + }) |
| 71 | + |
| 72 | + it('should work with a large file', async () => { |
| 73 | + const chunksCount = 30 |
| 74 | + const maxLinkPerNode = 5 |
| 75 | + const chunkSize = 50 |
| 76 | + const totalSize = chunksCount * chunkSize |
| 77 | + |
| 78 | + const chunks = Array.from({ length: chunksCount }).map(() => |
| 79 | + randomBytes(chunkSize), |
| 80 | + ) |
| 81 | + |
| 82 | + const blockstore = new MemoryBlockstore() |
| 83 | + |
| 84 | + const cid = cidToString( |
| 85 | + await processFileToIPLDFormat( |
| 86 | + blockstore, |
| 87 | + chunks, |
| 88 | + BigInt(totalSize), |
| 89 | + 'dag-cbor', |
| 90 | + { |
| 91 | + maxLinkPerNode, |
| 92 | + maxNodeSize: chunkSize + NODE_METADATA_SIZE, |
| 93 | + }, |
| 94 | + ), |
| 95 | + ) |
| 96 | + |
| 97 | + jest.spyOn(dsnFetcher, 'fetchNode').mockImplementation(async (cid) => { |
| 98 | + return decodeNode(await blockstore.get(stringToCid(cid))) |
| 99 | + }) |
| 100 | + |
| 101 | + let index = 0, |
| 102 | + received = 0 |
| 103 | + while (received < chunks.length) { |
| 104 | + const chunk = await dsnFetcher.getPartial(cid, index) |
| 105 | + index++ |
| 106 | + if (chunk && chunk?.length > 0) { |
| 107 | + expect(chunk.toString('hex')).toBe(chunks[received].toString('hex')) |
| 108 | + received++ |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + const finalResult = await dsnFetcher.getPartial(cid, index) |
| 113 | + expect(finalResult).toBeNull() |
| 114 | + }) |
| 115 | +}) |
0 commit comments