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 fetch from 'cross-fetch';
import sinon from 'sinon';
import chai, { expect } from 'chai';
import { DidIonResolver } from '../../src/did/did-ion-resolver.js';
// extends chai to test promises
chai.use(chaiAsPromised);
describe('DidIonResolver', () => {
    if (process.env.TESTSOFF === 'true') {
        return;
    }
    const defaultResolutionEndpoint = 'https://discover.did.msidentity.com/1.0/identifiers/';
    let networkAvailable = false;
    before(() => __awaiter(void 0, void 0, void 0, function* () {
        // test network connectivity, `networkAvailable` is used by tests to decide whether to run tests through real network calls or stubs
        const testDidUrl = `${defaultResolutionEndpoint}did:ion:EiClkZMDxPKqC9c-umQfTkR8vvZ9JPhl_xLDI9Nfk38w5w`;
        try {
            const response = yield fetch(testDidUrl);
            if (response.status === 200) {
                networkAvailable = true;
            }
        }
        catch (_a) {
            // no op, all tests will run through stubs
        }
    }));
    it('should set a default resolution endpoint when none is given in constructor', () => __awaiter(void 0, void 0, void 0, function* () {
        const didIonResolver = new DidIonResolver();
        // console.log('didIonResolver', didIonResolver);
        expect(didIonResolver['resolutionEndpoint']).to.equal(defaultResolutionEndpoint);
    }));
    it('should resolve an ION DID correctly', () => __awaiter(void 0, void 0, void 0, function* () {
        var _b;
        const did = 'did:ion:EiClkZMDxPKqC9c-umQfTkR8vvZ9JPhl_xLDI9Nfk38w5w';
        const didIonResolver = new DidIonResolver();
        // stub network call if network is not available
        if (!networkAvailable) {
            sinon.stub(globalThis, 'fetch').resolves({
                status: 200,
                json: () => __awaiter(void 0, void 0, void 0, function* () {
                    return Promise.resolve({
                        didDocument: { id: did },
                        didDocumentMetadata: { canonicalId: did }
                    });
                })
            });
        }
        const resolutionDocument = yield didIonResolver.resolve(did);
        expect((_b = resolutionDocument.didDocument) === null || _b === void 0 ? void 0 : _b.id).to.equal(did);
        expect(resolutionDocument.didDocumentMetadata.canonicalId).to.equal(did);
    }));
    it('should throw if ION DID cannot be resolved', () => __awaiter(void 0, void 0, void 0, function* () {
        const did = 'did:ion:SomethingThatCannotBeResolved';
        const didIonResolver = new DidIonResolver();
        // stub network call if network is not available
        if (!networkAvailable) {
            sinon.stub(globalThis, 'fetch').resolves({ status: 404 });
        }
        const resolutionPromise = didIonResolver.resolve(did);
        yield expect(resolutionPromise).to.be.rejectedWith('unable to resolve');
    }));
});
//# sourceMappingURL=did-ion-resolver.spec.js.map