Skip to content

Commit

Permalink
test cache refresh and update
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Oct 10, 2024
1 parent 47f418c commit 5c17558
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/agent/src/agent-did-resolver-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class AgentDidResolverCache extends DidResolverCacheLevel implements DidR
} catch(error: any) {
// if the error is not due to no changes detected, log the error
if (error.message && !error.message.includes('No changes detected, update aborted')) {
console.log(`Error updating DID: ${error.message}`);
console.error(`Error updating DID: ${error.message}`);
}
}
}
Expand Down
49 changes: 40 additions & 9 deletions packages/agent/tests/agent-did-resolver-cach.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TestAgent } from './utils/test-agent.js';

import sinon from 'sinon';
import { expect } from 'chai';
import { DidJwk } from '@web5/dids';
import { BearerDid, DidJwk } from '@web5/dids';
import { BearerIdentity } from '../src/bearer-identity.js';

describe('AgentDidResolverCache', () => {
Expand Down Expand Up @@ -76,21 +76,52 @@ describe('AgentDidResolverCache', () => {
expect(nextTickSpy.callCount).to.equal(1);
});

it('should resolve if the DID is managed by the agent', async () => {
const did = await DidJwk.create({});
it('should resolve and update if the DID is managed by the agent', async () => {
const did = await DidJwk.create();

const getStub = sinon.stub(resolverCache['cache'], 'get').resolves(JSON.stringify({ ttlMillis: Date.now() - 1000, value: { didDocument: { id: did.uri } } }));
const resolveSpy = sinon.spy(testHarness.agent.did, 'resolve');
const resolveSpy = sinon.spy(testHarness.agent.did, 'resolve').withArgs(did.uri);
const nextTickSpy = sinon.stub(resolverCache['cache'], 'nextTick').resolves();
sinon.stub(testHarness.agent.identity, 'get').resolves(new BearerIdentity({
metadata: { name: 'Some Name', uri: did.uri, tenant: did.uri },
did,
const didApiStub = sinon.stub(testHarness.agent.did, 'get');
const updateSpy = sinon.stub(testHarness.agent.did, 'update').resolves();
didApiStub.withArgs({ didUri: did.uri, tenant: testHarness.agent.agentDid.uri }).resolves(new BearerDid({
uri : did.uri,
document : { id: did.uri },
metadata : { },
keyManager : testHarness.agent.keyManager
}));

await resolverCache.get(did.uri),

// get should be called once, and we also resolve the DId as it's returned by the identity.get method
expect(getStub.callCount).to.equal(1);
expect(resolveSpy.callCount).to.equal(1);
expect(getStub.callCount).to.equal(1, 'get');
expect(resolveSpy.callCount).to.equal(1, 'resolve');
expect(updateSpy.callCount).to.equal(1, 'update');
});

it('should log an error if an update is attempted and fails', async () => {
const did = await DidJwk.create();

const getStub = sinon.stub(resolverCache['cache'], 'get').resolves(JSON.stringify({ ttlMillis: Date.now() - 1000, value: { didDocument: { id: did.uri } } }));
const resolveSpy = sinon.spy(testHarness.agent.did, 'resolve').withArgs(did.uri);
const nextTickSpy = sinon.stub(resolverCache['cache'], 'nextTick').resolves();
const didApiStub = sinon.stub(testHarness.agent.did, 'get');
const updateSpy = sinon.stub(testHarness.agent.did, 'update').rejects(new Error('Some Error'));
const consoleErrorSpy = sinon.stub(console, 'error');
didApiStub.withArgs({ didUri: did.uri, tenant: testHarness.agent.agentDid.uri }).resolves(new BearerDid({
uri : did.uri,
document : { id: did.uri },
metadata : { },
keyManager : testHarness.agent.keyManager
}));

await resolverCache.get(did.uri),

// get should be called once, and we also resolve the DId as it's returned by the identity.get method
expect(getStub.callCount).to.equal(1, 'get');
expect(resolveSpy.callCount).to.equal(1, 'resolve');
expect(updateSpy.callCount).to.equal(1, 'update');
expect(consoleErrorSpy.callCount).to.equal(1, 'console.error');
});

it('does not cache notFound records', async () => {
Expand Down

0 comments on commit 5c17558

Please sign in to comment.