@@ -6,6 +6,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
66import { emitUserPipelineWritten } from "@/utils/userPipelineWriteEvents" ;
77
88import { OnboardingProvider , useOnboarding } from "./OnboardingProvider" ;
9+ import { ONBOARDING_MY_RUN_COUNT_KEY } from "./onboardingQueryKeys" ;
910
1011const fetchWithErrorHandling = vi . hoisted ( ( ) =>
1112 vi . fn < ( url : string , options ?: RequestInit ) => Promise < unknown > > ( ) ,
@@ -49,6 +50,21 @@ function render() {
4950 return renderHook ( ( ) => useOnboarding ( ) , { wrapper } ) ;
5051}
5152
53+ function renderWithClient ( ) {
54+ const queryClient = new QueryClient ( {
55+ defaultOptions : { queries : { retry : false } } ,
56+ } ) ;
57+ const clientWrapper = ( { children } : { children : ReactNode } ) => (
58+ < QueryClientProvider client = { queryClient } >
59+ < OnboardingProvider > { children } </ OnboardingProvider >
60+ </ QueryClientProvider >
61+ ) ;
62+ return {
63+ ...renderHook ( ( ) => useOnboarding ( ) , { wrapper : clientWrapper } ) ,
64+ queryClient,
65+ } ;
66+ }
67+
5268function completedSteps ( result : { current : ReturnType < typeof useOnboarding > } ) {
5369 return result . current . steps
5470 . filter ( ( step ) => step . completed )
@@ -110,6 +126,37 @@ describe("OnboardingProvider", () => {
110126 expect ( patched ( ) ) . toBe ( false ) ;
111127 } ) ;
112128
129+ it ( "refreshes execute_run when the run-count query is invalidated after a run is created" , async ( ) => {
130+ const { result, queryClient } = renderWithClient ( ) ;
131+
132+ await waitFor ( ( ) => expect ( result . current . total ) . toBe ( 4 ) ) ;
133+ expect ( completedSteps ( result ) ) . not . toContain ( "execute_run" ) ;
134+
135+ // A run now exists, and the run-create mutation path invalidates the
136+ // shared onboarding run-count key — execute_run should flip without
137+ // waiting out the query's stale window.
138+ runsPayload = { pipeline_runs : [ { id : "run-1" } ] } ;
139+ await act ( async ( ) => {
140+ await queryClient . invalidateQueries ( {
141+ queryKey : ONBOARDING_MY_RUN_COUNT_KEY ,
142+ } ) ;
143+ } ) ;
144+
145+ await waitFor ( ( ) =>
146+ expect ( completedSteps ( result ) ) . toContain ( "execute_run" ) ,
147+ ) ;
148+ expect ( patched ( ) ) . toBe ( false ) ;
149+ } ) ;
150+
151+ it ( "treats a malformed run-count payload as zero runs" , async ( ) => {
152+ runsPayload = { pipeline_runs : "not-an-array" } ;
153+
154+ const { result } = render ( ) ;
155+
156+ await waitFor ( ( ) => expect ( result . current . total ) . toBe ( 4 ) ) ;
157+ expect ( completedSteps ( result ) ) . not . toContain ( "execute_run" ) ;
158+ } ) ;
159+
113160 it ( "persists create_pipeline when the user writes a pipeline" , async ( ) => {
114161 const { result } = render ( ) ;
115162
0 commit comments