11import * as NodeCrypto from "@effect/platform-node/NodeCrypto" ;
2- import { describe , expect , it } from "@effect/vitest" ;
2+ import * as NodeServices from "@effect/platform-node/NodeServices" ;
3+ import { assert , describe , expect , it } from "@effect/vitest" ;
34import { ProjectId , type TerminalEvent , type TerminalSessionSnapshot } from "@t3tools/contracts" ;
45import * as HostProcess from "@t3tools/shared/HostProcess" ;
56import * as Deferred from "effect/Deferred" ;
67import * as Effect from "effect/Effect" ;
78import * as Fiber from "effect/Fiber" ;
89import * as Layer from "effect/Layer" ;
910import * as Queue from "effect/Queue" ;
11+ import * as Stream from "effect/Stream" ;
12+ import { ChildProcess , ChildProcessSpawner } from "effect/process" ;
1013import { TestClock } from "effect/testing" ;
1114
1215import * as ServerSettings from "../serverSettings.ts" ;
@@ -33,6 +36,7 @@ const openSnapshot = (history: string): TerminalSessionSnapshot => ({
3336} ) ;
3437
3538const makeHarness = Effect . fn ( function * ( options : {
39+ readonly command ?: string ;
3640 readonly history ?: string ;
3741 readonly opening ?: Deferred . Deferred < void > ;
3842} ) {
@@ -76,7 +80,7 @@ const makeHarness = Effect.fn(function* (options: {
7680 {
7781 id : "setup" ,
7882 name : "Setup" ,
79- command : "vp i" ,
83+ command : options . command ?? "vp i" ,
8084 icon : "configure" ,
8185 runOnWorktreeCreate : true ,
8286 } ,
@@ -88,7 +92,9 @@ const makeHarness = Effect.fn(function* (options: {
8892 return { start, listeners, writes, outputLines, emit } ;
8993} ) ;
9094
91- const setup = Effect . fn ( function * ( options : { readonly history ?: string } = { } ) {
95+ const setup = Effect . fn ( function * (
96+ options : { readonly command ?: string ; readonly history ?: string } = { } ,
97+ ) {
9298 const harness = yield * makeHarness ( options ) ;
9399 const result = yield * harness . start ;
94100 const completion = result . status === "started" ? result . completion : undefined ;
@@ -110,15 +116,15 @@ describe("ProjectSetupScriptRunner", () => {
110116
111117 yield * emit ( { ...event , type : "activity" , hasRunningSubprocess : false , label : "zsh" } ) ;
112118 yield * emit ( { ...event , type : "output" , data : "\r\n% \x1b[?2004h" } ) ;
113- expect ( yield * Queue . take ( writes ) ) . toMatch ( / ^ \( v p i \r \) ; / ) ;
119+ expect ( yield * Queue . take ( writes ) ) . toContain ( " vp i" ) ;
114120 } ) ,
115121 ) ;
116122
117123 it . effect ( "types into a quiet shell that has no line editor" , ( ) =>
118124 Effect . gen ( function * ( ) {
119125 const { writes } = yield * setup ( ) ;
120126 yield * TestClock . adjust ( "3 seconds" ) ;
121- expect ( yield * Queue . take ( writes ) ) . toMatch ( / ^ \( v p i \r \) ; / ) ;
127+ expect ( yield * Queue . take ( writes ) ) . toContain ( " vp i" ) ;
122128 } ) ,
123129 ) ;
124130
@@ -140,7 +146,7 @@ describe("ProjectSetupScriptRunner", () => {
140146 yield * emit ( { ...event , type : "restarted" , snapshot : openSnapshot ( "" ) } ) ;
141147 // The restart lands inside a quiet window, so the next full window decides.
142148 yield * TestClock . adjust ( "6 seconds" ) ;
143- expect ( yield * Queue . take ( writes ) ) . toMatch ( / ^ \( v p i \r \) ; / ) ;
149+ expect ( yield * Queue . take ( writes ) ) . toContain ( " vp i" ) ;
144150 } ) ,
145151 ) ;
146152
@@ -166,3 +172,122 @@ describe("ProjectSetupScriptRunner", () => {
166172 } ) ,
167173 ) ;
168174} ) ;
175+
176+ const runObservedSetup = Effect . fnUntraced ( function * ( command : string , stdin = "" ) {
177+ const { writes, outputLines, emit, completion } = yield * setup ( { command } ) ;
178+ yield * emit ( { ...event , type : "output" , data : "\x1b[?2004h" } ) ;
179+ const submitted = yield * Queue . take ( writes ) ;
180+ const cwd = yield * HostProcess . WorkingDirectory ;
181+ const env = yield * HostProcess . Environment ;
182+
183+ // Enter becomes a newline at the terminal boundary; transport assertions below
184+ // cover line-editor submissions and canonical TTY limits that bash -c bypasses.
185+ const child = yield * Effect . gen ( function * ( ) {
186+ const spawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
187+ const handle = yield * spawner . spawn (
188+ ChildProcess . make (
189+ "/bin/bash" ,
190+ [ "--noprofile" , "--norc" , "-c" , submitted . replaceAll ( "\r" , "\n" ) ] ,
191+ {
192+ cwd,
193+ env : { PATH : env . PATH } ,
194+ stdin : Stream . make ( new TextEncoder ( ) . encode ( stdin ) ) ,
195+ } ,
196+ ) ,
197+ ) ;
198+ return yield * Effect . all (
199+ {
200+ stdout : handle . stdout . pipe ( Stream . decodeText ( ) , Stream . mkString ) ,
201+ stderr : handle . stderr . pipe ( Stream . decodeText ( ) , Stream . mkString ) ,
202+ exitCode : handle . exitCode ,
203+ } ,
204+ { concurrency : "unbounded" } ,
205+ ) ;
206+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ;
207+ yield * emit ( { ...event , type : "output" , data : submitted + "\n" } ) ;
208+ yield * emit ( { ...event , type : "output" , data : child . stdout + child . stderr } ) ;
209+ yield * emit ( { ...event , type : "closed" } ) ;
210+ return {
211+ submitted,
212+ lines : yield * Queue . clear ( outputLines ) ,
213+ exitCode : ( yield * completion ) . exitCode ,
214+ } ;
215+ } ) ;
216+
217+ describe . skipIf ( HostProcess . Platform . defaultValue ( ) === "win32" ) ( "POSIX setup completion" , ( ) => {
218+ it . live ( "submits one line while preserving multiline shell syntax and literal text" , ( ) =>
219+ Effect . gen ( function * ( ) {
220+ const result = yield * runObservedSetup (
221+ [
222+ "printf '%s\\n' \"it's ready\" 'literal \\n $(printf wrong) `printf wrong` café'" ,
223+ "cat <<'END'" ,
224+ "$literal \\path 雪" ,
225+ "END" ,
226+ "printf '%s\\n' done # trailing comment" ,
227+ ] . join ( "\r\n" ) ,
228+ ) ;
229+ assert . equal ( result . submitted . at ( - 1 ) , "\r" ) ;
230+ assert . notMatch ( result . submitted . slice ( 0 , - 1 ) , / [ \r \n ] / ) ;
231+ assert . deepEqual ( result . lines , [
232+ "it's ready" ,
233+ "literal \\n $(printf wrong) `printf wrong` café" ,
234+ "$literal \\path 雪" ,
235+ "done" ,
236+ ] ) ;
237+ assert . equal ( result . exitCode , 0 ) ;
238+ } ) ,
239+ ) ;
240+
241+ it . live ( "completes long scripts without exceeding canonical terminal line limits" , ( ) =>
242+ Effect . gen ( function * ( ) {
243+ const expected = Array . from (
244+ { length : 120 } ,
245+ ( _ , index ) => `line-${ index } : café 雪 'quoted' \\path` ,
246+ ) ;
247+ const command = expected
248+ . map ( ( line ) => `printf '%s\\n' "${ line . replaceAll ( "\\" , "\\\\" ) } "` )
249+ . join ( "\n" ) ;
250+ const utf8 = new TextEncoder ( ) ;
251+ assert . isAbove ( utf8 . encode ( command ) . byteLength , 4_096 ) ;
252+
253+ const result = yield * runObservedSetup ( command ) ;
254+ assert . deepEqual ( result . lines , expected ) ;
255+ assert . equal ( result . exitCode , 0 ) ;
256+ assert . equal ( result . submitted . at ( - 1 ) , "\r" ) ;
257+ assert . notInclude ( result . submitted , "\n" ) ;
258+ // The runner can write before the shell switches the terminal out of canonical mode.
259+ for ( const line of result . submitted . slice ( 0 , - 1 ) . split ( "\r" ) ) {
260+ assert . isBelow ( utf8 . encode ( line ) . byteLength , 1_024 ) ;
261+ }
262+ } ) ,
263+ ) ;
264+
265+ it . live ( "leaves stdin available to the script without consuming completion commands" , ( ) =>
266+ Effect . gen ( function * ( ) {
267+ const result = yield * runObservedSetup (
268+ "read -r value\nprintf '%s\\n' \"$value\"" ,
269+ "from stdin \\path\n" ,
270+ ) ;
271+ assert . deepEqual ( result . lines , [ "from stdin \\path" ] ) ;
272+ assert . equal ( result . exitCode , 0 ) ;
273+ } ) ,
274+ ) ;
275+
276+ it . live ( "preserves a trailing backslash-newline even when losing it would still exit zero" , ( ) =>
277+ Effect . gen ( function * ( ) {
278+ const result = yield * runObservedSetup ( "printf '%s\\n' direct\\\n" ) ;
279+ assert . deepEqual ( result . lines , [ "direct" ] ) ;
280+ assert . equal ( result . exitCode , 0 ) ;
281+ } ) ,
282+ ) ;
283+
284+ it . live . each ( [
285+ { name : "an explicit exit" , command : "exit 7" , exitCode : 7 } ,
286+ { name : "a syntax error" , command : "if then" , exitCode : 2 } ,
287+ ] ) ( "reports a nonzero completion for $name" , ( { command, exitCode } ) =>
288+ Effect . gen ( function * ( ) {
289+ const result = yield * runObservedSetup ( command ) ;
290+ assert . equal ( result . exitCode , exitCode ) ;
291+ } ) ,
292+ ) ;
293+ } ) ;
0 commit comments