@@ -68,6 +68,82 @@ func TestGetProjectByStaging(t *testing.T) {
6868 }
6969}
7070
71+ func TestGetCloudTeamsPageUrl (t * testing.T ) {
72+ endpoint := QdRootEndpoint {Url : "https://qodana.cloud" }
73+ result := endpoint .GetCloudTeamsPageUrl ("github" , "/path/to/myproject" )
74+ expected := "https://qodana.cloud/?origin=github&name=myproject"
75+ if result != expected {
76+ t .Errorf ("GetCloudTeamsPageUrl() = %q, want %q" , result , expected )
77+ }
78+ }
79+
80+ func TestParseProjectName (t * testing.T ) {
81+ tests := []struct {
82+ name string
83+ data string
84+ want string
85+ wantErr bool
86+ }{
87+ {"valid json" , `{"name": "my-project"}` , "my-project" , false },
88+ {"empty name" , `{"name": ""}` , "" , false },
89+ {"invalid json" , `{invalid}` , "" , true },
90+ }
91+ for _ , tt := range tests {
92+ t .Run (tt .name , func (t * testing.T ) {
93+ got , err := parseProjectName ([]byte (tt .data ))
94+ if (err != nil ) != tt .wantErr {
95+ t .Errorf ("parseProjectName() error = %v, wantErr %v" , err , tt .wantErr )
96+ return
97+ }
98+ if got != tt .want {
99+ t .Errorf ("parseProjectName() = %v, want %v" , got , tt .want )
100+ }
101+ })
102+ }
103+ }
104+
105+ func TestApiVersionMismatchError (t * testing.T ) {
106+ err := & ApiVersionMismatchError {
107+ ApiKind : "Cloud" ,
108+ SupportedVersions : []string {"1.0" , "1.1" },
109+ }
110+ msg := err .Error ()
111+ if msg == "" {
112+ t .Error ("Expected non-empty error message" )
113+ }
114+ if len (msg ) < 10 {
115+ t .Errorf ("Error message too short: %s" , msg )
116+ }
117+ }
118+
119+ func TestToCloudVersion (t * testing.T ) {
120+ tests := []struct {
121+ version string
122+ wantMajor int
123+ wantMinor int
124+ wantErr bool
125+ }{
126+ {"1.0" , 1 , 0 , false },
127+ {"2.5" , 2 , 5 , false },
128+ {"invalid" , 0 , 0 , true },
129+ {"1.2.3" , 0 , 0 , true },
130+ {"a.b" , 0 , 0 , true },
131+ {"1.b" , 0 , 0 , true },
132+ }
133+ for _ , tt := range tests {
134+ t .Run (tt .version , func (t * testing.T ) {
135+ got , err := ToCloudVersion (tt .version )
136+ if (err != nil ) != tt .wantErr {
137+ t .Errorf ("ToCloudVersion(%q) error = %v, wantErr %v" , tt .version , err , tt .wantErr )
138+ return
139+ }
140+ if ! tt .wantErr && (got .Major != tt .wantMajor || got .Minor != tt .wantMinor ) {
141+ t .Errorf ("ToCloudVersion(%q) = %v, want {%d, %d}" , tt .version , got , tt .wantMajor , tt .wantMinor )
142+ }
143+ })
144+ }
145+ }
146+
71147func TestGetReportUrl (t * testing.T ) {
72148 for _ , tc := range []struct {
73149 name string
@@ -103,3 +179,84 @@ func TestGetReportUrl(t *testing.T) {
103179 )
104180 }
105181}
182+
183+ func TestGetEnvWithDefault (t * testing.T ) {
184+ tests := []struct {
185+ name string
186+ env string
187+ value string
188+ setEnv bool
189+ defaultValue string
190+ expected string
191+ }{
192+ {"env set" , "TEST_ENV_SET" , "value" , true , "default" , "value" },
193+ {"env not set" , "TEST_ENV_NOT_SET" , "" , false , "default" , "default" },
194+ }
195+
196+ for _ , tt := range tests {
197+ t .Run (tt .name , func (t * testing.T ) {
198+ if tt .setEnv {
199+ _ = os .Setenv (tt .env , tt .value )
200+ defer func () {
201+ _ = os .Unsetenv (tt .env )
202+ }()
203+ }
204+ if got := GetEnvWithDefault (tt .env , tt .defaultValue ); got != tt .expected {
205+ t .Errorf ("GetEnvWithDefault() = %v, want %v" , got , tt .expected )
206+ }
207+ })
208+ }
209+ }
210+
211+ func TestGetEnvWithDefaultInt (t * testing.T ) {
212+ t .Run ("env set" , func (t * testing.T ) {
213+ _ = os .Setenv ("TEST_INT_ENV" , "42" )
214+ defer func () {
215+ _ = os .Unsetenv ("TEST_INT_ENV" )
216+ }()
217+ if got := GetEnvWithDefaultInt ("TEST_INT_ENV" , 10 ); got != 42 {
218+ t .Errorf ("GetEnvWithDefaultInt() = %v, want 42" , got )
219+ }
220+ })
221+
222+ t .Run ("env not set" , func (t * testing.T ) {
223+ if got := GetEnvWithDefaultInt ("TEST_INT_NOT_SET" , 99 ); got != 99 {
224+ t .Errorf ("GetEnvWithDefaultInt() = %v, want 99" , got )
225+ }
226+ })
227+ }
228+
229+ func TestParseRawURL (t * testing.T ) {
230+ tests := []struct {
231+ name string
232+ url string
233+ wantErr bool
234+ }{
235+ {"valid https" , "https://example.com" , false },
236+ {"valid http" , "http://example.com" , false },
237+ {"without scheme" , "example.com" , false },
238+ }
239+
240+ for _ , tt := range tests {
241+ t .Run (tt .name , func (t * testing.T ) {
242+ _ , err := parseRawURL (tt .url )
243+ if (err != nil ) != tt .wantErr {
244+ t .Errorf ("parseRawURL() error = %v, wantErr %v" , err , tt .wantErr )
245+ }
246+ })
247+ }
248+ }
249+
250+ func TestExtractVersions (t * testing.T ) {
251+ descriptions := []ApiVersionDescription {
252+ {Version : "1.0" , URL : "http://example.com/v1" },
253+ {Version : "2.0" , URL : "http://example.com/v2" },
254+ }
255+ versions := extractVersions (descriptions )
256+ if len (versions ) != 2 {
257+ t .Errorf ("extractVersions() returned %d versions, want 2" , len (versions ))
258+ }
259+ if versions [0 ] != "1.0" || versions [1 ] != "2.0" {
260+ t .Errorf ("extractVersions() returned incorrect versions: %v" , versions )
261+ }
262+ }
0 commit comments