@@ -2,9 +2,12 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
22import { it as effectIt } from "@effect/vitest" ;
33import * as HostProcess from "@t3tools/shared/HostProcess" ;
44import * as Effect from "effect/Effect" ;
5+ import * as Exit from "effect/Exit" ;
56import * as FileSystem from "effect/FileSystem" ;
67import * as Path from "effect/Path" ;
8+ import * as Result from "effect/Result" ;
79import * as TestClock from "effect/testing/TestClock" ;
10+ import * as Tracer from "effect/Tracer" ;
811import { describe , expect , it , vi } from "vite-plus/test" ;
912
1013import {
@@ -41,6 +44,19 @@ const withWindowsEnvironmentMocks = <A, E, R>(
4144 Effect . provideService ( CommandAvailability , commandAvailable ) ,
4245 ) ;
4346
47+ const recordCommandTrace = Effect . fnUntraced ( function * < A , E , R > ( effect : Effect . Effect < A , E , R > ) {
48+ const spans : Array < Tracer . NativeSpan > = [ ] ;
49+ const tracer = Tracer . make ( {
50+ span : ( options ) => {
51+ const span = new Tracer . NativeSpan ( options ) ;
52+ spans . push ( span ) ;
53+ return span ;
54+ } ,
55+ } ) ;
56+ const result = yield * effect . pipe ( Effect . withTracer ( tracer ) , Effect . result ) ;
57+ return { result, spans } ;
58+ } ) ;
59+
4460describe ( "readPathFromLoginShell" , ( ) => {
4561 it ( "uses a shell-agnostic printenv PATH probe" , ( ) => {
4662 const execFile = vi . fn <
@@ -338,6 +354,65 @@ describe("resolveKnownWindowsCliDirs", () => {
338354} ) ;
339355
340356effectIt . layer ( NodeServices . layer ) ( "isCommandAvailable" , ( it ) => {
357+ it . effect . each ( [ "win32" , "linux" ] as const ) (
358+ "records missing optional commands as successful lookups on %s" ,
359+ ( platform ) =>
360+ Effect . gen ( function * ( ) {
361+ const fs = yield * FileSystem . FileSystem ;
362+ const path = yield * Path . Path ;
363+ const cwd = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-command-trace-" } ) ;
364+ const env = { PATH : cwd , PATHEXT : ".CMD" } ;
365+ const checks = [
366+ isCommandAvailable ( "missing" , { env } ) ,
367+ isCommandAvailable ( "missing" , { env } ) ,
368+ isCommandAvailable ( "missing" , { env : { ...env , PATH : "" } } ) ,
369+ isCommandAvailable ( path . join ( cwd , "missing.cmd" ) , { env } ) ,
370+ ] ;
371+ for ( const check of checks ) {
372+ const { result, spans } = yield * recordCommandTrace ( check ) ;
373+ expect ( Result . getOrThrow ( result ) ) . toBe ( false ) ;
374+ expect ( spans . length ) . toBeGreaterThan ( 0 ) ;
375+ for ( const span of spans ) {
376+ expect ( span . status . _tag ) . toBe ( "Ended" ) ;
377+ if ( span . status . _tag === "Ended" ) {
378+ expect ( Exit . isSuccess ( span . status . exit ) , span . name ) . toBe ( true ) ;
379+ }
380+ }
381+ }
382+ } ) . pipe (
383+ Effect . provideService ( HostProcess . Platform , platform ) ,
384+ Effect . provideService ( CommandResolutionCache , new Map ( ) ) ,
385+ ) ,
386+ ) ;
387+
388+ it . effect . each ( [ "win32" , "linux" ] as const ) (
389+ "records available commands as successful lookups on %s" ,
390+ ( platform ) =>
391+ Effect . gen ( function * ( ) {
392+ const fs = yield * FileSystem . FileSystem ;
393+ const path = yield * Path . Path ;
394+ const cwd = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-command-trace-" } ) ;
395+ const executable = path . join ( cwd , platform === "win32" ? "available.cmd" : "available" ) ;
396+ yield * fs . writeFileString ( executable , "" ) ;
397+ yield * fs . chmod ( executable , 0o700 ) ;
398+ const env = { PATH : cwd , PATHEXT : ".CMD" } ;
399+ for ( const command of [ "available" , executable ] ) {
400+ const { result, spans } = yield * recordCommandTrace ( isCommandAvailable ( command , { env } ) ) ;
401+ expect ( Result . getOrThrow ( result ) ) . toBe ( true ) ;
402+ expect ( spans . length ) . toBeGreaterThan ( 0 ) ;
403+ for ( const span of spans ) {
404+ expect ( span . status . _tag ) . toBe ( "Ended" ) ;
405+ if ( span . status . _tag === "Ended" ) {
406+ expect ( Exit . isSuccess ( span . status . exit ) , span . name ) . toBe ( true ) ;
407+ }
408+ }
409+ }
410+ } ) . pipe (
411+ Effect . provideService ( HostProcess . Platform , platform ) ,
412+ Effect . provideService ( CommandResolutionCache , new Map ( ) ) ,
413+ ) ,
414+ ) ;
415+
341416 it . effect ( "returns false when PATH is empty" , ( ) =>
342417 Effect . gen ( function * ( ) {
343418 expect (
@@ -350,13 +425,29 @@ effectIt.layer(NodeServices.layer)("isCommandAvailable", (it) => {
350425} ) ;
351426
352427effectIt . layer ( NodeServices . layer ) ( "resolveCommandPath" , ( it ) => {
353- it . effect ( "fails when PATH is empty" , ( ) =>
428+ it . effect ( "records a successful lookup when a command is missing" , ( ) =>
429+ Effect . gen ( function * ( ) {
430+ const { result, spans } = yield * recordCommandTrace (
431+ resolveCommandPath ( "missing" , { env : { PATH : "" } } ) ,
432+ ) ;
433+ expect ( Result . getOrThrow ( result ) ) . toBeNull ( ) ;
434+ expect ( spans . length ) . toBeGreaterThan ( 0 ) ;
435+ for ( const span of spans ) {
436+ expect ( span . status . _tag ) . toBe ( "Ended" ) ;
437+ if ( span . status . _tag === "Ended" ) {
438+ expect ( Exit . isSuccess ( span . status . exit ) , span . name ) . toBe ( true ) ;
439+ }
440+ }
441+ } ) ,
442+ ) ;
443+
444+ it . effect ( "returns null when PATH is empty" , ( ) =>
354445 Effect . gen ( function * ( ) {
355446 const result = yield * resolveCommandPath ( "definitely-not-installed" , {
356447 env : { PATH : "" , PATHEXT : ".COM;.EXE;.BAT;.CMD" } ,
357- } ) . pipe ( Effect . provideService ( HostProcess . Platform , "win32" ) , Effect . result ) ;
448+ } ) . pipe ( Effect . provideService ( HostProcess . Platform , "win32" ) ) ;
358449
359- expect ( result . _tag ) . toBe ( "Failure" ) ;
450+ expect ( result ) . toBeNull ( ) ;
360451 } ) ,
361452 ) ;
362453
@@ -379,10 +470,9 @@ effectIt.layer(NodeServices.layer)("resolveCommandPath", (it) => {
379470 } ) ,
380471 } ) ,
381472 ) ,
382- Effect . result ,
383473 ) ;
384474
385- expect ( result . _tag ) . toBe ( "Failure" ) ;
475+ expect ( result ) . toBeNull ( ) ;
386476 return probed ;
387477 } ) ;
388478
@@ -448,13 +538,9 @@ effectIt.layer(NodeServices.layer)("resolveCommandPath", (it) => {
448538 const executable = path . join ( cwd , "appeared.CMD" ) ;
449539 const options = { env : { PATH : `${ cwd } ;${ cwd } ` , PATHEXT : ".CMD" } } ;
450540
451- expect ( ( yield * resolveCommandPath ( "appeared" , options ) . pipe ( Effect . result ) ) . _tag ) . toBe (
452- "Failure" ,
453- ) ;
541+ expect ( yield * resolveCommandPath ( "appeared" , options ) ) . toBeNull ( ) ;
454542 yield * fs . writeFileString ( executable , "@echo off\n" ) ;
455- expect ( ( yield * resolveCommandPath ( "appeared" , options ) . pipe ( Effect . result ) ) . _tag ) . toBe (
456- "Failure" ,
457- ) ;
543+ expect ( yield * resolveCommandPath ( "appeared" , options ) ) . toBeNull ( ) ;
458544 expect ( yield * resolveCommandPath ( executable , options ) ) . toBe ( executable ) ;
459545 yield * TestClock . adjust ( "30 seconds" ) ;
460546 expect ( yield * resolveCommandPath ( "appeared" , options ) ) . toBe ( executable ) ;
0 commit comments