var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';
import chai, { expect } from 'chai';
import { DidIonResolver } from '../../src/did/did-ion-resolver.js';
import { DidResolver } from '../../src/did/did-resolver.js';
import { DwnErrorCode } from '../../src/core/dwn-error.js';
// extends chai to test promises
chai.use(chaiAsPromised);
describe('DidResolver', () => {
    if (process.env.TESTSOFF === 'true') {
        return;
    }
    it('should cache the resolution result and use the cached result when available', () => __awaiter(void 0, void 0, void 0, function* () {
        const did = 'did:ion:unusedDid';
        const didIonResolver = new DidIonResolver('unusedResolutionEndpoint');
        const didResolver = new DidResolver([didIonResolver]);
        const mockResolution = {
            didDocument: 'any',
            didDocumentMetadata: 'any',
            didResolutionMetadata: 'any'
        };
        const ionDidResolveSpy = sinon.stub(didIonResolver, 'resolve').resolves(mockResolution);
        const cacheGetSpy = sinon.spy(didResolver['cache'], 'get');
        // calling resolve twice
        const resolutionResult1 = yield didResolver.resolve(did);
        expect(resolutionResult1).to.equal(mockResolution);
        const resolutionResult2 = yield didResolver.resolve(did);
        expect(resolutionResult2).to.equal(mockResolution);
        sinon.assert.calledTwice(cacheGetSpy); // should try to fetch from cache both times
        sinon.assert.calledOnce(ionDidResolveSpy); // should only resolve using ION resolver once (the first time)
    }));
    it('should throw error when invalid DID is used', () => __awaiter(void 0, void 0, void 0, function* () {
        const did = 'blah';
        const didIonResolver = new DidIonResolver();
        const didResolver = new DidResolver([didIonResolver]);
        yield expect(didResolver.resolve(did)).to.eventually.be.rejected.and.has.property('code', DwnErrorCode.DidNotValid);
    }));
    it('should throw error when unsupported DID method is used', () => __awaiter(void 0, void 0, void 0, function* () {
        const did = 'did:foo:EiClkZMDxPKqC9c-umQfTkR8vvZ9JPhl_xLDI9Nfk38w5w';
        const didIonResolver = new DidIonResolver();
        const didResolver = new DidResolver([didIonResolver]);
        yield expect(didResolver.resolve(did)).to.eventually.be.rejected.and.has.property('code', DwnErrorCode.DidMethodNotSupported);
    }));
    it('should throw error when resolution fails due to error in didResolutionMetadata', () => __awaiter(void 0, void 0, void 0, function* () {
        const did = 'did:ion:EiClkZMDxPKqC9c-umQfTkR8vvZ9JPhl_xLDI9Nfk38w5w';
        const didIonResolver = new DidIonResolver('unusedResolutionEndpoint');
        const didResolver = new DidResolver([didIonResolver]);
        const mockResolution = {
            didDocument: 'any',
            didResolutionMetadata: { error: 'some error' },
            didDocumentMetadata: 'any'
        };
        const ionDidResolveSpy = sinon.stub(didIonResolver, 'resolve').resolves(mockResolution);
        const cacheGetSpy = sinon.spy(didResolver['cache'], 'get');
        yield expect(didResolver.resolve(did)).to.eventually.be.rejected.and.has.property('code', DwnErrorCode.DidResolutionFailed);
        sinon.assert.calledOnce(cacheGetSpy);
        sinon.assert.calledOnce(ionDidResolveSpy);
    }));
    it('should throw error when resolution fails due to undefined didDocument', () => __awaiter(void 0, void 0, void 0, function* () {
        const did = 'did:ion:EiClkZMDxPKqC9c-umQfTkR8vvZ9JPhl_xLDI9Nfk38w5w';
        const didIonResolver = new DidIonResolver('unusedResolutionEndpoint');
        const didResolver = new DidResolver([didIonResolver]);
        const mockResolution = {
            didDocument: undefined,
            didResolutionMetadata: 'any',
            didDocumentMetadata: 'any'
        };
        const ionDidResolveSpy = sinon.stub(didIonResolver, 'resolve').resolves(mockResolution);
        const cacheGetSpy = sinon.spy(didResolver['cache'], 'get');
        yield expect(didResolver.resolve(did)).to.eventually.be.rejected.and.has.property('code', DwnErrorCode.DidResolutionFailed);
        sinon.assert.calledOnce(cacheGetSpy);
        sinon.assert.calledOnce(ionDidResolveSpy);
    }));
});
//# sourceMappingURL=did-resolver.spec.js.map