2024-10-28 13:48:22 -05:00
package combluotion
2024-09-13 17:45:06 -05:00
import (
"fmt"
"testing"
vocab "github.com/go-ap/activitypub"
"github.com/stretchr/testify/assert"
2025-01-24 16:27:05 -06:00
"go.uber.org/mock/gomock"
"codeberg.org/danjones000/combluotion/config"
2025-01-26 20:07:45 -06:00
confMock "codeberg.org/danjones000/combluotion/internal/testmocks/config"
2025-01-24 16:27:05 -06:00
storeMock "codeberg.org/danjones000/combluotion/internal/testmocks/store"
2024-09-13 17:45:06 -05:00
)
2025-01-26 20:07:45 -06:00
type appTest struct {
ctrl * gomock . Controller
store * storeMock . MockStore
conf * confMock . MockConfig
}
func setupAppTest ( t * testing . T ) appTest {
2025-01-24 16:27:05 -06:00
t . Helper ( )
ctrl := gomock . NewController ( t )
2025-01-26 20:07:45 -06:00
return appTest { ctrl , storeMock . NewMockStore ( ctrl ) , confMock . NewMockConfig ( ctrl ) }
2025-01-24 16:27:05 -06:00
}
2024-09-13 17:45:06 -05:00
func TestEmptyBaseURL ( t * testing . T ) {
2025-01-26 20:07:45 -06:00
th := setupAppTest ( t )
th . conf .
EXPECT ( ) .
BaseURL ( ) .
Return ( "" )
a , er := NewApp ( "0.0.0" , th . conf , th . store )
2024-09-13 17:45:06 -05:00
assert . Nil ( t , a )
2024-09-14 17:34:47 -05:00
assert . EqualError ( t , er , "missing BaseURL" )
2024-09-13 17:45:06 -05:00
}
func TestGivenEnvironment ( t * testing . T ) {
cases := [ ... ] struct {
given config . Env
exp config . Env
} {
2024-10-28 11:50:18 -05:00
{ config . Dev , config . Dev } ,
{ config . Prod , config . Prod } ,
{ config . Qa , config . Qa } ,
2024-09-13 17:45:06 -05:00
}
for _ , c := range cases {
t . Run ( string ( c . given ) , func ( t * testing . T ) {
2025-01-26 20:07:45 -06:00
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 )
2024-09-13 17:45:06 -05:00
assert . NoError ( t , er )
if assert . NotNil ( t , a ) {
2025-01-26 20:07:45 -06:00
assert . Equal ( t , th . conf , a . Config ( ) )
2024-09-13 17:45:06 -05:00
assert . Equal ( t , c . exp , a . Environment ( ) )
}
} )
}
}
func TestService ( t * testing . T ) {
2025-01-26 20:07:45 -06:00
th := setupAppTest ( t )
2024-09-13 17:45:06 -05:00
base := "http://localhost:1234/"
2025-01-26 20:07:45 -06:00
th . conf . EXPECT ( ) . BaseURL ( ) . Return ( base ) . Times ( 2 )
th . conf . EXPECT ( ) . Name ( ) . Return ( "" )
a , er := NewApp ( "0.0.0.0" , th . conf , th . store )
2024-09-13 17:45:06 -05:00
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 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 ) {
2025-01-26 20:07:45 -06:00
th := setupAppTest ( t )
th . conf . EXPECT ( ) . BaseURL ( ) . Return ( "http://localhost:1234/" ) . AnyTimes ( )
th . conf . EXPECT ( ) . Name ( ) . Return ( c . given ) . MinTimes ( 1 )
2024-09-13 17:45:06 -05:00
expStr := fmt . Sprintf ( "%s (%s)" , c . exp , "0.0.0.0" )
2025-01-26 20:07:45 -06:00
a , er := NewApp ( "0.0.0.0" , th . conf , th . store )
2024-09-13 17:45:06 -05:00
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 ( ) )
}
} )
}
}