import validator from 'bitbucket/internal/bbui/json-validation/json-validation';
import models from 'bitbucket/internal/bbui/models/models';

import mocks from 'mocks/models';

QUnit.module('bitbucket-models - mocks');

function validates(assert, model, mock, message) {
    try {
        validator(model)(mock);
        assert.ok(true, message);
    } catch (e) {
        assert.ok(false, message + ': ' + e.message);
    }
}

function testMock(model, idProp, validID) {
    return function(assert) {
        const mock1 = mocks[model](null, '1');
        const mock1again = mocks[model](null, '1');
        const mock2 = mocks[model](null, '2');

        const overrides = {};
        overrides[idProp] = validID;
        const mock3 = mocks[model](overrides, '3');

        assert.equal(mock1again[idProp], mock1[idProp], 'Same seed = same ID');
        assert.notEqual(mock1[idProp], mock2[idProp], 'Different seed = different ID');
        assert.strictEqual(mock3[idProp], validID, 'Overrides respected');
        validates(assert, models[model], mock1, 'mock1 validates');
        validates(assert, models[model], mock2, 'mock2 validates');
        validates(assert, models[model], mock3, 'mock3 validates');
    };
}

QUnit.test('user', testMock('user', 'name', 'dude'));
QUnit.test('pullRequest', testMock('pullRequest', 'id', 1));
QUnit.test('ref', testMock('ref', 'id', 'c0ffee'));
QUnit.test('repository', testMock('repository', 'slug', 'REPO'));
QUnit.test('project', testMock('project', 'key', 'PROJ'));
