-
Notifications
You must be signed in to change notification settings - Fork 8
Add tests #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add tests #17
Changes from all commits
89216e9
12cfe60
619786d
0744a34
b441613
0b293f9
64e7079
c3b2b3e
8255850
993eef3
86b5cc3
abb4c4f
39a0bc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,8 @@ name: Main CI | |||||
|
|
||||||
| on: [push] | ||||||
|
|
||||||
| permissions: {} | ||||||
|
|
||||||
| jobs: | ||||||
| lint: | ||||||
| runs-on: ubuntu-latest | ||||||
|
|
@@ -10,12 +12,34 @@ jobs: | |||||
| matrix: | ||||||
| node-version: [20.x] | ||||||
| steps: | ||||||
| - uses: actions/checkout@v3 | ||||||
| - uses: actions/checkout@v6 | ||||||
| with: | ||||||
| persist-credentials: false | ||||||
| - name: Use Node.js ${{ matrix.node-version }} | ||||||
| uses: actions/setup-node@v3 | ||||||
| uses: actions/setup-node@v6 | ||||||
| with: | ||||||
| node-version: ${{ matrix.node-version }} | ||||||
| - name: Install | ||||||
| run: npm install | ||||||
| - name: Lint | ||||||
| run: npm run lint | ||||||
| test: | ||||||
| runs-on: ubuntu-latest | ||||||
| timeout-minutes: 360 | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 360 is the default. I'd hope tests don't take that long! Maybe this should be lower or removed if default is fine. |
||||||
| strategy: | ||||||
| matrix: | ||||||
| node-version: [20.x] | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| steps: | ||||||
| - uses: actions/checkout@v6 | ||||||
| with: | ||||||
| persist-credentials: false | ||||||
| - name: Use Node.js ${{ matrix.node-version }} | ||||||
| uses: actions/setup-node@v6 | ||||||
| with: | ||||||
| node-version: ${{ matrix.node-version }} | ||||||
| - name: Install | ||||||
| run: npm install | ||||||
| - name: Fetch test suites | ||||||
| run: npm run fetch-test-suites | ||||||
| - name: Run test with Node.js ${{ matrix.node-version }} | ||||||
| run: npm run test-node | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,4 @@ coverage | |
| node_modules | ||
| v8.log | ||
| package-lock.json | ||
| node_modules/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "@context": "https://www.w3.org/ns/credentials/v2", | ||
| "id": "urn:uuid:2a1d4ddb-bbda-43cb-8886-fda9855bcf5d", | ||
| "type": ["VerifiableCredential"], | ||
| "credentialSubject": { | ||
| "id": "did:example:123", | ||
| "name": "Example" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,76 @@ | ||||||||
| import * as chai from 'chai'; | ||||||||
| import {promises as fs} from 'node:fs'; | ||||||||
| import http from 'node:http'; | ||||||||
| import {jsonldRequest} from '../lib/index.js'; | ||||||||
| import path from 'node:path'; | ||||||||
| import {spawn} from 'node:child_process'; | ||||||||
|
|
||||||||
| const should = chai.should(); | ||||||||
|
|
||||||||
| describe('jsonldRequest', function() { | ||||||||
| let fixturePath; | ||||||||
| let fixtureData; | ||||||||
|
|
||||||||
| before(async () => { | ||||||||
| fixturePath = path.join(import.meta.dirname, 'data', 'sample.jsonld'); | ||||||||
| fixtureData = await fs.readFile(fixturePath, 'utf8'); | ||||||||
| }); | ||||||||
|
|
||||||||
| it('loads a local file (file://) and parses JSON-LD', async () => { | ||||||||
| const fileUrl = `file://${fixturePath}`; | ||||||||
| const {data} = await jsonldRequest(fileUrl, {allow: ['file']}); | ||||||||
| data.should.deep.equal(JSON.parse(fixtureData)); | ||||||||
| }); | ||||||||
|
|
||||||||
| it('loads JSON-LD over HTTP and parses it', async () => { | ||||||||
| // start local HTTP server to serve the fixture | ||||||||
| const server = http.createServer((req, res) => { | ||||||||
| const headers = { | ||||||||
| 'Content-Type': 'application/ld+json; charset=utf-8' | ||||||||
| }; | ||||||||
| res.writeHead(200, headers); | ||||||||
| res.end(fixtureData); | ||||||||
| }); | ||||||||
| await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); | ||||||||
| const addr = server.address(); | ||||||||
| const url = `http://127.0.0.1:${addr.port}/fixture.json`; | ||||||||
| try { | ||||||||
| const {data} = await jsonldRequest(url, {allow: ['http', 'https']}); | ||||||||
| should.exist(data); | ||||||||
| data.should.be.an('object'); | ||||||||
| data.should.have.property('@context'); | ||||||||
|
Comment on lines
+40
to
+41
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be a full check too right?
Suggested change
|
||||||||
| } finally { | ||||||||
| server.close(); | ||||||||
| } | ||||||||
| }); | ||||||||
|
|
||||||||
| it('reads JSON-LD from stdin (child process runner)', async () => { | ||||||||
| const runner = path.join(import.meta.dirname, 'runner-stdin.js'); | ||||||||
| const child = spawn( | ||||||||
| process.execPath, | ||||||||
| [runner, '-'] | ||||||||
| ); | ||||||||
|
|
||||||||
| let stdout = ''; | ||||||||
| let stderr = ''; | ||||||||
| child.stdout.on('data', d => { | ||||||||
| stdout += d.toString(); | ||||||||
| }); | ||||||||
| child.stderr.on('data', d => { | ||||||||
| stderr += d.toString(); | ||||||||
| }); | ||||||||
|
|
||||||||
| // write fixture to child's stdin | ||||||||
| child.stdin.write(fixtureData); | ||||||||
| child.stdin.end(); | ||||||||
|
|
||||||||
| const exitCode = await new Promise(resolve => child.on('close', resolve)); | ||||||||
| if(exitCode !== 0) { | ||||||||
| throw new Error(`Runner exited with ${exitCode}: ${stderr}`); | ||||||||
| } | ||||||||
| const parsed = JSON.parse(stdout); | ||||||||
| should.exist(parsed); | ||||||||
| parsed.should.be.an('object'); | ||||||||
| parsed.should.have.property('@context'); | ||||||||
|
Comment on lines
+73
to
+74
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be a full check too, right?
Suggested change
|
||||||||
| }); | ||||||||
| }); | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #!/usr/bin/env node | ||
| import {jsonldRequest} from '../lib/index.js'; | ||
|
|
||
| const loc = process.argv[2] || '-'; | ||
| const options = {allow: ['stdin']}; | ||
|
|
||
| jsonldRequest(loc, options).then(({data}) => { | ||
| // print only the data as JSON to stdout | ||
| console.log(JSON.stringify(data)); | ||
| }).catch(err => { | ||
| // print error to stderr | ||
| console.error(err && err.stack ? err.stack : String(err)); | ||
| process.exit(1); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.