@@ -29,8 +29,8 @@ function makePlist(): string {
2929 return plist ;
3030}
3131
32- function runHelper ( script : string ) {
33- return spawnSync ( "bash" , [ "-lc" , script ] , {
32+ function runHelper ( script : string , shell = "bash" ) {
33+ return spawnSync ( shell , [ "-lc" , script ] , {
3434 cwd : process . cwd ( ) ,
3535 encoding : "utf8" ,
3636 } ) ;
@@ -69,6 +69,17 @@ function getSparkleBuildHelperBlock(): string {
6969 return script . slice ( start , end ) ;
7070}
7171
72+ function getMLXTTSHelperBuildBlock ( ) : string {
73+ const script = readFileSync ( scriptPath , "utf8" ) ;
74+ const start = script . indexOf ( "build_mlx_tts_helper() {" ) ;
75+ const end = script . indexOf ( "sparkle_framework_for_arch()" , start ) ;
76+
77+ expect ( start ) . toBeGreaterThanOrEqual ( 0 ) ;
78+ expect ( end ) . toBeGreaterThan ( start ) ;
79+
80+ return script . slice ( start , end ) ;
81+ }
82+
7283function getSwiftPackageResolutionBlock ( ) : string {
7384 const script = readFileSync ( scriptPath , "utf8" ) ;
7485 const start = script . indexOf ( "run_with_locked_swift_packages()" ) ;
@@ -495,6 +506,7 @@ describe("package-mac-app plist stamping", () => {
495506
496507 it ( "builds and bundles the MLX TTS helper for every requested architecture" , ( ) => {
497508 const script = readFileSync ( scriptPath , "utf8" ) ;
509+ const helperBlock = getMLXTTSHelperBuildBlock ( ) ;
498510 const buildLoop = script . slice (
499511 script . indexOf ( 'for arch in "${BUILD_ARCHS[@]}"; do' ) ,
500512 script . indexOf ( 'BIN_PRIMARY="$(bin_for_arch "$PRIMARY_ARCH")"' ) ,
@@ -504,16 +516,95 @@ describe("package-mac-app plist stamping", () => {
504516 script . indexOf ( "SPARKLE_FRAMEWORK_PRIMARY=" ) ,
505517 ) ;
506518
507- expect ( buildLoop ) . toContain ( 'swift build --package-path "$MLX_TTS_HELPER_ROOT"' ) ;
508- expect ( buildLoop ) . toContain ( '--product "$MLX_TTS_HELPER_PRODUCT"' ) ;
509- expect ( buildLoop ) . toContain ( '--arch "$arch"' ) ;
519+ expect ( buildLoop ) . toContain ( 'build_mlx_tts_helper "$arch"' ) ;
520+ expect ( helperBlock ) . toContain ( '--package-path "$MLX_TTS_HELPER_ROOT"' ) ;
521+ expect ( helperBlock ) . toContain ( '--product "$MLX_TTS_HELPER_PRODUCT"' ) ;
522+ expect ( helperBlock ) . toContain ( '--arch "$arch"' ) ;
510523 expect ( helperCopy ) . toContain (
511524 'cp "$(helper_bin_for_arch "$PRIMARY_ARCH")" "$APP_ROOT/Contents/MacOS/$MLX_TTS_HELPER_PRODUCT"' ,
512525 ) ;
513526 expect ( helperCopy ) . toContain ( '/usr/bin/lipo -create "${HELPER_BIN_INPUTS[@]}"' ) ;
514527 expect ( helperCopy ) . toContain ( 'chmod +x "$APP_ROOT/Contents/MacOS/$MLX_TTS_HELPER_PRODUCT"' ) ;
515528 } ) ;
516529
530+ it . each ( [
531+ { title : "keeps the default backend when Xcode's Metal shim works" , shimExit : 0 , xcrunExit : 0 } ,
532+ {
533+ title : "uses the native backend when xcrun can run Metal but Xcode's shim is broken" ,
534+ shimExit : 1 ,
535+ xcrunExit : 0 ,
536+ } ,
537+ {
538+ title : "keeps the default backend when no working Metal compiler is installed" ,
539+ shimExit : 1 ,
540+ xcrunExit : 1 ,
541+ } ,
542+ ] ) ( "$title" , ( { shimExit, xcrunExit } ) => {
543+ const helperBlock = getMLXTTSHelperBuildBlock ( ) ;
544+ const tempRoot = tempDirs . make ( "openclaw-package-mlx-metal-" ) ;
545+ const toolsDir = path . join ( tempRoot , "tools" ) ;
546+ const toolchainDir = path . join ( tempRoot , "xcode-toolchain" ) ;
547+ const invocationPath = path . join ( tempRoot , "swift-args" ) ;
548+
549+ mkdirSync ( toolsDir , { recursive : true } ) ;
550+ mkdirSync ( toolchainDir , { recursive : true } ) ;
551+
552+ const tools : Array < [ string , string ] > = [
553+ [
554+ path . join ( toolsDir , "xcrun" ) ,
555+ [
556+ "#!/usr/bin/env bash" ,
557+ 'case "$*" in' ,
558+ ' "--find swift") printf "%s\\n" "$MOCK_SWIFT_PATH" ;;' ,
559+ ' "metal --version") exit "$MOCK_XCRUN_EXIT" ;;' ,
560+ " *) exit 1 ;;" ,
561+ "esac" ,
562+ "" ,
563+ ] . join ( "\n" ) ,
564+ ] ,
565+ [
566+ path . join ( toolsDir , "swift" ) ,
567+ '#!/usr/bin/env bash\nprintf "%s\\n" "$@" > "$MOCK_SWIFT_ARGS"\n' ,
568+ ] ,
569+ [ path . join ( toolchainDir , "swift" ) , "#!/usr/bin/env bash\nexit 0\n" ] ,
570+ [ path . join ( toolchainDir , "metal" ) , `#!/usr/bin/env bash\nexit ${ shimExit } \n` ] ,
571+ ] ;
572+
573+ for ( const [ toolPath , contents ] of tools ) {
574+ writeFileSync ( toolPath , contents , "utf8" ) ;
575+ chmodSync ( toolPath , 0o755 ) ;
576+ }
577+
578+ const result = runHelper (
579+ `
580+ set -euo pipefail
581+ export PATH=${ JSON . stringify ( `${ toolsDir } :/usr/bin:/bin` ) }
582+ export MOCK_SWIFT_PATH=${ JSON . stringify ( path . join ( toolchainDir , "swift" ) ) }
583+ export MOCK_XCRUN_EXIT=${ JSON . stringify ( String ( xcrunExit ) ) }
584+ export MOCK_SWIFT_ARGS=${ JSON . stringify ( invocationPath ) }
585+ MLX_TTS_HELPER_ROOT=${ JSON . stringify ( path . join ( tempRoot , "helper" ) ) }
586+ MLX_TTS_HELPER_PRODUCT=openclaw-mlx-tts
587+ BUILD_CONFIG=debug
588+ helper_build_path_for_arch() { printf '%s\\n' ${ JSON . stringify ( path . join ( tempRoot , "build" ) ) } /"$1"; }
589+ ${ helperBlock }
590+ build_mlx_tts_helper arm64
591+ ` ,
592+ "/bin/bash" ,
593+ ) ;
594+
595+ expect ( result . status , result . stderr ) . toBe ( 0 ) ;
596+ const swiftArgs = readFileSync ( invocationPath , "utf8" ) . trim ( ) . split ( "\n" ) ;
597+ expect ( swiftArgs [ 0 ] ) . toBe ( "build" ) ;
598+ expect ( swiftArgs ) . toContain ( "--package-path" ) ;
599+ expect ( swiftArgs ) . toContain ( "--arch" ) ;
600+ expect ( swiftArgs ) . toContain ( "arm64" ) ;
601+ if ( shimExit === 0 || xcrunExit !== 0 ) {
602+ expect ( swiftArgs ) . not . toContain ( "--build-system" ) ;
603+ } else {
604+ expect ( swiftArgs . slice ( 1 , 3 ) ) . toEqual ( [ "--build-system" , "native" ] ) ;
605+ }
606+ } ) ;
607+
517608 it ( "falls back to corepack pnpm when the pnpm shim is absent" , ( ) => {
518609 const helperBlock = getPackageManagerHelperBlock ( ) ;
519610 const tempRoot = tempDirs . make ( "openclaw-package-pnpm-root-" ) ;
0 commit comments