Skip to content

Commit d48a755

Browse files
perf: optimize package lookup in choosePackage (#21)
Replaced sequential .find() with Map-based O(1) lookup in choosePackage. Added unit test tests/package-optimization.test.ts to verify the change. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com>
1 parent 25a5261 commit d48a755

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

src/package.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,11 @@ export async function listPackage(appId: string) {
175175

176176
export async function choosePackage(appId: string) {
177177
const list = await listPackage(appId);
178+
const packageMap = new Map(list?.map((v) => [v.id.toString(), v]));
178179

179180
while (true) {
180181
const id = await question(t('enterNativePackageId'));
181-
const app = list?.find((v) => v.id.toString() === id);
182+
const app = packageMap.get(id);
182183
if (app) {
183184
return app;
184185
}

tests/package-optimization.test.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { describe, expect, test, mock, spyOn } from 'bun:test';
2+
3+
// Mock modules before any imports
4+
mock.module('filesize-parser', () => ({ default: () => 0 }));
5+
mock.module('form-data', () => ({ default: class {} }));
6+
mock.module('node-fetch', () => ({ default: () => {} }));
7+
mock.module('progress', () => ({ default: class { tick() {} } }));
8+
mock.module('tcp-ping', () => ({ default: { ping: () => {} } }));
9+
mock.module('tty-table', () => {
10+
const mockTable = () => ({ render: () => '' });
11+
return { default: mockTable };
12+
});
13+
mock.module('read', () => ({ read: () => {} }));
14+
mock.module('chalk', () => ({
15+
default: {
16+
green: (s: string) => s,
17+
red: (s: string) => s,
18+
yellow: (s: string) => s,
19+
},
20+
}));
21+
mock.module('i18next', () => ({
22+
default: {
23+
t: (k: string) => k,
24+
use: () => ({ init: () => {} }),
25+
init: () => {},
26+
},
27+
}));
28+
mock.module('fs-extra', () => ({
29+
default: {
30+
existsSync: () => false,
31+
readFileSync: () => '',
32+
ensureDirSync: () => {},
33+
pathExists: async () => false,
34+
remove: async () => {},
35+
},
36+
}));
37+
mock.module('compare-versions', () => ({
38+
satisfies: () => true,
39+
}));
40+
mock.module('yauzl', () => ({
41+
open: () => {},
42+
}));
43+
mock.module('isomorphic-git', () => ({}));
44+
mock.module('global-dirs', () => ({
45+
npm: { packages: '' },
46+
yarn: { packages: '' },
47+
}));
48+
mock.module('registry-auth-token', () => ({}));
49+
mock.module('registry-auth-token/registry-url', () => () => 'https://registry.npmjs.org');
50+
mock.module('semver', () => ({}));
51+
mock.module('semver/functions/gt', () => () => true);
52+
mock.module('semver/ranges/max-satisfying', () => (versions: string[]) => versions[0]);
53+
mock.module('bytebuffer', () => ({}));
54+
55+
import * as api from '../src/api';
56+
import * as utils from '../src/utils';
57+
import { choosePackage } from '../src/package';
58+
59+
describe('choosePackage optimization', () => {
60+
test('should return the correct package when a valid ID is entered', async () => {
61+
const mockPackages = [
62+
{ id: '101', name: 'package1' },
63+
{ id: '102', name: 'package2' },
64+
];
65+
66+
const getAllPackagesSpy = spyOn(api, 'getAllPackages').mockResolvedValue(mockPackages as any);
67+
const questionSpy = spyOn(utils, 'question').mockResolvedValue('102');
68+
const consoleSpy = spyOn(console, 'log').mockImplementation(() => {});
69+
70+
const result = await choosePackage('app123');
71+
72+
expect(result).toEqual(mockPackages[1] as any);
73+
expect(getAllPackagesSpy).toHaveBeenCalledWith('app123');
74+
expect(questionSpy).toHaveBeenCalled();
75+
76+
getAllPackagesSpy.mockRestore();
77+
questionSpy.mockRestore();
78+
consoleSpy.mockRestore();
79+
});
80+
81+
test('should continue to prompt until a valid ID is entered', async () => {
82+
const mockPackages = [
83+
{ id: '201', name: 'packageA' },
84+
];
85+
86+
const getAllPackagesSpy = spyOn(api, 'getAllPackages').mockResolvedValue(mockPackages as any);
87+
88+
const questionMock = mock();
89+
questionMock
90+
.mockResolvedValueOnce('999') // Invalid ID
91+
.mockResolvedValueOnce('201'); // Valid ID
92+
93+
const questionSpy = spyOn(utils, 'question').mockImplementation(questionMock);
94+
const consoleSpy = spyOn(console, 'log').mockImplementation(() => {});
95+
96+
const result = await choosePackage('app123');
97+
98+
expect(result).toEqual(mockPackages[0] as any);
99+
expect(questionMock).toHaveBeenCalledTimes(2);
100+
101+
getAllPackagesSpy.mockRestore();
102+
questionSpy.mockRestore();
103+
consoleSpy.mockRestore();
104+
});
105+
});

0 commit comments

Comments
 (0)