@@ -27,6 +27,7 @@ describe('Workerify Vite Plugin', () => {
2727
2828 expect ( plugin . name ) . toBe ( 'vite:workerify' ) ;
2929 expect ( plugin . enforce ) . toBe ( 'pre' ) ;
30+ expect ( typeof plugin . configResolved ) . toBe ( 'function' ) ;
3031 expect ( typeof plugin . configureServer ) . toBe ( 'function' ) ;
3132 expect ( typeof plugin . generateBundle ) . toBe ( 'function' ) ;
3233 expect ( typeof plugin . resolveId ) . toBe ( 'function' ) ;
@@ -81,6 +82,118 @@ describe('Workerify Vite Plugin', () => {
8182 } ) ;
8283 } ) ;
8384
85+ describe ( 'Base path configuration' , ( ) => {
86+ it ( 'should handle base path from Vite config' , ( ) => {
87+ const plugin = workerifyPlugin ( ) ;
88+
89+ // Simulate Vite calling configResolved with a base path
90+ plugin . configResolved ?.( { base : '/app/' } ) ;
91+
92+ // Load the virtual module and check it contains the correct URL
93+ const virtualModule = plugin . load ?.( '\0virtual:workerify-register' ) ;
94+ expect ( virtualModule ) . toContain ( '/app/workerify-sw.js' ) ;
95+ } ) ;
96+
97+ it ( 'should handle base path without trailing slash' , ( ) => {
98+ const plugin = workerifyPlugin ( ) ;
99+
100+ plugin . configResolved ?.( { base : '/myapp' } ) ;
101+
102+ const virtualModule = plugin . load ?.( '\0virtual:workerify-register' ) ;
103+ expect ( virtualModule ) . toContain ( '/myapp/workerify-sw.js' ) ;
104+ } ) ;
105+
106+ it ( 'should handle empty base path' , ( ) => {
107+ const plugin = workerifyPlugin ( ) ;
108+
109+ plugin . configResolved ?.( { base : '' } ) ;
110+
111+ const virtualModule = plugin . load ?.( '\0virtual:workerify-register' ) ;
112+ expect ( virtualModule ) . toContain ( '/workerify-sw.js' ) ;
113+ } ) ;
114+
115+ it ( 'should handle undefined base path' , ( ) => {
116+ const plugin = workerifyPlugin ( ) ;
117+
118+ plugin . configResolved ?.( { } ) ;
119+
120+ const virtualModule = plugin . load ?.( '\0virtual:workerify-register' ) ;
121+ expect ( virtualModule ) . toContain ( '/workerify-sw.js' ) ;
122+ } ) ;
123+
124+ it ( 'should handle base path with custom SW filename' , ( ) => {
125+ const plugin = workerifyPlugin ( { swFileName : 'custom-sw.js' } ) ;
126+
127+ plugin . configResolved ?.( { base : '/my-app/' } ) ;
128+
129+ const virtualModule = plugin . load ?.( '\0virtual:workerify-register' ) ;
130+ expect ( virtualModule ) . toContain ( '/my-app/custom-sw.js' ) ;
131+ } ) ;
132+
133+ it ( 'should serve SW at correct URL with base path in dev' , async ( ) => {
134+ const plugin = workerifyPlugin ( { swFileName : 'test-sw.js' } ) ;
135+
136+ // Set base path
137+ plugin . configResolved ?.( { base : '/base/' } ) ;
138+
139+ // Configure server
140+ plugin . configureServer ?.( mockServer ) ;
141+
142+ const middleware = captureMockMiddleware ( mockServer ) ;
143+
144+ // Request with base path
145+ const req = createMockRequest ( '/base/test-sw.js' ) ;
146+ const res = createMockResponse ( ) ;
147+
148+ const handled = await callMiddleware ( middleware , req , res ) ;
149+
150+ expect ( handled ) . toBe ( true ) ;
151+ expect ( res . setHeader ) . toHaveBeenCalledWith ( 'Content-Type' , 'application/javascript' ) ;
152+ expect ( res . end ) . toHaveBeenCalledWith ( expect . any ( String ) ) ;
153+ } ) ;
154+
155+ it ( 'should not serve SW at wrong base path' , async ( ) => {
156+ const plugin = workerifyPlugin ( { swFileName : 'test-sw.js' } ) ;
157+
158+ // Set base path
159+ plugin . configResolved ?.( { base : '/correct-base/' } ) ;
160+
161+ // Configure server
162+ plugin . configureServer ?.( mockServer ) ;
163+
164+ const middleware = captureMockMiddleware ( mockServer ) ;
165+
166+ // Request without base path (wrong URL)
167+ const req = createMockRequest ( '/test-sw.js' ) ;
168+ const res = createMockResponse ( ) ;
169+
170+ const handled = await callMiddleware ( middleware , req , res ) ;
171+
172+ expect ( handled ) . toBe ( false ) ;
173+ expect ( res . setHeader ) . not . toHaveBeenCalled ( ) ;
174+ expect ( res . end ) . not . toHaveBeenCalled ( ) ;
175+ } ) ;
176+
177+ it ( 'should handle complex base paths' , ( ) => {
178+ const plugin = workerifyPlugin ( ) ;
179+
180+ plugin . configResolved ?.( { base : '/path/to/app/' } ) ;
181+
182+ const virtualModule = plugin . load ?.( '\0virtual:workerify-register' ) ;
183+ expect ( virtualModule ) . toContain ( '/path/to/app/workerify-sw.js' ) ;
184+ } ) ;
185+
186+ it ( 'should handle root base path correctly' , ( ) => {
187+ const plugin = workerifyPlugin ( ) ;
188+
189+ plugin . configResolved ?.( { base : '/' } ) ;
190+
191+ const virtualModule = plugin . load ?.( '\0virtual:workerify-register' ) ;
192+ expect ( virtualModule ) . toContain ( '/workerify-sw.js' ) ;
193+ expect ( virtualModule ) . not . toContain ( '//workerify-sw.js' ) ;
194+ } ) ;
195+ } ) ;
196+
84197 describe ( 'Development server middleware' , ( ) => {
85198 it ( 'should register middleware with Vite server' , ( ) => {
86199 const plugin = workerifyPlugin ( ) ;
@@ -256,12 +369,14 @@ describe('Workerify Vite Plugin', () => {
256369 const plugin = workerifyPlugin ( ) ;
257370
258371 expect ( plugin ) . toHaveProperty ( 'name' ) ;
372+ expect ( plugin ) . toHaveProperty ( 'configResolved' ) ;
259373 expect ( plugin ) . toHaveProperty ( 'configureServer' ) ;
260374 expect ( plugin ) . toHaveProperty ( 'generateBundle' ) ;
261375 expect ( plugin ) . toHaveProperty ( 'resolveId' ) ;
262376 expect ( plugin ) . toHaveProperty ( 'load' ) ;
263377
264378 expect ( typeof plugin . name ) . toBe ( 'string' ) ;
379+ expect ( typeof plugin . configResolved ) . toBe ( 'function' ) ;
265380 expect ( typeof plugin . configureServer ) . toBe ( 'function' ) ;
266381 expect ( typeof plugin . generateBundle ) . toBe ( 'function' ) ;
267382 expect ( typeof plugin . resolveId ) . toBe ( 'function' ) ;
0 commit comments