"use strict";
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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@angular-devkit/core");
const chai_1 = require("chai");
const testing_1 = require("@angular-devkit/schematics/testing");
const path = require("path");
const ast_utils_1 = require("../helpers/ast-utils");
const ts = require("typescript");
const json_file_1 = require("../helpers/json-file");
const collectionPath = path.join(__dirname, "../../collection.json");
const workspaceOptions = {
    name: "workspace",
    newProjectRoot: "projects",
    version: "6.0.0",
};
const appOptions = {
    name: "schematest",
};
let appTree;
const schematicRunner = new testing_1.SchematicTestRunner("ng-add", collectionPath);
let tree;
describe("Creates a The Graph Demo", () => {
    beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
        appTree = yield schematicRunner
            .runExternalSchematicAsync("@schematics/angular", "workspace", workspaceOptions)
            .toPromise();
        appTree = yield schematicRunner
            .runExternalSchematicAsync("@schematics/angular", "application", appOptions, appTree)
            .toPromise();
        tree = yield schematicRunner
            .runSchematicAsync("ng-add", {
            project: "default",
            test: true,
            demoToInstall: true,
            addOns: [],
            dappDemo: "theGraph",
        }, appTree)
            .toPromise();
    }));
    it("The Graph contract entry in Contract JSON Config File", () => __awaiter(void 0, void 0, void 0, function* () {
        const jsonNew = new json_file_1.JSONFile(tree, "hardhat/contract.config.json");
        (0, chai_1.expect)(jsonNew.get(["theGraph", "name"])).to.be.equal("GravatarRegistry");
    }));
    it("The Graph Demo app should install The Grpah Module", () => __awaiter(void 0, void 0, void 0, function* () {
        (0, chai_1.expect)(tree.exists((0, core_1.normalize)("/projects/schematest/src/app/5-the-graph/the-graph-demo.module.ts"))).to.be.true;
    }));
    it("The Graph Demo should copy minimal contract", () => __awaiter(void 0, void 0, void 0, function* () {
        (0, chai_1.expect)(tree.exists((0, core_1.normalize)("/hardhat/contracts/GravatarRegistry.sol")))
            .to.be.true;
    }));
    it("The Graph Demo should include TheGraphModule import in app.module", () => __awaiter(void 0, void 0, void 0, function* () {
        const appModulePath = `/projects/schematest/src/app/app.module.ts`;
        const appModuleFile = tree.read((0, core_1.normalize)(appModulePath)).toString("utf-8");
        const source_app = ts.createSourceFile(appModulePath, appModuleFile, ts.ScriptTarget.Latest, true);
        const fileName = "./5-the-graph/the-graph-demo.module";
        const symbolName = "TheGraphDemoModule";
        const allImports = (0, ast_utils_1.findNodes)(source_app, ts.SyntaxKind.ImportDeclaration);
        const relevantImports = allImports.filter((node) => {
            const importFiles = node
                .getChildren()
                .filter(ts.isStringLiteral)
                .map((n) => n.text);
            return importFiles.filter((file) => file === fileName).length === 1;
        });
        (0, chai_1.expect)(relevantImports.length).to.be.gt(0);
        if (relevantImports.length > 0) {
            let importsAsterisk = false;
            // imports from import file
            const imports = [];
            relevantImports.forEach((n) => {
                Array.prototype.push.apply(imports, (0, ast_utils_1.findNodes)(n, ts.SyntaxKind.Identifier));
                if ((0, ast_utils_1.findNodes)(n, ts.SyntaxKind.AsteriskToken).length > 0) {
                    importsAsterisk = true;
                }
            });
            const importTextNodes = imports.filter((n) => n.text === symbolName);
            (0, chai_1.expect)(importTextNodes.length).to.equal(1);
        }
    }));
    it("Add On subgraph should create subgraph folder", () => __awaiter(void 0, void 0, void 0, function* () {
        (0, chai_1.expect)(tree.exists((0, core_1.normalize)('/projects/schematest/src/app/dapp-injector/services/graph-ql/graph-ql.service.ts'))).to.be.true;
    }));
    it("Add On Graph Node should create graph node folder", () => __awaiter(void 0, void 0, void 0, function* () {
        (0, chai_1.expect)(tree.exists((0, core_1.normalize)("/add-ons/graph-node/Dockerfile"))).to.be.true;
    }));
    it("Add Graph Node scripts", () => __awaiter(void 0, void 0, void 0, function* () {
        const packageJson = new json_file_1.JSONFile(tree, "package.json");
        (0, chai_1.expect)(packageJson.get(['scripts', "clean-graph-node"])).to.be.equal("rm -rf add-ons/graph-node/data/");
    }));
    it("Add On subgraph should create subgraph folder", () => __awaiter(void 0, void 0, void 0, function* () {
        (0, chai_1.expect)(tree.exists((0, core_1.normalize)("/add-ons/subgraph/schema.graphql"))).to.be.true;
    }));
    it("It Add subgraph scripts", () => __awaiter(void 0, void 0, void 0, function* () {
        const packageJson = new json_file_1.JSONFile(tree, "package.json");
        (0, chai_1.expect)(packageJson.get(['scripts', "build-graph"])).to.be.equal('cd subgraph && graph build');
    }));
    it("It Add subgraph dependencies", () => __awaiter(void 0, void 0, void 0, function* () {
        const packageJson = new json_file_1.JSONFile(tree, "package.json");
        (0, chai_1.expect)(packageJson.get(['devDependencies', "@graphprotocol/graph-cli"])).to.be.equal("^0.22.1");
    }));
});
//# sourceMappingURL=demo-the-graph_tests.js.map