/*
 * Copyright 2022 The Kubernetes Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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 { basename } from 'path';
import { Common, CLI } from '@kui-shell/test';
import { encodeComponent } from '@kui-shell/core';
import inputs from './inputs';
inputs.forEach(markdown => {
    ;
    ['guide'].forEach(command => {
        describe(`markdown code block dependence tree using command="${command}" ${basename(markdown.input)} ${process.env.MOCHA_RUN_TARGET || ''}`, function () {
            before(Common.before(this));
            after(Common.after(this));
            const timeout = { timeout: CLI.waitTimeout };
            const checkNode = (tree, containerSelector) => __awaiter(this, void 0, void 0, function* () {
                console.error('5', tree.name);
                const nodeSelector = `${containerSelector} .pf-c-tree-view__node`;
                console.error('6', tree.name, nodeSelector);
                const nodeElement = yield this.app.client.$(nodeSelector);
                console.error('7', tree.name);
                yield nodeElement.waitForExist(timeout);
                console.error('8', tree.name);
                const nameSelector = '.pf-c-tree-view__node-text';
                console.error('9', tree.name);
                const nameElement = yield nodeElement.$(nameSelector);
                console.error('10', tree.name);
                yield nameElement.waitForExist(timeout);
                console.error('11', tree.name);
                yield this.app.client.waitUntil(() => __awaiter(this, void 0, void 0, function* () {
                    const actualText = yield nameElement.getText();
                    if (actualText !== tree.name) {
                        console.error('12a', tree.name, actualText);
                        console.error('12b', containerSelector);
                    }
                    return actualText === tree.name;
                }), timeout);
                console.error('13', tree.name);
            });
            const scanNode = (tree, containerSelector) => __awaiter(this, void 0, void 0, function* () {
                yield checkNode(tree, containerSelector);
                // eslint-disable-next-line @typescript-eslint/no-use-before-define
                yield scanNodes(tree.children, containerSelector);
            });
            const scanNodes = (children, containerSelector) => __awaiter(this, void 0, void 0, function* () {
                if (children) {
                    yield Promise.all(children.map((subtree, idx) => __awaiter(this, void 0, void 0, function* () {
                        const listItemSelector = `${containerSelector} > ul > li:nth-child(${idx + 1})`;
                        const listItemElement = yield this.app.client.$(listItemSelector);
                        yield listItemElement.waitForExist(timeout);
                        const isExpanded = yield listItemElement.getAttribute('aria-expanded');
                        if (!isExpanded) {
                            yield listItemElement.scrollIntoView();
                            yield listItemElement.click();
                        }
                        yield scanNode(subtree, listItemSelector);
                        if (!isExpanded) {
                            yield listItemElement.scrollIntoView();
                            yield listItemElement.click();
                        }
                    })));
                }
            });
            const tree = markdown.tree(command);
            it(`should load the markdown and show dependence tree with root=${tree[0].name}`, () => __awaiter(this, void 0, void 0, function* () {
                try {
                    yield CLI.command(`${command} ${encodeComponent(markdown.input)}`, this.app);
                    console.error('1');
                    const treeSelector = '.kui--dependence-tree';
                    console.error('2');
                    const treeElement = yield this.app.client.$(treeSelector);
                    console.error('3');
                    yield treeElement.waitForExist(timeout);
                    console.error('4');
                    yield scanNodes(tree, treeSelector);
                }
                catch (err) {
                    yield Common.oops(this, true)(err);
                }
            }));
        });
    });
});
//# sourceMappingURL=index.js.map