@@ -26,6 +26,7 @@ import {
2626 type ServerProvider ,
2727 ThreadId ,
2828 TextGenerationError ,
29+ ServerSettingsError ,
2930} from "@t3tools/contracts" ;
3031import * as Cause from "effect/Cause" ;
3132import * as Deferred from "effect/Deferred" ;
@@ -131,6 +132,7 @@ interface HarnessOptions {
131132 readonly generateTitle ?: TextGeneration . TextGeneration [ "Service" ] [ "generateThreadTitle" ] ;
132133 readonly generateBranchName ?: TextGeneration . TextGeneration [ "Service" ] [ "generateBranchName" ] ;
133134 readonly serverSettings ?: Parameters < typeof ServerSettings . layerTest > [ 0 ] ;
135+ readonly getLaunchSettings ?: ServerSettings . ServerSettingsService [ "Service" ] [ "getSettings" ] ;
134136 readonly providers ?: ReadonlyArray < ServerProvider > ;
135137 readonly git ?: Partial < GitWorkflow . GitWorkflowService [ "Service" ] > ;
136138}
@@ -244,6 +246,11 @@ function makeHarness(options: HarnessOptions = {}) {
244246 layerReceipts ,
245247 layerOrchestrator ,
246248 IdAllocator . layer ,
249+ options . getLaunchSettings === undefined
250+ ? Layer . empty
251+ : Layer . mock ( ServerSettings . ServerSettingsService ) ( {
252+ getSettings : options . getLaunchSettings ,
253+ } ) ,
247254 ) ,
248255 ) ,
249256 ) ;
@@ -969,6 +976,109 @@ const preparationSettled = (threadId: ThreadId) =>
969976 return yield * threads . getThreadProjection ( threadId ) ;
970977 } ) ;
971978
979+ it . effect . each ( [
980+ { prefix : "my-prefix" , collision : null , expected : / ^ m y - p r e f i x \/ [ 0 - 9 a - f ] { 8 } $ / } ,
981+ { prefix : "my-prefix" , collision : "my-prefix" , expected : / ^ m y - p r e f i x - [ 0 - 9 a - f ] { 8 } $ / } ,
982+ { prefix : "team/topic" , collision : "team" , expected : / ^ t e a m - t o p i c - [ 0 - 9 a - f ] { 8 } $ / } ,
983+ ] ) ( "keeps prefix '$prefix' with collision '$collision' when naming fails" , ( scenario ) =>
984+ Effect . gen ( function * ( ) {
985+ const namingAttempted = yield * Deferred . make < void > ( ) ;
986+ const harness = makeHarness ( {
987+ serverSettings : {
988+ branchNamePrefix : "global-prefix" ,
989+ projectSettingsOverrides : {
990+ [ projectId ] : { branchNamingMode : "static" , branchNamePrefix : scenario . prefix } ,
991+ } ,
992+ } ,
993+ hasCommit : ( { refName } ) => Effect . succeed ( refName === `refs/heads/${ scenario . collision } ` ) ,
994+ generateBranchName : ( ) =>
995+ Deferred . succeed ( namingAttempted , undefined ) . pipe (
996+ Effect . andThen (
997+ Effect . fail (
998+ new TextGenerationError ( { operation : "generateBranchName" , detail : "Unavailable" } ) ,
999+ ) ,
1000+ ) ,
1001+ ) ,
1002+ } ) ;
1003+ yield * Effect . gen ( function * ( ) {
1004+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
1005+ const launched = yield * launches . launch (
1006+ launchInput ( {
1007+ command : "prefix-launch" ,
1008+ thread : "prefix-thread" ,
1009+ message : "Fix the parser" ,
1010+ workspace : { type : "worktree" , baseRef : "main" } ,
1011+ } ) ,
1012+ ) ;
1013+ const projection = yield * preparationSettled ( launched . threadId ) ;
1014+ assert . match ( projection . thread . branch ! , scenario . expected ) ;
1015+ yield * Deferred . await ( namingAttempted ) ;
1016+ assert . equal ( projection . runs [ 0 ] ?. status , "starting" ) ;
1017+ assert . equal ( harness . createWorktree . mock . calls [ 0 ] ?. [ 0 ] ?. newRefName , projection . thread . branch ) ;
1018+ assert . equal ( harness . renameBranch . mock . calls . length , 0 ) ;
1019+ assert . isFalse ( projection . turnItems . some ( ( item ) => item . type === "error" ) ) ;
1020+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
1021+ } ) ,
1022+ ) ;
1023+
1024+ it . effect (
1025+ "starts a worktree with the default temporary name when settings secrets cannot be read" ,
1026+ ( ) => {
1027+ const harness = makeHarness ( {
1028+ getLaunchSettings : Effect . fail (
1029+ new ServerSettingsError ( { settingsPath : "/settings.json" , operation : "read-secret" } ) ,
1030+ ) ,
1031+ } ) ;
1032+ return Effect . gen ( function * ( ) {
1033+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
1034+ const launched = yield * launches . launch (
1035+ launchInput ( {
1036+ command : "settings-fallback" ,
1037+ thread : "settings-fallback-thread" ,
1038+ message : "Fix the parser" ,
1039+ workspace : { type : "worktree" , baseRef : "main" } ,
1040+ } ) ,
1041+ ) ;
1042+ const projection = yield * preparationSettled ( launched . threadId ) ;
1043+ assert . equal ( projection . runs [ 0 ] ?. status , "starting" ) ;
1044+ assert . match ( projection . thread . branch ! , / ^ t 3 \/ [ 0 - 9 a - f ] { 8 } $ / ) ;
1045+ assert . equal ( harness . createWorktree . mock . calls [ 0 ] ?. [ 0 ] ?. newRefName , projection . thread . branch ) ;
1046+ assert . equal ( harness . runSetup . mock . calls . length , 1 ) ;
1047+ assert . equal ( harness . renameBranch . mock . calls . length , 0 ) ;
1048+ assert . isFalse ( projection . turnItems . some ( ( item ) => item . type === "error" ) ) ;
1049+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
1050+ } ,
1051+ ) ;
1052+
1053+ it . effect ( "renames a temporary branch with a configured prefix when generation succeeds" , ( ) => {
1054+ const harness = makeHarness ( {
1055+ serverSettings : { branchNamingMode : "static" , branchNamePrefix : "my-prefix" } ,
1056+ generateBranchName : ( ) => Effect . succeed ( { branch : "my-prefix/fix-parser" } ) ,
1057+ } ) ;
1058+ return Effect . gen ( function * ( ) {
1059+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
1060+ const threads = yield * ThreadManagement . ThreadManagementService ;
1061+ const launched = yield * launches . launch (
1062+ launchInput ( {
1063+ command : "prefix-rename" ,
1064+ thread : "prefix-rename-thread" ,
1065+ message : "Fix the parser" ,
1066+ workspace : { type : "worktree" , baseRef : "main" } ,
1067+ } ) ,
1068+ ) ;
1069+ yield * threads . streamStoredEventsFrom ( { threadId : launched . threadId } ) . pipe (
1070+ Stream . filter ( ( stored ) => stored . commandId === "prefix-rename:branch-rename" ) ,
1071+ Stream . runHead ,
1072+ ) ;
1073+ const projection = yield * threads . getThreadProjection ( launched . threadId ) ;
1074+ assert . equal ( projection . thread . branch , "my-prefix/fix-parser" ) ;
1075+ assert . match (
1076+ harness . renameBranch . mock . calls [ 0 ] ?. [ 0 ] ?. oldBranch ?? "" ,
1077+ / ^ m y - p r e f i x \/ [ 0 - 9 a - f ] { 8 } $ / ,
1078+ ) ;
1079+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
1080+ } ) ;
1081+
9721082function scheduledTaskInput ( id : string , targetProjectId : ProjectId ) {
9731083 return {
9741084 id : ScheduledTaskId . make ( id ) ,
0 commit comments