@@ -197,7 +197,7 @@ function makeHarness(options: HarnessOptions = {}) {
197197 fetchWorktreeBaseCommit :
198198 options . fetchWorktreeBaseCommit ??
199199 ( ( ) => Effect . succeed ( { commitSha : "remote-main-sha" , remoteRefName : "origin/main" } ) ) ,
200- hasCommit : options . hasCommit ?? ( ( ) => Effect . succeed ( false ) ) ,
200+ hasCommit : options . hasCommit ?? ( ( { refName } ) => Effect . succeed ( refName === "HEAD" ) ) ,
201201 resolveWorktreeBaseRemote : ( ) => Effect . succeed ( { remoteName : "origin" , branchName : "main" } ) ,
202202 remoteExists : options . remoteExists ?? ( ( ) => Effect . succeed ( true ) ) ,
203203 remoteBranchExists : ( ) => Effect . succeed ( true ) ,
@@ -1017,6 +1017,106 @@ it.effect("runs a Scratch scheduled task saved with a worktree in its own folder
10171017 } ) . pipe ( Effect . provide ( harness . layer ) ) ;
10181018} ) ;
10191019
1020+ it . effect (
1021+ "runs a new worktree thread in the project folder when the repository has no commits" ,
1022+ ( ) =>
1023+ Effect . gen ( function * ( ) {
1024+ const harness = makeHarness ( {
1025+ hasCommit : ( ) => Effect . succeed ( false ) ,
1026+ git : { hasAnyRef : ( ) => Effect . succeed ( false ) } ,
1027+ } ) ;
1028+ yield * Effect . gen ( function * ( ) {
1029+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
1030+ const threads = yield * ThreadManagement . ThreadManagementService ;
1031+ const input = launchInput ( {
1032+ command : "command:launch:empty-repo" ,
1033+ thread : "thread:launch:empty-repo" ,
1034+ message : "Build the feature" ,
1035+ workspace : { type : "worktree" , baseRef : "main" } ,
1036+ } ) ;
1037+ const launched = yield * launches . launch ( input ) ;
1038+ yield * threads . streamStoredEventsFrom ( { threadId : launched . threadId } ) . pipe (
1039+ Stream . takeUntil (
1040+ ( stored ) =>
1041+ stored . commandId === CommandId . make ( `${ input . commandId } :release` ) &&
1042+ stored . event . type === "run.updated" ,
1043+ ) ,
1044+ Stream . runDrain ,
1045+ ) ;
1046+ const { thread, runs } = yield * threads . getThreadProjection ( launched . threadId ) ;
1047+ assert . isNull ( thread . worktreePath ) ;
1048+ assert . isNull ( thread . branch ) ;
1049+ assert . equal ( harness . createWorktree . mock . calls . length , 0 ) ;
1050+ // The orchestrator reads a worktree preparation without a worktree as
1051+ // failed setup and holds the queue behind it.
1052+ assert . deepEqual ( runs [ 0 ] ?. workspacePreparation , { type : "root" } ) ;
1053+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
1054+ } ) ,
1055+ ) ;
1056+
1057+ it . effect . each ( [
1058+ {
1059+ name : "the base is fetched from origin" ,
1060+ startFromOrigin : true ,
1061+ hasAnyRef : false ,
1062+ baseRef : "main" ,
1063+ requestedBaseHasCommit : false ,
1064+ } ,
1065+ {
1066+ name : "another ref has a commit" ,
1067+ startFromOrigin : false ,
1068+ hasAnyRef : true ,
1069+ baseRef : "main" ,
1070+ requestedBaseHasCommit : false ,
1071+ } ,
1072+ {
1073+ name : "the requested base is a commit by SHA" ,
1074+ startFromOrigin : false ,
1075+ hasAnyRef : false ,
1076+ baseRef : "requested-base-sha" ,
1077+ requestedBaseHasCommit : true ,
1078+ } ,
1079+ ] ) ( "keeps the worktree when HEAD has no commit but $name" , ( testCase ) =>
1080+ Effect . gen ( function * ( ) {
1081+ const harness = makeHarness ( {
1082+ hasCommit : ( { refName } ) =>
1083+ Effect . succeed (
1084+ refName === "remote-main-sha" ||
1085+ ( testCase . requestedBaseHasCommit === true && refName === testCase . baseRef ) ,
1086+ ) ,
1087+ git : { hasAnyRef : ( ) => Effect . succeed ( testCase . hasAnyRef ) } ,
1088+ } ) ;
1089+ yield * Effect . gen ( function * ( ) {
1090+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
1091+ const threads = yield * ThreadManagement . ThreadManagementService ;
1092+ const input = launchInput ( {
1093+ command : `command:launch:empty-repo-${ testCase . startFromOrigin } ` ,
1094+ thread : `thread:launch:empty-repo-${ testCase . startFromOrigin } ` ,
1095+ message : "Build the feature" ,
1096+ workspace : {
1097+ type : "worktree" ,
1098+ baseRef : testCase . baseRef ,
1099+ startFromOrigin : testCase . startFromOrigin ,
1100+ } ,
1101+ } ) ;
1102+ const launched = yield * launches . launch ( input ) ;
1103+ yield * threads . streamStoredEventsFrom ( { threadId : launched . threadId } ) . pipe (
1104+ Stream . takeUntil (
1105+ ( stored ) =>
1106+ stored . commandId === CommandId . make ( `${ input . commandId } :release` ) &&
1107+ stored . event . type === "run.updated" ,
1108+ ) ,
1109+ Stream . runDrain ,
1110+ ) ;
1111+ assert . equal ( harness . createWorktree . mock . calls . length , 1 ) ;
1112+ assert . equal (
1113+ harness . createWorktree . mock . calls [ 0 ] ?. [ 0 ] ?. refName ,
1114+ testCase . startFromOrigin ? "remote-main-sha" : testCase . baseRef ,
1115+ ) ;
1116+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
1117+ } ) ,
1118+ ) ;
1119+
10201120it . effect . each ( [ "stable" , "upstream/release" ] ) (
10211121 "launches from %s with the fetched commit and remote PR base" ,
10221122 ( baseRef ) =>
0 commit comments