Skip to content

Commit 7c21887

Browse files
committed
feat: tests succeed in multiple environemnts
1 parent eb714f5 commit 7c21887

File tree

12 files changed

+194
-140
lines changed

12 files changed

+194
-140
lines changed

.envrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ use asdf
33
export PATH=$(npm bin):${PATH}
44
export MOCK_PINNING_SERVER_PORT=3000
55
export MOCK_PINNING_SERVER_SECRET=secret
6-
export DEBUG=1
6+
export DEBUG=0

CONTRIBUTING.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## @ipfs-shipyard/pinning-service-client@1.0.0
2+
3+
This generator creates TypeScript/JavaScript client that utilizes fetch-api.
4+
5+
### Building
6+
7+
To build and compile the typescript sources to javascript use:
8+
```
9+
npm install
10+
npm run build
11+
```
12+
13+
### Publishing
14+
15+
First build the package then run ```npm publish```
16+
17+
### Consuming
18+
19+
navigate to the folder of your consuming project and run one of the following commands.
20+
21+
_published:_
22+
23+
```
24+
npm install @ipfs-shipyard/[email protected] --save
25+
```
26+
27+
_unPublished (not recommended):_
28+
29+
```
30+
npm install PATH_TO_GENERATED_PACKAGE --save

MockServer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ export class MockServer {
125125
// MockServer.portsInUse.remove(this.port)
126126
MockServer.debug(`${Date.now()}: MockServer stopped listening on port ${port}`)
127127
delete this._server
128-
}
129-
if (err != null) {
128+
} else if (err != null) {
130129
MockServer.error(err.name)
131130
MockServer.error(err.message)
132131
MockServer.error(err.stack)
@@ -148,7 +147,7 @@ export class MockServer {
148147
}
149148

150149
private static debug (...logObjects: unknown[]) {
151-
if (process.env.DEBUG != null) {
150+
if (process.env.DEBUG != null && Number(process.env.DEBUG) !== 0) {
152151
MockServer.log('debug', ...logObjects)
153152
}
154153
}

MockServerController.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ class MockServerController {
9191

9292
// To prevent duplicated cleanup, remove the process listeners on server close.
9393
this.server.on('close', () => {
94-
process.off('beforeExit', this.shutdown)
95-
process.off('SIGTERM', this.shutdown)
96-
process.off('SIGINT', this.shutdown)
97-
process.off('SIGHUP', this.shutdown)
9894
})
9995
}
10096

10197
async shutdown () {
98+
process.off('beforeExit', this.shutdown)
99+
process.off('SIGTERM', this.shutdown)
100+
process.off('SIGINT', this.shutdown)
101+
process.off('SIGHUP', this.shutdown)
102102
await new Promise<void>((resolve, reject) => {
103103
this.server.close((err) => {
104104
if (err != null) {

README.md

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11
## @ipfs-shipyard/pinning-service-client@1.0.0
22

3-
This generator creates TypeScript/JavaScript client that utilizes fetch-api.
3+
This client was generated using [openapi-generator](https://github.com/OpenAPITools/openapi-generator) from the [ipfs pinning services API spec](https://raw.githubusercontent.com/ipfs/pinning-services-api-spec/main/ipfs-pinning-service.yaml).
44

5-
### Building
5+
You can see the commands used to generate the client in the `gen:fetch` npm script.
66

7-
To build and compile the typescript sources to javascript use:
8-
```
9-
npm install
10-
npm run build
11-
```
7+
### Usage
128

13-
### Publishing
9+
This client only has a programmatic API at the moment (no CLI). You use it like so:
1410

15-
First build the package then run ```npm publish```
11+
```ts
1612

17-
### Consuming
13+
import { Configuration, PinsApi, Status } from '@ipfs-shipyard/pinning-service-client'
14+
import type { PinsGetRequest, PinResults } from '@ipfs-shipyard/pinning-service-client'
1815

19-
navigate to the folder of your consuming project and run one of the following commands.
16+
const config = new Configuration({
17+
basePath, // the URI for your pinning provider, e.g. `http://localhost:3000`
18+
accessToken, // the secret token/key given to you by your pinning provider
19+
// fetchApi: fetch, // You can pass your own fetchApi implementation, but we use fetch-ponyfill by default.
20+
})
2021

21-
_published:_
22+
const client = new PinsApi(config)
2223

23-
```
24-
npm install @ipfs-shipyard/[email protected] --save
25-
```
24+
(async () => {
25+
// Get 10 failed Pins
26+
const pinsGetOptions: PinsGetRequest = {
27+
limit: 10,
28+
status: Status.Failed
29+
}
30+
const {count, results}: PinResults = await client.pinsGet(pinsGetOptions)
31+
32+
console.log(count, results)
2633

27-
_unPublished (not recommended):_
34+
})()
2835

2936
```
30-
npm install PATH_TO_GENERATED_PACKAGE --save

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@
145145
"build:main": "aegir build -- -p tsconfig.json --suppress 6133@generated 6192@generated --stats",
146146
"build:docs": "aegir ts docs",
147147
"test": "run-s test:*",
148+
"test:browser": "aegir test --target browser",
149+
"test:webworker": "aegir test --target webworker",
148150
"test:electron": "aegir test --target electron-main",
149151
"test:node": "aegir test --target node --cov && npx nyc report",
150152
"pregen": "openapi-generator-cli validate -i https://raw.githubusercontent.com/ipfs/pinning-services-api-spec/main/ipfs-pinning-service.yaml",
@@ -155,6 +157,7 @@
155157
"gen:ts": "openapi-generator-cli generate --generator-key ts"
156158
},
157159
"dependencies": {
160+
"fetch-ponyfill": "^7.1.0",
158161
"patch-package": "^6.4.7"
159162
},
160163
"devDependencies": {
@@ -174,9 +177,7 @@
174177
"dotenvrc": "^1.0.1",
175178
"express": "^4.17.3",
176179
"express-promise-router": "^4.1.1",
177-
"fetch-ponyfill": "^7.1.0",
178180
"mock-ipfs-pinning-service": "^0.4.0",
179-
"node-fetch": "^2.6.1",
180181
"npm-run-all": "^4.1.5",
181182
"openapi-typescript": "^5.1.1",
182183
"portscanner": "^2.2.0",

src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1+
import fetchPonyfill from 'fetch-ponyfill'
2+
import { Configuration as GeneratedConfiguration, ConfigurationParameters } from '../generated/fetch/src/index'
3+
4+
class Configuration extends GeneratedConfiguration {
5+
constructor (options: ConfigurationParameters) {
6+
/**
7+
* Prevent the need for everyone to have to override the fetch API...
8+
*/
9+
if (options.fetchApi == null) {
10+
options.fetchApi = fetchPonyfill().fetch
11+
}
12+
super(options)
13+
}
14+
}
15+
116
export * from '../generated/fetch/src/index'
17+
export { Configuration }

test/browser-tests/.gitkeep

Whitespace-only changes.

test/browser.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
/* eslint-disable no-console */
1+
// /* eslint-disable no-console */
22

3-
import clientTests from './isomorphic-tests/client'
4-
// import configurationTests from './configuration.spec'
5-
import fetchPonyfill from 'fetch-ponyfill'
3+
// import clientTests from './isomorphic-tests/client'
4+
// // import configurationTests from './configuration.spec'
5+
// import fetchPonyfill from 'fetch-ponyfill'
66

7-
const setup = async () => {
8-
return {
9-
fetch: fetchPonyfill().fetch as GlobalFetch['fetch']
10-
}
11-
}
7+
// const setup = async () => {
8+
// return {
9+
// fetch: fetchPonyfill().fetch as GlobalFetch['fetch']
10+
// }
11+
// }
1212

13-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
14-
describe('browser', async (): Promise<void> => {
15-
// await configurationTests(setup)
16-
await clientTests(setup)
17-
})
13+
// // eslint-disable-next-line @typescript-eslint/no-misused-promises
14+
// describe('browser', async (): Promise<void> => {
15+
// // await configurationTests(setup)
16+
// await clientTests(setup)
17+
// })

test/client.spec.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* eslint-env browser, node, mocha */
2+
3+
import { expect } from 'aegir/utils/chai'
4+
import { Configuration, PinsApi, Status } from '../src'
5+
import type { Pin } from '../src'
6+
import fetchPonyfill from 'fetch-ponyfill'
7+
8+
// export default async (setup: () => Promise<{fetch: GlobalFetch['fetch']}>) => {
9+
const { fetch } = fetchPonyfill()
10+
11+
let Config = new Configuration({
12+
basePath: `http://127.0.0.1:${process.env.MOCK_PINNING_SERVER_PORT ?? '3000'}`,
13+
// fetchApi: fetch,
14+
accessToken: process.env.MOCK_PINNING_SERVER_SECRET
15+
})
16+
describe('Client', () => {
17+
it('Can be instantiated', () => {
18+
expect(() => new PinsApi(Config)).not.to.throw()
19+
})
20+
21+
describe('Operations', () => {
22+
// let mockServer: MockServer
23+
// let Config: Configuration
24+
beforeEach(async () => {
25+
const response = await fetch('http://localhost:3000/start')
26+
const { basePath, accessToken } = await response.json()
27+
Config = new Configuration({
28+
basePath,
29+
accessToken
30+
// fetchApi: fetch
31+
})
32+
})
33+
34+
afterEach(async () => {
35+
const [,,port] = Config.basePath.split(':')
36+
const response = await fetch(`http://localhost:3000/stop/${port}`)
37+
38+
const { success } = await response.json()
39+
40+
if (success === false) {
41+
throw new Error(`Unexpected error when attempting to stop the mockServer on port ${port}`)
42+
}
43+
})
44+
45+
it('GET: Can get failed Pins', async () => {
46+
const Client = new PinsApi(Config)
47+
const response = await Client.pinsGet({ limit: 1, status: new Set([Status.Failed]) })
48+
expect(response).to.deep.eq({ count: 0, results: new Set() })
49+
})
50+
51+
it('GET: Can add a Pin successfully', async () => {
52+
const Client = new PinsApi(Config)
53+
const pin: Pin = {
54+
cid: 'abc123',
55+
name: 'pinned-test1'
56+
}
57+
const response = await Client.pinsPost({ pin })
58+
expect(response).to.deep.includes({ status: Status.Pinned })
59+
expect(response.pin).to.deep.include({ ...pin })
60+
})
61+
62+
it('POST: Can handle a failed pinning', async () => {
63+
const Client = new PinsApi(Config)
64+
const pin: Pin = {
65+
cid: 'abc123',
66+
name: 'failed-test2'
67+
}
68+
const response = await Client.pinsPost({ pin })
69+
expect(response).to.deep.includes({ status: Status.Failed })
70+
expect(response.pin).to.deep.include({ ...pin })
71+
})
72+
})
73+
})
74+
// }

0 commit comments

Comments
 (0)