From 9b3777eddf1292c46657e9cc6592503c4b59b367 Mon Sep 17 00:00:00 2001 From: James Chartrand Date: Sun, 12 Jan 2025 13:40:55 -0500 Subject: [PATCH] switch testing to run after tsc compile --- package.json | 17 ++++----- src/Verify.ts | 14 +++---- src/issuerRegistries.ts | 2 +- src/test-fixtures/vc.ts | 22 ++++++----- test/Verify.spec.ts | 82 +++++++++++++++++++++++++---------------- tsconfig.spec.json | 6 +-- 6 files changed, 80 insertions(+), 63 deletions(-) diff --git a/package.json b/package.json index 0f13533..2bd014a 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "version": "0.0.1", "scripts": { "build": "npm run clear && tsc -d && tsc -p tsconfig.esm.json", + "build-test": "npm run clear && tsc -p tsconfig.spec.json", "clear": "rimraf dist/*", "lint": "eslint .", "lint:fix": "eslint --fix .", @@ -12,7 +13,8 @@ "rebuild": "npm run clear && npm run build", "test": "npm run lint && npm run test-node", "test-karma": "karma start karma.conf.js", - "test-node": "cross-env NODE_ENV=test TS_NODE_PROJECT=tsconfig.spec.json TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register --project tsconfig.spec.json 'test/*.ts'" + "test-node-old": "cross-env NODE_ENV=test TS_NODE_PROJECT=tsconfig.spec.json TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register --project tsconfig.spec.json 'test/*.ts'", + "test-node": "npm run build-test && mocha dist/esm/test/*.spec.js && rm -rf dist/esm/test || true" }, "files": [ "dist", @@ -61,23 +63,17 @@ "prettier": "^2.8.1", "rimraf": "^3.0.2", "ts-node": "^10.9.1", + "tsx": "^4.19.2", "typescript": "^4.9.4" }, "publishConfig": { "access": "public" }, - "mocha": { - "require": "ts-node/register", - "extension": [ - "ts" - ], - "spec": "test/**/*.ts" - }, "keywords": [ "dcc" ], "engines": { - "node": ">=16.0" + "node": ">=18.0" }, "author": { "name": "Digital Credentials Consortium", @@ -89,5 +85,6 @@ "url": "/~https://github.com/digitalcredentials/isomorphic-lib-template" }, "homepage": "/~https://github.com/digitalcredentials/isomorphic-lib-template", - "bugs": "/~https://github.com/digitalcredentials/isomorphic-lib-template/issues" + "bugs": "/~https://github.com/digitalcredentials/isomorphic-lib-template/issues", + "type": "module" } diff --git a/src/Verify.ts b/src/Verify.ts index 8f54330..b713557 100644 --- a/src/Verify.ts +++ b/src/Verify.ts @@ -2,20 +2,20 @@ import { Ed25519Signature2020 } from '@digitalcredentials/ed25519-signature-2020'; import * as vc from '@digitalcredentials/vc'; import { securityLoader } from '@digitalcredentials/security-document-loader'; -import { getCredentialStatusChecker } from './credentialStatus'; -import { addTrustedIssuersToVerificationResponse } from './issuerRegistries'; +import { getCredentialStatusChecker } from './credentialStatus.js'; +import { addTrustedIssuersToVerificationResponse } from './issuerRegistries.js'; + +import { Credential } from './types/credential.js'; +import { VerificationResponse } from './types/result.js'; -import { Credential } from './types/credential'; -import { VerificationResponse } from './types/result'; -/* // the new eddsa-rdfc-2022-cryptosuite import {DataIntegrityProof} from '@digitalbazaar/data-integrity'; import {cryptosuite as eddsaRdfc2022CryptoSuite} from '@digitalbazaar/eddsa-rdfc-2022-cryptosuite'; -const suite = new DataIntegrityProof({ +const eddsaSuite = new DataIntegrityProof({ cryptosuite: eddsaRdfc2022CryptoSuite }); -*/ + const documentLoader = securityLoader({ fetchRemoteContexts: true }).build(); const suite = new Ed25519Signature2020(); diff --git a/src/issuerRegistries.ts b/src/issuerRegistries.ts index c3f2463..4ee1999 100644 --- a/src/issuerRegistries.ts +++ b/src/issuerRegistries.ts @@ -1,5 +1,5 @@ import {RegistryClient} from '@digitalcredentials/issuer-registry-client'; -import { VerificationResponse } from './types/result'; +import { VerificationResponse } from './types/result.js'; const registries = new RegistryClient() const registryNotYetLoaded = true; /** diff --git a/src/test-fixtures/vc.ts b/src/test-fixtures/vc.ts index 045339e..72b5ca2 100644 --- a/src/test-fixtures/vc.ts +++ b/src/test-fixtures/vc.ts @@ -1,11 +1,12 @@ -import { v2Expired } from "./verifiableCredentials/v2/v2Expired" -import { v2Revoked } from "./verifiableCredentials/v2/v2Revoked" -import { v2WithValidStatus } from "./verifiableCredentials/v2/v2WithValidStatus" -import { v2ExpiredWithValidStatus } from "./verifiableCredentials/v2/v2ExpiredWithValidStatus" +import { v2NoStatus } from "./verifiableCredentials/v2/v2NoStatus.js" +import { v2Expired } from "./verifiableCredentials/v2/v2Expired.js" +import { v2Revoked } from "./verifiableCredentials/v2/v2Revoked.js" +import { v2WithValidStatus } from "./verifiableCredentials/v2/v2WithValidStatus.js" +import { v2ExpiredWithValidStatus } from "./verifiableCredentials/v2/v2ExpiredWithValidStatus.js" -import { v1WithValidStatus } from "./verifiableCredentials/v1/v1WithValidStatus" -import { v1NoStatus } from "./verifiableCredentials/v1/v1NoStatus" -import { v2NoStatus } from "./verifiableCredentials/v2/v2NoStatus" +import { v1WithValidStatus } from "./verifiableCredentials/v1/v1WithValidStatus.js" +import { v1NoStatus } from "./verifiableCredentials/v1/v1NoStatus.js" +import { v1Expired } from "./verifiableCredentials/v1/v1Expired.js" const getVCv1 = (): any => { return JSON.parse(JSON.stringify(v1NoStatus)) @@ -39,8 +40,8 @@ const getVCv1Tampered = (): any => { return signedVC1 } -const getExpiredVC1 = (): any => { - return null +const getVCv1Expired = (): any => { + return JSON.parse(JSON.stringify(v1Expired)) } const getVCv2Expired = (): any => { return JSON.parse(JSON.stringify(v2Expired)) @@ -64,13 +65,14 @@ export { getVCv2Expired, getVCv2Revoked, getVCv2Tampered, + getVCv2ValidStatus, getVCv2ExpiredAndTampered, getVCv2NoProof, getVCv2, + getVCv1Expired, getVCv1Tampered, getVCv1ValidStatus, - getVCv2ValidStatus, getVCv1, getVCv1NoProof } diff --git a/test/Verify.spec.ts b/test/Verify.spec.ts index 5bb564f..393790b 100644 --- a/test/Verify.spec.ts +++ b/test/Verify.spec.ts @@ -1,9 +1,9 @@ import chai from 'chai' import deepEqualInAnyOrder from 'deep-equal-in-any-order' import { strict as assert } from 'assert'; -import { verifyCredential } from '../src/Verify' -import { getVCv2Expired, getVCv1Tampered, getVCv2ValidStatus, getVCv2Tampered } from '../src/test-fixtures/vc' -import { knownDIDRegistries } from '../.knownDidRegistries'; +import { verifyCredential } from '../src/Verify.js' +import { getVCv2Expired, getVCv1Tampered, getVCv1Expired, getVCv1ValidStatus, getVCv2ValidStatus, getVCv2Tampered } from '../src/test-fixtures/vc.js' +import { knownDIDRegistries } from '../.knownDidRegistries.js'; chai.use(deepEqualInAnyOrder); const {expect} = chai; @@ -41,49 +41,67 @@ const expectedResult = { describe('Verify', () => { describe('.verifyCredential', () => { + describe('with VC version 1', () => { + describe('returns fatal error', () => { - it('when tampered with', async () => { - const tamperedVC1 : any = getVCv1Tampered() - const result = await verifyCredential({credential: tamperedVC1, reloadIssuerRegistry: true, knownDIDRegistries}) - assert.ok(result.verified === false); - }) + + it('when tampered with', async () => { + const tamperedVC1 : any = getVCv1Tampered() + const result = await verifyCredential({credential: tamperedVC1, reloadIssuerRegistry: true, knownDIDRegistries}) + assert.ok(result.verified === false); + }) + }) + + describe('returns as verified', () => { + it('when status is valid', async () => { + const credential : any = getVCv1ValidStatus() + expectedResult.credential = credential; + const result = await verifyCredential({credential, reloadIssuerRegistry: true, knownDIDRegistries}) + expect(result).to.deep.equalInAnyOrder(expectedResult) // eslint-disable-line no-use-before-define + }) + }) + + describe('returns as unverified', () => { + it('when expired', async () => { + const credential : any = getVCv1Expired() + const result = await verifyCredential({credential, reloadIssuerRegistry: true, knownDIDRegistries}) + assert.ok(result.verified === false); + assert.ok(result.log); + }) + }) + + }) + describe('with VC version 2', () => { describe('returns fatal error', () => { it('when tampered with', async () => { const tamperedVC2 : any = getVCv2Tampered() const result = await verifyCredential({credential: tamperedVC2, reloadIssuerRegistry: true, knownDIDRegistries}) - // console.log("result returned from verifyCredential call:") - // console.log(JSON.stringify(result,null,2)) assert.ok(result.verified === false); - //expect(result.verified).to.be.true }) - }) - + }) - it('verifies with valid status', async () => { - const credential : any = getVCv2ValidStatus() - expectedResult.credential = credential; - const result = await verifyCredential({credential, reloadIssuerRegistry: true, knownDIDRegistries}) - //console.log("result returned from verifyCredential call:") - //console.log(JSON.stringify(result,null,2)) - expect(result.verified).to.be.true - expect(result.credential).to.eql(credential) - expect(result).to.deep.equalInAnyOrder(expectedResult) // eslint-disable-line no-use-before-define - }) + describe('returns as verified', () => { + it('when status is valid', async () => { + const credential : any = getVCv2ValidStatus() + expectedResult.credential = credential; + const result = await verifyCredential({credential, reloadIssuerRegistry: true, knownDIDRegistries}) + expect(result).to.deep.equalInAnyOrder(expectedResult) // eslint-disable-line no-use-before-define + }) + }) - it('returns unverified when expired', async () => { - const expiredVC2 : any = getVCv2Expired() - const result = await verifyCredential({credential: expiredVC2, reloadIssuerRegistry: true, knownDIDRegistries}) - //console.log("result returned from verifyCredential call:") - // console.log(JSON.stringify(result,null,2)) - assert.ok(result.verified === false); - assert.ok(result.log); - //expect(result.verified).to.be.true - }) + describe('returns as unverified', () => { + it('when expired', async () => { + const credential : any = getVCv2Expired() + const result = await verifyCredential({credential, reloadIssuerRegistry: true, knownDIDRegistries}) + assert.ok(result.verified === false); + assert.ok(result.log); + }) + }) }) }) }) diff --git a/tsconfig.spec.json b/tsconfig.spec.json index dfd8525..389b0c1 100644 --- a/tsconfig.spec.json +++ b/tsconfig.spec.json @@ -3,9 +3,8 @@ "strict": true, "target": "es2022", "lib": ["es2022", "dom"], - "module": "commonjs", + "module": "es2022", "moduleResolution": "node", - "esModuleInterop": true, "outDir": "dist/esm", "noImplicitAny": true, "removeComments": false, @@ -20,8 +19,9 @@ }, "include": [ "src/**/*", + "test/**/*", ".eslintrc.js", "karma.conf.js" ], - "exclude": ["node_modules", "dist", "test"] + "exclude": ["node_modules", "dist"] }