@@ -11,8 +11,10 @@ import * as Effect from "effect/Effect";
1111import * as Exit from "effect/Exit" ;
1212import * as Fiber from "effect/Fiber" ;
1313import * as FileSystem from "effect/FileSystem" ;
14+ import * as Latch from "effect/Latch" ;
1415import * as Layer from "effect/Layer" ;
1516import * as Path from "effect/Path" ;
17+ import * as PlatformError from "effect/PlatformError" ;
1618import * as Sink from "effect/Sink" ;
1719import * as Stream from "effect/Stream" ;
1820import * as TestClock from "effect/testing/TestClock" ;
@@ -29,6 +31,7 @@ import * as ExternalLauncher from "./externalLauncher.ts";
2931const windowsHost = HostProcess . Platform . defaultValue ( ) === "win32" ;
3032
3133interface MockSpawnResult {
34+ readonly spawnError ?: PlatformError . PlatformError ;
3235 readonly exitCode ?: number ;
3336 readonly stdout ?: string ;
3437 readonly stall ?: boolean ;
@@ -69,15 +72,17 @@ const layerTest = (input: {
6972 const layerSpawner = Layer . succeed (
7073 ChildProcessSpawner . ChildProcessSpawner ,
7174 ChildProcessSpawner . make ( ( command ) =>
72- Effect . sync ( ( ) => {
75+ Effect . gen ( function * ( ) {
7376 assert . equal ( ChildProcess . isStandardCommand ( command ) , true ) ;
7477 if ( ! ChildProcess . isStandardCommand ( command ) ) {
7578 throw new Error ( "Expected a standard command" ) ;
7679 }
7780 input . onSpawn ?.( command ) ;
81+ const result = input . spawnResult ?.( command ) ;
82+ if ( result ?. spawnError ) return yield * result . spawnError ;
7883 return makeMockDetachedHandle ( {
7984 ...( input . onUnref === undefined ? { } : { onUnref : input . onUnref } ) ,
80- ...input . spawnResult ?. ( command ) ,
85+ ...result ,
8186 } ) ;
8287 } ) ,
8388 ) ,
@@ -857,6 +862,138 @@ it.effect.skipIf(windowsHost)("reveals by opening the containing directory on Li
857862 } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
858863) ;
859864
865+ for ( const wsl of [ false , true ] ) {
866+ it . effect . skipIf ( windowsHost ) (
867+ "selects a file over D-Bus on " + ( wsl ? "WSLg without interop PowerShell" : "Linux" ) ,
868+ ( ) =>
869+ Effect . gen ( function * ( ) {
870+ const fileSystem = yield * FileSystem . FileSystem ;
871+ const path = yield * Path . Path ;
872+ const binDir = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-editors-" } ) ;
873+ for ( const name of [ "xdg-open" , "xdg-mime" , "gdbus" , ...( wsl ? [ "explorer.exe" ] : [ ] ) ] ) {
874+ const filePath = path . join ( binDir , name ) ;
875+ yield * fileSystem . writeFileString ( filePath , "#!/bin/sh\n" ) ;
876+ yield * fileSystem . chmod ( filePath , 0o755 ) ;
877+ }
878+ const target = "/workspace/media/a 'quoted' \"clip\" #100% ? é\\name.mp4" ;
879+ const spawnedCommands : ChildProcess . StandardCommand [ ] = [ ] ;
880+ yield * Effect . gen ( function * ( ) {
881+ const launcher = yield * ExternalLauncher . ExternalLauncher ;
882+ yield * launcher . launchEditor ( { editor : "file-manager" , cwd : target , reveal : true } ) ;
883+ } ) . pipe (
884+ Effect . provide (
885+ layerTest ( {
886+ platform : "linux" ,
887+ env : {
888+ PATH : binDir ,
889+ WAYLAND_DISPLAY : "wayland-1" ,
890+ ...( wsl
891+ ? { WSL_DISTRO_NAME : "Ubuntu-24.04" , WSL_INTEROP : "/run/WSL/1_interop" }
892+ : { } ) ,
893+ } ,
894+ onSpawn : ( command ) => spawnedCommands . push ( command ) ,
895+ spawnResult : ( command ) =>
896+ command . command === "xdg-mime"
897+ ? { stdout : "com.thisisgm.flea.desktop\n" }
898+ : undefined ,
899+ } ) ,
900+ ) ,
901+ ) ;
902+
903+ const reveal = spawnedCommands . find ( ( command ) => command . command === "gdbus" ) ;
904+ assert . ok ( reveal ) ;
905+ assert . deepEqual ( reveal . args , [
906+ "call" ,
907+ "--session" ,
908+ "--dest" ,
909+ "org.freedesktop.FileManager1" ,
910+ "--object-path" ,
911+ "/org/freedesktop/FileManager1" ,
912+ "--method" ,
913+ "org.freedesktop.FileManager1.ShowItems" ,
914+ "[\"file:///workspace/media/a%20'quoted'%20%22clip%22%20%23100%25%20%3F%20%C3%A9%5Cname.mp4\"]" ,
915+ "" ,
916+ ] ) ;
917+ assert . deepEqual (
918+ spawnedCommands . filter ( ( command ) => command . command !== "xdg-mime" ) ,
919+ [ reveal ] ,
920+ ) ;
921+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
922+ ) ;
923+ }
924+
925+ for ( const scenario of [ "rejected" , "spawn-error" , "stalled" ] as const ) {
926+ it . effect . skipIf ( windowsHost ) ( "opens the containing directory when D-Bus is " + scenario , ( ) =>
927+ Effect . gen ( function * ( ) {
928+ const fileSystem = yield * FileSystem . FileSystem ;
929+ const path = yield * Path . Path ;
930+ const binDir = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-editors-" } ) ;
931+ for ( const name of [ "xdg-open" , "xdg-mime" , "gdbus" ] ) {
932+ const filePath = path . join ( binDir , name ) ;
933+ yield * fileSystem . writeFileString ( filePath , "#!/bin/sh\n" ) ;
934+ yield * fileSystem . chmod ( filePath , 0o755 ) ;
935+ }
936+
937+ const spawnedCommands : ChildProcess . StandardCommand [ ] = [ ] ;
938+ const started = yield * Latch . make ( ) ;
939+ const launch = Effect . gen ( function * ( ) {
940+ const launcher = yield * ExternalLauncher . ExternalLauncher ;
941+ yield * launcher . launchEditor ( {
942+ editor : "file-manager" ,
943+ cwd : "/workspace/media/linux-mini-v2.mp4" ,
944+ reveal : true ,
945+ } ) ;
946+ } ) . pipe (
947+ Effect . provide (
948+ layerTest ( {
949+ platform : "linux" ,
950+ env : { PATH : binDir , DISPLAY : ":0" } ,
951+ onSpawn : ( command ) => {
952+ spawnedCommands . push ( command ) ;
953+ if ( command . command === "gdbus" ) started . openUnsafe ( ) ;
954+ } ,
955+ spawnResult : ( command ) => {
956+ if ( command . command === "xdg-mime" ) return { stdout : "org.gnome.Nautilus.desktop\n" } ;
957+ if ( command . command !== "gdbus" ) return undefined ;
958+ if ( scenario === "spawn-error" ) {
959+ return {
960+ spawnError : new PlatformError . PlatformError (
961+ new PlatformError . SystemError ( {
962+ _tag : "NotFound" ,
963+ module : "ChildProcess" ,
964+ method : "spawn" ,
965+ pathOrDescriptor : "gdbus" ,
966+ } ) ,
967+ ) ,
968+ } ;
969+ }
970+ return { exitCode : 1 , stall : scenario === "stalled" } ;
971+ } ,
972+ } ) ,
973+ ) ,
974+ ) ;
975+ if ( scenario === "stalled" ) {
976+ const fiber = yield * launch . pipe ( Effect . forkChild ) ;
977+ yield * started . await ;
978+ yield * TestClock . adjust ( "1999 millis" ) ;
979+ assert . isUndefined ( spawnedCommands . find ( ( command ) => command . command === "xdg-open" ) ) ;
980+ yield * TestClock . adjust ( "1 millis" ) ;
981+ yield * Fiber . join ( fiber ) ;
982+ } else {
983+ yield * launch ;
984+ }
985+
986+ assert . deepEqual (
987+ spawnedCommands
988+ . filter ( ( command ) => command . command !== "xdg-mime" )
989+ . map ( ( command ) => command . command ) ,
990+ [ "gdbus" , "xdg-open" ] ,
991+ ) ;
992+ assert . deepEqual ( spawnedCommands . at ( - 1 ) ?. args , [ "/workspace/media" ] ) ;
993+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
994+ ) ;
995+ }
996+
860997it . effect . skipIf ( windowsHost ) (
861998 "does not advertise a Linux file manager without a graphical session" ,
862999 ( ) =>
0 commit comments