Skip to content

Commit

Permalink
chore(tests): tests should be failing, add errors to upload responses
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Sep 1, 2023
1 parent 8262c90 commit a88550c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 19 deletions.
31 changes: 27 additions & 4 deletions src/common/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class TurboUnauthenticatedUploadService

// NOTE: our axios config (validateStatus) swallows errors, so failed data items will be ignored
const dataItemResponses = await Promise.all(uploadPromises);
const errors: { id: string; status: number; message: string }[] = [];
const postedDataItems = dataItemResponses.reduce(
(
postedDataItemsMap: Record<
Expand All @@ -79,9 +80,15 @@ export class TurboUnauthenticatedUploadService
dataItemResponse: AxiosResponse<TurboUploadDataItemResponse, 'id'>,
) => {
// handle the fulfilled response
const { status, data } = dataItemResponse;
const { status, data, statusText } = dataItemResponse;
if (![200, 202].includes(status)) {
// TODO: add to failed data items array
errors.push({
id: data.id ?? 'unknown',

status,
message: statusText,
});
return postedDataItemsMap;
}
const { id, ...dataItemCache } = data;
Expand All @@ -93,6 +100,7 @@ export class TurboUnauthenticatedUploadService

return {
dataItems: postedDataItems,
errors,
};
}
}
Expand Down Expand Up @@ -133,13 +141,15 @@ export class TurboAuthenticatedUploadService
{
headers: {
'content-type': 'application/octet-stream',
'content-length': 10,
},
},
);
});

// NOTE: our axios config (validateStatus) swallows errors, so failed data items will be ignored
const dataItemResponses = await Promise.all(uploadPromises);
const errors: { id: string; status: number; message: string }[] = [];
const postedDataItems = dataItemResponses.reduce(
(
postedDataItemsMap: Record<
Expand All @@ -149,9 +159,14 @@ export class TurboAuthenticatedUploadService
dataItemResponse: AxiosResponse<TurboUploadDataItemResponse, 'id'>,
) => {
// handle the fulfilled response
const { status, data } = dataItemResponse;
const { status, data, statusText } = dataItemResponse;
if (![200, 202].includes(status)) {
// TODO: add to failed data items array
errors.push({
id: data.id ?? 'unknown',

status,
message: statusText,
});
return postedDataItemsMap;
}
const { id, ...dataItemCache } = data;
Expand All @@ -163,6 +178,7 @@ export class TurboAuthenticatedUploadService

return {
dataItems: postedDataItems,
errors,
};
}

Expand Down Expand Up @@ -191,6 +207,7 @@ export class TurboAuthenticatedUploadService

// NOTE: our axios config (validateStatus) swallows errors, so failed data items will be ignored
const dataItemResponses = await Promise.all(uploadPromises);
const errors: { id: string; status: number; message: string }[] = [];
const postedDataItems = dataItemResponses.reduce(
(
postedDataItemsMap: Record<
Expand All @@ -200,9 +217,14 @@ export class TurboAuthenticatedUploadService
dataItemResponse: AxiosResponse<TurboUploadDataItemResponse, 'id'>,
) => {
// handle the fulfilled response
const { status, data } = dataItemResponse;
const { status, data, statusText } = dataItemResponse;
if (![200, 202].includes(status)) {
// TODO: add to failed data items array
errors.push({
id: data.id ?? 'unknown',
status,
message: statusText,
});
return postedDataItemsMap;
}
const { id, ...dataItemCache } = data;
Expand All @@ -214,6 +236,7 @@ export class TurboAuthenticatedUploadService

return {
dataItems: postedDataItems,
errors,
};
}
}
1 change: 1 addition & 0 deletions src/types/turbo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type TurboUploadDataItemResponse = {

export type TurboUploadDataItemsResponse = {
dataItems: Record<string, Omit<TurboUploadDataItemResponse, 'id'>>;
errors: { id: string; status: number; message: string }[];
};

export type TurboSignedRequestHeaders = {
Expand Down
52 changes: 37 additions & 15 deletions tests/turbo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ describe('Node environment', () => {

before(async () => {
jwk = await Arweave.crypto.generateJWK();
turbo = TurboFactory.private({ privateKey: jwk });
turbo = TurboFactory.private({
privateKey: jwk,
});
});

it('getBalance()', async () => {
Expand All @@ -192,28 +194,30 @@ describe('Node environment', () => {
}
});

describe('uploadSignedDataItems()', () => {
it('should forward the Readable signed data items to turbo', async () => {
describe.only('uploadSignedDataItems()', () => {
it.only('should forward the Readable signed data items to turbo', async () => {
// hack - check balance to ensure the balance exists in the payment service database
const signer = new ArweaveSigner(jwk);
const readableGenerator = () => Readable.from('test stream');

const signedDataItem1 = await streamSigner(
readableGenerator(),
readableGenerator(),
signer,
);
// create and sign a data item
const signedDataItemBuffer = createData('test stream', signer);
await signedDataItemBuffer.sign(signer);

const signedDataItem2 = await streamSigner(
readableGenerator(),
readableGenerator(),
signer,
);
const readableStreamGenerator = () =>
Readable.from(signedDataItemBuffer.getRaw());

const response = await turbo.uploadSignedDataItems({
dataItemGenerator: [() => signedDataItem1, () => signedDataItem2],
dataItemGenerator: [
readableStreamGenerator,
readableStreamGenerator,
],
});

expect(response).to.not.be.undefined;
expect(response).to.have.property('errors');
expect(response['errors']).to.have.length(0);
expect(response).to.have.property('dataItems');
expect(response['dataItems']).to.have.length(2);
for (const dataItem of Object.values(response['dataItems'])) {
expect(dataItem).to.have.property('fastFinalityIndexes');
expect(dataItem).to.have.property('dataCaches');
Expand All @@ -231,13 +235,31 @@ describe('Node environment', () => {
});
expect(response).to.not.be.undefined;
expect(response).to.have.property('dataItems');
expect(response).to.have.property('errors');
expect(response['errors']).to.have.length(0);
for (const dataItem of Object.values(response['dataItems'])) {
expect(dataItem).to.have.property('fastFinalityIndexes');
expect(dataItem).to.have.property('dataCaches');
expect(dataItem).to.have.property('owner');
expect(dataItem['owner']).to.equal(jwkToPublicArweaveAddress(jwk));
}
});

it('should return an error for an invalid data item', async () => {
const unsignedDataItem = Buffer.from('an unsigned buffer');
const response = await turbo.uploadSignedDataItems({
dataItemGenerator: [() => unsignedDataItem],
});
expect(response).to.not.be.undefined;
expect(response).to.have.property('dataItems');
expect(response).to.have.property('errors');
expect(Object.keys(response['dataItems']).length).to.equal(0);
for (const error of response['errors']) {
// TODO: check the values of these
expect(error).to.have.property('status');
expect(error).to.have.property('message');
}
});
});
});
});
Expand Down

0 comments on commit a88550c

Please sign in to comment.