<commit_before>/*global describe, it*/
var expect = require('../unexpected-with-plugins'),
    estraverse = require('estraverse'),
    AssetGraph = require('../../lib');

describe('transforms/bundleSystemJs', function () {
    it('should handle a simple test case', function () {
        return new AssetGraph({root: __dirname + '/../../testdata/transforms/bundleSystemJs/simple/'})
            .loadAssets('index.html')
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 3);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /index\.html$/} }, 3);
            })
            .bundleSystemJs()
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 4);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /index\.html$/} }, 4);
                expect(assetGraph, 'to contain asset', {
                    type: 'JavaScript',
                    fileName: /bundle/
                });
                expect(assetGraph.findRelations({ from: { url: /index\.html$/} })[2].to.text, 'to contain', 'alert(\'main!\');');
                expect(assetGraph.findRelations({
                    from: { url: /index\.html$/ },
                    to: { fileName: /bundle/ }
                })[0].to.text, 'to contain', 'alert(\'main!\');');
            })
            .bundleRelations()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 1);
            });
    });

    it('should pick up the source map information', function () {
        return new AssetGraph({root: __dirname + '/../../testdata/transforms/bundleSystemJs/simple/'})
            .loadAssets('index.html')
            .populate()
            .bundleSystemJs()
            .populate()
            .queue(function (assetGraph) {
                var numNodesWithCorrectLoc = 0;
                estraverse.traverse(assetGraph.findAssets({
                    type: 'JavaScript',
                    fileName: /bundle/
                })[0].parseTree, {
                    enter: function (node) {
                        if (node.loc && node.loc.source === assetGraph.root + 'main.js') {
                            numNodesWithCorrectLoc += 1;
                        }
                    }
                });
                expect(numNodesWithCorrectLoc, 'to be greater than or equal to', 1);
            });
    });

    it('should handle a simple test case with an extra System.config call', function () {
        return new AssetGraph({root: __dirname + '/../../testdata/transforms/bundleSystemJs/simpleWithExtraConfigCall/'})
            .loadAssets('index.html')
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain no relation', 'JavaScriptSystemImport');
                expect(assetGraph, 'to contain assets', 'JavaScript', 4);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /index\.html$/} }, 4);
            })
            .bundleSystemJs()
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 5);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /index\.html$/} }, 5);
                expect(assetGraph, 'to contain asset', {
                    type: 'JavaScript',
                    fileName: /bundle/
                });
            })
            .bundleRelations()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 1);
            });
    });

    it('should handle a complex test case', function () {
        return new AssetGraph({root: __dirname + '/../../testdata/transforms/bundleSystemJs/test-tree/'})
            .loadAssets('index.html')
            .populate({ followRelations: { type: AssetGraph.query.not('JavaScriptSystemImport') } })
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 4);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /index\.html$/} }, 4);
            })
            .bundleSystemJs()
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 5);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /index\.html$/ } }, 5);
                expect(assetGraph, 'to contain asset', {
                    type: 'JavaScript',
                    fileName: /bundle/
                });
            })
            .bundleRelations()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 1);
            });
    });

    it('should handle a multi-page test case with one System.import call per page importing the same thing', function () {
        return new AssetGraph({root: __dirname + '/../../testdata/transforms/bundleSystemJs/multiPageOneSystemImportEach/'})
            .loadAssets('*.html')
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'Html', 2);
                expect(assetGraph, 'to contain assets', 'JavaScript', 4);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /page1\.html$/} }, 3);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /page2\.html$/} }, 3);
            })
            .bundleSystemJs()
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 6);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /page1\.html$/} }, 4);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /page2\.html$/} }, 4);
                // FIXME: Expect the two to be the same bundle asset?
                expect(assetGraph, 'to contain assets', {
                    type: 'JavaScript',
                    fileName: /bundle/
                }, 2);
                expect(assetGraph.findRelations({
                    from: { url: /page1\.html$/ },
                    to: { fileName: /bundle/ }
                })[0].to.text, 'to contain', 'alert(\'main!\');');
                expect(assetGraph.findRelations({
                    from: { url: /page2\.html$/ },
                    to: { fileName: /bundle/ }
                })[0].to.text, 'to contain', 'alert(\'main!\');');
            })
            .bundleRelations()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 2);
            });
    });

    it('should handle a multi-page test case with one System.import call per page importing different things', function () {
        return new AssetGraph({root: __dirname + '/../../testdata/transforms/bundleSystemJs/multiPageDifferentSystemImports/'})
            .loadAssets('*.html')
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'Html', 2);
                expect(assetGraph, 'to contain assets', 'JavaScript', 4);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /page1\.html$/} }, 3);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /page2\.html$/} }, 3);
            })
            .bundleSystemJs()
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 6);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /page1\.html$/} }, 4);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /page2\.html$/} }, 4);
                expect(assetGraph, 'to contain assets', {
                    type: 'JavaScript',
                    fileName: /bundle/
                }, 2);
                expect(assetGraph.findRelations({
                    from: { url: /page1\.html$/ },
                    to: { fileName: /bundle/ }
                })[0].to.text, 'to contain', 'alert(\'main!\');');
                expect(assetGraph.findRelations({
                    from: { url: /page2\.html$/ },
                    to: { fileName: /bundle/ }
                })[0].to.text, 'to contain', 'alert(\'otherMain!\');');
            })
            .bundleRelations()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 2);
            });
    });

    it('should handle a test case with a css plugin', function () {
        return new AssetGraph({root: __dirname + '/../../testdata/transforms/bundleSystemJs/cssPlugin/'})
            .loadAssets('index.html')
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'Html', 1);
                expect(assetGraph, 'to contain no assets', 'Css');
            })
            .bundleSystemJs()
            .populate({startAssets: {type: 'JavaScript'}})
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain asset', 'Css');
                expect(assetGraph, 'to contain relation', 'SystemJsBundle');
            })
            .populate({ startAssets: { type: 'JavaScript' } })
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain no relations', 'SystemJsBundle');
                expect(assetGraph, 'to contain relation', 'HtmlStyle');
            });
    });

    describe('with systemjs-plugin-less', function () {
        describe('and all JavaScript assets marked for removal via data-systemjs-remove', function () {
            it('should remove system.js and the configuration and not inject the bundle', function () {
                return new AssetGraph({root: __dirname + '/../../testdata/transforms/bundleSystemJs/onlyLess/'})
                    .loadAssets('index.html')
                    .populate()
                    .queue(function (assetGraph) {
                        expect(assetGraph, 'to contain assets', 'Html', 1);
                        expect(assetGraph, 'to contain no assets', 'Css');
                    })
                    .bundleSystemJs()
                    .populate()
                    .queue(function (assetGraph) {
                        expect(assetGraph.findAssets({type: 'Html'})[0].text, 'to contain', '<link rel="stylesheet" href="/styles.less">');<commit_msg>Fix test.<commit_after>/*global describe, it*/
var expect = require('../unexpected-with-plugins'),
    estraverse = require('estraverse'),
    AssetGraph = require('../../lib');

describe('transforms/bundleSystemJs', function () {
    it('should handle a simple test case', function () {
        return new AssetGraph({root: __dirname + '/../../testdata/transforms/bundleSystemJs/simple/'})
            .loadAssets('index.html')
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 3);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /index\.html$/} }, 3);
            })
            .bundleSystemJs()
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 4);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /index\.html$/} }, 4);
                expect(assetGraph, 'to contain asset', {
                    type: 'JavaScript',
                    fileName: /bundle/
                });
                expect(assetGraph.findRelations({ from: { url: /index\.html$/} })[2].to.text, 'to contain', 'alert(\'main!\');');
                expect(assetGraph.findRelations({
                    from: { url: /index\.html$/ },
                    to: { fileName: /bundle/ }
                })[0].to.text, 'to contain', 'alert(\'main!\');');
            })
            .bundleRelations()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 1);
            });
    });

    it('should pick up the source map information', function () {
        return new AssetGraph({root: __dirname + '/../../testdata/transforms/bundleSystemJs/simple/'})
            .loadAssets('index.html')
            .populate()
            .bundleSystemJs()
            .populate()
            .queue(function (assetGraph) {
                var numNodesWithCorrectLoc = 0;
                estraverse.traverse(assetGraph.findAssets({
                    type: 'JavaScript',
                    fileName: /bundle/
                })[0].parseTree, {
                    enter: function (node) {
                        if (node.loc && node.loc.source === assetGraph.root + 'main.js') {
                            numNodesWithCorrectLoc += 1;
                        }
                    }
                });
                expect(numNodesWithCorrectLoc, 'to be greater than or equal to', 1);
            });
    });

    it('should handle a simple test case with an extra System.config call', function () {
        return new AssetGraph({root: __dirname + '/../../testdata/transforms/bundleSystemJs/simpleWithExtraConfigCall/'})
            .loadAssets('index.html')
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain no relation', 'JavaScriptSystemImport');
                expect(assetGraph, 'to contain assets', 'JavaScript', 4);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /index\.html$/} }, 4);
            })
            .bundleSystemJs()
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 5);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /index\.html$/} }, 5);
                expect(assetGraph, 'to contain asset', {
                    type: 'JavaScript',
                    fileName: /bundle/
                });
            })
            .bundleRelations()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 1);
            });
    });

    it('should handle a complex test case', function () {
        return new AssetGraph({root: __dirname + '/../../testdata/transforms/bundleSystemJs/test-tree/'})
            .loadAssets('index.html')
            .populate({ followRelations: { type: AssetGraph.query.not('JavaScriptSystemImport') } })
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 4);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /index\.html$/} }, 4);
            })
            .bundleSystemJs()
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 5);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /index\.html$/ } }, 5);
                expect(assetGraph, 'to contain asset', {
                    type: 'JavaScript',
                    fileName: /bundle/
                });
            })
            .bundleRelations()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 1);
            });
    });

    it('should handle a multi-page test case with one System.import call per page importing the same thing', function () {
        return new AssetGraph({root: __dirname + '/../../testdata/transforms/bundleSystemJs/multiPageOneSystemImportEach/'})
            .loadAssets('*.html')
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'Html', 2);
                expect(assetGraph, 'to contain assets', 'JavaScript', 4);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /page1\.html$/} }, 3);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /page2\.html$/} }, 3);
            })
            .bundleSystemJs()
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 6);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /page1\.html$/} }, 4);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /page2\.html$/} }, 4);
                // FIXME: Expect the two to be the same bundle asset?
                expect(assetGraph, 'to contain assets', {
                    type: 'JavaScript',
                    fileName: /bundle/
                }, 2);
                expect(assetGraph.findRelations({
                    from: { url: /page1\.html$/ },
                    to: { fileName: /bundle/ }
                })[0].to.text, 'to contain', 'alert(\'main!\');');
                expect(assetGraph.findRelations({
                    from: { url: /page2\.html$/ },
                    to: { fileName: /bundle/ }
                })[0].to.text, 'to contain', 'alert(\'main!\');');
            })
            .bundleRelations()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 2);
            });
    });

    it('should handle a multi-page test case with one System.import call per page importing different things', function () {
        return new AssetGraph({root: __dirname + '/../../testdata/transforms/bundleSystemJs/multiPageDifferentSystemImports/'})
            .loadAssets('*.html')
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'Html', 2);
                expect(assetGraph, 'to contain assets', 'JavaScript', 4);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /page1\.html$/} }, 3);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /page2\.html$/} }, 3);
            })
            .bundleSystemJs()
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 6);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /page1\.html$/} }, 4);
                expect(assetGraph, 'to contain relations', { type: 'HtmlScript', from: { url: /page2\.html$/} }, 4);
                expect(assetGraph, 'to contain assets', {
                    type: 'JavaScript',
                    fileName: /bundle/
                }, 2);
                expect(assetGraph.findRelations({
                    from: { url: /page1\.html$/ },
                    to: { fileName: /bundle/ }
                })[0].to.text, 'to contain', 'alert(\'main!\');');
                expect(assetGraph.findRelations({
                    from: { url: /page2\.html$/ },
                    to: { fileName: /bundle/ }
                })[0].to.text, 'to contain', 'alert(\'otherMain!\');');
            })
            .bundleRelations()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'JavaScript', 2);
            });
    });

    it('should handle a test case with a css plugin', function () {
        return new AssetGraph({root: __dirname + '/../../testdata/transforms/bundleSystemJs/cssPlugin/'})
            .loadAssets('index.html')
            .populate()
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain assets', 'Html', 1);
                expect(assetGraph, 'to contain no assets', 'Css');
            })
            .bundleSystemJs()
            .populate({startAssets: {type: 'JavaScript'}})
            .queue(function (assetGraph) {
                expect(assetGraph, 'to contain asset', 'Css');
                expect(assetGraph, 'to contain no relations', 'SystemJsBundle');
                expect(assetGraph, 'to contain relation', 'HtmlStyle');
            });
    });

    describe('with systemjs-plugin-less', function () {
        describe('and all JavaScript assets marked for removal via data-systemjs-remove', function () {
            it('should remove system.js and the configuration and not inject the bundle', function () {
                return new AssetGraph({root: __dirname + '/../../testdata/transforms/bundleSystemJs/onlyLess/'})
                    .loadAssets('index.html')
                    .populate()
                    .queue(function (assetGraph) {
                        expect(assetGraph, 'to contain assets', 'Html', 1);
                        expect(assetGraph, 'to contain no assets', 'Css');
                    })
                    .bundleSystemJs()
                    .populate()
                    .queue(function (assetGraph) {
                        expect(assetGraph.findAssets({type: 'Html'})[0].text, 'to contain', '<link rel="stylesheet" href="/styles.less">');
{'fraction_non_alphanumeric': 0.14800407520607575, 'fraction_numerical': 0.0053718625544132625, 'mean_word_length': 1.7051233871977953, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3608857', 'n_tokens_mistral': 5815, 'n_tokens_neox': 5372, 'n_words': 1359}