33 */
44import * as chai from 'chai' ;
55import { contextsDir , credentialsDir } from '../lib/index.js' ;
6+ import { credentialMatches , queryByExample }
7+ from '@digitalbazaar/vc-querybyexample' ;
68import fs from 'node:fs' ;
9+ import { join } from 'node:path' ;
710
811const should = chai . should ( ) ;
12+ const expect = chai . expect ;
913
1014describe ( '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