11import { it , assert } from "@effect/vitest" ;
2+ import { ProjectId , ThreadId } from "@t3tools/contracts" ;
23import * as Net from "@t3tools/shared/Net" ;
34import * as Effect from "effect/Effect" ;
45import * as Deferred from "effect/Deferred" ;
56import * as Fiber from "effect/Fiber" ;
67import * as Layer from "effect/Layer" ;
78import * as Path from "effect/Path" ;
89import * as ProjectScriptPorts from "./ProjectScriptPorts.ts" ;
10+ import * as ProjectService from "./ProjectService.ts" ;
11+ import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts" ;
12+ import * as ServerSettings from "../serverSettings.ts" ;
13+ import { liveThreadShell } from "../mcp/McpToolAccess.testkit.ts" ;
14+
15+ const threadId = ThreadId . make ( "preview-thread" ) ;
16+ const projectId = ProjectId . make ( "preview-project" ) ;
917
1018const testLayer = ( probe : ( port : number ) => Effect . Effect < boolean > ) =>
1119 ProjectScriptPorts . layer . pipe (
@@ -15,7 +23,79 @@ const testLayer = (probe: (port: number) => Effect.Effect<boolean>) =>
1523 } ) ,
1624 ) ,
1725 Layer . provide ( Path . layer ) ,
26+ Layer . provide ( ServerSettings . layerTest ( ) ) ,
27+ Layer . provide (
28+ Layer . mock ( ProjectionStore . ProjectionStoreV2 ) ( {
29+ getThreadShell : ( id ) =>
30+ Effect . succeed (
31+ id === threadId
32+ ? { ...liveThreadShell ( id ) , projectId, worktreePath : "/worktree-119" }
33+ : null ,
34+ ) ,
35+ } ) ,
36+ ) ,
37+ Layer . provide (
38+ Layer . mock ( ProjectService . ProjectService ) ( {
39+ getShell : ( ) =>
40+ Effect . succeedSome ( {
41+ id : projectId ,
42+ title : "Project" ,
43+ workspaceRoot : "/repo" ,
44+ repositoryIdentity : null ,
45+ defaultModelSelection : null ,
46+ scripts : [
47+ {
48+ id : "dev" ,
49+ name : "Dev" ,
50+ command : "vp dev" ,
51+ icon : "play" ,
52+ runOnWorktreeCreate : false ,
53+ previewUrl : "http://localhost:${T3CODE_PREVIEW_PORT}" ,
54+ } ,
55+ {
56+ id : "fixed" ,
57+ name : "Fixed" ,
58+ command : "vp dev" ,
59+ icon : "play" ,
60+ runOnWorktreeCreate : false ,
61+ } ,
62+ ] ,
63+ createdAt : "2026-01-01T00:00:00.000Z" ,
64+ updatedAt : "2026-01-01T00:00:00.000Z" ,
65+ } ) ,
66+ } ) ,
67+ ) ,
68+ ) ;
69+
70+ it . effect ( "ProjectScriptPorts-fabricatedLookup-reservesOnlySavedThreadActions" , ( ) => {
71+ const probes : number [ ] = [ ] ;
72+ return Effect . gen ( function * ( ) {
73+ const service = yield * ProjectScriptPorts . ProjectScriptPorts ;
74+ for ( const cwd of [ "/fabricated-one" , "/fabricated-two" ] ) {
75+ const input = { threadId, cwd, scriptIds : [ "invented" , "fixed" ] } ;
76+ assert . deepEqual ( yield * service . get ( input ) , { ports : { } } ) ;
77+ }
78+ const input = { threadId, cwd : "/worktree-119" , scriptIds : [ "dev" , "invented" , "fixed" ] } ;
79+ assert . deepEqual ( yield * service . get ( input ) , { ports : { dev : 27170 } } ) ;
80+ assert . deepEqual ( yield * service . get ( input ) , { ports : { dev : 27170 } } ) ;
81+ const missing = {
82+ threadId : ThreadId . make ( "missing" ) ,
83+ cwd : "/fabricated-three" ,
84+ scriptIds : [ "invented" ] ,
85+ } ;
86+ assert . deepEqual ( yield * service . get ( missing ) , { ports : { } } ) ;
87+ assert . deepEqual ( probes , [ 27170 ] ) ;
88+ } ) . pipe (
89+ Effect . provide (
90+ testLayer ( ( port ) =>
91+ Effect . sync ( ( ) => {
92+ probes . push ( port ) ;
93+ return true ;
94+ } ) ,
95+ ) ,
96+ ) ,
1897 ) ;
98+ } ) ;
1999
20100it . effect ( "ProjectScriptPorts-concurrentCollisions-keepsDistinctStableAssignments" , ( ) => {
21101 const probes : number [ ] = [ ] ;
@@ -63,12 +143,14 @@ it.effect("ProjectScriptPorts-occupiedPreferredPort-wrapsAndRestartsDeterministi
63143 const layer = testLayer ( ( port ) => Effect . succeed ( port !== 29999 ) ) ;
64144 const allocate = Effect . gen ( function * ( ) {
65145 const service = yield * ProjectScriptPorts . ProjectScriptPorts ;
66- return yield * service . get ( { cwd : "/repo" , scriptIds : [ "dev-7202" , "dev" , "test" , "dev" ] } ) ;
146+ return yield * Effect . forEach ( [ "dev-7202" , "dev" , "test" , "dev" ] , ( scriptId ) =>
147+ service . reserve ( { cwd : "/repo" , scriptId } ) ,
148+ ) ;
67149 } ) ;
68150 return Effect . gen ( function * ( ) {
69151 const first = yield * allocate . pipe ( Effect . provide ( layer ) ) ;
70152 const restarted = yield * allocate . pipe ( Effect . provide ( layer ) ) ;
71- assert . deepEqual ( first , { ports : { "dev-7202" : 20000 , dev : 24299 , test : 27532 } } ) ;
153+ assert . deepEqual ( first , [ 20000 , 24299 , 27532 , 24299 ] ) ;
72154 assert . deepEqual ( restarted , first ) ;
73155 } ) ;
74156} ) ;
0 commit comments