This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a785d80
commit d1d3e8a
Showing
11 changed files
with
405 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { test } from './helpers/context.js' | ||
|
||
import git from 'git-rev-sync' | ||
import delay from 'delay' | ||
import { toString } from 'uint8arrays/to-string' | ||
import { Dealer } from '@web3-storage/filecoin-client' | ||
import { randomAggregate } from '@web3-storage/filecoin-api/test' | ||
|
||
import { getDealerClientConfig } from './helpers/dealer-client.js' | ||
import { pollTableItem } from './helpers/table.js' | ||
import { pollBucketItem } from './helpers/bucket.js' | ||
import { | ||
getApiEndpoint, | ||
getStage, | ||
getDealStoreDynamoDb, | ||
getOfferStoreBucketInfo | ||
} from './helpers/deployment.js' | ||
import pRetry from 'p-retry' | ||
|
||
test.before(t => { | ||
t.context = { | ||
apiEndpoint: getApiEndpoint(), | ||
dealStoreDynamo: getDealStoreDynamoDb(), | ||
offerStoreBucket: getOfferStoreBucketInfo() | ||
} | ||
}) | ||
|
||
test('GET /version', async t => { | ||
const stage = getStage() | ||
const response = await fetch(`${t.context.apiEndpoint}/version`) | ||
t.is(response.status, 200) | ||
|
||
const body = await response.json() | ||
t.is(body.env, stage) | ||
t.is(body.commit, git.long('.')) | ||
}) | ||
|
||
test('POST /', async t => { | ||
const { | ||
invocationConfig, | ||
connection | ||
} = await getDealerClientConfig(new URL(t.context.apiEndpoint)) | ||
|
||
// Create a random aggregate | ||
const label = 'label' | ||
const { pieces, aggregate } = await randomAggregate(100, 128) | ||
const offer = pieces.map((p) => p.link) | ||
|
||
const res = await Dealer.dealQueue( | ||
invocationConfig, | ||
aggregate.link.link(), | ||
offer, | ||
invocationConfig.with, | ||
label, | ||
{ connection } | ||
) | ||
|
||
t.truthy(res.out.ok) | ||
t.falsy(res.out.error) | ||
t.truthy(aggregate.link.link().equals(res.out.ok?.aggregate)) | ||
|
||
// wait for deal-store entry to exist given it is propagated with a queue message | ||
await delay(5_000) | ||
|
||
/** @type {import('../packages/core/src/data/deal.js').EncodedDeal | undefined} */ | ||
// @ts-expect-error does not automatically infer | ||
const dealEntry = await pollTableItem( | ||
t.context.dealStoreDynamo.client, | ||
t.context.dealStoreDynamo.tableName, | ||
{ aggregate: res.out.ok?.aggregate?.toString() } | ||
) | ||
if (!dealEntry) { | ||
throw new Error('deal store item was not found') | ||
} | ||
t.is(dealEntry.aggregate, aggregate.link.link().toString()) | ||
t.is(dealEntry.storefront, invocationConfig.with) | ||
t.is(dealEntry.stat, 0) | ||
t.truthy(dealEntry.insertedAt) | ||
t.truthy(dealEntry.offer) | ||
|
||
// Verify offer store | ||
// remove bucket encoding | ||
const bucketKey = dealEntry.offer.replace('s3://', '').replace(`${t.context.offerStoreBucket.bucket}/`, '') | ||
console.log('try to get bucket item...', bucketKey) | ||
const bucketItem = await pollBucketItem( | ||
t.context.offerStoreBucket.client, | ||
t.context.offerStoreBucket.bucket, | ||
bucketKey | ||
) | ||
if (!bucketItem) { | ||
throw new Error('offer store item was not found') | ||
} | ||
/** @type {import('../packages/core/src/data/offer.js').Offer} */ | ||
const encodedOffer = JSON.parse(toString(bucketItem)) | ||
|
||
t.is(encodedOffer.aggregate, aggregate.link.link().toString()) | ||
t.is(encodedOffer.tenant, invocationConfig.with) | ||
t.truthy(encodedOffer.orderID) | ||
t.deepEqual(encodedOffer.pieces, offer.map(o => o.toString())) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { GetObjectCommand } from '@aws-sdk/client-s3' | ||
import pRetry from 'p-retry' | ||
|
||
/** | ||
* @param {import('@aws-sdk/client-s3').S3Client} client | ||
* @param {string} bucketName | ||
* @param {string} key | ||
*/ | ||
export async function pollBucketItem (client, bucketName, key) { | ||
const cmd = new GetObjectCommand({ | ||
Bucket: bucketName, | ||
Key: key | ||
}) | ||
|
||
const response = await pRetry(async () => { | ||
let r | ||
try { | ||
r = await client.send(cmd) | ||
} catch (err) { | ||
if (err?.$metadata?.httpStatusCode === 404) { | ||
throw new Error('not found') | ||
} | ||
} | ||
|
||
return r | ||
}, { | ||
retries: 100, | ||
maxTimeout: 1000, | ||
minTimeout: 1000 | ||
}) | ||
|
||
return await response?.Body?.transformToByteArray() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import anyTest from 'ava' | ||
import dotenv from 'dotenv' | ||
|
||
dotenv.config({ | ||
path: '.env.local' | ||
}) | ||
|
||
/** | ||
* @typedef {object} Dynamo | ||
* @property {import('@aws-sdk/client-dynamodb').DynamoDBClient} client | ||
* @property {string} endpoint | ||
* @property {string} region | ||
* @property {string} tableName | ||
* | ||
* @typedef {object} Bucket | ||
* @property {import('@aws-sdk/client-s3').S3Client} client | ||
* @property {string} region | ||
* @property {string} bucket | ||
* | ||
* @typedef {object} Context | ||
* @property {string} apiEndpoint | ||
* @property {Dynamo} dealStoreDynamo | ||
* @property {Bucket} offerStoreBucket | ||
* | ||
* @typedef {import('ava').TestFn<Awaited<Context>>} TestContextFn | ||
*/ | ||
|
||
// eslint-disable-next-line unicorn/prefer-export-from | ||
export const test = /** @type {TestContextFn} */ (anyTest) |
Oops, something went wrong.