@@ -4,8 +4,8 @@ import { TestAgent } from './utils/test-agent.js';
4
4
5
5
import sinon from 'sinon' ;
6
6
import { expect } from 'chai' ;
7
- import { DidJwk } from '@web5/dids' ;
8
- import { BearerIdentity } from '../src/bearer-identity.js ' ;
7
+ import { BearerDid , DidJwk } from '@web5/dids' ;
8
+ import { logger } from '@web5/common ' ;
9
9
10
10
describe ( 'AgentDidResolverCache' , ( ) => {
11
11
let resolverCache : AgentDidResolverCache ;
@@ -61,11 +61,10 @@ describe('AgentDidResolverCache', () => {
61
61
} ) ;
62
62
63
63
it ( 'should not call resolve if the DID is not the agent DID or exists as an identity in the agent' , async ( ) => {
64
- const did = await DidJwk . create ( { } ) ;
64
+ const did = await DidJwk . create ( ) ;
65
65
const getStub = sinon . stub ( resolverCache [ 'cache' ] , 'get' ) . resolves ( JSON . stringify ( { ttlMillis : Date . now ( ) - 1000 , value : { didDocument : { id : did . uri } } } ) ) ;
66
- const resolveSpy = sinon . spy ( testHarness . agent . did , 'resolve' ) ;
66
+ const resolveSpy = sinon . spy ( testHarness . agent . did , 'resolve' ) . withArgs ( did . uri ) ;
67
67
const nextTickSpy = sinon . stub ( resolverCache [ 'cache' ] , 'nextTick' ) . resolves ( ) ;
68
- sinon . stub ( testHarness . agent . identity , 'get' ) . resolves ( undefined ) ;
69
68
70
69
await resolverCache . get ( did . uri ) ,
71
70
@@ -77,21 +76,52 @@ describe('AgentDidResolverCache', () => {
77
76
expect ( nextTickSpy . callCount ) . to . equal ( 1 ) ;
78
77
} ) ;
79
78
80
- it ( 'should resolve if the DID is managed by the agent' , async ( ) => {
81
- const did = await DidJwk . create ( { } ) ;
79
+ it ( 'should resolve and update if the DID is managed by the agent' , async ( ) => {
80
+ const did = await DidJwk . create ( ) ;
81
+
82
82
const getStub = sinon . stub ( resolverCache [ 'cache' ] , 'get' ) . resolves ( JSON . stringify ( { ttlMillis : Date . now ( ) - 1000 , value : { didDocument : { id : did . uri } } } ) ) ;
83
- const resolveSpy = sinon . spy ( testHarness . agent . did , 'resolve' ) ;
84
- const nextTickSpy = sinon . stub ( resolverCache [ 'cache' ] , 'nextTick' ) . resolves ( ) ;
85
- sinon . stub ( testHarness . agent . identity , 'get' ) . resolves ( new BearerIdentity ( {
86
- metadata : { name : 'Some Name' , uri : did . uri , tenant : did . uri } ,
87
- did,
83
+ const resolveSpy = sinon . spy ( testHarness . agent . did , 'resolve' ) . withArgs ( did . uri ) ;
84
+ sinon . stub ( resolverCache [ 'cache' ] , 'nextTick' ) . resolves ( ) ;
85
+ const didApiStub = sinon . stub ( testHarness . agent . did , 'get' ) ;
86
+ const updateSpy = sinon . stub ( testHarness . agent . did , 'update' ) . resolves ( ) ;
87
+ didApiStub . withArgs ( { didUri : did . uri , tenant : testHarness . agent . agentDid . uri } ) . resolves ( new BearerDid ( {
88
+ uri : did . uri ,
89
+ document : { id : did . uri } ,
90
+ metadata : { } ,
91
+ keyManager : testHarness . agent . keyManager
88
92
} ) ) ;
89
93
90
94
await resolverCache . get ( did . uri ) ,
91
95
92
96
// get should be called once, and we also resolve the DId as it's returned by the identity.get method
93
- expect ( getStub . callCount ) . to . equal ( 1 ) ;
94
- expect ( resolveSpy . callCount ) . to . equal ( 1 ) ;
97
+ expect ( getStub . callCount ) . to . equal ( 1 , 'get' ) ;
98
+ expect ( resolveSpy . callCount ) . to . equal ( 1 , 'resolve' ) ;
99
+ expect ( updateSpy . callCount ) . to . equal ( 1 , 'update' ) ;
100
+ } ) ;
101
+
102
+ it ( 'should log an error if an update is attempted and fails' , async ( ) => {
103
+ const did = await DidJwk . create ( ) ;
104
+
105
+ const getStub = sinon . stub ( resolverCache [ 'cache' ] , 'get' ) . resolves ( JSON . stringify ( { ttlMillis : Date . now ( ) - 1000 , value : { didDocument : { id : did . uri } } } ) ) ;
106
+ const resolveSpy = sinon . spy ( testHarness . agent . did , 'resolve' ) . withArgs ( did . uri ) ;
107
+ sinon . stub ( resolverCache [ 'cache' ] , 'nextTick' ) . resolves ( ) ;
108
+ const didApiStub = sinon . stub ( testHarness . agent . did , 'get' ) ;
109
+ const updateSpy = sinon . stub ( testHarness . agent . did , 'update' ) . rejects ( new Error ( 'Some Error' ) ) ;
110
+ const consoleErrorSpy = sinon . stub ( logger , 'error' ) ;
111
+ didApiStub . withArgs ( { didUri : did . uri , tenant : testHarness . agent . agentDid . uri } ) . resolves ( new BearerDid ( {
112
+ uri : did . uri ,
113
+ document : { id : did . uri } ,
114
+ metadata : { } ,
115
+ keyManager : testHarness . agent . keyManager
116
+ } ) ) ;
117
+
118
+ await resolverCache . get ( did . uri ) ,
119
+
120
+ // get should be called once, and we also resolve the DId as it's returned by the identity.get method
121
+ expect ( getStub . callCount ) . to . equal ( 1 , 'get' ) ;
122
+ expect ( resolveSpy . callCount ) . to . equal ( 1 , 'resolve' ) ;
123
+ expect ( updateSpy . callCount ) . to . equal ( 1 , 'update' ) ;
124
+ expect ( consoleErrorSpy . callCount ) . to . equal ( 1 , 'console.error' ) ;
95
125
} ) ;
96
126
97
127
it ( 'does not cache notFound records' , async ( ) => {
@@ -107,7 +137,7 @@ describe('AgentDidResolverCache', () => {
107
137
108
138
it ( 'throws if the error is anything other than a notFound error' , async ( ) => {
109
139
const did = testHarness . agent . agentDid . uri ;
110
- const getStub = sinon . stub ( resolverCache [ 'cache' ] , 'get' ) . rejects ( new Error ( 'Some Error' ) ) ;
140
+ sinon . stub ( resolverCache [ 'cache' ] , 'get' ) . rejects ( new Error ( 'Some Error' ) ) ;
111
141
112
142
try {
113
143
await resolverCache . get ( did ) ;
0 commit comments