import { tokenizeString, getTokensComparableValue, diff } from 'bitbucket/internal/bbui/ediff/ediff';

QUnit.module('bitbucket-ediff');

QUnit.test('sanity checks for tokenizeString', function(assert) {
    assert.deepEqual(tokenizeString('function'), [
        { value: 'function', start: 0, end: 8 },
    ]);
    assert.deepEqual(tokenizeString('function createNode(x, y, z)'), [
        { value: 'function', start: 0, end: 8 },
        { value: ' ', start: 8, end: 9 },
        { value: 'createNode', start: 9, end: 19 },
        { value: '(', start: 19, end: 20 },
        { value: 'x', start: 20, end: 21 },
        { value: ',', start: 21, end: 22 },
        { value: ' ', start: 22, end: 23 },
        { value: 'y', start: 23, end: 24 },
        { value: ',', start: 24, end: 25 },
        { value: ' ', start: 25, end: 26 },
        { value: 'z', start: 26, end: 27 },
        { value: ')', start: 27, end: 28 },
    ]);
});

QUnit.test('sanity check getTokensComparableValue', function(assert) {
    assert.strictEqual(getTokensComparableValue({ value: 'function' }), 'function');
    assert.strictEqual(getTokensComparableValue({ start: 1, value: '  ' }), '  ');
    assert.strictEqual(getTokensComparableValue({ start: 1, value: '  ' }), '  ');
    assert.strictEqual(getTokensComparableValue({ start: 1, value: ' \n ' }), ' \n');
    assert.strictEqual(getTokensComparableValue({ start: 0, value: '    ' }), '\n');
});

QUnit.test('sanity checks for diff algorithm', function(assert) {
    // hello world -> hello [cruel ]world
    assert.deepEqual(diff(tokenizeString('hello world'), tokenizeString('hello cruel world')), {
        originalRegions: [],
        revisedRegions: [
            { start: 6, end: 12, type: 'add' },
        ],
    }, 'validating add region');

    // hello [world] -> hello [jason]
    assert.deepEqual(diff(tokenizeString('hello world'), tokenizeString('hello jason')), {
        originalRegions: [
            { start: 6, end: 11, type: 'change' },
        ],
        revisedRegions: [
            { start: 6, end: 11, type: 'change' },
        ],
    }, 'validate change region');

    // hello [big ]world -> hello world
    assert.deepEqual(diff(tokenizeString('hello big world'), tokenizeString('hello world')), {
        originalRegions: [
            { start: 6, end: 10, type: 'delete' },
        ],
        revisedRegions: [],
    }, 'validate delete region');

    // create([foo]).[bar().]toString() -> [factory.]create([val]).toString()
    assert.deepEqual(diff(tokenizeString('create(foo).bar().toString()'), tokenizeString('factory.create(val).toString()')), {
        originalRegions: [
            { start: 7, end: 10, type: 'change' },
            { start: 12, end: 18, type: 'delete' },
        ],
        revisedRegions: [
            { start: 0, end: 8, type: 'add' },
            { start: 15, end: 18, type: 'change' },
        ],
    }, 'validate complex change');

    // foo(a,b,c) -> foo(a,[ ]b,[ ]c)
    assert.deepEqual(diff(tokenizeString('\tfoo(a,b,c);\n\treturn;'), tokenizeString('    foo(a, b, c);\n    return;')), {
        originalRegions: [
        ],
        revisedRegions: [
            { start: 10, end: 11, type: 'add' },
            { start: 13, end: 14, type: 'add' },
        ],
    }, 'validate whitespace');

    assert.deepEqual(diff(tokenizeString('    public'), tokenizeString('    public')), {
        originalRegions: [
        ],
        revisedRegions: [],
    }, 'validate matching whitespace');
});
