@@ -4,6 +4,7 @@ import type { VcsListRefsResult } from "@t3tools/contracts";
44import { executeAtomQuery } from "@t3tools/client-runtime/state/runtime" ;
55import { resolveAutoFeatureBranchName } from "@t3tools/shared/git" ;
66import * as Effect from "effect/Effect" ;
7+ import * as Stream from "effect/Stream" ;
78import { AsyncResult , Atom } from "effect/reactivity" ;
89import { afterAll , afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
910
@@ -18,7 +19,9 @@ const harness = vi.hoisted(() => ({
1819 renderCount : 0 ,
1920 onRefreshSettled : ( ) => { } ,
2021 branchAtom : null as Atom . Atom < AsyncResult . AsyncResult < VcsListRefsResult , unknown > > | null ,
22+ refsQueryAtom : null as Atom . Atom < AsyncResult . AsyncResult < VcsListRefsResult , unknown > > | null ,
2123 listRefs : vi . fn ( ) ,
24+ refsQuery : vi . fn ( ) ,
2225 showGitActionResult : vi . fn ( ) ,
2326} ) ) ;
2427
@@ -34,6 +37,7 @@ vi.mock("./use-atom-command", () => ({
3437} ) ) ;
3538vi . mock ( "./vcs" , ( ) => ( {
3639 vcsEnvironment : { refreshStatus : "refreshStatus" , listRefs : harness . listRefs } ,
40+ vcsRefsQuery : harness . refsQuery ,
3741 vcsActionManager : { runStackedAction : ( ) => "runStackedAction" , track : vi . fn ( ) } ,
3842} ) ) ;
3943vi . mock ( "./threads" , ( ) => ( { threadEnvironment : { updateMetadata : "updateMetadata" } } ) ) ;
@@ -98,6 +102,7 @@ beforeEach(() => {
98102 harness . renderCount = 0 ;
99103 harness . onRefreshSettled = ( ) => rerender ( ) ;
100104 harness . listRefs . mockReset ( ) . mockImplementation ( ( ) => harness . branchAtom ) ;
105+ harness . refsQuery . mockReset ( ) . mockImplementation ( ( ) => harness . refsQueryAtom ) ;
101106 harness . showGitActionResult . mockClear ( ) ;
102107} ) ;
103108
@@ -121,9 +126,18 @@ describe("useSelectedThreadGitActions", () => {
121126 totalCount : 0 ,
122127 } ;
123128 let response = cached ? Promise . resolve ( empty ) : request . promise ;
124- harness . branchAtom = Atom . make ( Effect . tryPromise ( ( ) => response ) ) . pipe ( Atom . keepAlive ) ;
129+ const fetchRefs = Effect . tryPromise ( ( ) => response ) ;
130+ harness . branchAtom = Atom . make (
131+ Stream . concat ( Stream . fromEffect ( fetchRefs ) , Stream . never ) ,
132+ ) . pipe ( Atom . keepAlive ) ;
133+ harness . refsQueryAtom = Atom . make ( fetchRefs ) . pipe ( Atom . keepAlive ) ;
134+ appAtomRegistry . get ( harness . branchAtom ) ;
125135 if ( cached ) {
126- await executeAtomQuery ( appAtomRegistry , harness . branchAtom ) ;
136+ await vi . waitFor ( ( ) =>
137+ expect ( appAtomRegistry . get ( harness . branchAtom ) . _tag ) . toBe ( "Success" ) ,
138+ ) ;
139+ expect ( appAtomRegistry . get ( harness . branchAtom ) . waiting ) . toBe ( true ) ;
140+ await executeAtomQuery ( appAtomRegistry , harness . refsQueryAtom ) ;
127141 response = request . promise ;
128142 }
129143 root = createRoot ( document . createElement ( "div" ) ) ;
@@ -136,10 +150,6 @@ describe("useSelectedThreadGitActions", () => {
136150 } ) ;
137151 await flush ( ) ;
138152 expect ( settled ) . toBe ( false ) ;
139- expect ( harness . listRefs ) . toHaveBeenCalledWith ( {
140- environmentId : "environment-1" ,
141- input : { cwd : "/repo" , limit : 100 } ,
142- } ) ;
143153 const local = {
144154 name : "feature/update" ,
145155 isRemote : false ,
@@ -157,7 +167,16 @@ describe("useSelectedThreadGitActions", () => {
157167 hasPrimaryRemote : true ,
158168 totalCount : 2 ,
159169 } ) ;
170+ await vi . waitFor ( ( ) => expect ( settled ) . toBe ( true ) ) ;
171+ expect ( appAtomRegistry . get ( harness . branchAtom ) ) . toMatchObject ( {
172+ _tag : "Success" ,
173+ waiting : true ,
174+ } ) ;
160175 const branches = await refresh ;
176+ expect ( harness . refsQuery ) . toHaveBeenCalledWith ( {
177+ environmentId : "environment-1" ,
178+ input : { cwd : "/repo" , limit : 100 } ,
179+ } ) ;
161180 expect ( branches ) . toEqual ( [ local ] ) ;
162181 expect ( resolveAutoFeatureBranchName ( branches ! . map ( ( branch ) => branch . name ) ) ) . toBe (
163182 "feature/update-2" ,
@@ -174,6 +193,7 @@ describe("useSelectedThreadGitActions", () => {
174193 catch : ( error ) => error ,
175194 } ) ,
176195 ) ;
196+ harness . refsQueryAtom = harness . branchAtom ;
177197 root = createRoot ( document . createElement ( "div" ) ) ;
178198 act ( ( ) => root ! . render ( < Consumer /> ) ) ;
179199 const refresh = actions . refreshSelectedThreadBranches ( ) ;
0 commit comments