"github.com/cloudfoundry/firehose-plugin/testhelpers"

	"github.com/cloudfoundry/sonde-go/events"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

const (
	ACCESS_TOKEN = "access_token"
)

var _ = Describe("NozzlePlugin", func() {
	Describe(".Run", func() {
		var fakeCliConnection *pluginfakes.FakeCliConnection
		var nozzlerCmd *NozzlerCmd
		var fakeFirehose *testhelpers.FakeFirehose

		BeforeEach(func() {
			fakeFirehose = testhelpers.NewFakeFirehose(ACCESS_TOKEN)
			fakeFirehose.SendEvent(events.Envelope_LogMessage, "Log Message")
			fakeFirehose.Start()

			fakeCliConnection = &pluginfakes.FakeCliConnection{}
			fakeCliConnection.AccessTokenReturns(ACCESS_TOKEN, nil)
			fakeCliConnection.DopplerEndpointReturns(fakeFirehose.URL(), nil)
			nozzlerCmd = &NozzlerCmd{}
		})

		AfterEach(func() {
			fakeFirehose.Close()
		})
	Describe("Start", func() {
		var options *firehose.ClientOptions
		Context("for app connections", func() {
			BeforeEach(func() {
				options = &firehose.ClientOptions{AppGUID: "spring-music", Debug: false, NoFilter: true}
			})
			Context("when the connection to doppler cannot be established", func() {
				It("shows a meaningful error", func() {
					client := firehose.NewClient("invalidToken", "badEndpoint", options, ui)
					client.Start()
					Expect(stdout).To(ContainSubstring("Error dialing trafficcontroller server"))
				})
			})
			Context("when the connection to doppler works", func() {
				var fakeFirehose *testhelpers.FakeFirehose
				BeforeEach(func() {
					fakeFirehose = testhelpers.NewFakeFirehoseInAppMode("ACCESS_TOKEN", "spring-music")
					fakeFirehose.SendEvent(events.Envelope_LogMessage, "This is a very special test message")
					fakeFirehose.SendEvent(events.Envelope_ValueMetric, "valuemetric")
					fakeFirehose.SendEvent(events.Envelope_CounterEvent, "counterevent")
					fakeFirehose.SendEvent(events.Envelope_ContainerMetric, "containermetric")
					fakeFirehose.SendEvent(events.Envelope_Error, "this is an error")
					fakeFirehose.SendEvent(events.Envelope_HttpStart, "start request")
					fakeFirehose.SendEvent(events.Envelope_HttpStop, "stop request")
					fakeFirehose.SendEvent(events.Envelope_HttpStartStop, "startstop request")
					fakeFirehose.Start()
				})
				It("prints out debug information if demanded", func() {
					options.Debug = true
					client := firehose.NewClient("ACCESS_TOKEN", fakeFirehose.URL(), options, ui)