1+ import { describe , it , expect } from 'vitest'
2+ import { extractApiVersionFromUrl , isAzureOpenAiUrl , removeApiVersionFromUrl } from '../azure-url-parser'
3+
4+ describe ( 'azure-url-parser' , ( ) => {
5+ describe ( 'extractApiVersionFromUrl' , ( ) => {
6+ it ( 'should extract API version from Azure OpenAI URL' , ( ) => {
7+ const url = 'https://myresource.openai.azure.com/openai/deployments/mymodel/chat/completions?api-version=2024-05-01-preview'
8+ const result = extractApiVersionFromUrl ( url )
9+ expect ( result ) . toBe ( '2024-05-01-preview' )
10+ } )
11+
12+ it ( 'should extract API version from URL with multiple query parameters' , ( ) => {
13+ const url = 'https://myresource.openai.azure.com/openai/deployments/mymodel/chat/completions?foo=bar&api-version=2024-12-01-preview&baz=qux'
14+ const result = extractApiVersionFromUrl ( url )
15+ expect ( result ) . toBe ( '2024-12-01-preview' )
16+ } )
17+
18+ it ( 'should return null when no api-version parameter exists' , ( ) => {
19+ const url = 'https://api.openai.com/v1/chat/completions'
20+ const result = extractApiVersionFromUrl ( url )
21+ expect ( result ) . toBeNull ( )
22+ } )
23+
24+ it ( 'should return null for invalid URLs' , ( ) => {
25+ const invalidUrl = 'not-a-valid-url'
26+ const result = extractApiVersionFromUrl ( invalidUrl )
27+ expect ( result ) . toBeNull ( )
28+ } )
29+
30+ it ( 'should handle empty api-version parameter' , ( ) => {
31+ const url = 'https://myresource.openai.azure.com/openai/deployments/mymodel/chat/completions?api-version='
32+ const result = extractApiVersionFromUrl ( url )
33+ expect ( result ) . toBe ( '' )
34+ } )
35+
36+ it ( 'should handle URL without query parameters' , ( ) => {
37+ const url = 'https://myresource.openai.azure.com/openai/deployments/mymodel/chat/completions'
38+ const result = extractApiVersionFromUrl ( url )
39+ expect ( result ) . toBeNull ( )
40+ } )
41+ } )
42+
43+ describe ( 'isAzureOpenAiUrl' , ( ) => {
44+ it ( 'should return true for Azure OpenAI URLs with .openai.azure.com' , ( ) => {
45+ const url = 'https://myresource.openai.azure.com/openai/deployments/mymodel/chat/completions'
46+ const result = isAzureOpenAiUrl ( url )
47+ expect ( result ) . toBe ( true )
48+ } )
49+
50+ it ( 'should return true for Azure URLs ending with .azure.com' , ( ) => {
51+ const url = 'https://myservice.azure.com/api/v1'
52+ const result = isAzureOpenAiUrl ( url )
53+ expect ( result ) . toBe ( true )
54+ } )
55+
56+ it ( 'should return true for URLs with /openai/deployments/ path' , ( ) => {
57+ const url = 'https://custom-domain.com/openai/deployments/mymodel/chat/completions'
58+ const result = isAzureOpenAiUrl ( url )
59+ expect ( result ) . toBe ( true )
60+ } )
61+
62+ it ( 'should return false for regular OpenAI URLs' , ( ) => {
63+ const url = 'https://api.openai.com/v1/chat/completions'
64+ const result = isAzureOpenAiUrl ( url )
65+ expect ( result ) . toBe ( false )
66+ } )
67+
68+ it ( 'should return false for other API URLs' , ( ) => {
69+ const url = 'https://api.anthropic.com/v1/messages'
70+ const result = isAzureOpenAiUrl ( url )
71+ expect ( result ) . toBe ( false )
72+ } )
73+
74+ it ( 'should return false for invalid URLs' , ( ) => {
75+ const invalidUrl = 'not-a-valid-url'
76+ const result = isAzureOpenAiUrl ( invalidUrl )
77+ expect ( result ) . toBe ( false )
78+ } )
79+
80+ it ( 'should handle case insensitive hostname matching' , ( ) => {
81+ const url = 'https://MYRESOURCE.OPENAI.AZURE.COM/openai/deployments/mymodel'
82+ const result = isAzureOpenAiUrl ( url )
83+ expect ( result ) . toBe ( true )
84+ } )
85+ } )
86+
87+ describe ( 'removeApiVersionFromUrl' , ( ) => {
88+ it ( 'should remove api-version parameter from URL' , ( ) => {
89+ const url = 'https://myresource.openai.azure.com/openai/deployments/mymodel/chat/completions?api-version=2024-05-01-preview'
90+ const result = removeApiVersionFromUrl ( url )
91+ expect ( result ) . toBe ( 'https://myresource.openai.azure.com/openai/deployments/mymodel/chat/completions' )
92+ } )
93+
94+ it ( 'should remove api-version parameter while preserving other parameters' , ( ) => {
95+ const url = 'https://myresource.openai.azure.com/openai/deployments/mymodel/chat/completions?foo=bar&api-version=2024-05-01-preview&baz=qux'
96+ const result = removeApiVersionFromUrl ( url )
97+ expect ( result ) . toBe ( 'https://myresource.openai.azure.com/openai/deployments/mymodel/chat/completions?foo=bar&baz=qux' )
98+ } )
99+
100+ it ( 'should return original URL when no api-version parameter exists' , ( ) => {
101+ const url = 'https://api.openai.com/v1/chat/completions?foo=bar'
102+ const result = removeApiVersionFromUrl ( url )
103+ expect ( result ) . toBe ( url )
104+ } )
105+
106+ it ( 'should return original URL for invalid URLs' , ( ) => {
107+ const invalidUrl = 'not-a-valid-url'
108+ const result = removeApiVersionFromUrl ( invalidUrl )
109+ expect ( result ) . toBe ( invalidUrl )
110+ } )
111+
112+ it ( 'should handle URL with only api-version parameter' , ( ) => {
113+ const url = 'https://myresource.openai.azure.com/openai/deployments/mymodel/chat/completions?api-version=2024-05-01-preview'
114+ const result = removeApiVersionFromUrl ( url )
115+ expect ( result ) . toBe ( 'https://myresource.openai.azure.com/openai/deployments/mymodel/chat/completions' )
116+ } )
117+
118+ it ( 'should handle URL without query parameters' , ( ) => {
119+ const url = 'https://myresource.openai.azure.com/openai/deployments/mymodel/chat/completions'
120+ const result = removeApiVersionFromUrl ( url )
121+ expect ( result ) . toBe ( url )
122+ } )
123+ } )
124+ } )
0 commit comments