<commit_before>var assetsPath = '/base/test/assets/';

function ajax(url, onSuccess, onError) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'arraybuffer';
    xhr.onload = function () {
        if (xhr.status === 200) {
            onSuccess(xhr.response);
        } else {
            onError(new Error(xhr.statusText + ' (' + xhr.status + ')'));
        }
    };
    xhr.onerror = function () {
        onError(new Error('Network error'));
    };
    xhr.send();
}

describe('ID3Writer', function () {

    it('should be possible to create an instance', function () {
        var buffer = new ArrayBuffer(0);
        var writer = new ID3Writer(buffer);
        expect(writer).to.be.instanceof(ID3Writer);
    });

    it('should throw an exception if no argument passed to constructor', function () {
        expect(function () {
            var writer = new ID3Writer();
        }).to.throw(Error, 'First argument should be an instance of ArrayBuffer');
    });

    it('should change byte count', function (done) {
        var songByteCount = 1141027;
        var songWithTagByteCount = 1145201;
        ajax(assetsPath + 'the_decant_session.mp3', function (arrayBuffer) {
            expect(arrayBuffer.byteLength).to.be.equal(songByteCount);
            var writer = new ID3Writer(arrayBuffer);
            writer.setFrame('TIT2', 'song name')
                .setFrame('TPE1', ['song artists']);
            var arrayBufferWithTag = writer.addTag();
            expect(arrayBufferWithTag.byteLength).to.be.equal(songWithTagByteCount);
            done();
        });
    });

    it('wrong frame value type should throw an exception', function () {
        var frames = ['TPE1', 'TCOM', 'TCON'];
        var buffer = new ArrayBuffer(0);
        var writer = new ID3Writer(buffer);
        frames.forEach(function (frameName) {
            expect(function () {
                writer.setFrame(frameName, '');
            }).to.throw(Error, 'frame value should be an array of strings');
            expect(writer.setFrame(frameName, [])).to.be.instanceof(ID3Writer);
        });
    });

    it('set incorrect frame name should throw an exception', function () {
        var buffer = new ArrayBuffer(0);
        var writer = new ID3Writer(buffer);
        expect(function () {
            writer.setFrame('wrongFrameName', 'val');
        }).to.throw(Error, 'Unsupported frame');
    });

    describe('APIC', function () {

        it('should throw error when mime type is not detected', function () {
            var writer = new ID3Writer(new ArrayBuffer(0));
            expect(function () {
                writer.setFrame('APIC', new ArrayBuffer(20));
            }).to.throw(Error, 'Unknown picture MIME type');<commit_msg>test(USLT): Added test for setting USLT frame<commit_after>var assetsPath = '/base/test/assets/';

function ajax(url, onSuccess, onError) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'arraybuffer';
    xhr.onload = function () {
        if (xhr.status === 200) {
            onSuccess(xhr.response);
        } else {
            onError(new Error(xhr.statusText + ' (' + xhr.status + ')'));
        }
    };
    xhr.onerror = function () {
        onError(new Error('Network error'));
    };
    xhr.send();
}

function typedArray2Array(typedArray) {
    return Array.prototype.slice.call(typedArray);
}

describe('ID3Writer', function () {

    it('should be possible to create an instance', function () {
        var buffer = new ArrayBuffer(0);
        var writer = new ID3Writer(buffer);
        expect(writer).to.be.instanceof(ID3Writer);
    });

    it('should throw an exception if no argument passed to constructor', function () {
        expect(function () {
            var writer = new ID3Writer();
        }).to.throw(Error, 'First argument should be an instance of ArrayBuffer');
    });

    it('should change byte count', function (done) {
        var songByteCount = 1141027;
        var songWithTagByteCount = 1145201;
        ajax(assetsPath + 'the_decant_session.mp3', function (arrayBuffer) {
            expect(arrayBuffer.byteLength).to.be.equal(songByteCount);
            var writer = new ID3Writer(arrayBuffer);
            writer.setFrame('TIT2', 'song name')
                .setFrame('TPE1', ['song artists']);
            var arrayBufferWithTag = writer.addTag();
            expect(arrayBufferWithTag.byteLength).to.be.equal(songWithTagByteCount);
            done();
        });
    });

    it('wrong frame value type should throw an exception', function () {
        var frames = ['TPE1', 'TCOM', 'TCON'];
        var buffer = new ArrayBuffer(0);
        var writer = new ID3Writer(buffer);
        frames.forEach(function (frameName) {
            expect(function () {
                writer.setFrame(frameName, '');
            }).to.throw(Error, 'frame value should be an array of strings');
            expect(writer.setFrame(frameName, [])).to.be.instanceof(ID3Writer);
        });
    });

    it('set incorrect frame name should throw an exception', function () {
        var buffer = new ArrayBuffer(0);
        var writer = new ID3Writer(buffer);
        expect(function () {
            writer.setFrame('wrongFrameName', 'val');
        }).to.throw(Error, 'Unsupported frame');
    });

    it('should set USLT frame', function () {
        var lyrics = 'Вышел заяц на крыльцо. Rabbit went out.';
        var writer = new ID3Writer(new ArrayBuffer(0));
        writer.setFrame('USLT', lyrics);
        var buffer = writer.addTag();

        var coder16 = new TextEncoder('utf-16le');
        var frameTotalSize = lyrics.length * 2 + 16;
        var bufferUint8 = new Uint8Array(buffer, 10, frameTotalSize);

        expect(bufferUint8).to.eql(new Uint8Array([
                85, 83, 76, 84, // 'USLT'
                0, 0, 0, frameTotalSize - 10, // size without header (should be less than 128)
                0, 0, // flags
                1, // encoding
                101, 110, 103, // language
                0, 0 // content descriptor
            ].concat(typedArray2Array(coder16.encode(lyrics)))
        ));
    });

    describe('APIC', function () {

        it('should throw error when mime type is not detected', function () {
            var writer = new ID3Writer(new ArrayBuffer(0));
            expect(function () {
                writer.setFrame('APIC', new ArrayBuffer(20));
            }).to.throw(Error, 'Unknown picture MIME type');
{'fraction_non_alphanumeric': 0.13472137170851195, 'fraction_numerical': 0.018524188609920392, 'mean_word_length': 2.331463539010709, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2340349', 'n_tokens_mistral': 1927, 'n_tokens_neox': 1826, 'n_words': 470}