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"
storeMock "codeberg.org/danjones000/combluotion/internal/testmocks/store"
2024-09-13 17:45:06 -05:00
)
2025-01-24 16:27:05 -06:00
func getStore ( t * testing . T ) * storeMock . MockStore {
t . Helper ( )
ctrl := gomock . NewController ( t )
return storeMock . NewMockStore ( ctrl )
}
2024-09-13 17:45:06 -05:00
func TestEmptyBaseURL ( t * testing . T ) {
c := config . Config { }
2025-01-24 16:27:05 -06:00
a , er := NewApp ( "0.0.0" , c , getStore ( t ) )
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 TestDefaultEnvironment ( t * testing . T ) {
2025-01-24 16:27:05 -06:00
store := getStore ( t )
2024-09-13 17:45:06 -05:00
c := config . Config { BaseURL : "http://localhost:1234/" }
2025-01-24 16:27:05 -06:00
a , er := NewApp ( "0.0.0" , c , store )
2024-09-13 17:45:06 -05:00
assert . NoError ( t , er )
if assert . NotNil ( t , a ) {
2024-10-28 11:50:18 -05:00
assert . Equal ( t , config . Dev , a . Environment ( ) )
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 } ,
{ config . Env ( "foo" ) , config . Dev } ,
{ config . Env ( "✨" ) , config . Dev } ,
2024-09-13 17:45:06 -05:00
}
for _ , c := range cases {
t . Run ( string ( c . given ) , func ( t * testing . T ) {
2025-01-24 16:27:05 -06:00
store := getStore ( t )
2024-09-13 17:45:06 -05:00
conf := config . Config { BaseURL : "http://localhost:1234/" , Env : c . given }
2025-01-24 16:27:05 -06:00
a , er := NewApp ( "0.0.0" , conf , store )
2024-09-13 17:45:06 -05:00
assert . NoError ( t , er )
if assert . NotNil ( t , a ) {
assert . Equal ( t , conf , a . Config ( ) )
assert . Equal ( t , c . exp , a . Environment ( ) )
}
} )
}
}
func TestService ( t * testing . T ) {
2025-01-24 16:27:05 -06:00
store := getStore ( t )
2024-09-13 17:45:06 -05:00
base := "http://localhost:1234/"
conf := config . Config { BaseURL : base }
2025-01-24 16:27:05 -06:00
a , er := NewApp ( "0.0.0.0" , conf , 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-24 16:27:05 -06:00
store := getStore ( t )
2024-09-13 17:45:06 -05:00
conf := config . Config { BaseURL : "http://localhost:1234/" , Name : c . given }
expStr := fmt . Sprintf ( "%s (%s)" , c . exp , "0.0.0.0" )
2025-01-24 16:27:05 -06:00
a , er := NewApp ( "0.0.0.0" , conf , 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 ( ) )
}
} )
}
}