define([
    'bitbucket/internal/widget/client-file-handlers/client-file-handler'
], function(
    ClientFileHandler
) {

    'use strict';

    module('image-tools-client-file-handler', {
        setup: function() {
            this.lameFile = file(1023, "application/lame");
            this.awesomeFile = file(1, "application/awesome");
        },
        teardown: function () {
        }
    });

    function file(size, type) {
        return {
            size : size,
            type : type
        };
    }

    test("ClientFileHandler returns files if they're all ok", function() {
        var handler = new ClientFileHandler({
            fileTypeFilter: ClientFileHandler.typeFilters.application,
            fileCountLimit: 3,
            fileSizeLimit: 1024 //1kb
        });

        var files = handler.filterFiles([this.lameFile, this.awesomeFile]);
        ok(files, "Files should be returned.");
        equal(files.valid.length, 2, "Should return 2 files");
        equal(files.valid[0], this.lameFile, "1st file should be lameFile");
        equal(files.valid[1], this.awesomeFile, "2nd file should be awesomeFile");
    });

    test("ClientFileHandler returns empty array if they're all bad", function() {
        var handler = new ClientFileHandler({
            fileTypeFilter: ClientFileHandler.typeFilters.image,
            fileCountLimit: 3,
            fileSizeLimit: 1024 //1kb
        });

        var files = handler.filterFiles([this.lameFile, this.awesomeFile]);
        ok(files, "Files should be returned.");
        equal(files.valid.length, 0, "Should be empty");
    });

    test("ClientFileHandler handles lack of filters as defaults", function() {
        var handler = new ClientFileHandler({
            fileTypeFilter: null,
            fileCountLimit: null,
            fileSizeLimit: null
        });

        var files = handler.filterFiles([this.lameFile, this.awesomeFile]);
        ok(files, "Files should be returned.");
        equal(files.valid.length, 2, "Should return 2 files");
        equal(files.valid[0], this.lameFile, "1st file should be lameFile");
        equal(files.valid[1], this.awesomeFile, "2nd file should be awesomeFile");
    });

    test("ClientFileHandler filters by type regex", function() {
        var handler = new ClientFileHandler({
            fileTypeFilter: /application\/(?!lame)/,
            fileCountLimit: null,
            fileSizeLimit: null
        });

        var files = handler.filterFiles([this.lameFile, this.awesomeFile]);
        ok(files, "Files should be returned.");
        equal(files.valid.length, 1, "Should return 1 file");
        equal(files.valid[0], this.awesomeFile, "1st file should be awesomeFile");
    });

    test("ClientFileHandler filters by size", function() {
        var handler = new ClientFileHandler({
            fileTypeFilter: null,
            fileCountLimit: null,
            fileSizeLimit: 20
        });

        var files = handler.filterFiles([this.lameFile, this.awesomeFile]);
        ok(files, "Files should be returned.");
        equal(files.valid.length, 1, "Should return 1 file");
        equal(files.valid[0], this.awesomeFile, "1st file should be awesomeFile");
    });

    test("ClientFileHandler filters by count", function() {
        var handler = new ClientFileHandler({
            fileTypeFilter: null,
            fileCountLimit: 1,
            fileSizeLimit: null
        });

        var files = handler.filterFiles([this.lameFile, this.awesomeFile]);
        ok(files, "Files should be returned.");
        equal(files.valid.length, 1, "Should return 1 file");
        equal(files.valid[0], this.lameFile, "1st file should be lameFile");
    });
});