<commit_before>/* global Errors: false */
describe('StackTrace', function () {
    var callback;
    var debugCallback;
    var errback;
    var debugErrback;

    beforeEach(function () {
        if (typeof Promise === 'undefined') {
            ES6Promise.polyfill();
        }

        callback = jasmine.createSpy('callback');
        errback = jasmine.createSpy('errback');
        debugCallback = function (stackframes) {
            console.log(stackframes);
        };
        debugErrback = function (e) {
            console.log(e.message);
            console.log(e.stack);
        };
    });

    describe('#get', function () {
        it('gets stacktrace from current location', function () {
            runs(function testStackTraceGet() {
                StackTrace.get().then(callback, errback)['catch'](errback);
            });
            waits(100);
            runs(function () {
                expect(callback).toHaveBeenCalled();
                expect(callback.mostRecentCall.args[0][0].functionName).toEqual('testStackTraceGet');
                expect(errback).not.toHaveBeenCalled();
            });
        });
    });

    describe('#fromError', function () {
        var server;
        beforeEach(function () {
            server = sinon.fakeServer.create();
        });
        afterEach(function () {
            server.restore();
        });

        it('rejects with Error given unparsable Error object', function () {
            runs(function () {
                StackTrace.fromError({message: 'ERROR_MESSAGE'})
                    .then(callback, errback)['catch'](errback);
            });
            waits(100);
            runs(function () {
                expect(callback).not.toHaveBeenCalled();
                expect(errback).toHaveBeenCalled();
            });
        });

        it('parses stacktrace from given Error object', function () {
            runs(function () {
                server.respondWith('GET', 'http://path/to/file.js', [404, {'Content-Type': 'text/plain'}, '']);
                StackTrace.fromError(Errors.IE_11)
                    .then(callback, errback)['catch'](errback);
                server.respond();
            });
            waits(100);
            runs(function () {
                expect(callback).toHaveBeenCalled();
                var stackFrames = callback.mostRecentCall.args[0];
                expect(stackFrames.length).toEqual(3);
                expect(stackFrames[0].fileName).toEqual('http://path/to/file.js');
                expect(errback).not.toHaveBeenCalled();
            });
        });

        it('filters returned stack', function () {
            runs(function () {
                function onlyFoos(stackFrame) {
                    return stackFrame.functionName === 'foo';
                }

                server.respondWith('GET', 'http://path/to/file.js', [404, {'Content-Type': 'text/plain'}, '']);
                StackTrace.fromError(Errors.IE_11, {filter: onlyFoos})
                    .then(callback, errback)['catch'](errback);
                server.respond();
            });
            waits(100);
            runs(function () {
                expect(callback).toHaveBeenCalled();
                var stackFrames = callback.mostRecentCall.args[0];
                expect(stackFrames.length).toEqual(1);
                expect(stackFrames[0].fileName).toEqual('http://path/to/file.js');
                expect(stackFrames[0].functionName).toEqual('foo');
                expect(errback).not.toHaveBeenCalled();
            });
        });

        it('uses source maps to enhance stack frames', function () {
            runs(function () {
                var sourceMin = 'var foo=function(){};function bar(){}var baz=eval("XXX");\n//@ sourceMappingURL=test.js.map';
                var sourceMap = '{"version":3,"sources":["./test.js"],"names":["foo","bar","baz","eval"],"mappings":"AAAA,GAAIA,KAAM,YACV,SAASC,QACT,GAAIC,KAAMC,KAAK","file":"test.min.js"}';
                server.respondWith('GET', 'http://path/to/file.js', [200, {'Content-Type': 'application/x-javascript'}, sourceMin]);
                server.respondWith('GET', 'test.js.map', [200, {'Content-Type': 'application/json'}, sourceMap]);

                var stack = 'TypeError: Unable to get property \'undef\' of undefined or null reference\n   at foo (http://path/to/file.js:45:13)';
                StackTrace.fromError({stack: stack}).then(callback, errback)['catch'](errback);
                server.respond();
            });
            waits(100);
            runs(function () {
                server.respond();
            });
            waits(100);
            runs(function () {
                expect(callback).toHaveBeenCalled();
                var stackFrames = callback.mostRecentCall.args[0];
                expect(stackFrames.length).toEqual(1);
                expect(stackFrames[0]).toMatchStackFrame(['foo', undefined, 'http://path/to/file.js', 45, 13]);
                expect(errback).not.toHaveBeenCalled();
            });
        });
    });

    describe('#generateArtificially', function () {
        it('gets stacktrace from current location', function () {
            runs(function testGenerateArtificially() {
                var stackFrameFilter = function (stackFrame) {
                    return stackFrame.getFunctionName() &&
                        stackFrame.getFunctionName().indexOf('testGenerateArtificially') > -1;
                };
                StackTrace.generateArtificially({filter: stackFrameFilter})
                    .then(callback, errback)['catch'](errback);
            });
            waits(100);
            runs(function () {
                expect(callback).toHaveBeenCalled();<commit_msg>Improve sourcemaps test based on feedback from @TwitchBronBron. Fixes #137<commit_after>/* global Errors: false */
describe('StackTrace', function () {
    var callback;
    var debugCallback;
    var errback;
    var debugErrback;

    beforeEach(function () {
        if (typeof Promise === 'undefined') {
            ES6Promise.polyfill();
        }

        callback = jasmine.createSpy('callback');
        errback = jasmine.createSpy('errback');
        debugCallback = function (stackframes) {
            console.log(stackframes);
        };
        debugErrback = function (e) {
            console.log(e.message);
            console.log(e.stack);
        };
    });

    describe('#get', function () {
        it('gets stacktrace from current location', function () {
            runs(function testStackTraceGet() {
                StackTrace.get().then(callback, errback)['catch'](errback);
            });
            waits(100);
            runs(function () {
                expect(callback).toHaveBeenCalled();
                expect(callback.mostRecentCall.args[0][0].functionName).toEqual('testStackTraceGet');
                expect(errback).not.toHaveBeenCalled();
            });
        });
    });

    describe('#fromError', function () {
        var server;
        beforeEach(function () {
            server = sinon.fakeServer.create();
        });
        afterEach(function () {
            server.restore();
        });

        it('rejects with Error given unparsable Error object', function () {
            runs(function () {
                StackTrace.fromError({message: 'ERROR_MESSAGE'})
                    .then(callback, errback)['catch'](errback);
            });
            waits(100);
            runs(function () {
                expect(callback).not.toHaveBeenCalled();
                expect(errback).toHaveBeenCalled();
            });
        });

        it('parses stacktrace from given Error object', function () {
            runs(function () {
                server.respondWith('GET', 'http://path/to/file.js', [404, {'Content-Type': 'text/plain'}, '']);
                StackTrace.fromError(Errors.IE_11)
                    .then(callback, errback)['catch'](errback);
                server.respond();
            });
            waits(100);
            runs(function () {
                expect(callback).toHaveBeenCalled();
                var stackFrames = callback.mostRecentCall.args[0];
                expect(stackFrames.length).toEqual(3);
                expect(stackFrames[0].fileName).toEqual('http://path/to/file.js');
                expect(errback).not.toHaveBeenCalled();
            });
        });

        it('filters returned stack', function () {
            runs(function () {
                function onlyFoos(stackFrame) {
                    return stackFrame.functionName === 'foo';
                }

                server.respondWith('GET', 'http://path/to/file.js', [404, {'Content-Type': 'text/plain'}, '']);
                StackTrace.fromError(Errors.IE_11, {filter: onlyFoos})
                    .then(callback, errback)['catch'](errback);
                server.respond();
            });
            waits(100);
            runs(function () {
                expect(callback).toHaveBeenCalled();
                var stackFrames = callback.mostRecentCall.args[0];
                expect(stackFrames.length).toEqual(1);
                expect(stackFrames[0].fileName).toEqual('http://path/to/file.js');
                expect(stackFrames[0].functionName).toEqual('foo');
                expect(errback).not.toHaveBeenCalled();
            });
        });

        it('uses source maps to enhance stack frames', function () {
            runs(function () {
                var sourceMin = 'function increment(){someVariable+=2;null.x()}var someVariable=2;increment();\n//# sourceMappingURL=file.min.js.map';
                var sourceMap = '{"version":3,"file":"file.min.js","sources":["file.js"],"names":["increment","someVariable","x"],"mappings":"AAAA,QAASA,aACLC,cAAgB,CAChB,MAAKC,IAET,GAAID,cAAe,CACnBD"}';
                server.respondWith('GET', 'http://path/to/file.min.js', [200, {'Content-Type': 'application/x-javascript'}, sourceMin]);
                server.respondWith('GET', 'http://path/to/file.min.js.map', [200, {'Content-Type': 'application/json'}, sourceMap]);

                var stack = 'TypeError: Cannot read property \'x\' of null\n   at increment (http://path/to/file.min.js:1:38)';
                StackTrace.fromError({stack: stack}).then(callback, debugErrback)['catch'](debugErrback);
                server.respond();
            });
            waits(100);
            runs(function () {
                server.respond();
            });
            waits(100);
            runs(function () {
                server.respond();
            });
            waits(100);
            runs(function () {
                expect(callback).toHaveBeenCalled();
                var stackFrames = callback.mostRecentCall.args[0];
                expect(stackFrames.length).toEqual(1);
                expect(stackFrames[0]).toMatchStackFrame(['null', undefined, 'file.js', 3, 4]);
                expect(errback).not.toHaveBeenCalled();
            });
        });
    });

    describe('#generateArtificially', function () {
        it('gets stacktrace from current location', function () {
            runs(function testGenerateArtificially() {
                var stackFrameFilter = function (stackFrame) {
                    return stackFrame.getFunctionName() &&
                        stackFrame.getFunctionName().indexOf('testGenerateArtificially') > -1;
                };
                StackTrace.generateArtificially({filter: stackFrameFilter})
                    .then(callback, errback)['catch'](errback);
            });
            waits(100);
            runs(function () {
                expect(callback).toHaveBeenCalled();
{'fraction_non_alphanumeric': 0.1493683851143735, 'fraction_numerical': 0.010669170365312394, 'mean_word_length': 1.8802851524090463, 'pattern_counts': {'":': 10, '<': 3, '<?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': '4704043', 'n_tokens_mistral': 3202, 'n_tokens_neox': 3016, 'n_words': 457}