Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed May 28, 2024
1 parent 28dcced commit 0a09254
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 33 deletions.
4 changes: 2 additions & 2 deletions packages/lambda/src/lib/store/block-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { DynamoTable } from './dynamo-table.js'

const CAR_CODE = 0x0202
const LIMIT = 25
const BUCKET_URL = `https://carpark-${process.env.STAGE ?? 'dev'}-0.r2.w3s.link`
export const BUCKET_URL = `https://carpark-${process.env.STAGE ?? 'dev'}-0.r2.w3s.link`

/**
* Materializes claims on demand using block indexes stored in DynamoDB.
Expand Down Expand Up @@ -127,7 +127,7 @@ const buildClaim = async (content, invocation) => {
*
* @param {string} key
*/
const bucketKeyToPartCID = key => {
export const bucketKeyToPartCID = key => {
const filename = String(key.split('/').at(-1))
const [hash] = filename.split('.')
try {
Expand Down
36 changes: 5 additions & 31 deletions packages/lambda/test/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { createDynamo, createDynamoTable, createDynamoBlocksTable, createS3, cre
import * as CARv2Index from './helpers/carv2-index.js'
import { PutItemCommand } from '@aws-sdk/client-dynamodb'
import { CarIndexer } from '@ipld/car/indexer'
import { BUCKET_URL, bucketKeyToPartCID } from '../src/lib/store/block-index.js'

/**
* @typedef {{
Expand Down Expand Up @@ -218,35 +219,6 @@ test('should return equivalence claim for either cid', async t => {
t.true(Bytes.equals(claim.bytes, equivalentClaim.bytes))
})

test('should materialize location claim from block index', async t => {
const { signer } = t.context
const dynamo = t.context.dynamo.client
const root = await Block.encode({ value: 'go go go', hasher: sha256, codec: dagCBOR })
const car = encodeCAR({ roots: [root], blocks: new Map([[root.cid.toString(), root]]) })
const carpath = 'region/bucket/pickup/rootCid/rootCid.root.car'
const indexer = await CarIndexer.fromBytes(car)
const index = new Map()
for await (const { cid, offset, blockOffset, blockLength } of indexer) {
index.set(cid.toString(), { cid, offset, blockOffset, blockLength })
}
const offset = index.get(root.cid.toString()).blockOffset
const blocksTable = await createDynamoBlocksTable(dynamo)
await dynamo.send(new PutItemCommand({
TableName: blocksTable,
Item: {
blockmultihash: { S: base58btc.encode(root.cid.multihash.bytes) },
carpath: { S: carpath },
offset: { N: offset },
length: { N: root.bytes.byteLength }
}
}))

const blockClaims = new BlockIndexClaimFetcher(dynamo, blocksTable, signer)
const res = await blockClaims.get(root.cid)
t.is(res.length, 1)
testLocationClaim({ t, value: res[0].value, root, carpath, offset, signer })
})

test('should materialize location claim from /raw block index', async t => {
const { signer } = t.context
const dynamo = t.context.dynamo.client
Expand Down Expand Up @@ -285,6 +257,8 @@ function testLocationClaim ({ t, value, root, carpath, offset, signer }) {
t.is(value.with, signer.did())
t.is(value.nb.content.toString(), root.cid.toString())
t.like(value.nb.range, { offset, length: root.bytes.byteLength })
const [, bucket, ...key] = carpath.split('/')
t.true(value.nb.location.includes(`https://${bucket}.s3.amazonaws.com/${key.join('/')}`))
const [, , ...key] = carpath.split('/')
const part = bucketKeyToPartCID(key.join('/'))
const url = new URL(`/${part}/${part}.car`, BUCKET_URL)
t.true(value.nb.location.includes(url.toString()))
}

0 comments on commit 0a09254

Please sign in to comment.