From 0b2bd4702349e3dea8fda890a58ac0214c6ee009 Mon Sep 17 00:00:00 2001 From: Narutama Aurum Date: Sun, 24 May 2026 04:48:40 +0800 Subject: [PATCH 1/2] test: normalize mocked package path suffixes --- src/__tests__/version-check.test.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/__tests__/version-check.test.ts b/src/__tests__/version-check.test.ts index 3d8938a..f43569e 100644 --- a/src/__tests__/version-check.test.ts +++ b/src/__tests__/version-check.test.ts @@ -13,6 +13,9 @@ vi.mock('../utils/logger', () => ({ }, })); +const hasPathSuffix = (path: string, suffix: string) => + path.replaceAll('\\', '/').endsWith(suffix); + describe('version-check utilities', () => { describe('compareSemver', () => { it('should return "lt" if a < b', () => { @@ -109,9 +112,14 @@ describe('version-check utilities', () => { vi.mocked(readFileSync).mockReset(); }); + it('should treat Windows and POSIX separators the same when matching package paths', () => { + expect(hasPathSuffix('C:\\repo\\src\\package.json', 'src/package.json')).toBe(true); + expect(hasPathSuffix('/repo/src/package.json', 'src/package.json')).toBe(true); + }); + it('should resolve and return the version if package.json in production path is valid', () => { vi.mocked(readFileSync).mockImplementation((path) => { - if (typeof path === 'string' && path.endsWith('src/package.json')) { + if (typeof path === 'string' && hasPathSuffix(path, 'src/package.json')) { return JSON.stringify({ version: '2.3.4' }); } throw new Error('File not found'); @@ -122,7 +130,7 @@ describe('version-check utilities', () => { it('should resolve and return the version if production path fails but development path succeeds', () => { vi.mocked(readFileSync).mockImplementation((path) => { - if (typeof path === 'string' && path.endsWith('package.json') && !path.endsWith('src/package.json')) { + if (typeof path === 'string' && hasPathSuffix(path, 'package.json') && !hasPathSuffix(path, 'src/package.json')) { return JSON.stringify({ version: '1.2.3' }); } throw new Error('File not found'); @@ -133,10 +141,10 @@ describe('version-check utilities', () => { it('should advance to the next path if JSON is malformed', () => { vi.mocked(readFileSync).mockImplementation((path) => { - if (typeof path === 'string' && path.endsWith('src/package.json')) { + if (typeof path === 'string' && hasPathSuffix(path, 'src/package.json')) { return '{ malformed json'; } - if (typeof path === 'string' && path.endsWith('package.json') && !path.endsWith('src/package.json')) { + if (typeof path === 'string' && hasPathSuffix(path, 'package.json') && !hasPathSuffix(path, 'src/package.json')) { return JSON.stringify({ version: '1.2.9' }); } throw new Error('File not found'); @@ -147,10 +155,10 @@ describe('version-check utilities', () => { it('should advance to next path if version is not a string or missing', () => { vi.mocked(readFileSync).mockImplementation((path) => { - if (typeof path === 'string' && path.endsWith('src/package.json')) { + if (typeof path === 'string' && hasPathSuffix(path, 'src/package.json')) { return JSON.stringify({ version: 12345 }); // non-string version } - if (typeof path === 'string' && path.endsWith('package.json') && !path.endsWith('src/package.json')) { + if (typeof path === 'string' && hasPathSuffix(path, 'package.json') && !hasPathSuffix(path, 'src/package.json')) { return JSON.stringify({ version: '1.2.8' }); } throw new Error('File not found'); From 160ac5eeb71aa6b9df254aa5793ae9a2b93ec6b2 Mon Sep 17 00:00:00 2001 From: Narutama Aurum Date: Sun, 24 May 2026 07:55:19 +0800 Subject: [PATCH 2/2] test: cover non-matching path suffix case --- src/__tests__/version-check.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/__tests__/version-check.test.ts b/src/__tests__/version-check.test.ts index f43569e..71d6252 100644 --- a/src/__tests__/version-check.test.ts +++ b/src/__tests__/version-check.test.ts @@ -115,6 +115,7 @@ describe('version-check utilities', () => { it('should treat Windows and POSIX separators the same when matching package paths', () => { expect(hasPathSuffix('C:\\repo\\src\\package.json', 'src/package.json')).toBe(true); expect(hasPathSuffix('/repo/src/package.json', 'src/package.json')).toBe(true); + expect(hasPathSuffix('/repo/src/package.json', 'src/other.json')).toBe(false); }); it('should resolve and return the version if package.json in production path is valid', () => {