<commit_before>
require('reflect-metadata');

var ms = require('../../../target/js/model.service'),
    when = require('saywhen');

describe('Model service', function() {
    var diffusionService;
    var onConnectSuccess;
    var onConnectFailure;
    var session;
    var onMessage;
    var onSendSuccess;
    var onSendFailure;
    var modelService;


    beforeEach(function() {
        diffusionService = jasmine.createSpyObj('diffusionService', ['get']);
        onConnectSuccess = when.captor();
        onConnectFailure = when.captor();

        var sessionPromise = jasmine.createSpyObj('promise', ['then']);
        when(sessionPromise.then).isCalledWith(onConnectSuccess).thenReturn(sessionPromise);
        when(sessionPromise.then).isCalledWith(onConnectSuccess, onConnectFailure).thenReturn(sessionPromise);
        when(diffusionService.get).isCalled.thenReturn(sessionPromise);

        session = {
            messages : jasmine.createSpyObj('messages', ['listen', 'send'])
        };
        onMessage = when.captor();
        onSendSuccess = when.captor();
        onSendFailure = when.captor();

        var sendPromise = jasmine.createSpyObj('promise', ['then']);
        when(sendPromise.then).isCalledWith(onSendSuccess).thenReturn(sendPromise);
        when(sendPromise.then).isCalledWith(onSendSuccess, onSendFailure).thenReturn(sendPromise);
        when(session.messages.send).isCalled.thenReturn(sendPromise);
        when(session.messages.listen).isCalledWith('adapter/rest/model/store', onMessage);

        modelService = new ms.ModelService(diffusionService);
    });

    it('can be created', function() {
        expect(modelService).toBeDefined();
    });

    it('can lookup model', function(done) {
        var modelPromise = modelService.getModel();
        expect(diffusionService.get).toHaveBeenCalled();
        expect(modelPromise).toBeDefined();

        onConnectSuccess.values()[0](session); // init
        onConnectSuccess.values()[1](); // make request

        expect(session.messages.send).toHaveBeenCalledWith('adapter/rest/model/store', jasmine.anything());
        expect(onSendSuccess.latest).toBeDefined();
        expect(onSendFailure.latest).toBeDefined();

        onConnectSuccess.latest([ 'service0' ]);

        modelPromise.then(function(result) {
            done();
        }, done.fail);

        onConnectSuccess.latest({});

    });
});<commit_msg>Improve test by using more of the actual code.<commit_after>
require('reflect-metadata');

var ms = require('../../../target/js/model.service'),
    when = require('saywhen'),
    diffusion = require('diffusion');

var jsonDataType = diffusion.datatypes.json();

describe('Model service', function() {
    var diffusionService;
    var session;
    var onMessage;
    var onSendSuccess;
    var onSendFailure;
    var modelService;

    beforeEach(function() {
        diffusionService = jasmine.createSpyObj('diffusionService', ['get']);
        onConnectSuccess = when.captor();
        onConnectFailure = when.captor();

        session = {
            messages : jasmine.createSpyObj('messages', ['listen', 'send'])
        };
        onMessage = when.captor();
        onSendSuccess = when.captor();
        onSendFailure = when.captor();

        when(session.messages.send).isCalled.then(function() {
            onMessage.latest({
                content: jsonDataType.writeValue({
                    id: 0,
                    response: [{
                        name: 'service0'
                    }]
                })
            });
        });
        when(session.messages.listen).isCalledWith('adapter/rest/model/store', onMessage);

        when(diffusionService.get).isCalled.thenReturn(Promise.resolve(session));

        modelService = new ms.ModelService(diffusionService);
    });

    it('can be created', function() {
        expect(modelService).toBeDefined();
    });

    it('can lookup model', function(done) {
        var modelPromise = modelService.getModel();
        expect(diffusionService.get).toHaveBeenCalled();
        expect(modelPromise).toBeDefined();

        modelPromise.then(function(model) {
            expect(session.messages.send).toHaveBeenCalledWith('adapter/rest/model/store', jasmine.anything());
            expect(model.services[0].name).toBe('service0');
            done();
        }, done.fail);
    });
});
{'fraction_non_alphanumeric': 0.13966225467822913, 'fraction_numerical': 0.001597444089456869, 'mean_word_length': 2.8787610619469026, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5937108', 'n_tokens_mistral': 1210, 'n_tokens_neox': 1176, 'n_words': 184}