<commit_before>'use strict';

var Promise = require("bluebird");
var WebRTCComms = require("../lib/WebRTCComms");
var ClientMocks = require("./ClientMocks");

describe("The WebRTC Comms layer", function () {

    var WAIT_FOR_CHANNEL_TO_OPEN_RESULT = "WAIT_FOR_CHANNEL_TO_OPEN_RESULT",
        SEND_SHUFFLE_REQUEST_RESULT = "SEND_SHUFFLE_REQUEST_RESULT",
        PROCESS_SHUFFLE_RESPONSE_RESULT = "PROCESS_SHUFFLE_RESULT_RESULT",
        SEND_RESPONSE_ACKNOWLEDGEMENT_RESULT = "SEND_RESPONSE_ACKNOWLEDGEMENT_RESULT",
        PROCESS_SHUFFLE_REQUEST_RESULT = "PROCESS_SHUFFLE_REQUEST_RESULT",
        WAIT_FOR_RESPONSE_ACKNOWLEDGEMENT_RESULT = "WAIT_FOR_RESPONSE_ACKNOWLEDGEMENT_RESULT";

    var comms,
        channel,
        rtc,
        shuffleStateFactory,
        outgoingShuffleState,
        localCyclonNode,
        destinationNodePointer,
        shuffleSet,
        incomingShuffleState,
        loggingService,
        successCallback,
        failureCallback,
        metadataProviders;

    beforeEach(function () {
        successCallback = ClientMocks.createSuccessCallback();
        failureCallback = ClientMocks.createFailureCallback();

        // Create mocks
        rtc = ClientMocks.mockRtc();
        channel = ClientMocks.mockChannel();
        shuffleStateFactory = ClientMocks.mockShuffleStateFactory();
        localCyclonNode = ClientMocks.mockCyclonNode();
        outgoingShuffleState = createSucceedingOutgoingShuffleState();
        incomingShuffleState = createSucceedingIncomingShuffleState();
        loggingService = ClientMocks.mockLoggingService();
        metadataProviders = {something : function() {return ""}};

        destinationNodePointer = createCacheEntry("destinationNodePointer", 12);
        shuffleSet = [createCacheEntry("a", 456), createCacheEntry("b", 123), createCacheEntry("c", 222)];

        //
        // Mock behaviour
        //
        rtc.openChannel.andReturn(Promise.resolve(WAIT_FOR_CHANNEL_TO_OPEN_RESULT));
        channel.getRemotePeer.andReturn(destinationNodePointer);
        shuffleStateFactory.createOutgoingShuffleState.andReturn(outgoingShuffleState);
        shuffleStateFactory.createIncomingShuffleState.andReturn(incomingShuffleState);

        comms = new WebRTCComms(rtc, shuffleStateFactory, loggingService);
    });

    describe("when initializing", function () {

        beforeEach(function() {
            comms.initialize(localCyclonNode, metadataProviders);
        });

        it("should initialize the RTC layer", function () {
            expect(rtc.connect).toHaveBeenCalledWith(metadataProviders);
        });

        it("should add a listener for incoming shuffle channels", function() {
            expect(rtc.onChannel).toHaveBeenCalledWith("cyclonShuffle", comms.handleIncomingShuffle);
        });
    });

    describe("before sending a shuffle request", function () {

        beforeEach(function () {
            comms.sendShuffleRequest(localCyclonNode, destinationNodePointer, shuffleSet);
        });

        it("should create a new outgoing shuffle state", function () {
            expect(shuffleStateFactory.createOutgoingShuffleState).toHaveBeenCalledWith(localCyclonNode, destinationNodePointer, shuffleSet);
        });
    });

    describe("when sending a shuffle request", function () {<commit_msg>Fix test<commit_after>'use strict';

var Promise = require("bluebird");
var WebRTCComms = require("../lib/WebRTCComms");
var ClientMocks = require("./ClientMocks");

describe("The WebRTC Comms layer", function () {

    var WAIT_FOR_CHANNEL_TO_OPEN_RESULT = "WAIT_FOR_CHANNEL_TO_OPEN_RESULT",
        SEND_SHUFFLE_REQUEST_RESULT = "SEND_SHUFFLE_REQUEST_RESULT",
        PROCESS_SHUFFLE_RESPONSE_RESULT = "PROCESS_SHUFFLE_RESULT_RESULT",
        SEND_RESPONSE_ACKNOWLEDGEMENT_RESULT = "SEND_RESPONSE_ACKNOWLEDGEMENT_RESULT",
        PROCESS_SHUFFLE_REQUEST_RESULT = "PROCESS_SHUFFLE_REQUEST_RESULT",
        WAIT_FOR_RESPONSE_ACKNOWLEDGEMENT_RESULT = "WAIT_FOR_RESPONSE_ACKNOWLEDGEMENT_RESULT";

    var comms,
        channel,
        rtc,
        shuffleStateFactory,
        outgoingShuffleState,
        localCyclonNode,
        destinationNodePointer,
        shuffleSet,
        incomingShuffleState,
        loggingService,
        successCallback,
        failureCallback,
        metadataProviders;

    beforeEach(function () {
        successCallback = ClientMocks.createSuccessCallback();
        failureCallback = ClientMocks.createFailureCallback();

        // Create mocks
        rtc = ClientMocks.mockRtc();
        channel = ClientMocks.mockChannel();
        shuffleStateFactory = ClientMocks.mockShuffleStateFactory();
        localCyclonNode = ClientMocks.mockCyclonNode();
        outgoingShuffleState = createSucceedingOutgoingShuffleState();
        incomingShuffleState = createSucceedingIncomingShuffleState();
        loggingService = ClientMocks.mockLoggingService();
        metadataProviders = {something : function() {return ""}};

        destinationNodePointer = createCacheEntry("destinationNodePointer", 12);
        shuffleSet = [createCacheEntry("a", 456), createCacheEntry("b", 123), createCacheEntry("c", 222)];

        //
        // Mock behaviour
        //
        rtc.openChannel.andReturn(Promise.resolve(WAIT_FOR_CHANNEL_TO_OPEN_RESULT));
        channel.getRemotePeer.andReturn(destinationNodePointer);
        shuffleStateFactory.createOutgoingShuffleState.andReturn(outgoingShuffleState);
        shuffleStateFactory.createIncomingShuffleState.andReturn(incomingShuffleState);

        comms = new WebRTCComms(rtc, shuffleStateFactory, loggingService);
    });

    describe("when initializing", function () {

        beforeEach(function() {
            comms.initialize(localCyclonNode, metadataProviders);
        });

        it("should initialize the RTC layer", function () {
            expect(rtc.connect).toHaveBeenCalledWith(metadataProviders, ["CyclonWebRTC"]);
        });

        it("should add a listener for incoming shuffle channels", function() {
            expect(rtc.onChannel).toHaveBeenCalledWith("cyclonShuffle", comms.handleIncomingShuffle);
        });
    });

    describe("before sending a shuffle request", function () {

        beforeEach(function () {
            comms.sendShuffleRequest(localCyclonNode, destinationNodePointer, shuffleSet);
        });

        it("should create a new outgoing shuffle state", function () {
            expect(shuffleStateFactory.createOutgoingShuffleState).toHaveBeenCalledWith(localCyclonNode, destinationNodePointer, shuffleSet);
        });
    });

    describe("when sending a shuffle request", function () {
{'fraction_non_alphanumeric': 0.09367012479326417, 'fraction_numerical': 0.003307773267177868, 'mean_word_length': 3.5530458590006844, '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': '1272683', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1802, 'n_words': 282}