@@ -18,11 +18,14 @@ import {
1818import * as NetService from "@t3tools/shared/Net" ;
1919import * as DateTime from "effect/DateTime" ;
2020import * as Effect from "effect/Effect" ;
21+ import * as Fiber from "effect/Fiber" ;
2122import * as FileSystem from "effect/FileSystem" ;
2223import * as Layer from "effect/Layer" ;
2324import * as References from "effect/References" ;
2425import * as Stream from "effect/Stream" ;
2526import { Command } from "effect/cli" ;
27+ import { FetchHttpClient } from "effect/http" ;
28+ import * as TestClock from "effect/testing/TestClock" ;
2629
2730import * as ServerSecretStore from "../auth/ServerSecretStore.ts" ;
2831import { cli } from "../binCli.ts" ;
@@ -37,6 +40,10 @@ import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts";
3740import * as ProjectService from "../project/ProjectService.ts" ;
3841import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts" ;
3942import * as T3ProjectFileLoader from "../project/T3ProjectFileLoader.ts" ;
43+ import {
44+ makePersistedServerRuntimeState ,
45+ persistServerRuntimeState ,
46+ } from "../serverRuntimeState.ts" ;
4047import * as ServerSettings from "../serverSettings.ts" ;
4148import * as WorkspacePaths from "../workspace/WorkspacePaths.ts" ;
4249
@@ -121,6 +128,66 @@ it.effect("adds, renames, and removes projects through the V2 project CLI domain
121128 } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
122129) ;
123130
131+ it . effect . each ( [ "snapshot" , "mutation" ] as const ) (
132+ "waits for a busy live server's %s response" ,
133+ ( slowRequest ) =>
134+ Effect . gen ( function * ( ) {
135+ const fs = yield * FileSystem . FileSystem ;
136+ const root = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-live-project-cli-" } ) ;
137+ const baseDir = NodePath . join ( root , "state" ) ;
138+ const workspaceRoot = NodePath . join ( root , "workspace" ) ;
139+ yield * fs . makeDirectory ( workspaceRoot ) ;
140+ yield * persistServerRuntimeState ( {
141+ path : NodePath . join ( baseDir , "userdata" , "server-runtime.json" ) ,
142+ state : yield * makePersistedServerRuntimeState ( {
143+ config : { host : "127.0.0.1" , devUrl : undefined } ,
144+ port : 3_773 ,
145+ } ) ,
146+ } ) ;
147+ const now = "2026-01-01T00:00:00.000Z" ;
148+ const requestReceived = Promise . withResolvers < void > ( ) ;
149+ const releaseResponse = Promise . withResolvers < void > ( ) ;
150+ let snapshotRequests = 0 ;
151+ let mutations = 0 ;
152+ const fetch : typeof globalThis . fetch = async ( _input , init ) => {
153+ const isMutation = init ?. method === "POST" ;
154+ if ( isMutation ) mutations ++ ;
155+ else snapshotRequests ++ ;
156+ if ( slowRequest === "mutation" ? isMutation : ! isMutation && snapshotRequests === 2 ) {
157+ requestReceived . resolve ( ) ;
158+ await releaseResponse . promise ;
159+ }
160+ return Response . json (
161+ isMutation
162+ ? {
163+ id : "live-project" ,
164+ title : NodePath . basename ( workspaceRoot ) ,
165+ workspaceRoot,
166+ defaultModelSelection : null ,
167+ scripts : [ ] ,
168+ createdAt : now ,
169+ updatedAt : now ,
170+ deletedAt : null ,
171+ }
172+ : { projects : [ ] , updatedAt : now } ,
173+ ) ;
174+ } ;
175+
176+ const add = yield * runCli ( [ "project" , "add" , workspaceRoot , "--base-dir" , baseDir ] ) . pipe (
177+ Effect . provideService ( FetchHttpClient . Fetch , fetch ) ,
178+ Effect . forkChild ,
179+ ) ;
180+ yield * Effect . promise ( ( ) => requestReceived . promise ) ;
181+ yield * TestClock . adjust ( "2 seconds" ) ;
182+ releaseResponse . resolve ( ) ;
183+ yield * Fiber . join ( add ) ;
184+
185+ assert . equal ( snapshotRequests , 2 ) ;
186+ assert . equal ( mutations , 1 ) ;
187+ assert . deepEqual ( ( yield * readProjects ( baseDir ) ) . projects , [ ] ) ;
188+ } ) . pipe ( Effect . provide ( NodeServices . layer ) , Effect . scoped ) ,
189+ ) ;
190+
124191const makeProjectLookupFixture = Effect . fn ( "ProjectCliTest.makeProjectLookupFixture" ) ( function * ( ) {
125192 const fs = yield * FileSystem . FileSystem ;
126193 const root = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-v2-project-lookup-" } ) ;
0 commit comments