package combluotion import ( "fmt" "testing" vocab "github.com/go-ap/activitypub" "github.com/stretchr/testify/assert" "go.uber.org/mock/gomock" "codeberg.org/danjones000/combluotion/config" confMock "codeberg.org/danjones000/combluotion/internal/testmocks/config" storeMock "codeberg.org/danjones000/combluotion/internal/testmocks/store" ) type appTest struct { ctrl *gomock.Controller store *storeMock.MockStore conf *confMock.MockConfig } func setupAppTest(t *testing.T) appTest { t.Helper() ctrl := gomock.NewController(t) return appTest{ctrl, storeMock.NewMockStore(ctrl), confMock.NewMockConfig(ctrl)} } func TestEmptyBaseURL(t *testing.T) { th := setupAppTest(t) th.conf. EXPECT(). BaseURL(). Return("") a, er := NewApp("0.0.0", th.conf, th.store) assert.Nil(t, a) assert.EqualError(t, er, "missing BaseURL") } func TestGivenEnvironment(t *testing.T) { cases := [...]struct { given config.Env exp config.Env }{ {config.Dev, config.Dev}, {config.Prod, config.Prod}, {config.Qa, config.Qa}, } for _, c := range cases { t.Run(string(c.given), func(t *testing.T) { th := setupAppTest(t) th.conf.EXPECT().BaseURL().Return("http://localhost:1234/").Times(2) th.conf.EXPECT().Name().Return("") th.conf. EXPECT(). Env(). Return(c.given) a, er := NewApp("0.0.0", th.conf, th.store) assert.NoError(t, er) if assert.NotNil(t, a) { assert.Equal(t, th.conf, a.Config()) assert.Equal(t, c.exp, a.Environment()) } }) } } func TestService(t *testing.T) { th := setupAppTest(t) base := "http://localhost:1234/" th.conf.EXPECT().BaseURL().Return(base).Times(2) th.conf.EXPECT().Name().Return("") a, er := NewApp("0.0.0.0", th.conf, th.store) assert.NoError(t, er) if assert.NotNil(t, a) { assert.Equal(t, vocab.IRI(base), a.ServiceIRI()) assert.Equal(t, vocab.IRI(base), a.Service().ID) } } func TestAppStorage(t *testing.T) { th := setupAppTest(t) base := "http://localhost:1234/" th.conf.EXPECT().BaseURL().Return(base).MinTimes(1) th.conf.EXPECT().Name().Return("").AnyTimes() a, er := NewApp("0.0.0.0", th.conf, th.store) assert.NoError(t, er) if assert.NotNil(t, a) { assert.Equal(t, th.store, a.Storage()) } } func TestAppUser(t *testing.T) { // TODO: Actually fill this out at some point th := setupAppTest(t) base := "http://localhost:1234/" th.conf.EXPECT().BaseURL().Return(base).MinTimes(1) th.conf.EXPECT().Name().Return("").AnyTimes() a, er := NewApp("0.0.0.0", th.conf, th.store) assert.NoError(t, er) if assert.NotNil(t, a) { assert.Zero(t, a.User()) } } func TestStrings(t *testing.T) { cases := [...]struct { given string exp string }{ {"", "Lenore"}, {"Lenore", "Lenore"}, {"Danny Ray", "Danny Ray"}, {"✨👹", "✨👹"}, } for _, c := range cases { t.Run(c.given, func(t *testing.T) { th := setupAppTest(t) th.conf.EXPECT().BaseURL().Return("http://localhost:1234/").AnyTimes() th.conf.EXPECT().Name().Return(c.given).MinTimes(1) expStr := fmt.Sprintf("%s (%s)", c.exp, "0.0.0.0") a, er := NewApp("0.0.0.0", th.conf, th.store) assert.NoError(t, er) if assert.NotNil(t, a) { assert.Equal(t, c.exp, a.Name()) assert.Equal(t, "0.0.0.0", a.Version()) assert.Equal(t, expStr, a.String()) } }) } }