"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Red Hat, Inc. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
const sinon = __importStar(require("sinon"));
const chai = __importStar(require("chai"));
const sinon_chai_1 = __importDefault(require("sinon-chai"));
const schemaSelectionHandlers_1 = require("../src/languageserver/handlers/schemaSelectionHandlers");
const yamlSchemaService_1 = require("../src/languageservice/services/yamlSchemaService");
const yamlSettings_1 = require("../src/yamlSettings");
const requestTypes_1 = require("../src/requestTypes");
const testHelper_1 = require("./utils/testHelper");
const expect = chai.expect;
chai.use(sinon_chai_1.default);
describe('Schema Selection Handlers', () => {
    const sandbox = sinon.createSandbox();
    const connection = {};
    let service;
    let requestServiceMock;
    beforeEach(() => {
        requestServiceMock = sandbox.fake.resolves(undefined);
        service = new yamlSchemaService_1.YAMLSchemaService(requestServiceMock);
        connection.client = {};
        const onRequest = sandbox.fake();
        connection.onRequest = onRequest;
    });
    afterEach(() => {
        sandbox.restore();
    });
    it('add handler for "getSchema" and "getAllSchemas" requests', () => {
        new schemaSelectionHandlers_1.JSONSchemaSelection(service, new yamlSettings_1.SettingsState(), connection);
        expect(connection.onRequest).calledWith(requestTypes_1.SchemaSelectionRequests.getSchema);
        expect(connection.onRequest).calledWith(requestTypes_1.SchemaSelectionRequests.getAllSchemas);
    });
    it('getAllSchemas should return all schemas', async () => {
        service.registerExternalSchema('https://some.com/some.json', ['foo.yaml'], undefined, 'Schema name', 'Schema description');
        const settings = new yamlSettings_1.SettingsState();
        const testTextDocument = (0, testHelper_1.setupSchemaIDTextDocument)('');
        settings.documents = new yamlSettings_1.TextDocumentTestManager();
        settings.documents.set(testTextDocument);
        const selection = new schemaSelectionHandlers_1.JSONSchemaSelection(service, settings, connection);
        const result = await selection.getAllSchemas(testTextDocument.uri);
        expect(result).length(1);
        expect(result[0]).to.be.eqls({
            uri: 'https://some.com/some.json',
            fromStore: true,
            usedForCurrentFile: false,
            name: 'Schema name',
            description: 'Schema description',
            versions: undefined,
        });
    });
    it('getAllSchemas should return all schemas and mark used for current file', async () => {
        service.registerExternalSchema('https://some.com/some.json', [testHelper_1.SCHEMA_ID], undefined, 'Schema name', 'Schema description');
        const settings = new yamlSettings_1.SettingsState();
        const testTextDocument = (0, testHelper_1.setupSchemaIDTextDocument)('');
        settings.documents = new yamlSettings_1.TextDocumentTestManager();
        settings.documents.set(testTextDocument);
        const selection = new schemaSelectionHandlers_1.JSONSchemaSelection(service, settings, connection);
        const result = await selection.getAllSchemas(testTextDocument.uri);
        expect(result).length(1);
        expect(result[0]).to.be.eqls({
            uri: 'https://some.com/some.json',
            name: 'Schema name',
            description: 'Schema description',
            fromStore: false,
            usedForCurrentFile: true,
            versions: undefined,
        });
    });
    it('getSchemas should return all schemas', async () => {
        service.registerExternalSchema('https://some.com/some.json', [testHelper_1.SCHEMA_ID], undefined, 'Schema name', 'Schema description');
        const settings = new yamlSettings_1.SettingsState();
        const testTextDocument = (0, testHelper_1.setupSchemaIDTextDocument)('');
        settings.documents = new yamlSettings_1.TextDocumentTestManager();
        settings.documents.set(testTextDocument);
        const selection = new schemaSelectionHandlers_1.JSONSchemaSelection(service, settings, connection);
        const result = await selection.getSchemas(testTextDocument.uri);
        expect(result).length(1);
        expect(result[0]).to.be.eqls({
            uri: 'https://some.com/some.json',
            name: 'Schema name',
            description: 'Schema description',
            versions: undefined,
        });
    });
    it('getSchemas should return an inline $schema', async () => {
        const schemaUri = 'https://some.com/inline.json';
        requestServiceMock = sandbox.fake((uri) => {
            if (uri === schemaUri) {
                return Promise.resolve(JSON.stringify({
                    title: 'Schema name',
                    type: 'object',
                    properties: {
                        $schema: {
                            type: 'string',
                        },
                        firstName: {
                            type: 'string',
                        },
                    },
                    required: ['firstName'],
                    additionalProperties: false,
                }));
            }
            return Promise.reject(`Resource ${uri} not found.`);
        });
        service = new yamlSchemaService_1.YAMLSchemaService(requestServiceMock);
        const settings = new yamlSettings_1.SettingsState();
        const testTextDocument = (0, testHelper_1.setupSchemaIDTextDocument)(`firstName: John\n$schema: ${schemaUri}`);
        settings.documents = new yamlSettings_1.TextDocumentTestManager();
        settings.documents.set(testTextDocument);
        const selection = new schemaSelectionHandlers_1.JSONSchemaSelection(service, settings, connection);
        const result = await selection.getSchemas(testTextDocument.uri);
        expect(result).to.eql([
            {
                uri: schemaUri,
                name: 'Schema name',
                description: undefined,
                versions: undefined,
            },
        ]);
        expect(requestServiceMock).calledOnceWith(schemaUri);
    });
    it('getSchemas should not resolve schema references', async () => {
        requestServiceMock = sandbox.fake((uri) => {
            if (uri === 'https://some.com/some.json') {
                return Promise.resolve(JSON.stringify({
                    title: 'Schema name',
                    description: 'Schema description',
                    properties: {
                        child: {
                            $ref: 'https://some.com/ref.json',
                        },
                    },
                }));
            }
            return Promise.reject(`Resource ${uri} not found.`);
        });
        service = new yamlSchemaService_1.YAMLSchemaService(requestServiceMock);
        service.registerExternalSchema('https://some.com/some.json', [testHelper_1.SCHEMA_ID]);
        const settings = new yamlSettings_1.SettingsState();
        const testTextDocument = (0, testHelper_1.setupSchemaIDTextDocument)('');
        settings.documents = new yamlSettings_1.TextDocumentTestManager();
        settings.documents.set(testTextDocument);
        const selection = new schemaSelectionHandlers_1.JSONSchemaSelection(service, settings, connection);
        const result = await selection.getSchemas(testTextDocument.uri);
        expect(result).length(1);
        expect(result[0]).to.be.eqls({
            uri: 'https://some.com/some.json',
            name: 'Schema name',
            description: 'Schema description',
            versions: undefined,
        });
        expect(requestServiceMock).calledOnceWith('https://some.com/some.json');
        expect(requestServiceMock).not.calledWith('https://some.com/ref.json');
    });
    it('getSchemas should use registered schema metadata without loading schema content', async () => {
        const versions = {
            '1.0.0': 'https://some.com/some-1.0.0.json',
            '2.0.0': 'https://some.com/some-2.0.0.json',
        };
        service.registerExternalSchema('https://some.com/some.json', [testHelper_1.SCHEMA_ID], undefined, 'Schema name', 'Schema description', versions);
        const settings = new yamlSettings_1.SettingsState();
        const testTextDocument = (0, testHelper_1.setupSchemaIDTextDocument)('');
        settings.documents = new yamlSettings_1.TextDocumentTestManager();
        settings.documents.set(testTextDocument);
        const selection = new schemaSelectionHandlers_1.JSONSchemaSelection(service, settings, connection);
        const result = await selection.getSchemas(testTextDocument.uri);
        expect(result).length(1);
        expect(result[0]).to.be.eqls({
            uri: 'https://some.com/some.json',
            name: 'Schema name',
            description: 'Schema description',
            versions,
        });
        expect(requestServiceMock).not.called;
    });
    it('getSchemas should handle empty schemas', async () => {
        const settings = new yamlSettings_1.SettingsState();
        const testTextDocument = (0, testHelper_1.setupSchemaIDTextDocument)('');
        settings.documents = new yamlSettings_1.TextDocumentTestManager();
        settings.documents.set(testTextDocument);
        const selection = new schemaSelectionHandlers_1.JSONSchemaSelection(service, settings, connection);
        const result = await selection.getSchemas(testTextDocument.uri);
        expect(result).length(0);
    });
});
//# sourceMappingURL=schemaSelectionHandlers.test.js.map