<commit_before>"use strict";
import { expect } from "chai";
import connect from "mq-connection";
import factory from "../src/main";

const URL = "amqp://rabbitmq";
const delay = (msecs, func) => setTimeout(func, msecs);
let pubsub, fakeConnection;


describe("Event (manager)", function() {
  beforeEach(() => {
    connect.reset();
    fakeConnection = connect.fake();
    pubsub = factory(URL);
  });
  afterEach(() => connect.real());


  it("has the event name", () => {
    const event = pubsub.event("myEvent");
    expect(event.name).to.equal("myEvent");
  });


  it("throws if an event name was not specified", () => {
    expect(() => pubsub.event()).to.throw();
    expect(() => pubsub.event("")).to.throw();
  });


  it("is not ready", () => {
    const event = pubsub.event("myEvent");
    expect(event.isReady).to.equal(false);
  });


  it("isReady when connection completes", (done) => {
    const event = pubsub.event("myEvent");
    expect(event.isReady).to.equal(false);
    event.ready().then(result => {
        expect(result.isReady).to.equal(true);
        done();
    });
  });


  describe("connection failures", function() {
    const CONNECTION_ERROR = new Error("Connection Fail!");
    beforeEach(() => {
      // Override the connect method to force failure.
      connect.reset();
      connect.connect = () => new Promise((resolve, reject) => reject(CONNECTION_ERROR));
    });


    it("reports connection error (isReady: false)", (done) => {
      const event = factory(URL).event("myEvent");
      delay(10, () => {
          expect(event.isReady).to.equal(false);
          expect(event.connectionError).to.equal(CONNECTION_ERROR);
          done();
      });
    });


    it.skip("fails on subscription if there is a connection error", () => {});


    it("fails on publish if there is a connection error", (done) => {
      const event = factory(URL).event("myEvent");
      delay(10, () => {
          event.publish({ foo:123 })
            .catch(err => {
                expect(err).to.equal(CONNECTION_ERROR);
                done();
            });
      });
    });
  });


  describe("publish", function() {
    it("publishes an event", (done) => {
      const event = pubsub.event("myEvent");
      let channel;
      event.ready()
        .then(() => {
            channel = fakeConnection.test.channels[0];
            return event.publish({ foo: 123 });
        })
        .then(() => {
            const args = channel.test.publish[0];
            const payload = JSON.parse(args.content.toString());
            expect(args.exchange).to.equal("pub-sub:myEvent");
            expect(args.routingKey).to.equal("");
            expect(payload.event).to.equal("myEvent");
            expect(payload.data).to.eql({ foo: 123 });
            done();
        });
    });

    it.skip("publishes no data (undefined)", () => {});
  });
});<commit_msg>Publish undefined.<commit_after>"use strict";
import { expect } from "chai";
import connect from "mq-connection";
import factory from "../src/main";

const URL = "amqp://rabbitmq";
const delay = (msecs, func) => setTimeout(func, msecs);
let pubsub, fakeConnection;


describe("Event (manager)", function() {
  beforeEach(() => {
    connect.reset();
    fakeConnection = connect.fake();
    pubsub = factory(URL);
  });
  afterEach(() => connect.real());


  it("has the event name", () => {
    const event = pubsub.event("myEvent");
    expect(event.name).to.equal("myEvent");
  });


  it("throws if an event name was not specified", () => {
    expect(() => pubsub.event()).to.throw();
    expect(() => pubsub.event("")).to.throw();
  });


  it("is not ready", () => {
    const event = pubsub.event("myEvent");
    expect(event.isReady).to.equal(false);
  });


  it("isReady when connection completes", (done) => {
    const event = pubsub.event("myEvent");
    expect(event.isReady).to.equal(false);
    event.ready().then(result => {
        expect(result.isReady).to.equal(true);
        done();
    });
  });


  describe("connection failures", function() {
    const CONNECTION_ERROR = new Error("Connection Fail!");
    beforeEach(() => {
      // Override the connect method to force failure.
      connect.reset();
      connect.connect = () => new Promise((resolve, reject) => reject(CONNECTION_ERROR));
    });


    it("reports connection error (isReady: false)", (done) => {
      const event = factory(URL).event("myEvent");
      delay(10, () => {
          expect(event.isReady).to.equal(false);
          expect(event.connectionError).to.equal(CONNECTION_ERROR);
          done();
      });
    });


    it.skip("fails on subscription if there is a connection error", () => {});


    it("fails on publish if there is a connection error", (done) => {
      const event = factory(URL).event("myEvent");
      delay(10, () => {
          event.publish({ foo:123 })
            .catch(err => {
                expect(err).to.equal(CONNECTION_ERROR);
                done();
            });
      });
    });
  });


  describe("publish", function() {
    it("publishes an event", (done) => {
      const event = pubsub.event("myEvent");
      let channel;
      event.ready()
        .then(() => {
            channel = fakeConnection.test.channels[0];
            return event.publish({ foo: 123 });
        })
        .then(() => {
            const args = channel.test.publish[0];
            const payload = JSON.parse(args.content.toString());
            expect(args.exchange).to.equal("pub-sub:myEvent");
            expect(args.routingKey).to.equal("");
            expect(payload.event).to.equal("myEvent");
            expect(payload.data).to.eql({ foo: 123 });
            done();
        });
    });

    it("publishes no data (undefined)", (done) => {
      const event = pubsub.event("myEvent");
      let channel;
      event.ready()
        .then(() => {
            channel = fakeConnection.test.channels[0];
            return event.publish();
        })
        .then(() => {
            const args = channel.test.publish[0];
            const payload = JSON.parse(args.content.toString());
            expect(payload.data).to.eql(undefined);
            done();
        });
    });
  });
});
{'fraction_non_alphanumeric': 0.18503051718599423, 'fraction_numerical': 0.00513973658849984, 'mean_word_length': 2.455604883462819, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 51, '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': '7514190', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1785, 'n_words': 374}