<commit_before><commit_msg>Added test/dwebp.coffee<commit_after>{dwebp: bin} = require 'webp'
fs = require 'fs'

{DWebp} = require '../src'

data = '<KEY>'
corrupt = '/9j/4AAQSkZJRgABAQEASABIAAD/7gAOQWRvYmUAZH//Z'

describe 'Webp', ->

  describe 'main', ->

    it 'should allow default bin rewriting', ->
      class Webp2 extends DWebp
        @bin: bin
      webp = new Webp2 new Buffer data, 'base64'
      webp.toBuffer().then (buffer) ->
        buffer.toString('utf8', 1, 4).should.be.equal 'PNG'

    it 'should accept bin as a constructor option', ->
      webp = new DWebp (new Buffer data, 'base64'), bin
      webp.toBuffer().then (buffer) ->
        buffer.toString('utf8', 1, 4).should.be.equal 'PNG'

    it 'should report errors', ->
      webp = new DWebp (new Buffer corrupt, 'base64'), bin
      webp.toBuffer().then ->
        throw new Error 'Should not be fulfilled'
      , (err) ->
        err.should.be.Error
        err.message.should.match /Status: 3 \(BITSTREAM_ERROR\)/

    it 'should cleanup tmp files on error', ->
      filename = ''
      webp = new DWebp (new Buffer corrupt, 'base64'), bin
      promise = webp.toBuffer()
      webp._tmpFilename.then (tmpFilename) ->
        filename = tmpFilename
        filename.should.not.be.empty
        fs.existsSync(filename).should.be.true
        promise
      .then ->
        throw new Error 'Should not be fulfilled'
      , (err) ->
        err.should.be.Error
        filename.should.not.be.empty
        fs.existsSync(filename).should.be.false
{'fraction_non_alphanumeric': 0.13594771241830064, 'fraction_numerical': 0.013071895424836602, 'mean_word_length': 2.4097995545657014, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 17, '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': '5241450', 'n_tokens_mistral': 518, 'n_tokens_neox': 497, 'n_words': 129}