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