@@ -34,36 +34,55 @@ const mockExistsSync = existsSync as jest.MockedFunction<typeof existsSync>;
3434const mockFsPromises = fsPromises as jest . Mocked < typeof fsPromises > ;
3535const mockOs = os as jest . Mocked < typeof os > ;
3636
37+ const isWindows = process . platform === 'win32' ;
38+ // On Windows, path.resolve prepends the current drive letter to
39+ // drive-relative Unix-style inputs and path.join uses backslashes, so
40+ // derive the expected fragments from the source helpers instead of literals.
41+ const encodeRegExp = ( value : string ) => new RegExp ( value . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ) ;
42+ const expectEncodedAs = ( encoded : string , posixEncoded : string ) => {
43+ // On Windows the resolved drive prefix ("D:") encodes to "D-" ahead of
44+ // the POSIX-shaped expectation.
45+ const prefix = isWindows ? '[a-zA-Z]-' : '' ;
46+ expect ( encoded ) . toMatch ( new RegExp ( `^${ prefix } ${ encodeRegExp ( posixEncoded ) . source } $` ) ) ;
47+ } ;
48+ const encodedTestVault = encodeVaultPathForSDK ( '/Users/test/vault' ) ;
49+ const expectedProjectsPath = path . join ( '/Users/test' , '.qoder' , 'projects' ) ;
50+ const expectedSessionPath = path . join ( expectedProjectsPath , encodedTestVault , 'session-abc.jsonl' ) ;
51+ const expectedArtifactsDir = path . join ( expectedProjectsPath , encodedTestVault , 'session-abc' ) ;
52+ const expectedSidecarPath = path . join ( expectedArtifactsDir , 'subagents' , 'agent-a123.jsonl' ) ;
53+
3754describe ( 'sdkSession' , ( ) => {
3855 beforeEach ( ( ) => {
3956 jest . clearAllMocks ( ) ;
4057 mockOs . homedir . mockReturnValue ( '/Users/test' ) ;
4158 } ) ;
4259
4360 describe ( 'encodeVaultPathForSDK' , ( ) => {
61+ // eslint-disable-next-line jest/expect-expect
4462 it ( 'encodes vault path by replacing all non-alphanumeric chars with dash' , ( ) => {
4563 const encoded = encodeVaultPathForSDK ( '/Users/test/vault' ) ;
4664 // SDK replaces ALL non-alphanumeric characters with `-`
47- expect ( encoded ) . toBe ( '-Users-test-vault' ) ;
65+ expectEncodedAs ( encoded , '-Users-test-vault' ) ;
4866 } ) ;
4967
68+ // eslint-disable-next-line jest/expect-expect
5069 it ( 'handles paths with spaces and special characters' , ( ) => {
5170 const encoded = encodeVaultPathForSDK ( "/Users/test/My Vault's~Data" ) ;
52- expect ( encoded ) . toBe ( '-Users-test-My-Vault-s-Data' ) ;
71+ expectEncodedAs ( encoded , '-Users-test-My-Vault-s-Data' ) ;
5372 } ) ;
5473
5574 it ( 'handles Unicode characters (Chinese, Japanese, etc.)' , ( ) => {
5675 // Unicode characters should be replaced with `-` to match SDK behavior
5776 const encoded = encodeVaultPathForSDK ( '/Volumes/[Work]弘毅之鹰/学习/东京大学/2025年 秋' ) ;
5877 // All non-alphanumeric (including Chinese, brackets) become `-`
59- expect ( encoded ) . toBe ( '-Volumes--Work--------------2025---' ) ;
78+ expectEncodedAs ( encoded , '-Volumes--Work--------------2025---' ) ;
6079 // Verify only ASCII alphanumeric and dash remain
6180 expect ( encoded ) . toMatch ( / ^ [ a - z A - Z 0 - 9 - ] + $ / ) ;
6281 } ) ;
6382
6483 it ( 'handles brackets and other special characters' , ( ) => {
6584 const encoded = encodeVaultPathForSDK ( '/Users/test/[my-vault](notes)' ) ;
66- expect ( encoded ) . toBe ( '-Users-test--my-vault--notes-' ) ;
85+ expectEncodedAs ( encoded , '-Users-test--my-vault--notes-' ) ;
6786 expect ( encoded ) . not . toContain ( '[' ) ;
6887 expect ( encoded ) . not . toContain ( ']' ) ;
6988 expect ( encoded ) . not . toContain ( '(' ) ;
@@ -190,9 +209,7 @@ describe('sdkSession', () => {
190209
191210 await deleteSDKSession ( '/Users/test/vault' , 'session-abc' ) ;
192211
193- expect ( mockFsPromises . unlink ) . toHaveBeenCalledWith (
194- '/Users/test/.qoder/projects/-Users-test-vault/session-abc.jsonl'
195- ) ;
212+ expect ( mockFsPromises . unlink ) . toHaveBeenCalledWith ( expectedSessionPath ) ;
196213 } ) ;
197214
198215 it ( 'does nothing when session file does not exist' , async ( ) => {
@@ -226,11 +243,9 @@ describe('sdkSession', () => {
226243
227244 await deleteSDKSessionArtifacts ( '/Users/test/vault' , 'session-abc' ) ;
228245
229- expect ( mockFsPromises . unlink ) . toHaveBeenCalledWith (
230- '/Users/test/.qoder/projects/-Users-test-vault/session-abc.jsonl'
231- ) ;
246+ expect ( mockFsPromises . unlink ) . toHaveBeenCalledWith ( expectedSessionPath ) ;
232247 expect ( mockFsPromises . rm ) . toHaveBeenCalledWith (
233- '/Users/test/.qoder/projects/-Users-test-vault/session-abc' ,
248+ expectedArtifactsDir ,
234249 { recursive : true , force : true } ,
235250 ) ;
236251 } ) ;
@@ -323,7 +338,7 @@ describe('sdkSession', () => {
323338 ) ;
324339
325340 expect ( mockFsPromises . readFile ) . toHaveBeenCalledWith (
326- '/Users/test/.qoder/projects/-Users-test-vault/session-abc/subagents/agent-a123.jsonl' ,
341+ expectedSidecarPath ,
327342 'utf-8'
328343 ) ;
329344 expect ( toolCalls ) . toHaveLength ( 1 ) ;
@@ -381,7 +396,7 @@ describe('sdkSession', () => {
381396
382397 expect ( result ) . toBe ( 'Final answer' ) ;
383398 expect ( mockFsPromises . readFile ) . toHaveBeenCalledWith (
384- '/Users/test/.qoder/projects/-Users-test-vault/session-abc/subagents/agent-a123.jsonl' ,
399+ expectedSidecarPath ,
385400 'utf-8'
386401 ) ;
387402 } ) ;
0 commit comments