"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const testing_1 = require("@angular-devkit/schematics/testing");
const schematics_1 = require("@angular-devkit/schematics");
const workspace_utils_1 = require("./workspace-utils");
function makeContext() {
    return { logger: { info: jasmine.createSpy('info'), warn: jasmine.createSpy('warn') } };
}
function makeTree(angularJson) {
    const tree = new testing_1.UnitTestTree(schematics_1.Tree.empty());
    tree.create('/angular.json', JSON.stringify(angularJson, null, 2));
    return tree;
}
const STYLE = 'node_modules/@seniorsistemas/angular-components/styles.css';
const BASE_ANGULAR_JSON = {
    projects: {
        app: {
            architect: {
                build: {
                    builder: '@angular-devkit/build-angular:browser',
                    options: {
                        styles: ['src/styles.css'],
                    },
                },
            },
        },
    },
};
describe('ensureAngularJsonStyle', () => {
    it('adiciona o style ao projeto com builder browser', () => {
        const tree = makeTree(BASE_ANGULAR_JSON);
        const ctx = makeContext();
        (0, workspace_utils_1.ensureAngularJsonStyle)(tree, STYLE, ctx);
        const result = JSON.parse(tree.readText('/angular.json'));
        expect(result.projects.app.architect.build.options.styles).toContain(STYLE);
        expect(ctx.logger.info).toHaveBeenCalled();
    });
    it('não duplica o style se já estiver presente (string)', () => {
        const tree = makeTree({
            ...BASE_ANGULAR_JSON,
            projects: {
                app: {
                    architect: {
                        build: {
                            builder: '@angular-devkit/build-angular:browser',
                            options: { styles: ['src/styles.css', STYLE] },
                        },
                    },
                },
            },
        });
        const ctx = makeContext();
        (0, workspace_utils_1.ensureAngularJsonStyle)(tree, STYLE, ctx);
        const result = JSON.parse(tree.readText('/angular.json'));
        const count = result.projects.app.architect.build.options.styles.filter((s) => s === STYLE).length;
        expect(count).toBe(1);
        expect(ctx.logger.info).not.toHaveBeenCalled();
    });
    it('não duplica o style se já estiver presente como objeto { input }', () => {
        const tree = makeTree({
            ...BASE_ANGULAR_JSON,
            projects: {
                app: {
                    architect: {
                        build: {
                            builder: '@angular-devkit/build-angular:browser',
                            options: { styles: [{ input: STYLE, bundleName: 'styles' }] },
                        },
                    },
                },
            },
        });
        const ctx = makeContext();
        (0, workspace_utils_1.ensureAngularJsonStyle)(tree, STYLE, ctx);
        const result = JSON.parse(tree.readText('/angular.json'));
        const styles = result.projects.app.architect.build.options.styles;
        const count = styles.filter((s) => s === STYLE || (typeof s === 'object' && s.input === STYLE)).length;
        expect(count).toBe(1);
    });
    it('não faz nada se angular.json não existir', () => {
        const tree = new testing_1.UnitTestTree(schematics_1.Tree.empty());
        const ctx = makeContext();
        expect(() => (0, workspace_utils_1.ensureAngularJsonStyle)(tree, STYLE, ctx)).not.toThrow();
        expect(ctx.logger.info).not.toHaveBeenCalled();
    });
    it('ignora projetos com builder não-browser (e2e, server, etc.)', () => {
        const tree = makeTree({
            projects: {
                e2e: {
                    architect: {
                        build: {
                            builder: '@angular-devkit/build-angular:karma',
                            options: { styles: [] },
                        },
                    },
                },
            },
        });
        const ctx = makeContext();
        (0, workspace_utils_1.ensureAngularJsonStyle)(tree, STYLE, ctx);
        const result = JSON.parse(tree.readText('/angular.json'));
        expect(result.projects.e2e.architect.build.options.styles).not.toContain(STYLE);
        expect(ctx.logger.info).not.toHaveBeenCalled();
    });
    it('suporta builder @angular-devkit/build-angular:application', () => {
        const tree = makeTree({
            projects: {
                app: {
                    architect: {
                        build: {
                            builder: '@angular-devkit/build-angular:application',
                            options: { styles: [] },
                        },
                    },
                },
            },
        });
        const ctx = makeContext();
        (0, workspace_utils_1.ensureAngularJsonStyle)(tree, STYLE, ctx);
        const result = JSON.parse(tree.readText('/angular.json'));
        expect(result.projects.app.architect.build.options.styles).toContain(STYLE);
    });
    it('adiciona o style em todos os projetos browser do workspace', () => {
        const tree = makeTree({
            projects: {
                app: {
                    architect: {
                        build: {
                            builder: '@angular-devkit/build-angular:browser',
                            options: { styles: [] },
                        },
                    },
                },
                admin: {
                    architect: {
                        build: {
                            builder: '@angular-devkit/build-angular:application',
                            options: { styles: ['src/admin.css'] },
                        },
                    },
                },
            },
        });
        const ctx = makeContext();
        (0, workspace_utils_1.ensureAngularJsonStyle)(tree, STYLE, ctx);
        const result = JSON.parse(tree.readText('/angular.json'));
        expect(result.projects.app.architect.build.options.styles).toContain(STYLE);
        expect(result.projects.admin.architect.build.options.styles).toContain(STYLE);
        expect(ctx.logger.info).toHaveBeenCalledTimes(2);
    });
});
//# sourceMappingURL=workspace-utils.test.js.map