@@ -25,6 +25,7 @@ import {
2525 ScheduledTaskId ,
2626 type ServerProvider ,
2727 ThreadId ,
28+ TextGenerationError ,
2829} from "@t3tools/contracts" ;
2930import * as Cause from "effect/Cause" ;
3031import * as Deferred from "effect/Deferred" ;
@@ -914,6 +915,82 @@ const preparationSettled = (threadId: ThreadId) =>
914915 return yield * threads . getThreadProjection ( threadId ) ;
915916 } ) ;
916917
918+ it . effect . each ( [
919+ { prefix : "my-prefix" , collision : null , expected : / ^ m y - p r e f i x \/ [ 0 - 9 a - f ] { 8 } $ / } ,
920+ { prefix : "my-prefix" , collision : "my-prefix" , expected : / ^ m y - p r e f i x - [ 0 - 9 a - f ] { 8 } $ / } ,
921+ { prefix : "team/topic" , collision : "team" , expected : / ^ t e a m - t o p i c - [ 0 - 9 a - f ] { 8 } $ / } ,
922+ { prefix : "team/topic" , collision : "team/topic" , expected : / ^ t e a m - t o p i c - [ 0 - 9 a - f ] { 8 } $ / } ,
923+ { prefix : "" , collision : null , expected : / ^ [ 0 - 9 a - f ] { 8 } $ / } ,
924+ ] ) ( "keeps prefix '$prefix' with collision '$collision' when naming fails" , ( scenario ) =>
925+ Effect . gen ( function * ( ) {
926+ const namingAttempted = yield * Deferred . make < void > ( ) ;
927+ const harness = makeHarness ( {
928+ serverSettings : {
929+ branchNamePrefix : "global-prefix" ,
930+ projectSettingsOverrides : {
931+ [ projectId ] : { branchNamingMode : "static" , branchNamePrefix : scenario . prefix } ,
932+ } ,
933+ } ,
934+ hasCommit : ( { refName } ) => Effect . succeed ( refName === `refs/heads/${ scenario . collision } ` ) ,
935+ generateBranchName : ( ) =>
936+ Deferred . succeed ( namingAttempted , undefined ) . pipe (
937+ Effect . andThen (
938+ Effect . fail (
939+ new TextGenerationError ( { operation : "generateBranchName" , detail : "Unavailable" } ) ,
940+ ) ,
941+ ) ,
942+ ) ,
943+ } ) ;
944+ yield * Effect . gen ( function * ( ) {
945+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
946+ const launched = yield * launches . launch (
947+ launchInput ( {
948+ command : "prefix-launch" ,
949+ thread : "prefix-thread" ,
950+ message : "Fix the parser" ,
951+ workspace : { type : "worktree" , baseRef : "main" } ,
952+ } ) ,
953+ ) ;
954+ const projection = yield * preparationSettled ( launched . threadId ) ;
955+ assert . match ( projection . thread . branch ! , scenario . expected ) ;
956+ yield * Deferred . await ( namingAttempted ) ;
957+ assert . equal ( projection . runs [ 0 ] ?. status , "starting" ) ;
958+ assert . equal ( harness . createWorktree . mock . calls [ 0 ] ?. [ 0 ] ?. newRefName , projection . thread . branch ) ;
959+ assert . equal ( harness . renameBranch . mock . calls . length , 0 ) ;
960+ assert . isFalse ( projection . turnItems . some ( ( item ) => item . type === "error" ) ) ;
961+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
962+ } ) ,
963+ ) ;
964+
965+ it . effect ( "renames a temporary branch with a configured prefix when generation succeeds" , ( ) => {
966+ const harness = makeHarness ( {
967+ serverSettings : { branchNamingMode : "static" , branchNamePrefix : "my-prefix" } ,
968+ generateBranchName : ( ) => Effect . succeed ( { branch : "my-prefix/fix-parser" } ) ,
969+ } ) ;
970+ return Effect . gen ( function * ( ) {
971+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
972+ const threads = yield * ThreadManagement . ThreadManagementService ;
973+ const launched = yield * launches . launch (
974+ launchInput ( {
975+ command : "prefix-rename" ,
976+ thread : "prefix-rename-thread" ,
977+ message : "Fix the parser" ,
978+ workspace : { type : "worktree" , baseRef : "main" } ,
979+ } ) ,
980+ ) ;
981+ yield * threads . streamStoredEventsFrom ( { threadId : launched . threadId } ) . pipe (
982+ Stream . filter ( ( stored ) => stored . commandId === "prefix-rename:branch-rename" ) ,
983+ Stream . runHead ,
984+ ) ;
985+ const projection = yield * threads . getThreadProjection ( launched . threadId ) ;
986+ assert . equal ( projection . thread . branch , "my-prefix/fix-parser" ) ;
987+ assert . match (
988+ harness . renameBranch . mock . calls [ 0 ] ?. [ 0 ] ?. oldBranch ?? "" ,
989+ / ^ m y - p r e f i x \/ [ 0 - 9 a - f ] { 8 } $ / ,
990+ ) ;
991+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
992+ } ) ;
993+
917994function scheduledTaskInput ( id : string , targetProjectId : ProjectId ) {
918995 return {
919996 id : ScheduledTaskId . make ( id ) ,
0 commit comments