|
| 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