Skip to content

Commit

Permalink
add more app upload tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dd-spells committed Mar 19, 2024
1 parent 98f0cf4 commit ee2deab
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/commands/synthetics/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ describe('run-test', () => {
expect.anything(),
expect.anything(),
expect.anything(),
expect.anything(),
]
}

Expand Down
20 changes: 15 additions & 5 deletions src/commands/synthetics/__tests__/mobile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ describe('uploadMobileApplicationVersion', () => {
return {fileName: 'abc-123', appUploadResponse: APP_UPLOAD_POLL_RESULTS}
})

await mobile.uploadMobileApplicationVersion(config)
const mockAppUploadReporter = getMockAppUploadReporter()
await mobile.uploadMobileApplicationVersion(config, mockAppUploadReporter)

expect(uploadMobileApplicationSpy).toHaveBeenCalledWith(
expect.anything(),
Expand All @@ -300,33 +301,42 @@ describe('uploadMobileApplicationVersion', () => {
isLatest: uploadCommandConfig.latest,
}
)
expect(mockAppUploadReporter.start).toHaveBeenCalledWith([{
appId: uploadCommandConfig.mobileApplicationId,
appPath: uploadCommandConfig.mobileApplicationVersionFilePath,
versionName: uploadCommandConfig.versionName,
}])
expect(mockAppUploadReporter.renderProgress).toHaveBeenCalledWith(1)
expect(mockAppUploadReporter.reportSuccess).toHaveBeenCalledTimes(1)
})

test('get pre-signed URL fails', async () => {
uploadMobileApplicationSpy.mockImplementation(() => {
throw new EndpointError('mock fail', 1)
})

await expect(mobile.uploadMobileApplicationVersion(config)).rejects.toThrow(EndpointError)
const mockAppUploadReporter = getMockAppUploadReporter()
await expect(mobile.uploadMobileApplicationVersion(config, mockAppUploadReporter)).rejects.toThrow(EndpointError)
expect(mockAppUploadReporter.reportFailure).toHaveBeenCalledTimes(1)
})

test('missing mobile application ID', async () => {
config.mobileApplicationId = ''
await expect(mobile.uploadMobileApplicationVersion(config)).rejects.toThrow(CiError)
await expect(mobile.uploadMobileApplicationVersion(config, getMockAppUploadReporter())).rejects.toThrow(CiError)

expect(uploadMobileApplicationSpy).toHaveBeenCalledTimes(0)
})

test('missing mobile application file', async () => {
delete config.mobileApplicationVersionFilePath
await expect(mobile.uploadMobileApplicationVersion(config)).rejects.toThrow(CiError)
await expect(mobile.uploadMobileApplicationVersion(config, getMockAppUploadReporter())).rejects.toThrow(CiError)

expect(uploadMobileApplicationSpy).toHaveBeenCalledTimes(0)
})

test('missing version name', async () => {
delete config.versionName
await expect(mobile.uploadMobileApplicationVersion(config)).rejects.toThrow(CiError)
await expect(mobile.uploadMobileApplicationVersion(config, getMockAppUploadReporter())).rejects.toThrow(CiError)

expect(uploadMobileApplicationSpy).toHaveBeenCalledTimes(0)
})
Expand Down
86 changes: 86 additions & 0 deletions src/commands/synthetics/__tests__/reporters/appUpload.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {BaseContext} from 'clipanion/lib/advanced'

import { AppUploadDetails } from '../../interfaces'
import { AppUploadReporter } from '../../reporters/appUpload'

describe('AppUploadReporter', () => {
let reporter: AppUploadReporter

beforeEach(() => {
const writeMock = jest.fn()
const mockContext: unknown = {
stdout: {write: writeMock},
}
reporter = new AppUploadReporter(mockContext as BaseContext)

})

afterEach(() => {
// Clean up any mocks
jest.restoreAllMocks()
})

describe('start', () => {
test('should write the correct output', () => {
const appsToUpload: AppUploadDetails[] = [
{
versionName: '1.0.0',
appId: '123',
appPath: '/path/to/app',
},
{
versionName: '2.0.0',
appId: '456',
appPath: '/path/to/another/app',
},
]

reporter.start(appsToUpload)

expect(reporter['context'].stdout.write).toHaveBeenCalledWith(
expect.stringContaining('2 mobile application(s) to upload:')
)
expect(reporter['context'].stdout.write).toHaveBeenCalledWith(
expect.stringContaining('Version 1.0.0 - Application ID 123 - Local Path /path/to/app')
)
expect(reporter['context'].stdout.write).toHaveBeenCalledWith(
expect.stringContaining('Version 2.0.0 - Application ID 456 - Local Path /path/to/another/app')
)
})
})

describe('reportSuccess', () => {
test('should write the correct output', () => {
reporter.reportSuccess()

// Assert that the correct output is written to stdout
expect(reporter['context'].stdout.write).toHaveBeenCalledWith(
expect.stringContaining('Uploaded applications in')
)
})
})

describe('reportFailure', () => {
test('should write the correct output', () => {
const error = new Error('Failed to upload')
const failedApp: AppUploadDetails = {
versionName: '1.0.0',
appId: '123',
appPath: '/path/to/app',
}

reporter.reportFailure(error, failedApp)

// Assert that the correct output is written to stdout
expect(reporter['context'].stdout.write).toHaveBeenCalledWith(
expect.stringContaining('Failed to upload application:')
)
expect(reporter['context'].stdout.write).toHaveBeenCalledWith(
expect.stringContaining('Version 1.0.0 - Application ID 123 - Local Path /path/to/app')
)
expect(reporter['context'].stdout.write).toHaveBeenCalledWith(
expect.stringContaining(error.message)
)
})
})
})
29 changes: 22 additions & 7 deletions src/commands/synthetics/mobile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ export const overrideMobileConfig = (
}

export const uploadMobileApplicationVersion = async (
config: UploadApplicationCommandConfig
config: UploadApplicationCommandConfig,
appUploadReporter: AppUploadReporter
): Promise<MobileAppUploadResult> => {
const api = getApiHelper(config)

Expand All @@ -215,12 +216,26 @@ export const uploadMobileApplicationVersion = async (
isLatest: config.latest,
} as MobileApplicationNewVersionParams

const {appUploadResponse} = await uploadMobileApplication(
api,
config.mobileApplicationVersionFilePath,
config.mobileApplicationId,
newVersionParams
)
const appRenderingInfo = {
appId: config.mobileApplicationId,
appPath: config.mobileApplicationVersionFilePath,
versionName: config.versionName,
}
appUploadReporter.start([appRenderingInfo])
appUploadReporter.renderProgress(1)
let appUploadResponse: MobileAppUploadResult
try {
({appUploadResponse} = await uploadMobileApplication(
api,
config.mobileApplicationVersionFilePath,
config.mobileApplicationId,
newVersionParams
))
appUploadReporter.reportSuccess()
} catch (error){
appUploadReporter.reportFailure(error, appRenderingInfo)
throw error
}

return appUploadResponse
}
Expand Down
21 changes: 8 additions & 13 deletions src/commands/synthetics/upload-application-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import terminalLink from 'terminal-link'
import {LogLevel, Logger} from '../../helpers/logger'
import {removeUndefinedValues, resolveConfigFromFile} from '../../helpers/utils'

import { EndpointError } from './api'
import { CiError, CriticalError } from './errors'
import {UploadApplicationCommandConfig} from './interfaces'
import {uploadMobileApplicationVersion} from './mobile'
import { AppUploadReporter } from './reporters/appUpload'
Expand Down Expand Up @@ -53,9 +55,7 @@ export class UploadApplicationCommand extends Command {
private mobileApplicationId = Option.String('--mobileApplicationId', {
description: 'ID of the application you want to upload the new version to.',
})
private versionName = Option.String('--versionName', {
description: 'Name of the new version. It has to be unique.',
})
private versionName = Option.String('--versionName', {description: 'Name of the new version. It has to be unique.'})
private latest = Option.Boolean('--latest', {
description:
'Marks the application as `latest`. Any tests that run on the latest version will use this version on their next run.',
Expand All @@ -75,19 +75,14 @@ export class UploadApplicationCommand extends Command {

return 1
}

const appUploadReporter = new AppUploadReporter(this.context)
const appRenderingInfo = {
appId: this.config.mobileApplicationId!,
appPath: this.config.mobileApplicationVersionFilePath!,
versionName: this.config.versionName!,
}
appUploadReporter.start([appRenderingInfo])
appUploadReporter.renderProgress(1)
try {
await uploadMobileApplicationVersion(this.config)
appUploadReporter.reportSuccess()
await uploadMobileApplicationVersion(this.config, appUploadReporter)
} catch (error) {
appUploadReporter.reportFailure(error, appRenderingInfo)
if (error instanceof CiError || error instanceof EndpointError || error instanceof CriticalError) {
this.logger.error(`Error: ${error.message}`)
}

return 1
}
Expand Down

0 comments on commit ee2deab

Please sign in to comment.