@@ -22,8 +22,31 @@ interface RecordedRegistration {
2222 readonly files : Array < { readonly path : string ; readonly content : string } > ;
2323 readonly commands : Array < { readonly command : string ; readonly args : ReadonlyArray < string > } > ;
2424 readonly copies : Array < { readonly source : string ; readonly destination : string } > ;
25+ readonly binaryReads : string [ ] ;
26+ readonly binaryWrites : Array < {
27+ readonly path : string ;
28+ readonly bytes : Uint8Array ;
29+ readonly mode : number | undefined ;
30+ } > ;
2531}
2632
33+ const iconSource = "/tmp/.mount_T3/resources/app.asar/apps/desktop/prod-resources/icon.png" ;
34+ const iconPath = "/home/alice/.local/share/icons/com.t3tools.T3Code.desktop.png" ;
35+ const iconBytes = new Uint8Array ( [ 0x89 , 0x50 , 0x00 , 0xff , 0x80 , 0x0a ] ) ;
36+ const currentDesktopEntry = DesktopLinuxUrlHandler . renderUrlHandlerDesktopEntry ( {
37+ displayName : "T3 Code (Alpha)" ,
38+ execTarget : "/home/alice/Applications/T3-Code.AppImage" ,
39+ scheme : "t3code" ,
40+ iconPath,
41+ } ) ;
42+ const registrationCommands = [
43+ { command : "update-desktop-database" , args : [ "/home/alice/.local/share/applications" ] } ,
44+ {
45+ command : "xdg-mime" ,
46+ args : [ "default" , "com.t3tools.T3Code.desktop" , "x-scheme-handler/t3code" ] ,
47+ } ,
48+ ] ;
49+
2750const makeEnvironment = ( path : Path . Path , overrides : Record < string , unknown > = { } ) =>
2851 DesktopEnvironment . DesktopEnvironment . of ( {
2952 platform : "linux" ,
@@ -64,7 +87,8 @@ const layerHandler = (
6487 readonly writeError ?: PlatformError . PlatformError ;
6588 readonly existingEntry ?: string ;
6689 readonly iconSource ?: string ;
67- readonly iconCopyError ?: PlatformError . PlatformError ;
90+ readonly existingIcon ?: Uint8Array ;
91+ readonly iconReadError ?: PlatformError . PlatformError ;
6892 } = { } ,
6993) =>
7094 DesktopLinuxUrlHandler . layer . pipe (
@@ -87,11 +111,34 @@ const layerHandler = (
87111 } ) ,
88112 FileSystem . layerNoop ( {
89113 copyFile : ( source , destination ) =>
90- input . iconCopyError
91- ? Effect . fail ( input . iconCopyError )
92- : Effect . sync ( ( ) => {
93- recorded . copies . push ( { source, destination } ) ;
94- } ) ,
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 . sync ( ( ) => {
136+ recorded . binaryWrites . push ( {
137+ path,
138+ bytes : new Uint8Array ( bytes ) ,
139+ mode : options ?. mode ,
140+ } ) ;
141+ } ) ,
95142 readFileString : ( ) => Effect . succeed ( input . existingEntry ?? "" ) ,
96143 makeDirectory : ( path ) =>
97144 Effect . sync ( ( ) => {
@@ -156,6 +203,8 @@ const emptyRecording = (): RecordedRegistration => ({
156203 files : [ ] ,
157204 commands : [ ] ,
158205 copies : [ ] ,
206+ binaryReads : [ ] ,
207+ binaryWrites : [ ] ,
159208} ) ;
160209
161210describe ( "DesktopLinuxUrlHandler" , ( ) => {
@@ -306,42 +355,57 @@ describe("DesktopLinuxUrlHandler", () => {
306355
307356 it . effect ( "installs a persistent icon even when the desktop entry is already current" , ( ) => {
308357 const recorded = emptyRecording ( ) ;
309- const iconPath = "/home/alice/.local/share/icons/com.t3tools.T3Code.desktop.png" ;
310358 return Effect . gen ( function * ( ) {
311359 yield * runRegister ( recorded , {
312- iconSource : "/tmp/.mount_T3/resources/icon.png" ,
313- existingEntry : DesktopLinuxUrlHandler . renderUrlHandlerDesktopEntry ( {
314- displayName : "T3 Code (Alpha)" ,
315- execTarget : "/home/alice/Applications/T3-Code.AppImage" ,
316- scheme : "t3code" ,
317- iconPath,
318- } ) ,
360+ iconSource,
361+ existingEntry : currentDesktopEntry ,
319362 } ) ;
320363 assert . deepEqual ( recorded . files , [ ] ) ;
321- assert . deepEqual ( recorded . copies , [
322- { source : "/tmp/.mount_T3/resources/icon.png" , destination : iconPath } ,
323- ] ) ;
324- assert . equal ( recorded . commands . at ( - 1 ) ?. command , "xdg-mime" ) ;
364+ assert . deepEqual ( recorded . copies , [ ] ) ;
365+ assert . deepEqual ( recorded . binaryReads , [ iconSource , iconPath ] ) ;
366+ assert . deepEqual ( recorded . binaryWrites , [ { path : iconPath , bytes : iconBytes , mode : 0o600 } ] ) ;
367+ assert . deepEqual ( recorded . commands , registrationCommands ) ;
325368 } ) ;
326369 } ) ;
327370
328- it . effect ( "still registers the handler when copying its icon fails " , ( ) => {
371+ it . effect ( "refreshes a stale icon and skips equal bytes on the next registration " , ( ) => {
329372 const recorded = emptyRecording ( ) ;
330373 return Effect . gen ( function * ( ) {
331374 yield * runRegister ( recorded , {
332- iconSource : "/tmp/.mount_T3/resources/icon.png" ,
333- iconCopyError : PlatformError . systemError ( {
375+ iconSource,
376+ existingEntry : currentDesktopEntry ,
377+ existingIcon : new Uint8Array ( [ 0x89 , 0x50 , 0x00 , 0xfe , 0x80 , 0x0a ] ) ,
378+ } ) ;
379+ assert . deepEqual ( recorded . binaryWrites , [ { path : iconPath , bytes : iconBytes , mode : 0o600 } ] ) ;
380+ yield * runRegister ( recorded , {
381+ iconSource,
382+ existingEntry : currentDesktopEntry ,
383+ existingIcon : new Uint8Array ( recorded . binaryWrites [ 0 ] ! . bytes ) ,
384+ } ) ;
385+ assert . equal ( recorded . binaryWrites . length , 1 ) ;
386+ assert . deepEqual ( recorded . copies , [ ] ) ;
387+ assert . deepEqual ( recorded . files , [ ] ) ;
388+ assert . deepEqual ( recorded . commands , [ ...registrationCommands , ...registrationCommands ] ) ;
389+ } ) ;
390+ } ) ;
391+
392+ it . effect ( "still registers the handler when reading its icon fails" , ( ) => {
393+ const recorded = emptyRecording ( ) ;
394+ return Effect . gen ( function * ( ) {
395+ yield * runRegister ( recorded , {
396+ iconSource,
397+ iconReadError : PlatformError . systemError ( {
334398 _tag : "PermissionDenied" ,
335399 module : "FileSystem" ,
336- method : "copyFile " ,
337- description : "read-only icon directory " ,
400+ method : "readFile " ,
401+ description : "unreadable bundled icon " ,
338402 } ) ,
339403 } ) ;
404+ assert . deepEqual ( recorded . binaryReads , [ iconSource ] ) ;
405+ assert . deepEqual ( recorded . binaryWrites , [ ] ) ;
406+ assert . deepEqual ( recorded . copies , [ ] ) ;
340407 assert . equal ( recorded . files . length , 1 ) ;
341- assert . deepEqual (
342- recorded . commands . map ( ( { command } ) => command ) ,
343- [ "update-desktop-database" , "xdg-mime" ] ,
344- ) ;
408+ assert . deepEqual ( recorded . commands , registrationCommands ) ;
345409 } ) ;
346410 } ) ;
347411
0 commit comments