import { IInjector, IConfig, Json, JsonObject, Config } from "merapi"; import { ISvxNats } from "../src/interface"; import SvxNats from "../src/svx_nats"; import { MockService } from "./test_helpers"; import { suite, test } from "mocha-typescript"; import { expect } from "chai"; import { equal, notEqual } from "assert"; import { stub, spy, assert, SinonStub } from "sinon"; import { IService, ServiceInfo } from "merapi-plugin-svx-rc"; import * as NATS from "nats"; const MockNats = require("mock-nats-client"); @suite("SvxNats") class SvxNatsTest { private svxNats : ISvxNats; private mockService : any; private mockConnection : NATS.Client; private stubNats : SinonStub; async before() { let config = Config.create({ nats: { namespace: "platform", showLog: false } }); this.mockService = new MockService(); this.mockConnection = MockNats.connect({}); this.stubNats = stub(NATS, "connect").returns(this.mockConnection); this.svxNats = new SvxNats(config, console, this.mockService); await this.svxNats.initialize(); } after() { this.stubNats.restore(); } @test "should connect to nats on initialize"() { expect(this.stubNats.callCount).to.eq(1); expect(this.svxNats.getConnection()).to.not.eq(null); } @test "should return name"() { expect(this.svxNats.name()).to.eq("nats"); } @test "should return priority"() { expect(this.svxNats.priority()).to.eq(1); } @test "should return info"() { expect(this.svxNats.info()).to.not.eq(null); expect(this.svxNats.info()).to.deep.eq( { version: "1", transport: { api: true, command: true, topic: true, queue: true, discovery: true, log: true } } ); } @test "hasTransport should return true if available"() { expect(this.svxNats.hasTransport("api")).to.eq(true); } @test "hasTransport should return true if not available"() { expect(this.svxNats.hasTransport(null)).to.eq(false); } @test "constructSubject should construct subject key"() { let resA = this.svxNats.constructSubject("services", "diaenne", "topics", "outgoingMessages", 1); expect(resA).to.eq("platform.services.diaenne.topics.v1.outgoingMessages"); let resB = this.svxNats.constructSubject("services", "diaenne", "commands", "blastMessages"); expect(resB).to.eq("platform.services.diaenne.commands.blastMessages"); } @test async "publishTopic should publish to 2 subject"() { let stubConnection = stub(this.mockConnection, "publish"); await this.svxNats.publishTopic("diaenne", "outgoingMessages", 1, {"message": "test"}, {}); expect(stubConnection.callCount).to.equal(2); stubConnection.restore(); } @test async "subscribeTopic should call nats subscribe"() { let stubConnection = stub(this.mockConnection, "subscribe"); let stubService = stub(this.mockService, "createSource").returns({serviceName : 123}) this.svxNats.subscribeTopic("diaenne", "outgoingMessages", 1, async () => { return }); expect(stubConnection.callCount).to.equal(1); stubConnection.restore(); stubService.restore(); } @test async "pushQueue should call nats publish"() { let stubConnection = stub(this.mockConnection, "publish"); await this.svxNats.pushQueue("diaenne", "converse", 1, {"message": "test"},{}); expect(stubConnection.callCount).to.equal(1); stubConnection.restore(); } @test "consumeQueue should call nats subscribe"() { let stubConnection = stub(this.mockConnection, "subscribe"); let stubService = stub(this.mockService, "createSource").returns({instanceId : 123}) this.svxNats.consumeQueue("diaenne", "converse", 1, async () => { return }); expect(stubConnection.callCount).to.equal(2); stubConnection.restore(); stubService.restore() } @test.skip async "callApi should call nats requestOne"() { // TODO // requestOne not supported in mock nats lib } @test "callApi should call nats subscribe"() { let stubConnection = stub(this.mockConnection, "subscribe"); let stubService = stub(this.mockService, "createSource").returns({instanceId : 123}) this.svxNats.createApi("diaenne", "blastMessage", 1, async () => { return {} }); expect(stubConnection.callCount).to.equal(2); stubConnection.restore(); stubService.restore() } @test.skip "execCommand should call requestOne"() { // TODO } @test "createCommand should call nats subscribe"() { let stubConnection = stub(this.mockConnection, "subscribe"); let stubService = stub(this.mockService, "createSource").returns({instanceId : 123}) this.svxNats.createCommand("diaenne", "blastMessage", async () => { return {} }); expect(stubConnection.callCount).to.equal(2); stubConnection.restore(); stubService.restore() } @test "broadcast should call nats publish"() { let stubConnection = stub(this.mockConnection, "publish"); this.svxNats.broadcast({ name: 'diaenne', id: '7612', version: "1.2.3"} as ServiceInfo); expect(stubConnection.callCount).to.equal(1); stubConnection.restore(); } @test "discover should call nats publish"() { let stubConnection = stub(this.mockConnection, "publish"); this.svxNats.discover(); expect(stubConnection.callCount).to.equal(1); stubConnection.restore(); } @test "log should call nats publish"() { let stubConnection = stub(this.mockConnection, "publish"); let stubService = stub(this.mockService, "createSource").returns({instanceId : 123}) this.svxNats.log("diaenne", "botManager", "converse", "info", "success"); expect(stubConnection.callCount).to.equal(2); stubConnection.restore(); stubService.restore(); } }