Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Oli Evans <oli@protocol.ai>
  • Loading branch information
olizilla committed Oct 17, 2023
1 parent abb0058 commit 17a093a
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 82 deletions.
14 changes: 11 additions & 3 deletions packages/capabilities/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ export interface StoreItemNotFound extends Ucanto.Failure {

export type StoreRemoveFailure = StoreItemNotFound | Ucanto.Failure

export type StoreGetSuccess = StoreListItem

export type StoreGetFailure = StoreItemNotFound | Ucanto.Failure

export interface StoreListSuccess extends ListResponse<StoreListItem> {}
Expand All @@ -281,13 +283,21 @@ export interface StoreListItem {
link: UnknownLink
size: number
origin?: UnknownLink
insertedAt: string
}

export interface UploadAddSuccess {
export interface UploadListItem {
root: UnknownLink
shards?: CARLink[]
insertedAt: string
updatedAt: string
}

// TODO: (olizilla) make this an UploadListItem too?
export type UploadAddSuccess = Omit<UploadListItem, 'insertedAt' | 'updatedAt'>

export type UploadGetSuccess = UploadListItem

export type UploadRemoveSuccess = UploadDidRemove | UploadDidNotRemove

export interface UploadDidRemove extends UploadAddSuccess {}
Expand All @@ -299,8 +309,6 @@ export interface UploadDidNotRemove {

export interface UploadListSuccess extends ListResponse<UploadListItem> {}

export interface UploadListItem extends UploadAddSuccess {}

// UCAN core events

export type UCANRevoke = InferInvokedCapability<typeof UCANCaps.revoke>
Expand Down
2 changes: 1 addition & 1 deletion packages/upload-api/src/store/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as API from '../types.js'

/**
* @param {API.StoreServiceContext} context
* @returns {API.ServiceMethod<API.StoreGet, API.StoreGetOk, API.Failure>}
* @returns {API.ServiceMethod<API.StoreGet, API.StoreGetSuccess, API.Failure>}
*/
export function storeGetProvider(context) {
return Server.provide(Store.get, async ({ capability }) => {
Expand Down
72 changes: 8 additions & 64 deletions packages/upload-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ import {
UCANRevoke,
ListResponse,
CARLink,
StoreGetSuccess,
UploadGetSuccess,
} from '@web3-storage/capabilities/types'
import * as Capabilities from '@web3-storage/capabilities'
import { RevocationsStorage } from './types/revocations'
Expand All @@ -146,13 +148,13 @@ export type { RateLimitsStorage, RateLimit } from './types/rate-limits'
export interface Service {
store: {
add: ServiceMethod<StoreAdd, StoreAddSuccess, Failure>
get: ServiceMethod<StoreGet, StoreGetOk, StoreGetFailure>
get: ServiceMethod<StoreGet, StoreGetSuccess, StoreGetFailure>
remove: ServiceMethod<StoreRemove, StoreRemoveSuccess, StoreRemoveFailure>
list: ServiceMethod<StoreList, StoreListSuccess, Failure>
}
upload: {
add: ServiceMethod<UploadAdd, UploadAddSuccess, Failure>
get: ServiceMethod<UploadGet, UploadGetOk, UploadGetFailure>
get: ServiceMethod<UploadGet, UploadGetSuccess, UploadGetFailure>
remove: ServiceMethod<UploadRemove, UploadRemoveSuccess, Failure>
list: ServiceMethod<UploadList, UploadListSuccess, Failure>
}
Expand Down Expand Up @@ -383,13 +385,8 @@ export interface StoreTable {
list: (
space: DID,
options?: ListOptions
) => Promise<
(StoreAddInput & StoreListItem & { insertedAt: string }) | undefined
>
get(
space: DID,
link: UnknownLink
): Promise<(StoreGetItem) | undefined>
) => Promise<ListResponse<StoreAddInput & StoreListItem>>
get: (space: DID,link: UnknownLink) => Promise<(StoreListItem) | undefined>
}

export interface UploadTable {
Expand All @@ -400,13 +397,8 @@ export interface UploadTable {
list: (
space: DID,
options?: ListOptions
) => Promise<
ListResponse<UploadListItem & { insertedAt: string; updatedAt: string }>
>
get(
space: DID,
link: UnknownLink
): Promise<(UploadGetItem) | undefined>
) => Promise<ListResponse<UploadListItem>>
get: (space: DID,link: UnknownLink) => Promise<(UploadListItem) | undefined>
}

export type SpaceInfoSuccess = {
Expand Down Expand Up @@ -452,32 +444,6 @@ export interface StoreInspectOk {
spaces: Array<{ did: DID; insertedAt: string }>
}

export type StoreListItem = StoreAddOutput & StoreMetadata

export type StoreGetItem = StoreAddInput & StoreMetadata

export interface StoreListOk extends ListResponse<StoreListItem> {}

export type StoreGetOk = StoreGetItem

export type StoreAddOk = StoreAddDone | StoreAddUpload

export interface StoreAddDone {
status: 'done'
with: DID
link: UnknownLink
url?: undefined
headers?: undefined
}

export interface StoreAddUpload {
status: 'upload'
with: DID
link: UnknownLink
url: URL
headers: Record<string, string>
}

export interface UploadAddInput {
space: DID
root: UnknownLink
Expand All @@ -486,32 +452,10 @@ export interface UploadAddInput {
invocation: UCANLink
}

export interface UploadMetadata {
insertedAt: string
updatedAt: string
}

export interface UploadAddOk
extends Omit<UploadAddInput, 'space' | 'issuer' | 'invocation'> {}
export type UploadRemoveOk = UploadDIDRemove | UploadDidNotRemove

export type UploadListItem = UploadAddOk & UploadMetadata
export type UploadGetItem = UploadAddInput & UploadMetadata

export interface UploadDIDRemove extends UploadAddOk {}
export interface UploadDidNotRemove {
root?: undefined
shards?: undefined
}

export interface UploadInspectOk {
spaces: Array<{ did: DID; insertedAt: string }>
}

export interface UploadListOk extends ListResponse<UploadListItem> {}

export interface UploadGetOk extends UploadGetItem {}

export interface ListOptions {
size?: number
cursor?: string
Expand Down
2 changes: 1 addition & 1 deletion packages/upload-api/src/upload/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as API from '../types.js'

/**
* @param {API.UploadServiceContext} context
* @returns {API.ServiceMethod<API.UploadGet, API.UploadGetOk, API.Failure>}
* @returns {API.ServiceMethod<API.UploadGet, API.UploadGetSuccess, API.Failure>}
*/
export function uploadGetProvider(context) {
return Server.provide(Upload.get, async ({ capability }) => {
Expand Down
7 changes: 0 additions & 7 deletions packages/upload-api/test/handlers/store.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createServer, connect } from '../../src/lib.js'
import * as API from '../../src/types.js'
import { CID } from 'multiformats'
import * as CAR from '@ucanto/transport/car'
import { base64pad } from 'multiformats/bases/base64'
import * as StoreCapabilities from '@web3-storage/capabilities/store'
Expand Down Expand Up @@ -80,10 +79,8 @@ export const test = {

assert.deepEqual(
{
space: item.space,
link: item.link.toString(),
size: item.size,
issuer: item.issuer,
},
{
space: spaceDid,
Expand All @@ -93,7 +90,6 @@ export const test = {
}
)

assert.equal(CID.parse(item.invocation.toString()) != null, true)
assert.equal(
Date.now() - new Date(item?.insertedAt).getTime() < 60_000,
true
Expand Down Expand Up @@ -304,10 +300,8 @@ export const test = {

assert.deepEqual(
{
space: item.space,
link: item.link.toString(),
size: item.size,
issuer: item.issuer,
},
{
space: spaceDid,
Expand Down Expand Up @@ -739,7 +733,6 @@ export const test = {

assert.deepEqual(storeGet.out.ok.link, links[0])
assert.equal(storeGet.out.ok.size, data[0].byteLength)
assert.equal(storeGet.out.ok.space, spaceDid)
assert.ok(storeGet.out.ok.insertedAt)
},

Expand Down
3 changes: 0 additions & 3 deletions packages/upload-api/test/handlers/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from '../util.js'
import { createServer, connect } from '../../src/lib.js'
import { Upload } from '@web3-storage/capabilities'
import { uploadListProvider } from '../../src/upload/list.js'

// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-dynamodb/classes/batchwriteitemcommand.html
const BATCH_MAX_SAFE_LIMIT = 25
Expand Down Expand Up @@ -901,8 +900,6 @@ export const test = {
throw new Error('invocation failed', { cause: uploadGet })
}

assert.equal(uploadGet.out.ok.issuer, alice.toDIDKey())
assert.equal(uploadGet.out.ok.space, spaceDid)
assert.equal(uploadGet.out.ok.shards?.[0].toString(), cars[0].cid.toString())
assert.equal(uploadGet.out.ok.root.toString(), cars[0].roots[0].toString())
},
Expand Down
1 change: 0 additions & 1 deletion packages/upload-api/test/helpers/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export const createContext = async (options = {}) => {
mail: /** @type {TestTypes.DebugEmail} */ (serviceContext.email),
service: /** @type {TestTypes.ServiceSigner} */ (serviceContext.id),
connection,
testStoreTable: storeTable,
fetch,
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/upload-api/test/storage/store-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as API from '../../src/types.js'
*/
export class StoreTable {
constructor() {
/** @type {(API.StoreGetItem)[]} */
/** @type {(API.StoreAddInput & API.StoreListItem)[]} */
this.items = []
}

Expand Down Expand Up @@ -45,7 +45,7 @@ export class StoreTable {
* Get info for a single shard or undefined if it doesn't exist
* @param {API.DID} space
* @param {API.UnknownLink} link
* @returns {Promise<API.StoreGetItem | undefined>}
* @returns {Promise<(API.StoreAddInput & API.StoreListItem) | undefined>}
*/
async get(space, link) {
return this.items.find(
Expand Down
8 changes: 8 additions & 0 deletions packages/upload-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
StoreAddSuccess,
StoreAddSuccessUpload,
StoreAddSuccessDone,
StoreGet,
StoreGetFailure,
StoreList,
StoreListSuccess,
StoreListItem,
Expand All @@ -34,6 +36,10 @@ import {
ListResponse,
CARLink,
PieceLink,
StoreGetSuccess,
UploadGet,
UploadGetSuccess,
UploadGetFailure,
} from '@web3-storage/capabilities/types'
import * as UnixFS from '@ipld/unixfs/src/unixfs'

Expand Down Expand Up @@ -70,11 +76,13 @@ export type ProgressFn = (status: ProgressStatus) => void
export interface Service {
store: {
add: ServiceMethod<StoreAdd, StoreAddSuccess, Failure>
get: ServiceMethod<StoreGet, StoreGetSuccess, StoreGetFailure>
remove: ServiceMethod<StoreRemove, StoreRemoveSuccess, StoreRemoveFailure>
list: ServiceMethod<StoreList, StoreListSuccess, Failure>
}
upload: {
add: ServiceMethod<UploadAdd, UploadAddSuccess, Failure>
get: ServiceMethod<UploadGet, UploadGetSuccess, UploadGetFailure>
remove: ServiceMethod<UploadRemove, UploadRemoveSuccess, Failure>
list: ServiceMethod<UploadList, UploadListSuccess, Failure>
}
Expand Down
2 changes: 2 additions & 0 deletions packages/upload-client/test/helpers/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ export function mockService(impl) {
return {
store: {
add: withCallCount(impl.store?.add ?? notImplemented),
get: withCallCount(impl.store?.get ?? notImplemented),
list: withCallCount(impl.store?.list ?? notImplemented),
remove: withCallCount(impl.store?.remove ?? notImplemented),
},
upload: {
add: withCallCount(impl.upload?.add ?? notImplemented),
get: withCallCount(impl.upload?.get ?? notImplemented),
list: withCallCount(impl.upload?.list ?? notImplemented),
remove: withCallCount(impl.upload?.remove ?? notImplemented),
},
Expand Down
3 changes: 3 additions & 0 deletions packages/upload-client/test/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ describe('Store.list', () => {
{
link: car.cid,
size: 123,
insertedAt: 'foo'
},
],
}
Expand Down Expand Up @@ -422,6 +423,7 @@ describe('Store.list', () => {
{
link: (await randomCAR(128)).cid,
size: 123,
insertedAt: 'time1'
},
],
}
Expand All @@ -431,6 +433,7 @@ describe('Store.list', () => {
{
link: (await randomCAR(128)).cid,
size: 123,
insertedAt: 'time1'
},
],
}
Expand Down
6 changes: 6 additions & 0 deletions packages/upload-client/test/upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ describe('Upload.list', () => {
{
root: car.roots[0],
shards: [car.cid],
insertedAt: 'foo',
updatedAt: 'bar'
},
],
}
Expand Down Expand Up @@ -208,6 +210,8 @@ describe('Upload.list', () => {
{
root: car0.roots[0],
shards: [car0.cid],
insertedAt: 'foo',
updatedAt: 'bar'
},
],
}
Expand All @@ -218,6 +222,8 @@ describe('Upload.list', () => {
{
root: car1.roots[0],
shards: [car1.cid],
insertedAt: 'foo',
updatedAt: 'bar'
},
],
}
Expand Down
1 change: 1 addition & 0 deletions packages/w3up-client/test/capability/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('StoreClient', () => {
{
link: (await randomCAR(128)).cid,
size: 123,
insertedAt: 'foo'
},
],
}
Expand Down
2 changes: 2 additions & 0 deletions packages/w3up-client/test/capability/upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ describe('StoreClient', () => {
{
root: car.roots[0],
shards: [car.cid],
insertedAt: 'foo',
updatedAt: 'bar'
},
],
}
Expand Down

0 comments on commit 17a093a

Please sign in to comment.