@@ -2,6 +2,7 @@ import * as chai from "chai";
22import sinon from "sinon" ;
33import { PackageManager , toStringArray } from "../../load-package.ts" ;
44import { genMockAPI , genMockModule } from "./test-helper.ts" ;
5+ import { calculateInstallBaseUrl } from "../../load-package.ts" ;
56
67describe ( "PackageManager" , ( ) => {
78 it ( "should initialize with API and Module" , ( ) => {
@@ -120,3 +121,111 @@ describe("getLoadedPackageChannel", () => {
120121 } ) ;
121122 } ) ;
122123} ) ;
124+
125+ describe ( "calculateInstallBaseUrl" , ( ) => {
126+ let originalLocation : any ;
127+
128+ beforeEach ( ( ) => {
129+ // Store original location
130+ originalLocation = globalThis . location ;
131+ } ) ;
132+
133+ afterEach ( ( ) => {
134+ // Restore original location
135+ if ( originalLocation ) {
136+ globalThis . location = originalLocation ;
137+ } else {
138+ delete ( globalThis as any ) . location ;
139+ }
140+ } ) ;
141+
142+ it ( "Should extract base URL from absolute HTTP URL" , ( ) => {
143+ const result = calculateInstallBaseUrl (
144+ "https://cdn.example.com/pyodide/pyodide-lock.json" ,
145+ ) ;
146+ chai . assert . equal ( result , "https://cdn.example.com/pyodide/" ) ;
147+ } ) ;
148+
149+ it ( "Should extract base URL from file URL" , ( ) => {
150+ const result = calculateInstallBaseUrl (
151+ "file:///tmp/pyodide/pyodide-lock.json" ,
152+ ) ;
153+ chai . assert . equal ( result , "file:///tmp/pyodide/" ) ;
154+ } ) ;
155+
156+ it ( "Should extract base URL from relative URL with path" , ( ) => {
157+ const result = calculateInstallBaseUrl ( "./pyodide/pyodide-lock.json" ) ;
158+ chai . assert . equal ( result , "./pyodide/" ) ;
159+ } ) ;
160+
161+ it ( "Should extract base URL from relative URL with parent directory" , ( ) => {
162+ const result = calculateInstallBaseUrl ( "../pyodide/pyodide-lock.json" ) ;
163+ chai . assert . equal ( result , "../pyodide/" ) ;
164+ } ) ;
165+
166+ it ( "Should handle URL with no path component" , ( ) => {
167+ const result = calculateInstallBaseUrl ( "pyodide-lock.json" ) ;
168+ chai . assert . equal ( result , "." ) ;
169+ } ) ;
170+
171+ it ( "Should handle empty string" , ( ) => {
172+ const result = calculateInstallBaseUrl ( "" ) ;
173+ chai . assert . equal ( result , "." ) ;
174+ } ) ;
175+
176+ it ( "Should fallback to location when URL has no slash" , ( ) => {
177+ // Mock browser location
178+ ( globalThis as any ) . location = {
179+ toString : ( ) => "https://example.com/app/" ,
180+ } ;
181+
182+ const result = calculateInstallBaseUrl ( "pyodide-lock.json" ) ;
183+ chai . assert . equal ( result , "https://example.com/app/" ) ;
184+ } ) ;
185+
186+ it ( "Should fallback to location when URL is empty" , ( ) => {
187+ // Mock browser location
188+ ( globalThis as any ) . location = {
189+ toString : ( ) => "https://example.com/app/" ,
190+ } ;
191+
192+ const result = calculateInstallBaseUrl ( "" ) ;
193+ chai . assert . equal ( result , "https://example.com/app/" ) ;
194+ } ) ;
195+
196+ it ( "Should fallback to '.' when no location available" , ( ) => {
197+ // Remove location to simulate environment without location
198+ delete ( globalThis as any ) . location ;
199+
200+ const result = calculateInstallBaseUrl ( "pyodide-lock.json" ) ;
201+ chai . assert . equal ( result , "." ) ;
202+ } ) ;
203+
204+ it ( "Should handle URL with query parameters" , ( ) => {
205+ const result = calculateInstallBaseUrl (
206+ "https://cdn.example.com/pyodide/pyodide-lock.json?v=1.0" ,
207+ ) ;
208+ chai . assert . equal ( result , "https://cdn.example.com/pyodide/" ) ;
209+ } ) ;
210+
211+ it ( "Should handle URL with hash fragment" , ( ) => {
212+ const result = calculateInstallBaseUrl (
213+ "https://cdn.example.com/pyodide/pyodide-lock.json#section" ,
214+ ) ;
215+ chai . assert . equal ( result , "https://cdn.example.com/pyodide/" ) ;
216+ } ) ;
217+
218+ it ( "Should handle URL with both query parameters and hash" , ( ) => {
219+ const result = calculateInstallBaseUrl (
220+ "https://cdn.example.com/pyodide/pyodide-lock.json?v=1.0#section" ,
221+ ) ;
222+ chai . assert . equal ( result , "https://cdn.example.com/pyodide/" ) ;
223+ } ) ;
224+
225+ it ( "Should handle URL with username and password" , ( ) => {
226+ const result = calculateInstallBaseUrl (
227+ "https://user:[email protected] /pyodide/pyodide-lock.json" , 228+ ) ;
229+ chai . assert . equal ( result , "https://user:[email protected] /pyodide/" ) ; 230+ } ) ;
231+ } ) ;
0 commit comments