11import { AuthOrchestrationOperateScope , AuthSourceControlWriteScope } from "@t3tools/contracts" ;
2+ import type { VcsListRefsResult } from "@t3tools/contracts" ;
3+ import { resolveAutoFeatureBranchName } from "@t3tools/shared/git" ;
24import * as Cause from "effect/Cause" ;
35import { AsyncResult } from "effect/reactivity" ;
46import { beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
@@ -20,6 +22,8 @@ const state = vi.hoisted(() => ({
2022 results : [ ] as { type : string ; description ?: string } [ ] ,
2123 afterGitAction : undefined as ( ( ) => void ) | undefined ,
2224 statusRefreshes : 0 ,
25+ readRefs : vi . fn ( ) ,
26+ cachedRefs : [ ] as VcsListRefsResult [ "refs" ] ,
2327} ) ) ;
2428
2529vi . mock ( "react" , ( ) => ( {
@@ -46,7 +50,7 @@ vi.mock("./use-selected-thread-worktree", () => ({
4650 } ) ,
4751} ) ) ;
4852vi . mock ( "./queries" , ( ) => ( {
49- useBranches : ( ) => ( { data : { refs : [ ] } , refresh : ( ) => { } } ) ,
53+ useBranches : ( ) => ( { data : { refs : state . cachedRefs } , refresh : ( ) => { } } ) ,
5054} ) ) ;
5155vi . mock ( "./use-atom-command" , ( ) => ( { useAtomCommand : ( command : unknown ) => command } ) ) ;
5256vi . mock ( "./atom-registry" , ( ) => ( { appAtomRegistry : { } } ) ) ;
@@ -79,6 +83,7 @@ vi.mock("./threads", () => ({
7983} ) ) ;
8084vi . mock ( "./vcs" , ( ) => ( {
8185 vcsEnvironment : {
86+ readRefs : state . readRefs ,
8287 refreshStatus : async ( ) => {
8388 state . statusRefreshes += 1 ;
8489 return AsyncResult . success ( { refName : state . branch } ) ;
@@ -231,3 +236,71 @@ describe("thread Git mutation permissions", () => {
231236 } ,
232237 ) ;
233238} ) ;
239+
240+ describe ( "automatic feature branch refs" , ( ) => {
241+ const empty : VcsListRefsResult = {
242+ refs : [ ] ,
243+ isRepo : true ,
244+ hasPrimaryRemote : true ,
245+ nextCursor : null ,
246+ totalCount : 0 ,
247+ } ;
248+
249+ it . each ( [ false , true ] ) ( "awaits fresh refs before naming, cached: %s" , async ( cached ) => {
250+ const request = Promise . withResolvers < VcsListRefsResult > ( ) ;
251+ state . cachedRefs = cached
252+ ? [
253+ {
254+ name : "old-branch" ,
255+ isRemote : false ,
256+ current : false ,
257+ isDefault : false ,
258+ worktreePath : null ,
259+ } ,
260+ ]
261+ : [ ] ;
262+ state . readRefs . mockReturnValue ( request . promise . then ( AsyncResult . success ) ) ;
263+ let settled = false ;
264+ const refresh = useSelectedThreadGitActions ( )
265+ . refreshSelectedThreadBranches ( )
266+ . then ( ( refs ) => {
267+ settled = true ;
268+ return refs ;
269+ } ) ;
270+ await Promise . resolve ( ) ;
271+ expect ( settled ) . toBe ( false ) ;
272+ const local = {
273+ name : "feature/update" ,
274+ isRemote : false ,
275+ current : false ,
276+ isDefault : false ,
277+ worktreePath : null ,
278+ } ;
279+ request . resolve ( {
280+ ...empty ,
281+ refs : [
282+ local ,
283+ { ...local , name : "origin/feature/update" , isRemote : true , remoteName : "origin" } ,
284+ ] ,
285+ totalCount : 2 ,
286+ } ) ;
287+ const refs = await refresh ;
288+ expect ( refs ) . toEqual ( [ local ] ) ;
289+ expect ( resolveAutoFeatureBranchName ( refs ! . map ( ( ref ) => ref . name ) ) ) . toBe ( "feature/update-2" ) ;
290+ expect ( state . readRefs ) . toHaveBeenCalledWith ( {
291+ environmentId : "environment" ,
292+ input : { cwd : "/repo" , limit : 100 , refresh : true } ,
293+ } ) ;
294+ } ) ;
295+
296+ it ( "reports lookup failure instead of returning empty refs" , async ( ) => {
297+ state . results = [ ] ;
298+ state . readRefs . mockResolvedValue (
299+ AsyncResult . failure ( Cause . fail ( new Error ( "Cannot list refs" ) ) ) ,
300+ ) ;
301+ expect ( await useSelectedThreadGitActions ( ) . refreshSelectedThreadBranches ( ) ) . toBeNull ( ) ;
302+ expect ( state . results ) . toEqual ( [
303+ { type : "error" , title : "Git action failed" , description : "Cannot list refs" } ,
304+ ] ) ;
305+ } ) ;
306+ } ) ;
0 commit comments