|
| 1 | +import { afterEach, describe, expect, spyOn, test } from 'bun:test'; |
| 2 | +import fs from 'fs'; |
| 3 | +import * as api from '../src/api'; |
| 4 | +import { getAppCommands, getSelectedApp } from '../src/app'; |
| 5 | +import { |
| 6 | + createPublishBundleRequest, |
| 7 | + normalizeBundleOptions, |
| 8 | +} from '../src/bundle'; |
| 9 | + |
| 10 | +describe('bundle target context', () => { |
| 11 | + test('preserves appId and config in the publish request', () => { |
| 12 | + const normalized = normalizeBundleOptions( |
| 13 | + { |
| 14 | + appId: '42', |
| 15 | + config: 'configs/release.update.json', |
| 16 | + }, |
| 17 | + 'ios', |
| 18 | + ); |
| 19 | + |
| 20 | + expect(normalized.appId).toBe('42'); |
| 21 | + expect(normalized.config).toBe('configs/release.update.json'); |
| 22 | + expect( |
| 23 | + createPublishBundleRequest('dist/ios.ppk', 'ios', { |
| 24 | + appId: normalized.appId, |
| 25 | + config: normalized.config, |
| 26 | + name: 'v1', |
| 27 | + }), |
| 28 | + ).toEqual({ |
| 29 | + args: ['dist/ios.ppk'], |
| 30 | + options: { |
| 31 | + platform: 'ios', |
| 32 | + appId: '42', |
| 33 | + config: 'configs/release.update.json', |
| 34 | + name: 'v1', |
| 35 | + }, |
| 36 | + }); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +describe('app config target', () => { |
| 41 | + let postSpy: ReturnType<typeof spyOn>; |
| 42 | + let getSpy: ReturnType<typeof spyOn>; |
| 43 | + let readFileSpy: ReturnType<typeof spyOn>; |
| 44 | + let writeFileSpy: ReturnType<typeof spyOn>; |
| 45 | + let consoleLogSpy: ReturnType<typeof spyOn>; |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + postSpy?.mockRestore(); |
| 49 | + getSpy?.mockRestore(); |
| 50 | + readFileSpy?.mockRestore(); |
| 51 | + writeFileSpy?.mockRestore(); |
| 52 | + consoleLogSpy?.mockRestore(); |
| 53 | + }); |
| 54 | + |
| 55 | + test('getSelectedApp reads the explicit config path', async () => { |
| 56 | + readFileSpy = spyOn(fs.promises, 'readFile').mockResolvedValue( |
| 57 | + JSON.stringify({ ios: { appId: 42, appKey: 'key-42' } }), |
| 58 | + ); |
| 59 | + |
| 60 | + await expect( |
| 61 | + getSelectedApp('ios', 'configs/release.update.json'), |
| 62 | + ).resolves.toEqual({ |
| 63 | + appId: '42', |
| 64 | + appKey: 'key-42', |
| 65 | + platform: 'ios', |
| 66 | + }); |
| 67 | + expect(readFileSpy).toHaveBeenCalledWith( |
| 68 | + 'configs/release.update.json', |
| 69 | + 'utf8', |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + test('createApp selects the new app in the explicit config file', async () => { |
| 74 | + postSpy = spyOn(api, 'post').mockResolvedValue({ id: 10 }); |
| 75 | + getSpy = spyOn(api, 'get').mockResolvedValue({ appKey: 'key-ios-10' }); |
| 76 | + const enoentError = Object.assign(new Error('ENOENT'), { code: 'ENOENT' }); |
| 77 | + readFileSpy = spyOn(fs.promises, 'readFile').mockRejectedValue(enoentError); |
| 78 | + writeFileSpy = spyOn(fs.promises, 'writeFile').mockResolvedValue(); |
| 79 | + consoleLogSpy = spyOn(console, 'log').mockImplementation(() => {}); |
| 80 | + |
| 81 | + await getAppCommands().createApp({ |
| 82 | + options: { |
| 83 | + name: 'SmallWOD', |
| 84 | + downloadUrl: '', |
| 85 | + platform: 'ios', |
| 86 | + config: 'configs/release.update.json', |
| 87 | + }, |
| 88 | + }); |
| 89 | + |
| 90 | + expect(readFileSpy).toHaveBeenCalledWith( |
| 91 | + 'configs/release.update.json', |
| 92 | + 'utf8', |
| 93 | + ); |
| 94 | + expect(writeFileSpy).toHaveBeenCalledWith( |
| 95 | + 'configs/release.update.json', |
| 96 | + JSON.stringify( |
| 97 | + { |
| 98 | + ios: { |
| 99 | + appId: 10, |
| 100 | + appKey: 'key-ios-10', |
| 101 | + }, |
| 102 | + }, |
| 103 | + null, |
| 104 | + 4, |
| 105 | + ), |
| 106 | + 'utf8', |
| 107 | + ); |
| 108 | + }); |
| 109 | +}); |
0 commit comments