11import { promises as fsp } from 'node:fs'
22import { tmpdir } from 'node:os'
33import { join } from 'node:path'
4- import { describe , expect , it , vi } from 'vitest'
4+ import { afterEach , describe , expect , it , vi } from 'vitest'
55import {
6+ createStagingPath ,
67 discoverInstalledPackageFromDirectory ,
78 getTargetSkillRoot ,
89 isValidSkillName ,
@@ -297,6 +298,10 @@ describe('getTargetSkillRoot', () => {
297298} )
298299
299300describe ( 'swapDir and replaceSkillTree' , ( ) => {
301+ afterEach ( ( ) => {
302+ vi . restoreAllMocks ( )
303+ } )
304+
300305 it ( 'replaces a tree only once the new one is complete' , async ( ) => {
301306 const root = await fsp . mkdtemp ( join ( tmpdir ( ) , 'skill-hub-swap-' ) )
302307 const target = join ( root , 'skill' )
@@ -305,7 +310,6 @@ describe('swapDir and replaceSkillTree', () => {
305310
306311 await expect ( replaceSkillTree ( join ( root , 'missing' ) , target ) ) . rejects . toThrow ( )
307312 await expect ( fsp . readFile ( join ( target , 'SKILL.md' ) , 'utf8' ) ) . resolves . toBe ( 'old' )
308- await expect ( fsp . access ( `${ target } .staging` ) ) . rejects . toBeDefined ( )
309313
310314 const staging = join ( root , 'staging' )
311315 await fsp . mkdir ( staging )
@@ -316,4 +320,86 @@ describe('swapDir and replaceSkillTree', () => {
316320 expect ( ( await fsp . readdir ( root ) ) . sort ( ) ) . toEqual ( [ 'skill' ] )
317321 await fsp . rm ( root , { recursive : true , force : true } )
318322 } )
323+
324+ async function createSwapFixture ( ) {
325+ const root = await fsp . mkdtemp ( join ( tmpdir ( ) , 'skill-hub-swap-' ) )
326+ const target = join ( root , 'skill' )
327+ const staging = join ( root , 'staging' )
328+ await fsp . mkdir ( target )
329+ await fsp . writeFile ( join ( target , 'SKILL.md' ) , 'old' )
330+ await fsp . mkdir ( staging )
331+ await fsp . writeFile ( join ( staging , 'SKILL.md' ) , 'new' )
332+ return { root, target, staging }
333+ }
334+
335+ /** Makes `fsp.rename` fail with EPERM, as Windows does, for the first `times` moves from `from`. */
336+ function denyRenames ( from : string , times : number ) {
337+ const rename = fsp . rename . bind ( fsp )
338+ let denied = 0
339+ return vi . spyOn ( fsp , 'rename' ) . mockImplementation ( async ( source , destination ) => {
340+ if ( source === from && denied < times ) {
341+ denied ++
342+ throw Object . assign ( new Error ( `EPERM: operation not permitted, rename '${ source } ' -> '${ destination } '` ) , { code : 'EPERM' } )
343+ }
344+ return rename ( source , destination )
345+ } )
346+ }
347+
348+ it ( 'retries a rename that Windows denies for a moment' , async ( ) => {
349+ const { root, target, staging } = await createSwapFixture ( )
350+ denyRenames ( staging , 2 )
351+
352+ await swapDir ( staging , target )
353+
354+ await expect ( fsp . readFile ( join ( target , 'SKILL.md' ) , 'utf8' ) ) . resolves . toBe ( 'new' )
355+ expect ( ( await fsp . readdir ( root ) ) . sort ( ) ) . toEqual ( [ 'skill' ] )
356+ await fsp . rm ( root , { recursive : true , force : true } )
357+ } )
358+
359+ it ( 'puts the old tree back when the new one cannot move in' , async ( ) => {
360+ const { root, target, staging } = await createSwapFixture ( )
361+ denyRenames ( staging , Number . POSITIVE_INFINITY )
362+
363+ await expect ( swapDir ( staging , target ) ) . rejects . toMatchObject ( { code : 'EPERM' } )
364+
365+ await expect ( fsp . readFile ( join ( target , 'SKILL.md' ) , 'utf8' ) ) . resolves . toBe ( 'old' )
366+ expect ( ( await fsp . readdir ( root ) ) . sort ( ) ) . toEqual ( [ 'skill' , 'staging' ] )
367+ await fsp . rm ( root , { recursive : true , force : true } )
368+ } )
369+
370+ it ( 'keeps the tree another process published while this one was swapping' , async ( ) => {
371+ const { root, target, staging } = await createSwapFixture ( )
372+ const rename = fsp . rename . bind ( fsp )
373+ vi . spyOn ( fsp , 'rename' ) . mockImplementation ( async ( source , destination ) => {
374+ if ( source === staging ) {
375+ await fsp . mkdir ( target , { recursive : true } )
376+ await fsp . writeFile ( join ( target , 'SKILL.md' ) , 'other' )
377+ throw Object . assign ( new Error ( 'EPERM' ) , { code : 'EPERM' } )
378+ }
379+ return rename ( source , destination )
380+ } )
381+
382+ await swapDir ( staging , target )
383+
384+ await expect ( fsp . readFile ( join ( target , 'SKILL.md' ) , 'utf8' ) ) . resolves . toBe ( 'other' )
385+ expect ( ( await fsp . readdir ( root ) ) . sort ( ) ) . toEqual ( [ 'skill' , 'staging' ] )
386+ await fsp . rm ( root , { recursive : true , force : true } )
387+ } )
388+
389+ it ( 'gives each run its own staging path and removes ones left by dead runs' , async ( ) => {
390+ const root = await fsp . mkdtemp ( join ( tmpdir ( ) , 'skill-hub-staging-' ) )
391+ const target = join ( root , 'skill' )
392+ const live = `${ target } .staging-${ process . pid } -live`
393+ await fsp . mkdir ( live )
394+ await fsp . mkdir ( `${ target } .staging` )
395+ await fsp . mkdir ( `${ target } .staging-2147483646-dead` )
396+
397+ const first = await createStagingPath ( target )
398+ const second = await createStagingPath ( target )
399+
400+ expect ( first ) . not . toBe ( second )
401+ expect ( first . startsWith ( `${ target } .staging-${ process . pid } -` ) ) . toBe ( true )
402+ expect ( await fsp . readdir ( root ) ) . toEqual ( [ `skill.staging-${ process . pid } -live` ] )
403+ await fsp . rm ( root , { recursive : true , force : true } )
404+ } )
319405} )
0 commit comments