1+ import * as NodeServices from "@effect/platform-node/NodeServices" ;
12import { assert , describe , it } from "@effect/vitest" ;
3+ import type { CursorConfigureOptions } from "@cursor/sdk" ;
4+ import * as HostProcess from "@t3tools/shared/HostProcess" ;
5+ import * as Context from "effect/Context" ;
26import * as Effect from "effect/Effect" ;
7+ import * as FileSystem from "effect/FileSystem" ;
38import * as Layer from "effect/Layer" ;
9+ import * as Logger from "effect/Logger" ;
10+ import * as Path from "effect/Path" ;
411import { vi } from "vite-plus/test" ;
512
613import * as CursorSdk from "./CursorSdk.ts" ;
714
15+ const sdkDefaults = vi . hoisted ( ( ) => ( { useHttp1ForAgent : undefined as boolean | undefined } ) ) ;
16+
17+ // Holds the SDK's process-wide default the way it does: only keys present change, null clears.
18+ vi . mock ( "./sdk.ts" , ( ) => ( {
19+ loadCursorSdk : ( ) => ( {
20+ Cursor : {
21+ configure : ( { local } : CursorConfigureOptions ) => {
22+ if ( local !== undefined && "useHttp1ForAgent" in local ) {
23+ sdkDefaults . useHttp1ForAgent = local . useHttp1ForAgent ?? undefined ;
24+ }
25+ } ,
26+ } ,
27+ } ) ,
28+ loadCursorSqlite : ( ) => ( { } ) ,
29+ } ) ) ;
30+
31+ const withHost = ( environment : NodeJS . ProcessEnv , home : string ) =>
32+ Effect . provide (
33+ Layer . mergeAll (
34+ NodeServices . layer ,
35+ Layer . succeed ( HostProcess . Environment , environment ) ,
36+ Layer . succeed ( HostProcess . HomeDirectory , home ) ,
37+ ) ,
38+ ) ;
39+
840// Shaped like the SDK's minified class: the constructor is not named after the error.
941// @effect -diagnostics-next-line extendsNativeError:off - mimics the Cursor SDK's own native Error class.
1042class o extends Error {
@@ -27,7 +59,7 @@ const rejectThroughGuard = (reason: unknown) =>
2759 for ( const listener of others ) process . on ( "unhandledRejection" , listener ) ;
2860 }
2961 return exit . mock . calls ;
30- } ) . pipe ( Effect . scoped ) ;
62+ } ) . pipe ( Effect . scoped , withHost ( { } , "/nonexistent-home" ) ) ;
3163
3264describe ( "CursorSdk unhandled rejection guard" , ( ) => {
3365 it . effect ( "keeps the server running when the SDK writes to a closed stream" , ( ) =>
@@ -70,3 +102,76 @@ describe("CursorSdk unhandled rejection guard", () => {
70102 } ) ,
71103 ) ;
72104} ) ;
105+
106+ const http1 = JSON . stringify ( { network : { useHttp1ForAgent : true } } ) ;
107+
108+ describe ( "CursorSdk agent transport" , ( ) => {
109+ it . effect . each ( [
110+ {
111+ name : "when set in CURSOR_CONFIG_DIR over XDG_CONFIG_HOME" ,
112+ env : { CURSOR_CONFIG_DIR : "config" , XDG_CONFIG_HOME : "xdg" } ,
113+ file : "config/cli-config.json" ,
114+ text : http1 ,
115+ expected : true ,
116+ } ,
117+ {
118+ name : "when set in XDG_CONFIG_HOME" ,
119+ env : { XDG_CONFIG_HOME : "xdg" } ,
120+ file : "xdg/cursor/cli-config.json" ,
121+ text : http1 ,
122+ expected : true ,
123+ } ,
124+ {
125+ name : "when set in the home directory" ,
126+ env : { } ,
127+ file : "home/.cursor/cli-config.json" ,
128+ text : http1 ,
129+ expected : true ,
130+ } ,
131+ {
132+ name : "when it is off" ,
133+ env : { } ,
134+ file : "home/.cursor/cli-config.json" ,
135+ text : JSON . stringify ( { network : { useHttp1ForAgent : false } } ) ,
136+ expected : undefined ,
137+ } ,
138+ {
139+ name : "when the file is damaged" ,
140+ env : { } ,
141+ file : "home/.cursor/cli-config.json" ,
142+ text : "{" ,
143+ expected : undefined ,
144+ warns : true ,
145+ } ,
146+ { name : "when there is no file" , env : { } , file : undefined , text : "" , expected : undefined } ,
147+ ] ) ( "follows the Cursor CLI's HTTP/1.1 setting $name" , ( { env, file, text, expected, warns } ) =>
148+ Effect . gen ( function * ( ) {
149+ const fileSystem = yield * FileSystem . FileSystem ;
150+ const path = yield * Path . Path ;
151+ const root = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-cursor-cli-config-" } ) ;
152+ if ( file !== undefined ) {
153+ yield * fileSystem . makeDirectory ( path . dirname ( path . join ( root , file ) ) , { recursive : true } ) ;
154+ yield * fileSystem . writeFileString ( path . join ( root , file ) , text ) ;
155+ }
156+ const environment = Object . fromEntries (
157+ Object . entries ( env ) . map ( ( [ key , directory ] ) => [ key , path . join ( root , directory ) ] ) ,
158+ ) ;
159+ const warnings : Array < unknown > = [ ] ;
160+ const captureWarnings = Logger . make ( ( { logLevel, message } ) => {
161+ if ( logLevel === "Warn" ) warnings . push ( message ) ;
162+ } ) ;
163+ yield * Effect . gen ( function * ( ) {
164+ const sdk = Context . get ( yield * Layer . build ( CursorSdk . layer ) , CursorSdk . CursorSdk ) ;
165+ assert . isUndefined ( sdkDefaults . useHttp1ForAgent ) ;
166+ sdk . load ( ) ;
167+ assert . strictEqual ( sdkDefaults . useHttp1ForAgent , expected ) ;
168+ } ) . pipe (
169+ Effect . scoped ,
170+ withHost ( environment , path . join ( root , "home" ) ) ,
171+ Effect . provide ( Logger . layer ( [ captureWarnings ] , { mergeWithExisting : false } ) ) ,
172+ ) ;
173+ assert . isUndefined ( sdkDefaults . useHttp1ForAgent ) ;
174+ assert . lengthOf ( warnings , warns ? 1 : 0 ) ;
175+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
176+ ) ;
177+ } ) ;
0 commit comments