From b9686f9b6f8d38498cd70c5462ea3bf063462389 Mon Sep 17 00:00:00 2001 From: Benjamin Young Date: Tue, 24 Mar 2026 10:05:55 -0400 Subject: [PATCH 01/13] Ignore node_modules. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index fc42ab9..f53f58b 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ coverage node_modules v8.log package-lock.json +node_modules/ From befd2069789558d91a4da0157d97b23d84525aa5 Mon Sep 17 00:00:00 2001 From: Benjamin Young Date: Tue, 24 Mar 2026 14:42:54 -0400 Subject: [PATCH 02/13] Add mocha/chai tests for stdin, files, & HTTP. --- package.json | 7 +++- test/fixtures/sample.jsonld | 9 ++++ test/jsonld-request.test.js | 83 +++++++++++++++++++++++++++++++++++++ test/runner-stdin.js | 14 +++++++ 4 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/sample.jsonld create mode 100644 test/jsonld-request.test.js create mode 100644 test/runner-stdin.js diff --git a/package.json b/package.json index 7ea96fc..298257e 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,9 @@ }, "devDependencies": { "@digitalbazaar/eslint-config": "^8.0.1", - "eslint": "^9.39.4" + "eslint": "^9.39.4", + "mocha": "^11.7.5", + "chai": "^6.2.2" }, "engines": { "node": "^20.19.0 || ^22.13.0 || >=24.0.0" @@ -57,6 +59,7 @@ "jsonld" ], "scripts": { - "lint": "eslint ." + "lint": "eslint .", + "test": "mocha \"test/**/*.test.js\"" } } diff --git a/test/fixtures/sample.jsonld b/test/fixtures/sample.jsonld new file mode 100644 index 0000000..ab123b5 --- /dev/null +++ b/test/fixtures/sample.jsonld @@ -0,0 +1,9 @@ +{ + "@context": "https://www.w3.org/2018/credentials/v1", + "id": "urn:uuid:0000-0000-0000-0000", + "type": ["VerifiableCredential"], + "credentialSubject": { + "id": "did:example:123", + "name": "Example" + } +} diff --git a/test/jsonld-request.test.js b/test/jsonld-request.test.js new file mode 100644 index 0000000..87d4599 --- /dev/null +++ b/test/jsonld-request.test.js @@ -0,0 +1,83 @@ +import * as chai from 'chai'; +import {fileURLToPath} from 'node:url'; +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(); +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +describe('jsonldRequest', function() { + this.timeout(10000); + + let fixturePath; + let fixtureData; + + before(async () => { + fixturePath = path.join(__dirname, 'fixtures', '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']}); + should.exist(data); + data.should.be.an('object'); + data.should.have.property('@context'); + }); + + 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'); + } finally { + server.close(); + } + }); + + it('reads JSON-LD from stdin (child process runner)', async () => { + const runner = path.join(__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'); + }); +}); diff --git a/test/runner-stdin.js b/test/runner-stdin.js new file mode 100644 index 0000000..cb4a886 --- /dev/null +++ b/test/runner-stdin.js @@ -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); +}); From e2231fccfd4d6ff79580afc8ddc9f90d9178817c Mon Sep 17 00:00:00 2001 From: Benjamin Young Date: Thu, 26 Mar 2026 13:40:24 -0400 Subject: [PATCH 03/13] Make test input data more real. --- test/fixtures/sample.jsonld | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/fixtures/sample.jsonld b/test/fixtures/sample.jsonld index ab123b5..d7c6c25 100644 --- a/test/fixtures/sample.jsonld +++ b/test/fixtures/sample.jsonld @@ -1,6 +1,6 @@ { - "@context": "https://www.w3.org/2018/credentials/v1", - "id": "urn:uuid:0000-0000-0000-0000", + "@context": "https://www.w3.org/ns/credentials/v2", + "id": "urn:uuid:2a1d4ddb-bbda-43cb-8886-fda9855bcf5d", "type": ["VerifiableCredential"], "credentialSubject": { "id": "did:example:123", From 5efdc0695b3f28300c2877baae2eaed41d5e4571 Mon Sep 17 00:00:00 2001 From: Benjamin Young Date: Thu, 26 Mar 2026 13:41:06 -0400 Subject: [PATCH 04/13] Rename folder from `fixtures` to `data`. --- test/{fixtures => data}/sample.jsonld | 0 test/jsonld-request.test.js | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename test/{fixtures => data}/sample.jsonld (100%) diff --git a/test/fixtures/sample.jsonld b/test/data/sample.jsonld similarity index 100% rename from test/fixtures/sample.jsonld rename to test/data/sample.jsonld diff --git a/test/jsonld-request.test.js b/test/jsonld-request.test.js index 87d4599..59615fd 100644 --- a/test/jsonld-request.test.js +++ b/test/jsonld-request.test.js @@ -17,7 +17,7 @@ describe('jsonldRequest', function() { let fixtureData; before(async () => { - fixturePath = path.join(__dirname, 'fixtures', 'sample.jsonld'); + fixturePath = path.join(__dirname, 'data', 'sample.jsonld'); fixtureData = await fs.readFile(fixturePath, 'utf8'); }); From 0b34f445a50011659e933e4cf233e860aaaaaf5e Mon Sep 17 00:00:00 2001 From: Benjamin Young Date: Thu, 26 Mar 2026 13:42:55 -0400 Subject: [PATCH 05/13] Remove use of old school aliases. --- test/jsonld-request.test.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/test/jsonld-request.test.js b/test/jsonld-request.test.js index 59615fd..dce7cc6 100644 --- a/test/jsonld-request.test.js +++ b/test/jsonld-request.test.js @@ -1,5 +1,4 @@ import * as chai from 'chai'; -import {fileURLToPath} from 'node:url'; import {promises as fs} from 'node:fs'; import http from 'node:http'; import {jsonldRequest} from '../lib/index.js'; @@ -7,8 +6,6 @@ import path from 'node:path'; import {spawn} from 'node:child_process'; const should = chai.should(); -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); describe('jsonldRequest', function() { this.timeout(10000); @@ -17,7 +14,7 @@ describe('jsonldRequest', function() { let fixtureData; before(async () => { - fixturePath = path.join(__dirname, 'data', 'sample.jsonld'); + fixturePath = path.join(import.meta.dirname, 'data', 'sample.jsonld'); fixtureData = await fs.readFile(fixturePath, 'utf8'); }); @@ -52,7 +49,7 @@ describe('jsonldRequest', function() { }); it('reads JSON-LD from stdin (child process runner)', async () => { - const runner = path.join(__dirname, 'runner-stdin.js'); + const runner = path.join(import.meta.dirname, 'runner-stdin.js'); const child = spawn( process.execPath, [runner, '-'] From 9b9bfe374ca19857bb3be241e72dbb1215d9641e Mon Sep 17 00:00:00 2001 From: Benjamin Young Date: Thu, 26 Mar 2026 13:44:45 -0400 Subject: [PATCH 06/13] Remove timeout. --- test/jsonld-request.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/jsonld-request.test.js b/test/jsonld-request.test.js index dce7cc6..f4f4efe 100644 --- a/test/jsonld-request.test.js +++ b/test/jsonld-request.test.js @@ -8,8 +8,6 @@ import {spawn} from 'node:child_process'; const should = chai.should(); describe('jsonldRequest', function() { - this.timeout(10000); - let fixturePath; let fixtureData; From 28c2739beeae7f4a9c21ea7ca366dbdaf390d52f Mon Sep 17 00:00:00 2001 From: Benjamin Young Date: Thu, 26 Mar 2026 14:08:04 -0400 Subject: [PATCH 07/13] Use deep.equal instead of item checks. --- test/jsonld-request.test.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/jsonld-request.test.js b/test/jsonld-request.test.js index f4f4efe..23e7de4 100644 --- a/test/jsonld-request.test.js +++ b/test/jsonld-request.test.js @@ -19,9 +19,7 @@ describe('jsonldRequest', function() { it('loads a local file (file://) and parses JSON-LD', async () => { const fileUrl = `file://${fixturePath}`; const {data} = await jsonldRequest(fileUrl, {allow: ['file']}); - should.exist(data); - data.should.be.an('object'); - data.should.have.property('@context'); + data.should.deep.equal(JSON.parse(fixtureData)); }); it('loads JSON-LD over HTTP and parses it', async () => { From 8bbcd8252644acf72781a7a62304ba35cce02941 Mon Sep 17 00:00:00 2001 From: Benjamin Young Date: Tue, 31 Mar 2026 16:02:28 -0400 Subject: [PATCH 08/13] Update CHANGELOG.md. --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 367c035..69a3062 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ - Upgraded `@digitalbazaar/http-client` and `jsonld` to latest. - Set `engines.node` to current LTS versions. +### Fixed +- Add mocha/chai tests for `stdin`, file, and `http` document loading. + ## 2.0.1 - 2023-09-26 ### Fixed From 5db88dd2ae72b55005b941eff32b1d67d86557d3 Mon Sep 17 00:00:00 2001 From: Benjamin Young Date: Tue, 31 Mar 2026 16:16:00 -0400 Subject: [PATCH 09/13] Restrict GitHub Action restrictions. --- .github/workflows/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index bfcfae6..91f08dc 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,6 +2,8 @@ name: Main CI on: [push] +permissions: {} + jobs: lint: runs-on: ubuntu-latest From 78be10e860de2693c44415e5dc3c4258de2ce78e Mon Sep 17 00:00:00 2001 From: Benjamin Young Date: Tue, 31 Mar 2026 16:16:25 -0400 Subject: [PATCH 10/13] Upgrade current `lint` action to use newer stuff. --- .github/workflows/main.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 91f08dc..5ed2822 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,9 +12,11 @@ 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 From 8f394c78d64de614de0f7376a725271133d32678 Mon Sep 17 00:00:00 2001 From: Benjamin Young Date: Tue, 31 Mar 2026 16:27:23 -0400 Subject: [PATCH 11/13] Add test action. --- .github/workflows/main.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5ed2822..9ce2033 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -23,3 +23,21 @@ jobs: run: npm install - name: Lint run: npm run lint + test: + runs-on: ubuntu-latest + timeout-minutes: 360 + strategy: + matrix: + node-version: [20.x] + 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: Run test with Node.js ${{ matrix.node-version }} + run: npm run test From e2afba300332559256b02a7fefff050e82a59efd Mon Sep 17 00:00:00 2001 From: BigBlueHat Date: Thu, 28 May 2026 08:58:44 -0400 Subject: [PATCH 12/13] Bump node versions in action; deep equal fixture. Thanks @davidlehn Co-authored-by: David I. Lehn --- .github/workflows/main.yml | 4 ++-- test/jsonld-request.test.js | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9ce2033..72e002f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,7 +10,7 @@ jobs: timeout-minutes: 10 strategy: matrix: - node-version: [20.x] + node-version: [24.x] steps: - uses: actions/checkout@v6 with: @@ -28,7 +28,7 @@ jobs: timeout-minutes: 360 strategy: matrix: - node-version: [20.x] + node-version: [20.x, 22.x, 24.x] steps: - uses: actions/checkout@v6 with: diff --git a/test/jsonld-request.test.js b/test/jsonld-request.test.js index 23e7de4..0d4df79 100644 --- a/test/jsonld-request.test.js +++ b/test/jsonld-request.test.js @@ -37,8 +37,7 @@ describe('jsonldRequest', function() { try { const {data} = await jsonldRequest(url, {allow: ['http', 'https']}); should.exist(data); - data.should.be.an('object'); - data.should.have.property('@context'); + data.should.deep.equal(JSON.parse(fixtureData)); } finally { server.close(); } @@ -70,7 +69,6 @@ describe('jsonldRequest', function() { } const parsed = JSON.parse(stdout); should.exist(parsed); - parsed.should.be.an('object'); - parsed.should.have.property('@context'); + parsed.should.deep.equal(JSON.parse(fixtureData)); }); }); From a537d96f4b865f6bdb8338018cfeca9a0569c0de Mon Sep 17 00:00:00 2001 From: Benjamin Young Date: Thu, 28 May 2026 09:06:35 -0400 Subject: [PATCH 13/13] Remove test timeout. --- .github/workflows/main.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 72e002f..006aa94 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -25,7 +25,6 @@ jobs: run: npm run lint test: runs-on: ubuntu-latest - timeout-minutes: 360 strategy: matrix: node-version: [20.x, 22.x, 24.x]