@@ -21,8 +21,31 @@ interface RecordedRegistration {
2121 readonly files : Array < { readonly path : string ; readonly content : string } > ;
2222 readonly commands : Array < { readonly command : string ; readonly args : ReadonlyArray < string > } > ;
2323 readonly copies : Array < { readonly source : string ; readonly destination : string } > ;
24+ readonly binaryReads : string [ ] ;
25+ readonly binaryWrites : Array < {
26+ readonly path : string ;
27+ readonly bytes : Uint8Array ;
28+ readonly mode : number | undefined ;
29+ } > ;
2430}
2531
32+ const iconSource = "/tmp/.mount_T3/resources/app.asar/apps/desktop/prod-resources/icon.png" ;
33+ const iconPath = "/home/alice/.local/share/icons/com.t3tools.T3Code.desktop.png" ;
34+ const iconBytes = new Uint8Array ( [ 0x89 , 0x50 , 0x00 , 0xff , 0x80 , 0x0a ] ) ;
35+ const currentDesktopEntry = DesktopLinuxUrlHandler . renderUrlHandlerDesktopEntry ( {
36+ displayName : "T3 Code (Alpha)" ,
37+ execTarget : "/home/alice/Applications/T3-Code.AppImage" ,
38+ scheme : "t3code" ,
39+ iconPath,
40+ } ) ;
41+ const registrationCommands = [
42+ { command : "update-desktop-database" , args : [ "/home/alice/.local/share/applications" ] } ,
43+ {
44+ command : "xdg-mime" ,
45+ args : [ "default" , "com.t3tools.T3Code.desktop" , "x-scheme-handler/t3code" ] ,
46+ } ,
47+ ] ;
48+
2649const makeEnvironment = ( path : Path . Path , overrides : Record < string , unknown > = { } ) =>
2750 DesktopEnvironment . DesktopEnvironment . of ( {
2851 platform : "linux" ,
@@ -63,7 +86,9 @@ const layerHandler = (
6386 readonly writeError ?: PlatformError . PlatformError ;
6487 readonly existingEntry ?: string ;
6588 readonly iconSource ?: string ;
66- readonly iconCopyError ?: PlatformError . PlatformError ;
89+ readonly existingIcon ?: Uint8Array ;
90+ readonly iconReadError ?: PlatformError . PlatformError ;
91+ readonly iconWriteError ?: PlatformError . PlatformError ;
6792 } = { } ,
6893) =>
6994 DesktopLinuxUrlHandler . layer . pipe (
@@ -86,11 +111,35 @@ const layerHandler = (
86111 } ) ,
87112 FileSystem . layerNoop ( {
88113 copyFile : ( source , destination ) =>
89- input . iconCopyError
90- ? Effect . fail ( input . iconCopyError )
91- : Effect . sync ( ( ) => {
92- recorded . copies . push ( { source, destination } ) ;
93- } ) ,
114+ Effect . sync ( ( ) => {
115+ recorded . copies . push ( { source, destination } ) ;
116+ } ) ,
117+ readFile : ( path ) =>
118+ Effect . gen ( function * ( ) {
119+ recorded . binaryReads . push ( path ) ;
120+ if ( path === input . iconSource ) {
121+ if ( input . iconReadError ) return yield * input . iconReadError ;
122+ return new Uint8Array ( iconBytes ) ;
123+ }
124+ if ( path === iconPath && input . existingIcon !== undefined ) {
125+ return input . existingIcon ;
126+ }
127+ return yield * PlatformError . systemError ( {
128+ _tag : "NotFound" ,
129+ module : "FileSystem" ,
130+ method : "readFile" ,
131+ pathOrDescriptor : path ,
132+ } ) ;
133+ } ) ,
134+ writeFile : ( path , bytes , options ) =>
135+ Effect . gen ( function * ( ) {
136+ recorded . binaryWrites . push ( {
137+ path,
138+ bytes : new Uint8Array ( bytes ) ,
139+ mode : options ?. mode ,
140+ } ) ;
141+ if ( input . iconWriteError ) return yield * input . iconWriteError ;
142+ } ) ,
94143 readFileString : ( ) => Effect . succeed ( input . existingEntry ?? "" ) ,
95144 makeDirectory : ( path ) =>
96145 Effect . sync ( ( ) => {
@@ -155,6 +204,8 @@ const emptyRecording = (): RecordedRegistration => ({
155204 files : [ ] ,
156205 commands : [ ] ,
157206 copies : [ ] ,
207+ binaryReads : [ ] ,
208+ binaryWrites : [ ] ,
158209} ) ;
159210
160211describe ( "DesktopLinuxUrlHandler" , ( ) => {
@@ -227,6 +278,9 @@ describe("DesktopLinuxUrlHandler", () => {
227278 'Exec="/home/alice/Applications/T3-Code.AppImage" %U' ,
228279 ) ;
229280 assert . include ( recorded . files [ 0 ] ?. content , "MimeType=x-scheme-handler/t3code;" ) ;
281+ assert . deepEqual ( recorded . binaryReads , [ ] ) ;
282+ assert . deepEqual ( recorded . binaryWrites , [ ] ) ;
283+ assert . deepEqual ( recorded . copies , [ ] ) ;
230284 assert . deepEqual ( recorded . commands , [
231285 {
232286 command : "update-desktop-database" ,
@@ -284,56 +338,112 @@ describe("DesktopLinuxUrlHandler", () => {
284338
285339 it . effect ( "installs a persistent icon even when the desktop entry is already current" , ( ) => {
286340 const recorded = emptyRecording ( ) ;
287- const iconPath = "/home/alice/.local/share/icons/com.t3tools.T3Code.desktop.png" ;
288341 return Effect . gen ( function * ( ) {
289342 yield * runRegister ( recorded , {
290- iconSource : "/tmp/.mount_T3/resources/icon.png" ,
291- existingEntry : DesktopLinuxUrlHandler . renderUrlHandlerDesktopEntry ( {
292- displayName : "T3 Code (Alpha)" ,
293- execTarget : "/home/alice/Applications/T3-Code.AppImage" ,
294- scheme : "t3code" ,
295- iconPath,
296- } ) ,
343+ iconSource,
344+ existingEntry : currentDesktopEntry ,
297345 } ) ;
298346 assert . deepEqual ( recorded . files , [ ] ) ;
299- assert . deepEqual ( recorded . copies , [
300- { source : "/tmp/.mount_T3/resources/icon.png" , destination : iconPath } ,
301- ] ) ;
302- assert . equal ( recorded . commands . at ( - 1 ) ?. command , "xdg-mime" ) ;
347+ assert . deepEqual ( recorded . copies , [ ] ) ;
348+ assert . deepEqual ( recorded . binaryReads , [ iconSource , iconPath ] ) ;
349+ assert . deepEqual ( recorded . binaryWrites , [ { path : iconPath , bytes : iconBytes , mode : 0o600 } ] ) ;
350+ assert . deepEqual ( recorded . commands , registrationCommands ) ;
303351 } ) ;
304352 } ) ;
305353
306- it . effect ( "still registers the handler when copying its icon fails" , ( ) => {
354+ it . effect . each ( [
355+ {
356+ label : "same length with different bytes" ,
357+ current : new Uint8Array ( [ 0x89 , 0x50 , 0x00 , 0xfe , 0x80 , 0x0a ] ) ,
358+ } ,
359+ { label : "shorter" , current : iconBytes . subarray ( 0 , - 1 ) } ,
360+ { label : "longer" , current : new Uint8Array ( [ ...iconBytes , 0x00 ] ) } ,
361+ ] ) ( "replaces a $label icon independently of a current desktop entry" , ( { current } ) => {
307362 const recorded = emptyRecording ( ) ;
308363 return Effect . gen ( function * ( ) {
309364 yield * runRegister ( recorded , {
310- iconSource : "/tmp/.mount_T3/resources/icon.png" ,
311- iconCopyError : PlatformError . systemError ( {
312- _tag : "PermissionDenied" ,
313- module : "FileSystem" ,
314- method : "copyFile" ,
315- description : "read-only icon directory" ,
316- } ) ,
365+ iconSource,
366+ existingEntry : currentDesktopEntry ,
367+ existingIcon : current ,
317368 } ) ;
318- assert . equal ( recorded . files . length , 1 ) ;
319- assert . deepEqual (
320- recorded . commands . map ( ( { command } ) => command ) ,
321- [ "update-desktop-database" , "xdg-mime" ] ,
322- ) ;
369+ assert . deepEqual ( recorded . files , [ ] ) ;
370+ assert . deepEqual ( recorded . binaryReads , [ iconSource , iconPath ] ) ;
371+ assert . deepEqual ( recorded . binaryWrites , [ { path : iconPath , bytes : iconBytes , mode : 0o600 } ] ) ;
372+ assert . deepEqual ( recorded . copies , [ ] ) ;
373+ assert . deepEqual ( recorded . commands , registrationCommands ) ;
323374 } ) ;
324375 } ) ;
325376
377+ it . effect . each ( [
378+ { label : "current" , existingEntry : currentDesktopEntry } ,
379+ { label : "missing" , existingEntry : undefined } ,
380+ ] ) (
381+ "skips equal icon writes and still registers with a $label desktop entry" ,
382+ ( { existingEntry } ) => {
383+ const recorded = emptyRecording ( ) ;
384+ return Effect . gen ( function * ( ) {
385+ for ( let registration = 0 ; registration < 2 ; registration ++ ) {
386+ yield * runRegister ( recorded , {
387+ iconSource,
388+ ...( existingEntry === undefined ? { } : { existingEntry } ) ,
389+ existingIcon : new Uint8Array ( iconBytes ) ,
390+ } ) ;
391+ }
392+ assert . equal ( recorded . files . length , existingEntry === undefined ? 2 : 0 ) ;
393+ assert . deepEqual ( recorded . binaryReads , [ iconSource , iconPath , iconSource , iconPath ] ) ;
394+ assert . deepEqual ( recorded . binaryWrites , [ ] ) ;
395+ assert . deepEqual ( recorded . copies , [ ] ) ;
396+ assert . deepEqual ( recorded . commands , [ ...registrationCommands , ...registrationCommands ] ) ;
397+ } ) ;
398+ } ,
399+ ) ;
400+
401+ it . effect . each ( [ "readFile" , "writeFile" ] as const ) (
402+ "still registers the handler when the icon %s fails" ,
403+ ( method ) => {
404+ const recorded = emptyRecording ( ) ;
405+ const previousIcon = new Uint8Array ( [ 0xff , 0x00 ] ) ;
406+ const error = PlatformError . systemError ( {
407+ _tag : "PermissionDenied" ,
408+ module : "FileSystem" ,
409+ method,
410+ description : "icon unavailable" ,
411+ pathOrDescriptor : method === "readFile" ? iconSource : iconPath ,
412+ } ) ;
413+ return Effect . gen ( function * ( ) {
414+ yield * runRegister ( recorded , {
415+ iconSource,
416+ existingIcon : previousIcon ,
417+ ...( method === "readFile" ? { iconReadError : error } : { iconWriteError : error } ) ,
418+ } ) ;
419+ assert . deepEqual (
420+ recorded . binaryReads ,
421+ method === "readFile" ? [ iconSource ] : [ iconSource , iconPath ] ,
422+ ) ;
423+ assert . deepEqual (
424+ recorded . binaryWrites ,
425+ method === "readFile" ? [ ] : [ { path : iconPath , bytes : iconBytes , mode : 0o600 } ] ,
426+ ) ;
427+ assert . deepEqual ( previousIcon , new Uint8Array ( [ 0xff , 0x00 ] ) ) ;
428+ assert . deepEqual ( recorded . copies , [ ] ) ;
429+ assert . equal ( recorded . files . length , 1 ) ;
430+ assert . deepEqual ( recorded . commands , registrationCommands ) ;
431+ } ) ;
432+ } ,
433+ ) ;
434+
326435 it . effect ( "writes the portal identity without claiming the URL scheme in development" , ( ) => {
327436 const nonLinux = emptyRecording ( ) ;
328437 const unpackaged = emptyRecording ( ) ;
329438
330439 return Effect . gen ( function * ( ) {
331- yield * runRegister ( nonLinux , { environment : { platform : "darwin" } } ) ;
440+ yield * runRegister ( nonLinux , { environment : { platform : "darwin" } , iconSource } ) ;
332441 yield * runRegister ( unpackaged , {
333442 environment : {
334443 isPackaged : false ,
335444 linuxDesktopEntryName : "com.t3tools.T3Code.Development.desktop" ,
336445 } ,
446+ iconSource,
337447 } ) ;
338448
339449 assert . deepEqual ( nonLinux . files , [ ] ) ;
@@ -342,6 +452,11 @@ describe("DesktopLinuxUrlHandler", () => {
342452 "/home/alice/.local/share/applications/com.t3tools.T3Code.Development.desktop" ,
343453 ) ;
344454 assert . deepEqual ( unpackaged . commands , [ ] ) ;
455+ for ( const recorded of [ nonLinux , unpackaged ] ) {
456+ assert . deepEqual ( recorded . binaryReads , [ ] ) ;
457+ assert . deepEqual ( recorded . binaryWrites , [ ] ) ;
458+ assert . deepEqual ( recorded . copies , [ ] ) ;
459+ }
345460 } ) ;
346461 } ) ;
347462
0 commit comments