Skip to content

Commit e3cb3cd

Browse files
committed
Add QueryByExample tests.
1 parent 9a19104 commit e3cb3cd

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

package-lock.json

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"devDependencies": {
2424
"@11ty/eleventy-fetch": "^5.1.0",
2525
"@digitalbazaar/eslint-config": "^7.0.1",
26+
"@digitalbazaar/vc-querybyexample": "github:digitalbazaar/vc-querybyexample",
2627
"chai": "^6.2.2",
2728
"cross-env": "^10.0.0",
2829
"eslint": "^9.39.2",

test/vc-examples.spec.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
*/
44
import * as chai from 'chai';
55
import {contextsDir, credentialsDir} from '../lib/index.js';
6+
import {credentialMatches, queryByExample}
7+
from '@digitalbazaar/vc-querybyexample';
68
import fs from 'node:fs';
9+
import {join} from 'node:path';
710

811
const should = chai.should();
12+
const expect = chai.expect;
913

1014
describe('credentialsDir & contextsDir', () => {
1115
it('should exist path to credentials directory.', async () => {
@@ -19,3 +23,42 @@ describe('credentialsDir & contextsDir', () => {
1923
fs.existsSync(contextsDir).should.be.true;
2024
});
2125
});
26+
27+
describe('credential and queries work together', () => {
28+
const dirents = fs.readdirSync(credentialsDir, {withFileTypes: true});
29+
const subdirs = dirents.filter(d => d.isDirectory()).map(d => d.name);
30+
31+
for(const sub of subdirs) {
32+
const dirPath = join(credentialsDir, sub);
33+
const credentialPath = join(dirPath, 'credential.json');
34+
const queriesPath = join(dirPath, 'queries.json');
35+
if(!fs.existsSync(credentialPath) || !fs.existsSync(queriesPath)) {
36+
continue;
37+
}
38+
39+
it(`${sub} should have one or more queries that match it's credential`,
40+
async () => {
41+
const credential = JSON.parse(fs.readFileSync(credentialPath, 'utf8'));
42+
const queries = JSON.parse(fs.readFileSync(queriesPath, 'utf8'));
43+
for(const [name, q] of Object.entries(queries)) {
44+
// a list of queries is an `AND` operation, so these should all pass
45+
const queriesList = !Array.isArray(q.credentialQuery) ?
46+
[q.credentialQuery] : q.credentialQuery;
47+
for(const exampleQuery of queriesList) {
48+
const pointers = queryByExample.toJsonPointerMap(exampleQuery);
49+
try {
50+
expect(credentialMatches({credential, map: pointers}),
51+
`${sub}'s ${name} queries do not match it's credential`
52+
).to.be.true;
53+
} catch(e) {
54+
console.error(e);
55+
console.dir(credential);
56+
console.dir(pointers);
57+
process.exit();
58+
}
59+
}
60+
}
61+
}
62+
);
63+
}
64+
});

0 commit comments

Comments
 (0)