Skip to content

Commit cb5fd71

Browse files
author
liuxuezhuo
committed
fix(test): handle Windows drive prefixes and MSYS single-letter paths
- derive expected SDK session paths via path.join + encodeVaultPathForSDK since path.resolve prepends the current drive letter on Windows hosts - allow a drive-letter prefix in encodeVaultPathForSDK expectations - use multi-letter segments in parsePathEntries/findConflictingPath fixtures because /a is a legitimate MSYS drive reference on Windows - expect host separators for non-existent env var normalization
1 parent a2aab18 commit cb5fd71

4 files changed

Lines changed: 44 additions & 23 deletions

File tree

‎tests/unit/core/context/external-context.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,11 @@ describe('externalContext utilities', () => {
180180
});
181181

182182
it('should return first conflict when multiple exist', () => {
183-
const result = findConflictingPath('/a/b', ['/a', '/a/b/c']);
184-
// Should return /a as it appears first and is a parent
185-
expect(result).toEqual({ path: '/a', type: 'parent' });
183+
// Multi-letter segments: "/a" would be a MSYS drive reference on
184+
// Windows hosts and normalize to "a:", breaking nesting checks.
185+
const result = findConflictingPath('/proj-a/proj-b', ['/proj-a', '/proj-a/proj-b/proj-c']);
186+
// Should return /proj-a as it appears first and is a parent
187+
expect(result).toEqual({ path: '/proj-a', type: 'parent' });
186188
});
187189
});
188190
});

‎tests/unit/core/fs/path.platform.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ describe('normalizePathForFilesystem', () => {
110110
});
111111

112112
it('handles non-existent environment variables', () => {
113-
// Non-existent env vars should be left as-is
114-
expect(normalizePathForFilesystem('$NONEXISTENT/path')).toBe('$NONEXISTENT/path');
115-
expect(normalizePathForFilesystem('%NONEXISTENT%/path')).toBe('%NONEXISTENT%/path');
113+
// Non-existent env vars should be left as-is; only the separator
114+
// differs because win32.normalize rewrites slashes on Windows hosts.
115+
const sep = isWindows ? '\\' : '/';
116+
expect(normalizePathForFilesystem('$NONEXISTENT/path')).toBe(`$NONEXISTENT${sep}path`);
117+
expect(normalizePathForFilesystem('%NONEXISTENT%/path')).toBe(`%NONEXISTENT%${sep}path`);
116118
});
117119

118120
it('handles mixed path separators', () => {

‎tests/unit/core/fs/path.test.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,13 @@ describe('parsePathEntries', () => {
149149
});
150150

151151
it('splits on platform separator', () => {
152+
// Multi-letter segments: a single "/a" entry is a valid MSYS drive
153+
// reference and gets translated to "A:" on Windows hosts.
152154
const sep = isWindows ? ';' : ':';
153-
const result = parsePathEntries(`/a${sep}/b${sep}/c`);
154-
expect(result).toContain('/a');
155-
expect(result).toContain('/b');
156-
expect(result).toContain('/c');
155+
const result = parsePathEntries(`/dir-a${sep}/dir-b${sep}/dir-c`);
156+
expect(result).toContain('/dir-a');
157+
expect(result).toContain('/dir-b');
158+
expect(result).toContain('/dir-c');
157159
});
158160

159161
it('filters out empty segments', () => {

‎tests/unit/qoder/history/qoder-history-store.test.ts‎

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,55 @@ const mockExistsSync = existsSync as jest.MockedFunction<typeof existsSync>;
3434
const mockFsPromises = fsPromises as jest.Mocked<typeof fsPromises>;
3535
const 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+
3754
describe('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-zA-Z0-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

Comments
 (0)