|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestLoadNotFoundFile(t *testing.T) { |
| 11 | + os.Clearenv() |
| 12 | + _, err := Load(".env.this_file_does_not_exist") |
| 13 | + if err == nil { |
| 14 | + t.Errorf("Unexpected Load() success: expected:failure, got:success") |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +func TestLoadErrors(t *testing.T) { |
| 19 | + expectedErrors := [4]string{ |
| 20 | + `"NOTION_API_SECRET" should not be empty`, |
| 21 | + `"NOTION_PAGE_ID" should not be empty`, |
| 22 | + `"IGDB_CLIENT_ID" should not be empty`, |
| 23 | + `"IGDB_SECRET" should not be empty`, |
| 24 | + } |
| 25 | + |
| 26 | + os.Clearenv() |
| 27 | + _, err := Load(".env.test_invalid") |
| 28 | + |
| 29 | + for _, expected := range expectedErrors { |
| 30 | + if !strings.Contains(err.Error(), expected) { |
| 31 | + t.Errorf("Expected to find error '%s'", expected) |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestLoadDefaultValues(t *testing.T) { |
| 37 | + os.Clearenv() |
| 38 | + config, err := Load(".env.test_default") |
| 39 | + if err != nil { |
| 40 | + t.Errorf("Unexpected Load() error:'%s'", err) |
| 41 | + } |
| 42 | + |
| 43 | + if assertConfig("UpdaterHost", config.UpdaterHost, "127.0.0.1") != nil { |
| 44 | + t.Error(err) |
| 45 | + } |
| 46 | + |
| 47 | + if assertConfig("UpdaterPort", config.UpdaterPort, 8080) != nil { |
| 48 | + t.Error(err) |
| 49 | + } |
| 50 | + |
| 51 | + if assertConfig("WatcherTickDelay", config.WatcherTickDelay, 5) != nil { |
| 52 | + t.Error(err) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestLoadSuccess(t *testing.T) { |
| 57 | + os.Clearenv() |
| 58 | + config, err := Load(".env.test_success") |
| 59 | + if err != nil { |
| 60 | + t.Errorf("Unexpected Load() error:'%s'", err) |
| 61 | + } |
| 62 | + |
| 63 | + if assertConfig("NotionAPISecret", config.NotionAPISecret, "u3M3sAdjqkLm8JSJihaGu3GfscAGE2") != nil { |
| 64 | + t.Error(err) |
| 65 | + } |
| 66 | + |
| 67 | + if assertConfig("NotionPageID", config.NotionPageID, "sEQCpMkvVCRfd4JAzVjmsd4EGYCFNC") != nil { |
| 68 | + t.Error(err) |
| 69 | + } |
| 70 | + |
| 71 | + if assertConfig("IGDBClientID", config.IGDBClientID, "pUnDQo4LzgjrYifQR5W7cAeaxACx92") != nil { |
| 72 | + t.Error(err) |
| 73 | + } |
| 74 | + |
| 75 | + if assertConfig("IGDBSecret", config.IGDBSecret, "WjMpHfNk6Mt4b4FV8TFT2pDiwiUeKu") != nil { |
| 76 | + t.Error(err) |
| 77 | + } |
| 78 | + |
| 79 | + if assertConfig("UpdaterHost", config.UpdaterHost, "76.388.29.99") != nil { |
| 80 | + t.Error(err) |
| 81 | + } |
| 82 | + |
| 83 | + if assertConfig("UpdaterPort", config.UpdaterPort, 7654) != nil { |
| 84 | + t.Error(err) |
| 85 | + } |
| 86 | + |
| 87 | + if assertConfig("WatcherTickDelay", config.WatcherTickDelay, 99) != nil { |
| 88 | + t.Error(err) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestUpdaterURL(t *testing.T) { |
| 93 | + config := Config{ |
| 94 | + UpdaterHost: "172.16.4.97", |
| 95 | + UpdaterPort: 9999, |
| 96 | + } |
| 97 | + |
| 98 | + result := config.UpdaterURL() |
| 99 | + expected := "http://172.16.4.97:9999/" |
| 100 | + |
| 101 | + if result != expected { |
| 102 | + t.Errorf("Unexpected UpdaterURL: expected:%s, got:%s", expected, result) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func assertConfig(key string, value any, expected any) (err error) { |
| 107 | + if value != expected { |
| 108 | + err = fmt.Errorf("Unexpected %s value: expected:%v, got:%v", key, expected, value) |
| 109 | + } |
| 110 | + |
| 111 | + return |
| 112 | +} |
0 commit comments