1+ import { scopeProjectRef } from "@t3tools/client-runtime/environment" ;
12import {
3+ AuthSourceControlWriteScope ,
24 EnvironmentId ,
35 ProjectId ,
46 ThreadId ,
7+ type AuthSessionState ,
58 type ScopedThreadRef ,
69 type ThreadPullRequestLink ,
10+ type VcsRef ,
711} from "@t3tools/contracts" ;
12+ import * as Effect from "effect/Effect" ;
813import { AsyncResult , Atom } from "effect/reactivity" ;
914import { expect , it , vi } from "vite-plus/test" ;
1015import { page } from "vite-plus/test/browser" ;
@@ -16,6 +21,9 @@ const state = vi.hoisted(() => ({
1621 folder : "" ,
1722 pullRequests : [ ] as unknown [ ] ,
1823 configLoaded : true ,
24+ hasServerThread : true ,
25+ statusAtom : null as unknown ,
26+ refsAtom : null as unknown ,
1927} ) ) ;
2028
2129// The read of t3.json is a cache: a re-read is the only way to see what is on disk now.
@@ -59,46 +67,73 @@ vi.mock(import("~/components/files/projectFilesQueryState"), async (importOrigin
5967 } ;
6068} ) ;
6169
62- vi . mock ( import ( "~/state/entities" ) , async ( importOriginal ) => ( {
63- ...( await importOriginal ( ) ) ,
64- useProject : ( ( ref : unknown ) =>
65- ref === null
66- ? null
67- : {
68- id : "project" ,
69- environmentId : "environment" ,
70- title : state . workspaceRoot === SCRATCH_ROOT ? "No project" : "plain" ,
71- workspaceRoot : state . workspaceRoot ,
72- scripts : [ ] ,
73- } ) as never ,
74- useProjects : ( ( ) => [ ] ) as never ,
75- useQuietThreadShell : ( ( ref : unknown ) =>
76- ref === null
70+ vi . mock ( import ( "~/state/entities" ) , async ( importOriginal ) => {
71+ const threadShell = ( ref : unknown ) =>
72+ ref === null || ! state . hasServerThread
7773 ? null
7874 : {
7975 id : "thread" ,
8076 environmentId : "environment" ,
8177 projectId : "project" ,
8278 worktreePath : state . folder ,
8379 branch : null ,
80+ runtime : null ,
8481 pullRequests : state . pullRequests ,
85- } ) as never ,
86- useServerConfigs : ( ( ) =>
87- new Map (
88- state . configLoaded
89- ? [
90- [
91- EnvironmentId . make ( "environment" ) ,
92- {
93- scratchWorkspaceRoot : SCRATCH_ROOT ,
94- environment : { capabilities : { pullRequests : true } } ,
95- } ,
96- ] ,
97- ]
98- : [ ] ,
99- ) ) as never ,
100- } ) ) ;
101- // Neither folder is a repository, which is what the server reports for both.
82+ } ;
83+ return {
84+ ...( await importOriginal ( ) ) ,
85+ useProject : ( ( ref : unknown ) =>
86+ ref === null
87+ ? null
88+ : {
89+ id : "project" ,
90+ environmentId : "environment" ,
91+ title : state . workspaceRoot === SCRATCH_ROOT ? "No project" : "plain" ,
92+ workspaceRoot : state . workspaceRoot ,
93+ scripts : [ ] ,
94+ } ) as never ,
95+ useProjects : ( ( ) => [ ] ) as never ,
96+ useQuietThreadShell : threadShell as never ,
97+ readThreadShell : threadShell as never ,
98+ useServerConfigs : ( ( ) =>
99+ new Map (
100+ state . configLoaded
101+ ? [
102+ [
103+ EnvironmentId . make ( "environment" ) ,
104+ {
105+ scratchWorkspaceRoot : SCRATCH_ROOT ,
106+ environment : { capabilities : { pullRequests : true } } ,
107+ } ,
108+ ] ,
109+ ]
110+ : [ ] ,
111+ ) ) as never ,
112+ } ;
113+ } ) ;
114+ vi . mock ( import ( "~/state/session" ) , async ( importOriginal ) => {
115+ const actual = await importOriginal ( ) ;
116+ const session = Atom . make (
117+ AsyncResult . success ( {
118+ authenticated : true ,
119+ scopes : [ AuthSourceControlWriteScope ] ,
120+ auth : {
121+ policy : "remote-reachable" ,
122+ bootstrapMethods : [ "one-time-token" ] ,
123+ sessionMethods : [ "bearer-access-token" ] ,
124+ sessionCookieName : "t3_session" ,
125+ } ,
126+ } as const satisfies AuthSessionState ) ,
127+ ) ;
128+ return {
129+ ...actual ,
130+ environmentSession : {
131+ ...actual . environmentSession ,
132+ sessionStateAtom : ( ( ) => session ) as never ,
133+ } ,
134+ } ;
135+ } ) ;
136+ // Neither folder is a repository by default, which is what the server reports for both.
102137vi . mock ( import ( "~/state/vcs" ) , async ( importOriginal ) => {
103138 const actual = await importOriginal ( ) ;
104139 const notARepo = Atom . make (
@@ -118,11 +153,25 @@ vi.mock(import("~/state/vcs"), async (importOriginal) => {
118153 ) ;
119154 return {
120155 ...actual ,
121- vcsEnvironment : { ...actual . vcsEnvironment , status : ( ( ) => notARepo ) as never } ,
156+ vcsEnvironment : {
157+ ...actual . vcsEnvironment ,
158+ status : ( ( ) => state . statusAtom ?? notARepo ) as never ,
159+ listRefs : ( ( ) => state . refsAtom ) as never ,
160+ refreshLocalStatusForPicker : {
161+ ...actual . vcsEnvironment . refreshLocalStatusForPicker ,
162+ run : ( ) => Effect . void ,
163+ } as never ,
164+ switchRef : {
165+ ...actual . vcsEnvironment . switchRef ,
166+ run : ( _registry : unknown , target : { input : { refName : string } } ) =>
167+ Effect . succeed ( { refName : target . input . refName } ) ,
168+ } as never ,
169+ } ,
122170 } ;
123171} ) ;
124172
125173import { render } from "../../../test/render" ;
174+ import { DraftId , useComposerDraftStore } from "../../composerDraftStore" ;
126175import { useRightPanelStore } from "../../rightPanelStore" ;
127176import { PopoverCreateHandle } from "../ui/popover" ;
128177import { ChatCanvasContext } from "./ChatCanvasContext" ;
@@ -151,18 +200,53 @@ function setup(input: {
151200 folder : string ;
152201 pullRequests ?: ThreadPullRequestLink [ ] ;
153202 configLoaded ?: boolean ;
203+ hasServerThread ?: boolean ;
204+ repo ?: { currentBranch : string ; refs : readonly VcsRef [ ] } ;
205+ draftId ?: DraftId ;
154206} ) {
155207 state . configLoaded = input . configLoaded ?? true ;
156208 state . workspaceRoot = input . workspaceRoot ;
157209 state . folder = input . folder ;
158210 state . pullRequests = input . pullRequests ?? [ ] ;
211+ state . hasServerThread = input . hasServerThread ?? true ;
212+ state . statusAtom = input . repo
213+ ? Atom . make (
214+ AsyncResult . success ( {
215+ isRepo : true ,
216+ hasPrimaryRemote : true ,
217+ isDefaultRef : false ,
218+ refName : input . repo . currentBranch ,
219+ hasWorkingTreeChanges : false ,
220+ workingTree : { rootPath : input . workspaceRoot , files : [ ] , insertions : 0 , deletions : 0 } ,
221+ hasUpstream : false ,
222+ aheadCount : 0 ,
223+ behindCount : 0 ,
224+ aheadOfDefaultCount : 0 ,
225+ pr : null ,
226+ } ) ,
227+ )
228+ : null ;
229+ state . refsAtom = input . repo
230+ ? Atom . make (
231+ AsyncResult . success ( {
232+ refs : input . repo . refs ,
233+ isRepo : true ,
234+ hasPrimaryRemote : true ,
235+ nextCursor : null ,
236+ totalCount : input . repo . refs . length ,
237+ } ) ,
238+ )
239+ : null ;
159240 useRightPanelStore . setState ( { threadPanelVisibilityByThreadKey : { } } ) ;
160241 const anchor = document . createElement ( "div" ) ;
161242 document . body . append ( anchor ) ;
162243 return {
163244 anchor : { current : anchor } ,
164245 handle : PopoverCreateHandle ( ) ,
165- [ Symbol . dispose ] : ( ) => anchor . remove ( ) ,
246+ [ Symbol . dispose ] : ( ) => {
247+ if ( input . draftId ) useComposerDraftStore . getState ( ) . clearDraftThread ( input . draftId ) ;
248+ anchor . remove ( ) ;
249+ } ,
166250 } ;
167251}
168252
@@ -176,13 +260,23 @@ const machines = (["This Mac", "Server"] as const).map((label, index) => ({
176260
177261function Panel ( {
178262 fixture,
263+ draftId,
179264 draftEnvMode,
265+ worktreePath,
266+ isGitRepo = false ,
267+ offersWorkspaceModes = false ,
268+ forceNewWorktree = false ,
180269 onMachines = false ,
181270 withScripts = false ,
182271} : {
183272 fixture : ReturnType < typeof setup > ;
273+ draftId ?: DraftId ;
184274 /** Renders an unsent draft in this mode instead of a started thread. */
185275 draftEnvMode ?: "local" | "worktree" ;
276+ worktreePath ?: string | null ;
277+ isGitRepo ?: boolean ;
278+ offersWorkspaceModes ?: boolean ;
279+ forceNewWorktree ?: boolean ;
186280 onMachines ?: boolean ;
187281 withScripts ?: boolean ;
188282} ) {
@@ -208,8 +302,15 @@ function Panel({
208302 environmentId = { threadRef . environmentId }
209303 threadId = { threadRef . threadId }
210304 projectId = { ProjectId . make ( "project" ) }
211- composerDraftTarget = { threadRef }
212- worktreePath = { draftEnvMode === undefined ? state . folder : null }
305+ { ...( draftId ? { draftId } : { } ) }
306+ composerDraftTarget = { draftId ?? threadRef }
307+ worktreePath = {
308+ worktreePath !== undefined
309+ ? worktreePath
310+ : draftEnvMode === undefined
311+ ? state . folder
312+ : null
313+ }
213314 isServerThread = { draftEnvMode === undefined }
214315 activeProjectName = { state . workspaceRoot === SCRATCH_ROOT ? "No project" : "plain" }
215316 activeProjectScripts = { withScripts ? [ ] : undefined }
@@ -220,8 +321,9 @@ function Panel({
220321 openInShortcutEnabled = { false }
221322 gitCwd = { state . folder }
222323 workspaceMutationId = { null }
223- isGitRepo = { false }
224- offersWorkspaceModes = { false }
324+ isGitRepo = { isGitRepo }
325+ offersWorkspaceModes = { offersWorkspaceModes }
326+ forceNewWorktree = { forceNewWorktree }
225327 envLocked = { draftEnvMode === undefined }
226328 availableEnvironments = { onMachines ? machines : [ ] }
227329 { ...( onMachines ? { onEnvironmentChange : noop } : { } ) }
@@ -316,3 +418,56 @@ it("lists the t3.json actions on disk when the actions menu opens again after th
316418 await chooser . click ( ) ;
317419 await expect . element ( page . getByRole ( "menuitem" , { name : / D e v 4 2 0 0 / } ) ) . toBeVisible ( ) ;
318420} ) ;
421+
422+ it ( "updates the draft base branch instead of checking out a previous worktree when forceNewWorktree is set" , async ( ) => {
423+ const worktreePath = "/work/repo/.t3/worktrees/prev" ;
424+ const draftId = DraftId . make ( "multi-model-draft" ) ;
425+ using fixture = setup ( {
426+ workspaceRoot : "/work/repo" ,
427+ folder : worktreePath ,
428+ hasServerThread : false ,
429+ repo : {
430+ currentBranch : "t3/previous" ,
431+ refs : [
432+ { name : "t3/previous" , current : true , isDefault : false , worktreePath } ,
433+ { name : "feature" , current : false , isDefault : false , worktreePath : null } ,
434+ { name : "main" , current : false , isDefault : true , worktreePath : "/work/repo" } ,
435+ ] ,
436+ } ,
437+ draftId,
438+ } ) ;
439+ const store = useComposerDraftStore . getState ( ) ;
440+ store . setProjectDraftThreadId (
441+ scopeProjectRef ( threadRef . environmentId , ProjectId . make ( "project" ) ) ,
442+ draftId ,
443+ ) ;
444+ store . setDraftThreadContext ( draftId , {
445+ branch : "t3/previous" ,
446+ worktreePath,
447+ envMode : "worktree" ,
448+ } ) ;
449+
450+ await using _screen = await render (
451+ < Panel
452+ fixture = { fixture }
453+ draftId = { draftId }
454+ draftEnvMode = "worktree"
455+ worktreePath = { worktreePath }
456+ isGitRepo
457+ offersWorkspaceModes
458+ forceNewWorktree
459+ /> ,
460+ ) ;
461+
462+ const branchTrigger = page . getByRole ( "combobox" , { name : / ^ B r a n c h : / } ) ;
463+ await expect . element ( branchTrigger ) . toHaveAccessibleName ( "Branch: From t3/previous" ) ;
464+ await branchTrigger . click ( ) ;
465+ await page . getByRole ( "option" , { name : "feature" , exact : true } ) . click ( ) ;
466+
467+ await expect . element ( branchTrigger ) . toHaveAccessibleName ( "Branch: From feature" ) ;
468+ expect ( useComposerDraftStore . getState ( ) . getDraftSession ( draftId ) ) . toMatchObject ( {
469+ branch : "feature" ,
470+ worktreePath : null ,
471+ envMode : "worktree" ,
472+ } ) ;
473+ } ) ;
0 commit comments