Skip to content

Commit

Permalink
test: w3up-client tsconfig.json now includes "./test" so its tests ha…
Browse files Browse the repository at this point in the history
…ve some type checking (#669)
  • Loading branch information
gobengo authored Mar 29, 2023
1 parent de519d8 commit 0a60e02
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/w3up-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"devDependencies": {
"@docusaurus/core": "^2.2.0",
"@ipld/car": "^5.1.1",
"@types/mocha": "^10.0.1",
"@ucanto/server": "^6.1.0",
"assert": "^2.0.0",
"c8": "^7.13.0",
Expand Down
8 changes: 5 additions & 3 deletions packages/w3up-client/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,19 @@ export class Client extends Base {
/**
* Use a specific space.
*
* @param {import('./types').DID<'key'>} did
* @param {import('./types').DID} did
*/
async setCurrentSpace(did) {
await this._agent.setCurrentSpace(did)
await this._agent.setCurrentSpace(/** @type {`did:key:${string}`} */ (did))
}

/**
* Spaces available to this agent.
*/
spaces() {
return [...this._agent.spaces].map(([did, meta]) => new Space(did, meta))
return [...this._agent.spaces].map(([did, meta]) => {
return new Space(did, meta)
})
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/w3up-client/test/capability/access.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('AccessClient', () => {
const invCap = invocation.capabilities[0]
assert.equal(invCap.can, AccessCapabilities.claim.can)
return {
delegations: [],
delegations: {},
}
}),
},
Expand All @@ -33,6 +33,7 @@ describe('AccessClient', () => {
})

const alice = new Client(await AgentData.create(), {
// @ts-ignore
serviceConf: await mockServiceConf(server),
})

Expand Down
11 changes: 2 additions & 9 deletions packages/w3up-client/test/capability/space.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ describe('SpaceClient', () => {
assert.equal(invCap.can, SpaceCapabilities.info.can)
assert.equal(invCap.with, space.did())
return {
did: space.did(),
agent: alice.agent().did(),
email: 'mailto:[email protected]',
product: 'product:test',
updated_at: '',
inserted_at: '',
did: /** @type {`did:key:${string}`} */ (space.did()),
}
}),
},
Expand All @@ -39,6 +34,7 @@ describe('SpaceClient', () => {
})

const alice = new Client(await AgentData.create(), {
// @ts-ignore
serviceConf: await mockServiceConf(server),
})

Expand All @@ -51,9 +47,6 @@ describe('SpaceClient', () => {
assert.equal(service.space.info.callCount, 1)

assert.equal(info.did, space.did())
assert.equal(info.agent, alice.agent().did())
assert.equal(info.email, 'mailto:[email protected]')
assert.equal(info.product, 'product:test')
})
})
})
5 changes: 5 additions & 0 deletions packages/w3up-client/test/capability/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ describe('StoreClient', () => {
status: 'upload',
headers: { 'x-test': 'true' },
url: 'http://localhost:9200',
link: car.cid,
with: space.did(),
}
}),
},
Expand All @@ -37,6 +39,7 @@ describe('StoreClient', () => {
})

const alice = new Client(await AgentData.create(), {
// @ts-ignore
serviceConf: await mockServiceConf(server),
})

Expand Down Expand Up @@ -88,6 +91,7 @@ describe('StoreClient', () => {
})

const alice = new Client(await AgentData.create(), {
// @ts-ignore
serviceConf: await mockServiceConf(server),
})

Expand Down Expand Up @@ -130,6 +134,7 @@ describe('StoreClient', () => {
})

const alice = new Client(await AgentData.create(), {
// @ts-ignore
serviceConf: await mockServiceConf(server),
})

Expand Down
6 changes: 5 additions & 1 deletion packages/w3up-client/test/capability/upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('StoreClient', () => {
})

const alice = new Client(await AgentData.create(), {
// @ts-ignore
serviceConf: await mockServiceConf(server),
})

Expand Down Expand Up @@ -92,6 +93,7 @@ describe('StoreClient', () => {
})

const alice = new Client(await AgentData.create(), {
// @ts-ignore
serviceConf: await mockServiceConf(server),
})

Expand Down Expand Up @@ -121,7 +123,8 @@ describe('StoreClient', () => {
const invCap = invocation.capabilities[0]
assert.equal(invCap.can, UploadCapabilities.remove.can)
assert.equal(invCap.with, alice.currentSpace()?.did())
return null
// eslint-disable-next-line unicorn/no-useless-undefined
return undefined
}),
},
})
Expand All @@ -134,6 +137,7 @@ describe('StoreClient', () => {
})

const alice = new Client(await AgentData.create(), {
// @ts-ignore
serviceConf: await mockServiceConf(server),
})

Expand Down
21 changes: 17 additions & 4 deletions packages/w3up-client/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ describe('Client', () => {
status: 'upload',
headers: { 'x-test': 'true' },
url: 'http://localhost:9200',
link: /** @type {import('@web3-storage/upload-client/types').CARLink} */ (
invocation.capabilities[0].nb?.link
),
with: space.did(),
}
}),
},
Expand Down Expand Up @@ -62,6 +66,7 @@ describe('Client', () => {
})

const alice = new Client(await AgentData.create(), {
// @ts-ignore
serviceConf: await mockServiceConf(server),
})

Expand Down Expand Up @@ -118,6 +123,10 @@ describe('Client', () => {
status: 'upload',
headers: { 'x-test': 'true' },
url: 'http://localhost:9200',
link: /** @type {import('@web3-storage/upload-client/types').CARLink} */ (
invocation.capabilities[0].nb?.link
),
with: space.did(),
}
}),
},
Expand All @@ -143,6 +152,7 @@ describe('Client', () => {
})

const alice = new Client(await AgentData.create(), {
// @ts-ignore
serviceConf: await mockServiceConf(server),
})

Expand Down Expand Up @@ -184,6 +194,10 @@ describe('Client', () => {
status: 'upload',
headers: { 'x-test': 'true' },
url: 'http://localhost:9200',
link: /** @type {import('@web3-storage/upload-client/types').CARLink} */ (
invocation.capabilities[0].nb?.link
),
with: space.did(),
}
}),
},
Expand All @@ -196,7 +210,8 @@ describe('Client', () => {
assert.equal(invCap.with, space.did())
if (!invCap.nb) throw new Error('nb must be present')
assert.equal(invCap.nb.shards?.length, 1)
assert.equal(invCap.nb.shards[0].toString(), carCID.toString())
assert.ok(carCID)
assert.equal(invCap.nb.shards?.[0].toString(), carCID.toString())
return invCap.nb
}),
},
Expand All @@ -210,6 +225,7 @@ describe('Client', () => {
})

const alice = new Client(await AgentData.create(), {
// @ts-ignore
serviceConf: await mockServiceConf(server),
})

Expand All @@ -225,9 +241,6 @@ describe('Client', () => {
assert.equal(service.store.add.callCount, 1)
assert(service.upload.add.called)
assert.equal(service.upload.add.callCount, 1)

assert(carCID)
assert.equal(carCID.toString(), car.cid.toString())
})
})

Expand Down
6 changes: 5 additions & 1 deletion packages/w3up-client/test/helpers/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const notImplemented = () => {

/**
* @param {Partial<{
* access: Partial<import('@web3-storage/access/types').Service['access']>
* provider: Partial<import('@web3-storage/access/types').Service['provider']>
* store: Partial<import('@web3-storage/upload-client/types').Service['store']>
* upload: Partial<import('@web3-storage/upload-client/types').Service['upload']>
* voucher: Partial<import('@web3-storage/access/types').Service['voucher']>
Expand Down Expand Up @@ -61,7 +63,9 @@ function withCallCount(fn) {
}

/**
* @param {import('@ucanto/interface').ServerView} server
* @template {string} K
* @template {Record<K, any>} Service - describes methods exposed via ucanto server
* @param {import('@ucanto/interface').ServerView<Service>} server
*/
export async function mockServiceConf(server) {
const connection = connect({
Expand Down
5 changes: 1 addition & 4 deletions packages/w3up-client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
"composite": true,
"noUnusedParameters": false
},
"include": [
"./src"
// @todo add "./test",
],
"include": ["./src", "./test"],
"typedocOptions": {
"entryPoints": ["./src"],
"entryPointStrategy": "expand",
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0a60e02

Please sign in to comment.