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

describe('AssetGraph.findAssets', function () {
    it('should handle a test case with 6 assets', async function () {
        const assetGraph = new AssetGraph();
        await assetGraph.loadAssets(
            new AssetGraph.Html({text: 'a', foo: 'bar'}),
            new AssetGraph.Html({text: 'b', foo: 'bar'}),
            new AssetGraph.Html({text: 'c', foo: 'quux'}),
            new AssetGraph.Css({text: 'body { color: #ddd; }', foo: 'baz'}),
            new AssetGraph.Css({text: 'body { color: #eee; }'}),
            new AssetGraph.Htc({text: 'f', foo: 'baz'})
        );

        expect(assetGraph, 'to contain assets', {foo: 'bar'}, 2);
        expect(assetGraph, 'to contain assets', {foo: 'baz'}, 2);
        expect(assetGraph, 'to contain asset', {foo: 'quux'});
        expect(assetGraph, 'to contain asset', {foo: ['quux']});
        expect(assetGraph, 'to contain asset', {foo: function (val) {return typeof val === 'undefined';}});

        expect(assetGraph, 'to contain assets', {foo: ['bar', 'quux']}, 3);
        expect(assetGraph, 'to contain assets', {foo: ['bar', 'baz']}, 4);
        expect(assetGraph, 'to contain assets', {foo: AssetGraph.query.or('quux', function (val) {return typeof val === 'undefined';})}, 2);

        expect(assetGraph, 'to contain assets', 'Html', 3);
        expect(assetGraph, 'to contain assets', 'Css', 2);
        expect(assetGraph, 'to contain asset', 'Htc');

        expect(assetGraph, 'to contain assets', {type: ['Css', 'Html']}, 5);
        expect(assetGraph, 'to contain assets', {type: ['Htc', 'Css', 'Html']}, 6);
        expect(assetGraph, 'to contain assets', {type: ['Htc', 'Html']}, 4);
        expect(assetGraph, 'to contain assets', {type: ['Css', 'Htc']}, 3);

        expect(assetGraph, 'to contain asset', {foo: 'baz', type: 'Css'});
        expect(assetGraph, 'to contain assets', {foo: 'bar', type: 'Html'}, 2);
        expect(assetGraph, 'to contain no assets', {foo: 'quux', type: 'Htc'});

        expect(assetGraph, 'to contain no assets', {type: 'Html', incoming: {type: 'HtmlAnchor'}});

        expect(assetGraph, 'to contain no assets', {outgoing: {type: 'HtmlAnchor'}});

        // FIXME: "node: false" is to prevent it Asset#addRelation from
        // attempting to attach the anchors, which will fail because
        // HtmlRelation#attachNodeBeforeOrAfter requires 'before' or 'after'
        // and an adjacent node, which we don't have here.
        // The test sidestepped this problem before by not calling
        // Relation#attach, but that's now a side effect of Asset#addRelation
        // if the relation does not have a node property already.
        assetGraph.findAssets({text: 'a'})[0].addRelation({
            type: 'HtmlAnchor',
            to: assetGraph.findAssets({text: 'b'})[0],
            node: false
        }, 'last');
        assetGraph.findAssets({text: 'a'})[0].addRelation({ // Identical to the first
            type: 'HtmlAnchor',
            to: assetGraph.findAssets({text: 'b'})[0],
            node: false
        }, 'last');
        assetGraph.findAssets({text: 'a'})[0].addRelation({
            type: 'HtmlAnchor',
            to: assetGraph.findAssets({text: 'c'})[0],
            node: false
        }, 'last');
        assetGraph.findAssets({text: 'body { color: #eee; }'})[0].addRelation({
            type: 'CssImage',
            to: assetGraph.findAssets({text: 'f'})[0],
            node: false
        }, 'last');

        expect(assetGraph, 'to contain assets', {type: 'Html', incoming: {type: 'HtmlAnchor'}}, 2);
        expect(assetGraph, 'to contain assets', {incoming: {type: 'HtmlAnchor'}}, 2);

        expect(assetGraph, 'to contain asset', {outgoing: {type: 'HtmlAnchor'}});
        expect(assetGraph, 'to contain asset', {outgoing: {to: {text: 'f'}}});
    });
});<commit_msg>findAssets: Fix wrong relation type in test<commit_after>/*global describe, it*/
const expect = require('./unexpected-with-plugins');
const AssetGraph = require('../lib/AssetGraph');

describe('AssetGraph.findAssets', function () {
    it('should handle a test case with 6 assets', async function () {
        const assetGraph = new AssetGraph();
        await assetGraph.loadAssets(
            new AssetGraph.Html({text: 'a', foo: 'bar'}),
            new AssetGraph.Html({text: 'b', foo: 'bar'}),
            new AssetGraph.Html({text: 'c', foo: 'quux'}),
            new AssetGraph.Css({text: 'body { color: #ddd; }', foo: 'baz'}),
            new AssetGraph.Css({text: 'body { color: #eee; }'}),
            new AssetGraph.Htc({text: 'f', foo: 'baz'})
        );

        expect(assetGraph, 'to contain assets', {foo: 'bar'}, 2);
        expect(assetGraph, 'to contain assets', {foo: 'baz'}, 2);
        expect(assetGraph, 'to contain asset', {foo: 'quux'});
        expect(assetGraph, 'to contain asset', {foo: ['quux']});
        expect(assetGraph, 'to contain asset', {foo: function (val) {return typeof val === 'undefined';}});

        expect(assetGraph, 'to contain assets', {foo: ['bar', 'quux']}, 3);
        expect(assetGraph, 'to contain assets', {foo: ['bar', 'baz']}, 4);
        expect(assetGraph, 'to contain assets', {foo: AssetGraph.query.or('quux', function (val) {return typeof val === 'undefined';})}, 2);

        expect(assetGraph, 'to contain assets', 'Html', 3);
        expect(assetGraph, 'to contain assets', 'Css', 2);
        expect(assetGraph, 'to contain asset', 'Htc');

        expect(assetGraph, 'to contain assets', {type: ['Css', 'Html']}, 5);
        expect(assetGraph, 'to contain assets', {type: ['Htc', 'Css', 'Html']}, 6);
        expect(assetGraph, 'to contain assets', {type: ['Htc', 'Html']}, 4);
        expect(assetGraph, 'to contain assets', {type: ['Css', 'Htc']}, 3);

        expect(assetGraph, 'to contain asset', {foo: 'baz', type: 'Css'});
        expect(assetGraph, 'to contain assets', {foo: 'bar', type: 'Html'}, 2);
        expect(assetGraph, 'to contain no assets', {foo: 'quux', type: 'Htc'});

        expect(assetGraph, 'to contain no assets', {type: 'Html', incoming: {type: 'HtmlAnchor'}});

        expect(assetGraph, 'to contain no assets', {outgoing: {type: 'HtmlAnchor'}});

        // FIXME: "node: false" is to prevent it Asset#addRelation from
        // attempting to attach the anchors, which will fail because
        // HtmlRelation#attachNodeBeforeOrAfter requires 'before' or 'after'
        // and an adjacent node, which we don't have here.
        // The test sidestepped this problem before by not calling
        // Relation#attach, but that's now a side effect of Asset#addRelation
        // if the relation does not have a node property already.
        assetGraph.findAssets({text: 'a'})[0].addRelation({
            type: 'HtmlAnchor',
            to: assetGraph.findAssets({text: 'b'})[0],
            node: false
        }, 'last');
        assetGraph.findAssets({text: 'a'})[0].addRelation({ // Identical to the first
            type: 'HtmlAnchor',
            to: assetGraph.findAssets({text: 'b'})[0],
            node: false
        }, 'last');
        assetGraph.findAssets({text: 'a'})[0].addRelation({
            type: 'HtmlAnchor',
            to: assetGraph.findAssets({text: 'c'})[0],
            node: false
        }, 'last');
        assetGraph.findAssets({text: 'body { color: #eee; }'})[0].addRelation({
            type: 'CssBehavior',
            to: assetGraph.findAssets({text: 'f'})[0],
            node: false
        }, 'last');

        expect(assetGraph, 'to contain assets', {type: 'Html', incoming: {type: 'HtmlAnchor'}}, 2);
        expect(assetGraph, 'to contain assets', {incoming: {type: 'HtmlAnchor'}}, 2);

        expect(assetGraph, 'to contain asset', {outgoing: {type: 'HtmlAnchor'}});
        expect(assetGraph, 'to contain asset', {outgoing: {to: {text: 'f'}}});
    });
});
{'fraction_non_alphanumeric': 0.17968652037617555, 'fraction_numerical': 0.005768025078369906, 'mean_word_length': 2.9426594167078597, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3580317', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2415, 'n_words': 724}