@@ -17,10 +17,12 @@ import * as NetService from "@t3tools/shared/Net";
1717import * as ConfigProvider from "effect/ConfigProvider" ;
1818import * as Effect from "effect/Effect" ;
1919import * as Layer from "effect/Layer" ;
20+ import * as Schema from "effect/Schema" ;
2021import { Command } from "effect/unstable/cli" ;
2122import { afterEach , describe , expect , vi } from "vite-plus/test" ;
2223
2324import { makeCli } from "../binCli.ts" ;
25+ import { PersistedServerRuntimeState } from "../serverRuntimeState.ts" ;
2426
2527vi . mock ( "node:os" , async ( importOriginal ) => {
2628 const os = await importOriginal < typeof import ( "node:os" ) > ( ) ;
@@ -29,6 +31,8 @@ vi.mock("node:os", async (importOriginal) => {
2931
3032afterEach ( ( ) => vi . mocked ( NodeOS . homedir ) . mockReset ( ) ) ;
3133
34+ const encodeRuntimeState = Schema . encodeEffect ( Schema . fromJsonString ( PersistedServerRuntimeState ) ) ;
35+
3236const runCli = ( args : ReadonlyArray < string > , env : Record < string , string > = { } ) =>
3337 Command . runWith ( makeCli ( ) , { version : "0.0.0" } ) ( args ) . pipe (
3438 Effect . provide (
@@ -130,6 +134,70 @@ const withTempDirectory = <A, E, R>(
130134 ( root ) => Effect . promise ( ( ) => NodeFSP . rm ( root , { recursive : true , force : true } ) ) ,
131135 ) ;
132136
137+ describe ( "t3 server command safety" , ( ) => {
138+ it . effect ( "rejects unknown command words without creating a home or project" , ( ) =>
139+ withTempDirectory ( "t3-cli-unknown-" , ( root ) =>
140+ Effect . gen ( function * ( ) {
141+ const baseDir = NodePath . join ( root , "home" ) ;
142+ for ( const word of [ "account" , "login" , "clients" , "conenct" ] ) {
143+ const error = yield * runCli ( [ word , "--base-dir" , baseDir ] ) . pipe ( Effect . flip ) ;
144+ expect ( String ( error ) ) . toContain ( `Unknown command "${ word } "` ) ;
145+ expect ( yield * pathExists ( word ) ) . toBe ( false ) ;
146+ expect ( yield * pathExists ( baseDir ) ) . toBe ( false ) ;
147+ }
148+ } ) ,
149+ ) ,
150+ ) ;
151+
152+ it . effect ( "shows help without creating state" , ( ) =>
153+ withTempDirectory ( "t3-cli-help-" , ( root ) =>
154+ Effect . gen ( function * ( ) {
155+ const baseDir = NodePath . join ( root , "home" ) ;
156+ const help = yield * runCli ( [ "help" ] , { T3CODE_HOME : baseDir } ) . pipe ( Effect . flip ) ;
157+ expect ( help ) . toMatchObject ( { _tag : "ShowHelp" , commandPath : [ "t3" ] , errors : [ ] } ) ;
158+ expect ( yield * pathExists ( baseDir ) ) . toBe ( false ) ;
159+ } ) ,
160+ ) ,
161+ ) ;
162+
163+ it . effect ( "refuses manual startup over a live server before creating directories" , ( ) =>
164+ withTempDirectory ( "t3-cli-running-" , ( root ) =>
165+ Effect . gen ( function * ( ) {
166+ const baseDir = NodePath . join ( root , "home" ) ;
167+ const stateDir = NodePath . join ( baseDir , "userdata" ) ;
168+ const statePath = NodePath . join ( stateDir , "server-runtime.json" ) ;
169+ const record = yield * encodeRuntimeState ( {
170+ version : 1 ,
171+ pid : process . pid ,
172+ port : 3773 ,
173+ origin : "http://127.0.0.1:3773" ,
174+ startedAt : "2026-10-01T00:00:00.000Z" ,
175+ serviceManaged : true ,
176+ } ) ;
177+ yield * Effect . promise ( ( ) => NodeFSP . mkdir ( stateDir , { recursive : true } ) ) ;
178+ yield * Effect . promise ( ( ) => NodeFSP . writeFile ( statePath , record ) ) ;
179+ const newDirectory = NodePath . join ( root , "new-project" ) ;
180+ for ( const args of [
181+ [ ] ,
182+ [ "start" ] ,
183+ [ "." ] ,
184+ [ "node_modules" ] ,
185+ [ newDirectory ] ,
186+ [ "start" , newDirectory ] ,
187+ ] ) {
188+ const error = yield * runCli ( args , { T3CODE_HOME : baseDir } ) . pipe ( Effect . flip ) ;
189+ expect ( String ( error ) ) . toContain ( "A T3 Code server is already running" ) ;
190+ expect ( yield * Effect . promise ( ( ) => NodeFSP . readFile ( statePath , "utf8" ) ) ) . toBe ( record ) ;
191+ expect ( yield * pathExists ( newDirectory ) ) . toBe ( false ) ;
192+ expect ( yield * Effect . promise ( ( ) => NodeFSP . readdir ( stateDir ) ) ) . toEqual ( [
193+ "server-runtime.json" ,
194+ ] ) ;
195+ }
196+ } ) ,
197+ ) ,
198+ ) ;
199+ } ) ;
200+
133201describe ( "t3 app" , ( ) => {
134202 it . effect ( "rejects SSH before it tries to reach a desktop app" , ( ) =>
135203 withTempDirectory ( "t3-app-ssh-test-" , ( root ) =>
0 commit comments