Skip to content

Commit 5acf471

Browse files
Copilotmanekinekko
andauthored
chore: add fallback for getuserAgentSuffix when package.json is unavailable (#1303)
* Initial plan * Add fallback for getuserAgentSuffix when package.json is unavailable Co-authored-by: manekinekko <[email protected]> * Update jest config to exclude dist folder from test runs Co-authored-by: manekinekko <[email protected]> * Update lib/cosmos-db/cosmos-db.utils.ts * Update lib/cosmos-db/cosmos-db.utils.spec.ts * Apply suggestions from code review --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: manekinekko <[email protected]> Co-authored-by: Wassim Chegham <[email protected]>
1 parent b5eb3a3 commit 5acf471

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
22
preset: 'ts-jest',
33
testEnvironment: 'node',
4-
testPathIgnorePatterns: ['<rootDir>/sample/'],
4+
testPathIgnorePatterns: ['<rootDir>/sample/', '<rootDir>/dist/'],
55
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { getuserAgentSuffix } from './cosmos-db.utils';
2+
import { readFile } from 'fs/promises';
3+
import { join } from 'path';
4+
5+
// Mock fs/promises
6+
jest.mock('fs/promises');
7+
const mockReadFile = readFile as jest.MockedFunction<typeof readFile>;
8+
9+
describe('cosmos-db.utils', () => {
10+
describe('getuserAgentSuffix', () => {
11+
beforeEach(() => {
12+
jest.clearAllMocks();
13+
});
14+
15+
it('should return user agent with package info when package.json exists', async () => {
16+
const mockPackageJson = {
17+
name: '@nestjs/azure-database',
18+
version: '4.0.0',
19+
};
20+
21+
mockReadFile.mockResolvedValue(JSON.stringify(mockPackageJson));
22+
23+
const result = await getuserAgentSuffix();
24+
25+
expect(mockReadFile).toHaveBeenCalledWith(join(__dirname, '..', '..', 'package.json'), 'utf8');
26+
expect(result).toBe(
27+
`node.js/${process.version} (${process.platform}; ${process.arch}) ${mockPackageJson.name}/${mockPackageJson.version}`,
28+
);
29+
});
30+
31+
it('should return fallback user agent when package.json cannot be read', async () => {
32+
mockReadFile.mockRejectedValue(new Error('ENOENT: no such file or directory'));
33+
34+
const result = await getuserAgentSuffix();
35+
36+
expect(mockReadFile).toHaveBeenCalledWith(join(__dirname, '..', '..', 'package.json'), 'utf8');
37+
expect(result).toBe(`node.js/${process.version} (${process.platform}; ${process.arch}) @nestjs/azure-database/0.0.0`);
38+
});
39+
40+
it('should return fallback user agent when package.json is invalid JSON', async () => {
41+
mockReadFile.mockResolvedValue('invalid json content');
42+
43+
const result = await getuserAgentSuffix();
44+
45+
expect(mockReadFile).toHaveBeenCalledWith(join(__dirname, '..', '..', 'package.json'), 'utf8');
46+
expect(result).toBe(`node.js/${process.version} (${process.platform}; ${process.arch}) @nestjs/azure-database/0.0.0`);
47+
});
48+
49+
it('should return fallback user agent when package.json has missing properties', async () => {
50+
const mockPackageJson = {
51+
name: '@nestjs/azure-database',
52+
// version is missing
53+
};
54+
55+
mockReadFile.mockResolvedValue(JSON.stringify(mockPackageJson));
56+
57+
const result = await getuserAgentSuffix();
58+
59+
expect(result).toBe(`node.js/${process.version} (${process.platform}; ${process.arch}) @nestjs/azure-database/0.0.0`);
60+
});
61+
});
62+
});

lib/cosmos-db/cosmos-db.utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ import { readFile } from 'fs/promises';
66
import { join } from 'path';
77

88
export async function getuserAgentSuffix(): Promise<string> {
9-
const data = await readFile(join(__dirname, '..', '..', 'package.json'), 'utf8');
10-
const json = await JSON.parse(data);
11-
return `node.js/${process.version} (${process.platform}; ${process.arch}) ${json.name}/${json.version}`;
9+
try {
10+
const data = await readFile(join(__dirname, '..', '..', 'package.json'), 'utf8');
11+
const json = await JSON.parse(data);
12+
if (json.name && json.version) {
13+
return `node.js/${process.version} (${process.platform}; ${process.arch}) ${json.name}/${json.version}`;
14+
}
15+
throw new Error('Missing required package.json properties');
16+
} catch {
17+
return `node.js/${process.version} (${process.platform}; ${process.arch}) @nestjs/azure-database/0.0.0`;
18+
}
1219
}
1320

1421
export function getModelToken(model: string) {

0 commit comments

Comments
 (0)