@@ -4,10 +4,13 @@ import { ClaudeSettings, type ServerProvider } from "@t3tools/contracts";
44import { vi } from "vite-plus/test" ;
55import { assert , it } from "@effect/vitest" ;
66import * as Effect from "effect/Effect" ;
7+ import * as FileSystem from "effect/FileSystem" ;
8+ import * as Path from "effect/Path" ;
79import * as Schema from "effect/Schema" ;
810
911import {
1012 buildClaudeCapabilitiesProbeQueryOptions ,
13+ makeClaudeWorkspaceCommands ,
1114 probeClaudeWorkspaceSnapshot ,
1215} from "./ClaudeProvider.ts" ;
1316
@@ -37,7 +40,193 @@ it.effect("reports a deleted worktree's scan as finished, not pending", () =>
3740 { skills : [ ] , slashCommands : [ ] } as unknown as ServerProvider ,
3841 "/nonexistent/t3-deleted-worktree" ,
3942 { HOME : "/nonexistent/t3-home" } ,
43+ ( ) => Effect . die ( "a deleted worktree must not start the CLI" ) ,
4044 ) ;
4145 assert . strictEqual ( snapshot . slashCommandsPending , false ) ;
4246 } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
4347) ;
48+
49+ const makeCommandSourcesFixture = Effect . gen ( function * ( ) {
50+ const fileSystem = yield * FileSystem . FileSystem ;
51+ const path = yield * Path . Path ;
52+ const root = yield * fileSystem . realPath ( yield * fileSystem . makeTempDirectoryScoped ( ) ) ;
53+ const write = Effect . fnUntraced ( function * ( file : string , contents = "contents" ) {
54+ yield * fileSystem . makeDirectory ( path . dirname ( file ) , { recursive : true } ) ;
55+ yield * fileSystem . writeFileString ( file , contents ) ;
56+ } ) ;
57+ // A linked worktree, as `git worktree add` lays it out, with the cwd below its root.
58+ const main = path . join ( root , "main" ) ;
59+ const worktree = path . join ( root , "worktree" ) ;
60+ const gitDirectory = path . join ( main , ".git" , "worktrees" , "worktree" ) ;
61+ const cwd = path . join ( worktree , "packages" , "app" ) ;
62+ yield * write ( path . join ( worktree , ".git" ) , `gitdir: ${ gitDirectory } \n` ) ;
63+ yield * write ( path . join ( gitDirectory , "commondir" ) , "../..\n" ) ;
64+ yield * write ( path . join ( gitDirectory , "gitdir" ) , `${ path . join ( worktree , ".git" ) } \n` ) ;
65+ yield * fileSystem . makeDirectory ( cwd , { recursive : true } ) ;
66+
67+ let probes = 0 ;
68+ let failing = false ;
69+ const config = path . join ( root , "config" ) ;
70+ const makeCommands = ( environment : NodeJS . ProcessEnv = { } ) =>
71+ makeClaudeWorkspaceCommands ( { homePath : config } , environment , ( ) =>
72+ Effect . suspend ( ( ) => {
73+ probes += 1 ;
74+ return failing ? Effect . fail ( "probe failed" ) : Effect . succeed ( [ { name : "review" } ] ) ;
75+ } ) ,
76+ ) ;
77+ return {
78+ path,
79+ write,
80+ root,
81+ main,
82+ worktree,
83+ cwd,
84+ config,
85+ makeCommands,
86+ probes : ( ) => probes ,
87+ failNextProbes : ( value : boolean ) => {
88+ failing = value ;
89+ } ,
90+ } ;
91+ } ) ;
92+
93+ const machine = { version : "1.0.0" , auth : { status : "authenticated" } , slashCommands : [ ] } as const ;
94+
95+ it . effect ( "starts the CLI again only when a source of its command list changes" , ( ) =>
96+ Effect . gen ( function * ( ) {
97+ const { path, write, main, worktree, cwd, config, makeCommands, probes } =
98+ yield * makeCommandSourcesFixture ;
99+ const commands = yield * makeCommands ( ) ;
100+
101+ yield * commands . get ( machine , cwd ) ;
102+ yield * commands . get ( machine , cwd ) ;
103+ assert . strictEqual ( probes ( ) , 1 ) ;
104+
105+ // Each of these changed real CLI output or feeds it; see fingerprintClaudeCommandSources.
106+ for ( const source of [
107+ path . join ( config , "commands" , "nested" , "a.md" ) ,
108+ path . join ( config , "skills" , "a" , "SKILL.md" ) ,
109+ path . join ( config , "workflows" , "a.js" ) ,
110+ path . join ( config , "synced" , "account" , "a" , "SKILL.md" ) ,
111+ path . join ( config , "settings.json" ) ,
112+ path . join ( config , "remote-settings.json" ) ,
113+ path . join ( config , "plugins" , "installed_plugins.json" ) ,
114+ path . join ( config , "plugins" , "synced" , "account" , ".marketplace-a.json" ) ,
115+ path . join ( config , "plugins" , "cache" , "market" , "plugin" , "1.0.0" , "commands" , "a.md" ) ,
116+ path . join ( config , "plugins" , "cache" , "market" , "plugin" , "1.0.0" , "skills" , "store" , "a.md" ) ,
117+ path . join ( cwd , ".claude" , "commands" , "a.md" ) ,
118+ path . join ( cwd , ".claude" , "skills" , "a" , "SKILL.md" ) ,
119+ path . join ( cwd , ".claude" , "workflows" , "a.js" ) ,
120+ path . join ( cwd , ".claude" , "settings.json" ) ,
121+ path . join ( cwd , ".claude" , "settings.local.json" ) ,
122+ path . join ( worktree , ".claude" , "commands" , "a.md" ) ,
123+ path . join ( main , ".claude" , "skills" , "a" , "SKILL.md" ) ,
124+ ] ) {
125+ const before = probes ( ) ;
126+ yield * write (
127+ source ,
128+ source . endsWith ( "installed_plugins.json" ) ? '{"plugins":{}}' : "contents" ,
129+ ) ;
130+ yield * commands . get ( machine , cwd ) ;
131+ assert . strictEqual ( probes ( ) , before + 1 , `${ source } should start the CLI` ) ;
132+ yield * commands . get ( machine , cwd ) ;
133+ assert . strictEqual ( probes ( ) , before + 1 , `${ source } should settle` ) ;
134+ }
135+
136+ const edited = path . join ( cwd , ".claude" , "commands" , "a.md" ) ;
137+ const beforeEdit = probes ( ) ;
138+ yield * write ( edited , "edited description" ) ;
139+ yield * commands . get ( machine , cwd ) ;
140+ assert . strictEqual ( probes ( ) , beforeEdit + 1 ) ;
141+
142+ const beforeUnrelated = probes ( ) ;
143+ yield * write ( path . join ( cwd , ".claude" , "agents" , "a.md" ) ) ;
144+ yield * write ( path . join ( config , "plugins" , "marketplaces" , "market" , "README.md" ) ) ;
145+ yield * write ( path . join ( config , "plugins" , "store" , "runtime-state.json" ) ) ;
146+ yield * write ( path . join ( config , "plugins" , "synced" , "account" , ".last-complete-round" ) ) ;
147+ yield * write ( path . join ( config , "plugins" , "synced" , "account" , ".marketplaces.json" ) ) ;
148+ yield * commands . get ( machine , cwd ) ;
149+ assert . strictEqual ( probes ( ) , beforeUnrelated ) ;
150+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
151+ ) ;
152+
153+ it . effect ( "starts the CLI again when the health check's view of it changes" , ( ) =>
154+ Effect . gen ( function * ( ) {
155+ const { cwd, makeCommands, probes } = yield * makeCommandSourcesFixture ;
156+ const commands = yield * makeCommands ( ) ;
157+ yield * commands . get ( machine , cwd ) ;
158+ yield * commands . get ( { ...machine , version : "1.0.1" } , cwd ) ;
159+ assert . strictEqual ( probes ( ) , 2 ) ;
160+ yield * commands . get ( { ...machine , auth : { status : "unauthenticated" } } , cwd ) ;
161+ assert . strictEqual ( probes ( ) , 3 ) ;
162+ yield * commands . get ( { ...machine , slashCommands : [ { name : "upgrade" } ] } , cwd ) ;
163+ assert . strictEqual ( probes ( ) , 4 ) ;
164+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
165+ ) ;
166+
167+ it . effect ( "keeps folders apart, retries failures, and forgets on invalidate" , ( ) =>
168+ Effect . gen ( function * ( ) {
169+ const { cwd, worktree, makeCommands, probes, failNextProbes } =
170+ yield * makeCommandSourcesFixture ;
171+ const commands = yield * makeCommands ( ) ;
172+
173+ failNextProbes ( true ) ;
174+ yield * commands . get ( machine , cwd ) . pipe ( Effect . result ) ;
175+ failNextProbes ( false ) ;
176+ yield * commands . get ( machine , cwd ) ;
177+ assert . strictEqual ( probes ( ) , 2 ) ;
178+
179+ yield * commands . get ( machine , worktree ) ;
180+ assert . strictEqual ( probes ( ) , 3 ) ;
181+
182+ yield * commands . invalidate ;
183+ yield * commands . get ( machine , cwd ) ;
184+ assert . strictEqual ( probes ( ) , 4 ) ;
185+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
186+ ) ;
187+
188+ it . effect ( "always starts the CLI when its sources cannot be vouched for" , ( ) =>
189+ Effect . gen ( function * ( ) {
190+ const { path, write, root, cwd, config, makeCommands, probes } =
191+ yield * makeCommandSourcesFixture ;
192+ const probesTwice = ( commands : Effect . Success < ReturnType < typeof makeCommands > > ) =>
193+ Effect . gen ( function * ( ) {
194+ const before = probes ( ) ;
195+ yield * commands . get ( machine , cwd ) ;
196+ yield * commands . get ( machine , cwd ) ;
197+ assert . strictEqual ( probes ( ) , before + 2 ) ;
198+ } ) ;
199+
200+ yield * probesTwice ( yield * makeCommands ( { CLAUDE_CODE_PLUGIN_DIRS : "/elsewhere" } ) ) ;
201+
202+ const commands = yield * makeCommands ( ) ;
203+ const settings = path . join ( cwd , ".claude" , "settings.json" ) ;
204+ yield * write ( settings , '{"env":{"CLAUDE_CODE_PLUGIN_SEED_DIR":"/elsewhere"}}' ) ;
205+ yield * probesTwice ( commands ) ;
206+ yield * write ( settings , "{}" ) ;
207+
208+ const installed = path . join ( config , "plugins" , "installed_plugins.json" ) ;
209+ yield * write (
210+ installed ,
211+ JSON . stringify ( { plugins : { "a@b" : [ { installPath : path . join ( root , "elsewhere" ) } ] } } ) ,
212+ ) ;
213+ yield * probesTwice ( commands ) ;
214+ yield * write ( installed , "not json" ) ;
215+ yield * probesTwice ( commands ) ;
216+ yield * write ( installed , '{"plugins":{}}' ) ;
217+
218+ const marketplaces = path . join ( config , "plugins" , "known_marketplaces.json" ) ;
219+ yield * write ( marketplaces , '{"local":{"source":{"source":"directory","path":"/elsewhere"}}}' ) ;
220+ yield * probesTwice ( commands ) ;
221+ yield * write ( marketplaces , '{"remote":{"source":{"source":"github","repo":"a/b"}}}' ) ;
222+ const beforeRemote = probes ( ) ;
223+ yield * commands . get ( machine , cwd ) ;
224+ yield * commands . get ( machine , cwd ) ;
225+ assert . strictEqual ( probes ( ) , beforeRemote + 1 ) ;
226+
227+ for ( let index = 0 ; index <= 2_000 ; index ++ ) {
228+ yield * write ( path . join ( cwd , ".claude" , "skills" , `${ index } .md` ) ) ;
229+ }
230+ yield * probesTwice ( commands ) ;
231+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
232+ ) ;
0 commit comments