code cleanup

This commit is contained in:
Fran Jurmanović
2025-09-18 13:33:51 +02:00
parent 901dbe697e
commit 5e7c96697a
83 changed files with 2832 additions and 2186 deletions

View File

@@ -12,25 +12,20 @@ import (
)
func TestConfigService_GetConfiguration_ValidFile(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
// Create test config files
err := helper.CreateTestConfigFiles()
tests.AssertNoError(t, err)
// Create repositories and service
configRepo := repository.NewConfigRepository(helper.DB)
serverRepo := repository.NewServerRepository(helper.DB)
configService := service.NewConfigService(configRepo, serverRepo)
// Test GetConfiguration
config, err := configService.GetConfiguration(helper.TestData.Server)
tests.AssertNoError(t, err)
tests.AssertNotNil(t, config)
// Verify the result is the expected configuration
tests.AssertEqual(t, model.IntString(9231), config.UdpPort)
tests.AssertEqual(t, model.IntString(9232), config.TcpPort)
tests.AssertEqual(t, model.IntString(30), config.MaxConnections)
@@ -40,21 +35,17 @@ func TestConfigService_GetConfiguration_ValidFile(t *testing.T) {
}
func TestConfigService_GetConfiguration_MissingFile(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
// Create server directory but no config files
serverConfigDir := filepath.Join(helper.TestData.Server.Path, "cfg")
err := os.MkdirAll(serverConfigDir, 0755)
tests.AssertNoError(t, err)
// Create repositories and service
configRepo := repository.NewConfigRepository(helper.DB)
serverRepo := repository.NewServerRepository(helper.DB)
configService := service.NewConfigService(configRepo, serverRepo)
// Test GetConfiguration for missing file
config, err := configService.GetConfiguration(helper.TestData.Server)
if err == nil {
t.Fatal("Expected error for missing file, got nil")
@@ -65,25 +56,20 @@ func TestConfigService_GetConfiguration_MissingFile(t *testing.T) {
}
func TestConfigService_GetEventConfig_ValidFile(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
// Create test config files
err := helper.CreateTestConfigFiles()
tests.AssertNoError(t, err)
// Create repositories and service
configRepo := repository.NewConfigRepository(helper.DB)
serverRepo := repository.NewServerRepository(helper.DB)
configService := service.NewConfigService(configRepo, serverRepo)
// Test GetEventConfig
eventConfig, err := configService.GetEventConfig(helper.TestData.Server)
tests.AssertNoError(t, err)
tests.AssertNotNil(t, eventConfig)
// Verify the result is the expected event configuration
tests.AssertEqual(t, "spa", eventConfig.Track)
tests.AssertEqual(t, model.IntString(80), eventConfig.PreRaceWaitingTimeSeconds)
tests.AssertEqual(t, model.IntString(120), eventConfig.SessionOverTimeSeconds)
@@ -91,7 +77,6 @@ func TestConfigService_GetEventConfig_ValidFile(t *testing.T) {
tests.AssertEqual(t, float64(0.3), eventConfig.CloudLevel)
tests.AssertEqual(t, float64(0.0), eventConfig.Rain)
// Verify sessions
tests.AssertEqual(t, 3, len(eventConfig.Sessions))
if len(eventConfig.Sessions) > 0 {
tests.AssertEqual(t, model.SessionPractice, eventConfig.Sessions[0].SessionType)
@@ -101,20 +86,16 @@ func TestConfigService_GetEventConfig_ValidFile(t *testing.T) {
func TestConfigService_SaveConfiguration_Success(t *testing.T) {
t.Skip("Temporarily disabled due to path issues")
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
// Create test config files
err := helper.CreateTestConfigFiles()
tests.AssertNoError(t, err)
// Create repositories and service
configRepo := repository.NewConfigRepository(helper.DB)
serverRepo := repository.NewServerRepository(helper.DB)
configService := service.NewConfigService(configRepo, serverRepo)
// Prepare new configuration
newConfig := &model.Configuration{
UdpPort: model.IntString(9999),
TcpPort: model.IntString(10000),
@@ -124,16 +105,13 @@ func TestConfigService_SaveConfiguration_Success(t *testing.T) {
ConfigVersion: model.IntString(2),
}
// Test SaveConfiguration
err = configService.SaveConfiguration(helper.TestData.Server, newConfig)
tests.AssertNoError(t, err)
// Verify the configuration was saved
configPath := filepath.Join(helper.TestData.Server.Path, "cfg", "configuration.json")
fileContent, err := os.ReadFile(configPath)
tests.AssertNoError(t, err)
// Convert from UTF-16 to UTF-8 for verification
utf8Content, err := service.DecodeUTF16LEBOM(fileContent)
tests.AssertNoError(t, err)
@@ -141,7 +119,6 @@ func TestConfigService_SaveConfiguration_Success(t *testing.T) {
err = json.Unmarshal(utf8Content, &savedConfig)
tests.AssertNoError(t, err)
// Verify the saved values
tests.AssertEqual(t, "9999", savedConfig["udpPort"])
tests.AssertEqual(t, "10000", savedConfig["tcpPort"])
tests.AssertEqual(t, "40", savedConfig["maxConnections"])
@@ -151,25 +128,20 @@ func TestConfigService_SaveConfiguration_Success(t *testing.T) {
}
func TestConfigService_LoadConfigs_Success(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
// Create test config files
err := helper.CreateTestConfigFiles()
tests.AssertNoError(t, err)
// Create repositories and service
configRepo := repository.NewConfigRepository(helper.DB)
serverRepo := repository.NewServerRepository(helper.DB)
configService := service.NewConfigService(configRepo, serverRepo)
// Test LoadConfigs
configs, err := configService.LoadConfigs(helper.TestData.Server)
tests.AssertNoError(t, err)
tests.AssertNotNil(t, configs)
// Verify all configurations are loaded
tests.AssertEqual(t, model.IntString(9231), configs.Configuration.UdpPort)
tests.AssertEqual(t, model.IntString(9232), configs.Configuration.TcpPort)
tests.AssertEqual(t, "Test ACC Server", configs.Settings.ServerName)
@@ -183,21 +155,17 @@ func TestConfigService_LoadConfigs_Success(t *testing.T) {
}
func TestConfigService_LoadConfigs_MissingFiles(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
// Create server directory but no config files
serverConfigDir := filepath.Join(helper.TestData.Server.Path, "cfg")
err := os.MkdirAll(serverConfigDir, 0755)
tests.AssertNoError(t, err)
// Create repositories and service
configRepo := repository.NewConfigRepository(helper.DB)
serverRepo := repository.NewServerRepository(helper.DB)
configService := service.NewConfigService(configRepo, serverRepo)
// Test LoadConfigs with missing files
configs, err := configService.LoadConfigs(helper.TestData.Server)
if err == nil {
t.Fatal("Expected error for missing files, got nil")
@@ -208,20 +176,16 @@ func TestConfigService_LoadConfigs_MissingFiles(t *testing.T) {
}
func TestConfigService_MalformedJSON(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
// Create malformed config file
err := helper.CreateMalformedConfigFile("configuration.json")
tests.AssertNoError(t, err)
// Create repositories and service
configRepo := repository.NewConfigRepository(helper.DB)
serverRepo := repository.NewServerRepository(helper.DB)
configService := service.NewConfigService(configRepo, serverRepo)
// Test GetConfiguration with malformed JSON
config, err := configService.GetConfiguration(helper.TestData.Server)
if err == nil {
t.Fatal("Expected error for malformed JSON, got nil")
@@ -232,31 +196,24 @@ func TestConfigService_MalformedJSON(t *testing.T) {
}
func TestConfigService_UTF16_Encoding(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
// Create test config files
err := helper.CreateTestConfigFiles()
tests.AssertNoError(t, err)
// Test UTF-16 encoding and decoding
originalData := `{"udpPort": "9231", "tcpPort": "9232"}`
// Encode to UTF-16 LE BOM
encoded, err := service.EncodeUTF16LEBOM([]byte(originalData))
tests.AssertNoError(t, err)
// Decode back to UTF-8
decoded, err := service.DecodeUTF16LEBOM(encoded)
tests.AssertNoError(t, err)
// Verify it matches original
tests.AssertEqual(t, originalData, string(decoded))
}
func TestConfigService_DecodeFileName(t *testing.T) {
// Test that all supported file names have decoders
testCases := []string{
"configuration.json",
"assistRules.json",
@@ -272,7 +229,6 @@ func TestConfigService_DecodeFileName(t *testing.T) {
})
}
// Test invalid filename
decoder := service.DecodeFileName("invalid.json")
if decoder != nil {
t.Fatal("Expected nil decoder for invalid filename, got non-nil")
@@ -280,22 +236,18 @@ func TestConfigService_DecodeFileName(t *testing.T) {
}
func TestConfigService_IntString_Conversion(t *testing.T) {
// Test IntString unmarshaling from string
var intStr model.IntString
// Test string input
err := json.Unmarshal([]byte(`"123"`), &intStr)
tests.AssertNoError(t, err)
tests.AssertEqual(t, 123, intStr.ToInt())
tests.AssertEqual(t, "123", intStr.ToString())
// Test int input
err = json.Unmarshal([]byte(`456`), &intStr)
tests.AssertNoError(t, err)
tests.AssertEqual(t, 456, intStr.ToInt())
tests.AssertEqual(t, "456", intStr.ToString())
// Test empty string
err = json.Unmarshal([]byte(`""`), &intStr)
tests.AssertNoError(t, err)
tests.AssertEqual(t, 0, intStr.ToInt())
@@ -303,28 +255,23 @@ func TestConfigService_IntString_Conversion(t *testing.T) {
}
func TestConfigService_IntBool_Conversion(t *testing.T) {
// Test IntBool unmarshaling from int
var intBool model.IntBool
// Test int input (1 = true)
err := json.Unmarshal([]byte(`1`), &intBool)
tests.AssertNoError(t, err)
tests.AssertEqual(t, 1, intBool.ToInt())
tests.AssertEqual(t, true, intBool.ToBool())
// Test int input (0 = false)
err = json.Unmarshal([]byte(`0`), &intBool)
tests.AssertNoError(t, err)
tests.AssertEqual(t, 0, intBool.ToInt())
tests.AssertEqual(t, false, intBool.ToBool())
// Test bool input (true)
err = json.Unmarshal([]byte(`true`), &intBool)
tests.AssertNoError(t, err)
tests.AssertEqual(t, 1, intBool.ToInt())
tests.AssertEqual(t, true, intBool.ToBool())
// Test bool input (false)
err = json.Unmarshal([]byte(`false`), &intBool)
tests.AssertNoError(t, err)
tests.AssertEqual(t, 0, intBool.ToInt())
@@ -332,25 +279,20 @@ func TestConfigService_IntBool_Conversion(t *testing.T) {
}
func TestConfigService_Caching_Configuration(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
// Create test config files (already UTF-16 encoded)
err := helper.CreateTestConfigFiles()
tests.AssertNoError(t, err)
// Create repositories and service
configRepo := repository.NewConfigRepository(helper.DB)
serverRepo := repository.NewServerRepository(helper.DB)
configService := service.NewConfigService(configRepo, serverRepo)
// First call - should load from disk
config1, err := configService.GetConfiguration(helper.TestData.Server)
tests.AssertNoError(t, err)
tests.AssertNotNil(t, config1)
// Modify the file on disk with UTF-16 encoding
configPath := filepath.Join(helper.TestData.Server.Path, "cfg", "configuration.json")
modifiedContent := `{"udpPort": "5555", "tcpPort": "5556"}`
utf16Modified, err := service.EncodeUTF16LEBOM([]byte(modifiedContent))
@@ -359,36 +301,29 @@ func TestConfigService_Caching_Configuration(t *testing.T) {
err = os.WriteFile(configPath, utf16Modified, 0644)
tests.AssertNoError(t, err)
// Second call - should return cached result (not the modified file)
config2, err := configService.GetConfiguration(helper.TestData.Server)
tests.AssertNoError(t, err)
tests.AssertNotNil(t, config2)
// Should still have the original cached values
tests.AssertEqual(t, model.IntString(9231), config2.UdpPort)
tests.AssertEqual(t, model.IntString(9232), config2.TcpPort)
}
func TestConfigService_Caching_EventConfig(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
// Create test config files (already UTF-16 encoded)
err := helper.CreateTestConfigFiles()
tests.AssertNoError(t, err)
// Create repositories and service
configRepo := repository.NewConfigRepository(helper.DB)
serverRepo := repository.NewServerRepository(helper.DB)
configService := service.NewConfigService(configRepo, serverRepo)
// First call - should load from disk
event1, err := configService.GetEventConfig(helper.TestData.Server)
tests.AssertNoError(t, err)
tests.AssertNotNil(t, event1)
// Modify the file on disk with UTF-16 encoding
configPath := filepath.Join(helper.TestData.Server.Path, "cfg", "event.json")
modifiedContent := `{"track": "monza", "preRaceWaitingTimeSeconds": "60"}`
utf16Modified, err := service.EncodeUTF16LEBOM([]byte(modifiedContent))
@@ -397,12 +332,10 @@ func TestConfigService_Caching_EventConfig(t *testing.T) {
err = os.WriteFile(configPath, utf16Modified, 0644)
tests.AssertNoError(t, err)
// Second call - should return cached result (not the modified file)
event2, err := configService.GetEventConfig(helper.TestData.Server)
tests.AssertNoError(t, err)
tests.AssertNotNil(t, event2)
// Should still have the original cached values
tests.AssertEqual(t, "spa", event2.Track)
tests.AssertEqual(t, model.IntString(80), event2.PreRaceWaitingTimeSeconds)
}