<commit_before>assert = require 'assert'
Upload = require '../src/index.coffee'

fs = require('fs')
gm = require('gm').subClass imageMagick: true

hash = require('crypto').createHash
rand = require('crypto').pseudoRandomBytes

upload = listObjects = putObject = null
cleanup = []

beforeEach ->
  upload = new Upload process.env.AWS_BUCKET_NAME,
    awsBucketPath: 'images_test/'
    awsBucketRegion: 'eu-west-1'
    awsBucketAcl: 'public-read'
    versions: [{
      original: true
      awsImageAcl: 'private'
    },{
      maxHeight: 1040
      maxWidth: 1040
      suffix: '-large'
      quality: 80
    },{
      maxHeight: 780
      maxWidth: 780
      suffix: '-medium'
    },{
      maxHeight: 320
      maxWidth: 320
      suffix: '-small'
    }]

  # Mock S3 API calls
  if process.env.INTEGRATION_TEST isnt 'true'
    listObjects = upload.s3.listObjects
    upload.s3.listObjects = (path, cb) ->
      process.nextTick -> cb null, Contents: []

    putObject = upload.s3.putObject
    upload.s3.putObject = (opts, cb) ->
      process.nextTick -> cb null, ETag: '"' + hash('md5').update(rand(32)).digest('hex') + '"'

# Clean up S3 objects
if process.env.INTEGRATION_TEST is 'true'
  afterEach (done) ->
    return process.nextTick done if cleanup.length is 0

    upload.s3.deleteObjects Delete: Objects: cleanup, (err) ->
      throw err if err
      cleanup = []
      done()

describe 'Upload', ->
  describe 'constructor', ->
    it 'should throw error for missing awsBucketName param', ->
      assert.throws ->
        new Upload()
      , /Bucket name can not be undefined/

    it 'should set default values if not provided', ->
      upload = new Upload 'myBucket'

      assert upload.s3 instanceof require('aws-sdk').S3
      assert upload.versions instanceof Array
      assert.equal upload.awsBucketPath, ''
      assert.equal upload.awsBucketUrl, 'https://s3-us-east-1.amazonaws.com/myBucket'
      assert.equal upload.awsBucketAcl, 'privat'<commit_msg>Increase S3 cleanup timeout<commit_after>assert = require 'assert'
Upload = require '../src/index.coffee'

fs = require('fs')
gm = require('gm').subClass imageMagick: true

hash = require('crypto').createHash
rand = require('crypto').pseudoRandomBytes

upload = listObjects = putObject = null
cleanup = []

beforeEach ->
  upload = new Upload process.env.AWS_BUCKET_NAME,
    awsBucketPath: 'images_test/'
    awsBucketRegion: 'eu-west-1'
    awsBucketAcl: 'public-read'
    versions: [{
      original: true
      awsImageAcl: 'private'
    },{
      maxHeight: 1040
      maxWidth: 1040
      suffix: '-large'
      quality: 80
    },{
      maxHeight: 780
      maxWidth: 780
      suffix: '-medium'
    },{
      maxHeight: 320
      maxWidth: 320
      suffix: '-small'
    }]

  # Mock S3 API calls
  if process.env.INTEGRATION_TEST isnt 'true'
    listObjects = upload.s3.listObjects
    upload.s3.listObjects = (path, cb) ->
      process.nextTick -> cb null, Contents: []

    putObject = upload.s3.putObject
    upload.s3.putObject = (opts, cb) ->
      process.nextTick -> cb null, ETag: '"' + hash('md5').update(rand(32)).digest('hex') + '"'

# Clean up S3 objects
if process.env.INTEGRATION_TEST is 'true'
  afterEach (done) ->
    @timeout 40000

    return process.nextTick done if cleanup.length is 0

    upload.s3.deleteObjects Delete: Objects: cleanup, (err) ->
      throw err if err
      cleanup = []
      done()

describe 'Upload', ->
  describe 'constructor', ->
    it 'should throw error for missing awsBucketName param', ->
      assert.throws ->
        new Upload()
      , /Bucket name can not be undefined/

    it 'should set default values if not provided', ->
      upload = new Upload 'myBucket'

      assert upload.s3 instanceof require('aws-sdk').S3
      assert upload.versions instanceof Array
      assert.equal upload.awsBucketPath, ''
      assert.equal upload.awsBucketUrl, 'https://s3-us-east-1.amazonaws.com/myBucket'
      assert.equal upload.awsBucketAcl, 'privat'
{'fraction_non_alphanumeric': 0.125, 'fraction_numerical': 0.020541082164328657, 'mean_word_length': 2.8137535816618913, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 2, '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': '1309251', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1231, 'n_words': 326}