diff --git a/src/package.ts b/src/package.ts index 88a5829..be7470d 100644 --- a/src/package.ts +++ b/src/package.ts @@ -175,10 +175,11 @@ export async function listPackage(appId: string) { export async function choosePackage(appId: string) { const list = await listPackage(appId); + const packageMap = new Map(list?.map((v) => [v.id.toString(), v])); while (true) { const id = await question(t('enterNativePackageId')); - const app = list?.find((v) => v.id.toString() === id); + const app = packageMap.get(id); if (app) { return app; } diff --git a/tests/package-optimization.test.ts b/tests/package-optimization.test.ts new file mode 100644 index 0000000..512340a --- /dev/null +++ b/tests/package-optimization.test.ts @@ -0,0 +1,105 @@ +import { describe, expect, test, mock, spyOn } from 'bun:test'; + +// Mock modules before any imports +mock.module('filesize-parser', () => ({ default: () => 0 })); +mock.module('form-data', () => ({ default: class {} })); +mock.module('node-fetch', () => ({ default: () => {} })); +mock.module('progress', () => ({ default: class { tick() {} } })); +mock.module('tcp-ping', () => ({ default: { ping: () => {} } })); +mock.module('tty-table', () => { + const mockTable = () => ({ render: () => '' }); + return { default: mockTable }; +}); +mock.module('read', () => ({ read: () => {} })); +mock.module('chalk', () => ({ + default: { + green: (s: string) => s, + red: (s: string) => s, + yellow: (s: string) => s, + }, +})); +mock.module('i18next', () => ({ + default: { + t: (k: string) => k, + use: () => ({ init: () => {} }), + init: () => {}, + }, +})); +mock.module('fs-extra', () => ({ + default: { + existsSync: () => false, + readFileSync: () => '', + ensureDirSync: () => {}, + pathExists: async () => false, + remove: async () => {}, + }, +})); +mock.module('compare-versions', () => ({ + satisfies: () => true, +})); +mock.module('yauzl', () => ({ + open: () => {}, +})); +mock.module('isomorphic-git', () => ({})); +mock.module('global-dirs', () => ({ + npm: { packages: '' }, + yarn: { packages: '' }, +})); +mock.module('registry-auth-token', () => ({})); +mock.module('registry-auth-token/registry-url', () => () => 'https://registry.npmjs.org'); +mock.module('semver', () => ({})); +mock.module('semver/functions/gt', () => () => true); +mock.module('semver/ranges/max-satisfying', () => (versions: string[]) => versions[0]); +mock.module('bytebuffer', () => ({})); + +import * as api from '../src/api'; +import * as utils from '../src/utils'; +import { choosePackage } from '../src/package'; + +describe('choosePackage optimization', () => { + test('should return the correct package when a valid ID is entered', async () => { + const mockPackages = [ + { id: '101', name: 'package1' }, + { id: '102', name: 'package2' }, + ]; + + const getAllPackagesSpy = spyOn(api, 'getAllPackages').mockResolvedValue(mockPackages as any); + const questionSpy = spyOn(utils, 'question').mockResolvedValue('102'); + const consoleSpy = spyOn(console, 'log').mockImplementation(() => {}); + + const result = await choosePackage('app123'); + + expect(result).toEqual(mockPackages[1] as any); + expect(getAllPackagesSpy).toHaveBeenCalledWith('app123'); + expect(questionSpy).toHaveBeenCalled(); + + getAllPackagesSpy.mockRestore(); + questionSpy.mockRestore(); + consoleSpy.mockRestore(); + }); + + test('should continue to prompt until a valid ID is entered', async () => { + const mockPackages = [ + { id: '201', name: 'packageA' }, + ]; + + const getAllPackagesSpy = spyOn(api, 'getAllPackages').mockResolvedValue(mockPackages as any); + + const questionMock = mock(); + questionMock + .mockResolvedValueOnce('999') // Invalid ID + .mockResolvedValueOnce('201'); // Valid ID + + const questionSpy = spyOn(utils, 'question').mockImplementation(questionMock); + const consoleSpy = spyOn(console, 'log').mockImplementation(() => {}); + + const result = await choosePackage('app123'); + + expect(result).toEqual(mockPackages[0] as any); + expect(questionMock).toHaveBeenCalledTimes(2); + + getAllPackagesSpy.mockRestore(); + questionSpy.mockRestore(); + consoleSpy.mockRestore(); + }); +});