package patterns

import (
	"context"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
)

var _ = Describe("Runnable", func() {
	var (
		runnable Runnable
	)

	BeforeEach(func() {
		runnable = NewRunnable()
	})

	Describe("Start", func() {
		It("should start if not started", func() {
			err := runnable.Start()
			Expect(err).Should(BeNil())
			Expect(runnable.Context()).ShouldNot(BeNil())
		})

		It("should return error if already started", func() {
			_ = runnable.Start()
			err := runnable.Start()
			Expect(err).Should(Equal(ErrRunnableAlreadyStarted))
		})
	})

	Describe("StartWithContext", func() {
		It("should start if not started", func() {
			err := runnable.StartWithContext(context.TODO())
			Expect(err).Should(BeNil())
			Expect(runnable.Context()).ShouldNot(BeNil())
		})

		It("should return error if already started", func() {
			_ = runnable.Start()
			err := runnable.StartWithContext(context.TODO())
			Expect(err).Should(Equal(ErrRunnableAlreadyStarted))
		})
	})

	Describe("Stop", func() {
		It("should stop if already started", func() {
			_ = runnable.Start()
			err := runnable.Stop()
			Expect(err).Should(BeNil())
			Expect(runnable.Context()).Should(BeNil())
		})

		It("should return error if not started", func() {
			err := runnable.Stop()
			Expect(err).Should(Equal(ErrRunnableNotStarted))
			Expect(runnable.Context()).Should(BeNil())
		})
	})
})
