@@ -2,18 +2,143 @@ package config
22
33import (
44 "os"
5+ "path/filepath"
56 "testing"
67)
78
8- func TestMain (m * testing.M ) {
9- // 设置测试环境变量
10- os .Setenv ("OPENCODE_SERVER_HOST" , "127.0.0.1" )
11- os .Setenv ("OPENCODE_SERVER_PORT" , "4096" )
12- os .Setenv ("OPENCODE_SERVER_USERNAME" , "opencode" )
13- os .Setenv ("OPENCODE_SERVER_PASSWORD" , "test" )
9+ func TestInitWithMissingConfig (t * testing.T ) {
10+ // 清除所有可能影响测试的环境变量
11+ os .Unsetenv ("OPENCODE_SERVER_HOST" )
12+ os .Unsetenv ("OPENCODE_SERVER_PORT" )
13+ os .Unsetenv ("OPENCODE_SERVER_USERNAME" )
14+ os .Unsetenv ("OPENCODE_SERVER_PASSWORD" )
15+
16+ // 确保没有配置文件在默认路径
17+ home , _ := os .UserHomeDir ()
18+ configPath := filepath .Join (home , ".config" , "oho" , "config.json" )
19+ os .RemoveAll (filepath .Dir (configPath ))
1420
1521 // 初始化配置
16- _ = Init ()
22+ err := Init ()
23+ if err != nil {
24+ t .Fatalf ("Init() returned error: %v" , err )
25+ }
26+
27+ // 验证默认值被使用
28+ cfg := Get ()
29+ if cfg .Host != "127.0.0.1" {
30+ t .Errorf ("Expected Host '127.0.0.1', got '%s'" , cfg .Host )
31+ }
32+ if cfg .Port != 4096 {
33+ t .Errorf ("Expected Port 4096, got %d" , cfg .Port )
34+ }
35+ if cfg .Username != "opencode" {
36+ t .Errorf ("Expected Username 'opencode', got '%s'" , cfg .Username )
37+ }
38+ if cfg .Password != "" {
39+ t .Errorf ("Expected empty Password, got '%s'" , cfg .Password )
40+ }
41+ }
42+
43+ func TestInitWithEnvOverrides (t * testing.T ) {
44+ // 设置环境变量
45+ os .Setenv ("OPENCODE_SERVER_HOST" , "192.168.1.1" )
46+ os .Setenv ("OPENCODE_SERVER_PORT" , "8080" )
47+ os .Setenv ("OPENCODE_SERVER_USERNAME" , "testuser" )
48+ os .Setenv ("OPENCODE_SERVER_PASSWORD" , "testpass" )
49+ defer func () {
50+ os .Unsetenv ("OPENCODE_SERVER_HOST" )
51+ os .Unsetenv ("OPENCODE_SERVER_PORT" )
52+ os .Unsetenv ("OPENCODE_SERVER_USERNAME" )
53+ os .Unsetenv ("OPENCODE_SERVER_PASSWORD" )
54+ }()
55+
56+ err := Init ()
57+ if err != nil {
58+ t .Fatalf ("Init() returned error: %v" , err )
59+ }
60+
61+ cfg := Get ()
62+ if cfg .Host != "192.168.1.1" {
63+ t .Errorf ("Expected Host '192.168.1.1', got '%s'" , cfg .Host )
64+ }
65+ if cfg .Port != 8080 {
66+ t .Errorf ("Expected Port 8080, got %d" , cfg .Port )
67+ }
68+ if cfg .Username != "testuser" {
69+ t .Errorf ("Expected Username 'testuser', got '%s'" , cfg .Username )
70+ }
71+ if cfg .Password != "testpass" {
72+ t .Errorf ("Expected Password 'testpass', got '%s'" , cfg .Password )
73+ }
74+ }
75+
76+ func TestGetBaseURL (t * testing.T ) {
77+ os .Unsetenv ("OPENCODE_SERVER_HOST" )
78+ os .Unsetenv ("OPENCODE_SERVER_PORT" )
79+ os .Unsetenv ("OPENCODE_SERVER_USERNAME" )
80+ os .Unsetenv ("OPENCODE_SERVER_PASSWORD" )
81+
82+ Init ()
83+
84+ baseURL := GetBaseURL ()
85+ expected := "http://127.0.0.1:4096"
86+ if baseURL != expected {
87+ t .Errorf ("Expected BaseURL '%s', got '%s'" , expected , baseURL )
88+ }
89+ }
90+
91+ func TestGetBaseURLWithCustomHostPort (t * testing.T ) {
92+ os .Setenv ("OPENCODE_SERVER_HOST" , "10.0.0.1" )
93+ os .Setenv ("OPENCODE_SERVER_PORT" , "3000" )
94+ defer func () {
95+ os .Unsetenv ("OPENCODE_SERVER_HOST" )
96+ os .Unsetenv ("OPENCODE_SERVER_PORT" )
97+ }()
98+
99+ Init ()
100+
101+ baseURL := GetBaseURL ()
102+ expected := "http://10.0.0.1:3000"
103+ if baseURL != expected {
104+ t .Errorf ("Expected BaseURL '%s', got '%s'" , expected , baseURL )
105+ }
106+ }
107+
108+ func TestGetConfigPathFallback (t * testing.T ) {
109+ // 测试 getConfigPath 返回有效路径
110+ path := getConfigPath ()
111+ if path == "" {
112+ t .Error ("getConfigPath() returned empty string" )
113+ }
114+
115+ // 验证路径包含 oho 和 config.json
116+ if ! filepath .IsAbs (path ) && path != filepath .Join ("." , ".config" , "oho" , "config.json" ) {
117+ // 如果不是绝对路径,应该是相对路径 fallback
118+ expectedRelative := filepath .Join ("." , ".config" , "oho" , "config.json" )
119+ if path != expectedRelative {
120+ t .Errorf ("Unexpected relative path: got %s, expected %s" , path , expectedRelative )
121+ }
122+ }
123+
124+ // 如果是绝对路径,应该包含正确的目录结构
125+ if filepath .IsAbs (path ) {
126+ if ! contains (path , ".config" ) || ! contains (path , "oho" ) || ! contains (path , "config.json" ) {
127+ t .Errorf ("Path does not contain expected components: %s" , path )
128+ }
129+ }
130+ }
131+
132+ // contains 是一个简单的字符串包含检查
133+ func contains (s , substr string ) bool {
134+ return len (s ) >= len (substr ) && (s == substr || len (s ) > 0 && containsHelper (s , substr ))
135+ }
17136
18- m .Run ()
137+ func containsHelper (s , substr string ) bool {
138+ for i := 0 ; i <= len (s )- len (substr ); i ++ {
139+ if s [i :i + len (substr )] == substr {
140+ return true
141+ }
142+ }
143+ return false
19144}
0 commit comments