-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add apps:builds:share command and --share flag on apps:builds:create
#178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cd8a648
feat: add `apps:builds:share` command and `--share` flag on `apps:bui…
robingenz d331dfa
fix: validate share expiry days and reject share sub-flags without `-…
robingenz 590fd58
feat: use PNG format for share QR code URLs
robingenz 6419adb
feat: add `--build-number` option to `apps:builds:share`
robingenz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { DEFAULT_API_BASE_URL, DEFAULT_CONSOLE_BASE_URL } from '@/config/consts.js'; | ||
| import authorizationService from '@/services/authorization-service.js'; | ||
| import userConfig from '@/utils/user-config.js'; | ||
| import consola from 'consola'; | ||
| import nock from 'nock'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import createCommand from './create.js'; | ||
|
|
||
| // Mock dependencies | ||
| vi.mock('@/utils/user-config.js'); | ||
| vi.mock('@/utils/prompt.js'); | ||
| vi.mock('@/services/authorization-service.js'); | ||
| vi.mock('@/utils/job.js'); | ||
| vi.mock('consola'); | ||
|
|
||
| vi.mock('@/utils/environment.js', () => ({ | ||
| isInteractive: () => false, | ||
| })); | ||
|
|
||
| describe('apps-builds-create', () => { | ||
| const mockUserConfig = vi.mocked(userConfig); | ||
| const mockAuthorizationService = vi.mocked(authorizationService); | ||
| const mockConsola = vi.mocked(consola); | ||
|
|
||
| const testToken = 'test-token'; | ||
| const appId = '00000000-0000-0000-0000-000000000001'; | ||
| const buildId = '00000000-0000-0000-0000-000000000002'; | ||
| const validAppId = '11111111-1111-4111-8111-111111111111'; | ||
| const shareId = 'share-abc'; | ||
|
|
||
| beforeEach(async () => { | ||
| vi.clearAllMocks(); | ||
|
|
||
| mockUserConfig.read.mockReturnValue({ token: testToken }); | ||
| mockAuthorizationService.hasAuthorizationToken.mockReturnValue(true); | ||
| mockAuthorizationService.getCurrentAuthorizationToken.mockReturnValue(testToken); | ||
|
|
||
| // Mock waitForJobCompletion to resolve immediately as succeeded | ||
| const jobUtils = await import('@/utils/job.js'); | ||
| vi.mocked(jobUtils.waitForJobCompletion).mockResolvedValue({ | ||
| id: 'job-1', | ||
| status: 'succeeded', | ||
| createdAt: '2024-01-01T00:00:00Z', | ||
| }); | ||
|
|
||
| vi.spyOn(process, 'exit').mockImplementation((code?: string | number | null | undefined) => { | ||
| throw new Error(`Process exited with code ${code}`); | ||
| }); | ||
| vi.spyOn(console, 'log').mockImplementation(() => {}); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| nock.cleanAll(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('should create a share and merge it into the JSON output', async () => { | ||
| const options = { appId, platform: 'web' as const, gitRef: 'main', share: true, json: true }; | ||
|
|
||
| const buildScope = nock(DEFAULT_API_BASE_URL) | ||
| .post(`/v1/apps/${appId}/builds`) | ||
| .matchHeader('Authorization', `Bearer ${testToken}`) | ||
| .reply(201, { id: buildId, jobId: 'job-1', numberAsString: '42' }); | ||
|
|
||
| const findScope = nock(DEFAULT_API_BASE_URL) | ||
| .get(`/v1/apps/${appId}/builds/${buildId}`) | ||
| .query({ relations: 'appBuildArtifacts' }) | ||
| .reply(200, { id: buildId, appBuildArtifacts: [] }); | ||
|
|
||
| const shareScope = nock(DEFAULT_API_BASE_URL) | ||
| .post(`/v1/apps/${appId}/builds/${buildId}/shares`, {}) | ||
| .matchHeader('Authorization', `Bearer ${testToken}`) | ||
| .reply(201, { | ||
| id: shareId, | ||
| appBuildId: buildId, | ||
| description: null, | ||
| expiresAt: null, | ||
| createdAt: '2024-01-01T00:00:00Z', | ||
| updatedAt: '2024-01-01T00:00:00Z', | ||
| }); | ||
|
|
||
| await createCommand.action(options, undefined); | ||
|
|
||
| expect(buildScope.isDone()).toBe(true); | ||
| expect(findScope.isDone()).toBe(true); | ||
| expect(shareScope.isDone()).toBe(true); | ||
|
|
||
| const url = `${DEFAULT_CONSOLE_BASE_URL}/app-build-shares/${shareId}`; | ||
| const qrCodeUrl = `${DEFAULT_API_BASE_URL}/v1/qrcodes?content=${encodeURIComponent(url)}&format=png`; | ||
| expect(console.log).toHaveBeenCalledWith( | ||
| JSON.stringify( | ||
| { | ||
| id: buildId, | ||
| numberAsString: '42', | ||
| share: { id: shareId, url, qrCodeUrl, expiresAt: null }, | ||
| }, | ||
| null, | ||
| 2, | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| it('should reject --share combined with --detached', async () => { | ||
| const options = { appId, platform: 'web' as const, gitRef: 'main', share: true, detached: true }; | ||
|
|
||
| await expect(createCommand.action(options, undefined)).rejects.toThrow('Process exited with code 1'); | ||
|
|
||
| expect(mockConsola.error).toHaveBeenCalledWith( | ||
| 'The --detached flag cannot be used with --share. Sharing requires waiting for completion.', | ||
| ); | ||
| }); | ||
|
|
||
| it('should reject --share-description without --share', async () => { | ||
| const options = { appId, platform: 'web' as const, gitRef: 'main', shareDescription: 'What to test' }; | ||
|
|
||
| await expect(createCommand.action(options, undefined)).rejects.toThrow('Process exited with code 1'); | ||
|
|
||
| expect(mockConsola.error).toHaveBeenCalledWith( | ||
| 'The --share-description and --share-expires-in-days flags require --share.', | ||
| ); | ||
| }); | ||
|
|
||
| it('should reject --share-expires-in-days without --share', async () => { | ||
| const options = { appId, platform: 'web' as const, gitRef: 'main', shareExpiresInDays: 7 }; | ||
|
|
||
| await expect(createCommand.action(options, undefined)).rejects.toThrow('Process exited with code 1'); | ||
|
|
||
| expect(mockConsola.error).toHaveBeenCalledWith( | ||
| 'The --share-description and --share-expires-in-days flags require --share.', | ||
| ); | ||
| }); | ||
|
|
||
| it('should reject a non-positive shareExpiresInDays value', () => { | ||
| const schema = createCommand.options?.schema; | ||
|
|
||
| for (const shareExpiresInDays of [0, -1]) { | ||
| const result = schema?.safeParse({ | ||
| appId: validAppId, | ||
| platform: 'web', | ||
| gitRef: 'main', | ||
| share: true, | ||
| shareExpiresInDays, | ||
| }); | ||
| expect(result?.success).toBe(false); | ||
| expect(result?.error?.issues[0]?.message).toBe('Expires in days must be a positive integer.'); | ||
| } | ||
| }); | ||
|
|
||
| it('should accept a positive shareExpiresInDays value', () => { | ||
| const schema = createCommand.options?.schema; | ||
|
|
||
| const result = schema?.safeParse({ | ||
| appId: validAppId, | ||
| platform: 'web', | ||
| gitRef: 'main', | ||
| share: true, | ||
| shareExpiresInDays: 7, | ||
| }); | ||
| expect(result?.success).toBe(true); | ||
| expect(result?.data?.shareExpiresInDays).toBe(7); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.