-
Notifications
You must be signed in to change notification settings - Fork 34
feat(broadcasts): add cancel command #359
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
3 commits
Select commit
Hold shift + click to select a range
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,38 @@ | ||
| import { Command } from '@commander-js/extra-typings'; | ||
| import { runWrite } from '../../lib/actions'; | ||
| import type { GlobalOpts } from '../../lib/client'; | ||
| import { buildHelpText } from '../../lib/help-text'; | ||
| import { pickId } from '../../lib/prompts'; | ||
| import { cancelBroadcastPickerConfig } from './utils'; | ||
|
|
||
| export const cancelBroadcastCommand = new Command('cancel') | ||
| .description('Cancel a queued or scheduled broadcast') | ||
| .argument('[id]', 'Broadcast ID') | ||
| .addHelpText( | ||
| 'after', | ||
| buildHelpText({ | ||
| context: `Only queued or scheduled broadcasts can be cancelled; draft and sent broadcasts cannot. | ||
| Cancelling a scheduled broadcast stops the scheduled delivery. Cancelling a queued | ||
| broadcast stops it mid-send — emails already sent are not affected.`, | ||
| output: ` {"object":"broadcast","id":"<id>"}`, | ||
| errorCodes: ['auth_error', 'cancel_error'], | ||
| examples: [ | ||
| 'resend broadcasts cancel d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6', | ||
| 'resend broadcasts cancel d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6 --json', | ||
| ], | ||
| }), | ||
| ) | ||
| .action(async (idArg, _opts, cmd) => { | ||
| const globalOpts = cmd.optsWithGlobals() as GlobalOpts; | ||
| const id = await pickId(idArg, cancelBroadcastPickerConfig, globalOpts); | ||
|
|
||
| await runWrite( | ||
| { | ||
| loading: 'Cancelling broadcast...', | ||
| sdkCall: (resend) => resend.broadcasts.cancel(id), | ||
| errorCode: 'cancel_error', | ||
| successMsg: 'Broadcast cancelled', | ||
| }, | ||
| globalOpts, | ||
| ); | ||
| }); | ||
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
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
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,136 @@ | ||
| import { | ||
| afterEach, | ||
| beforeEach, | ||
| describe, | ||
| expect, | ||
| it, | ||
| type MockInstance, | ||
| vi, | ||
| } from 'vitest'; | ||
| import { | ||
| captureTestEnv, | ||
| expectExit1, | ||
| mockExitThrow, | ||
| mockSdkError, | ||
| setNonInteractive, | ||
| setupOutputSpies, | ||
| } from '../../helpers'; | ||
|
|
||
| const mockCancel = vi.fn(async () => ({ | ||
| data: { object: 'broadcast', id: 'd1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6' }, | ||
| error: null, | ||
| })); | ||
|
|
||
| vi.mock('resend', () => ({ | ||
| Resend: class MockResend { | ||
| constructor(public key: string) {} | ||
| broadcasts = { cancel: mockCancel }; | ||
| }, | ||
| })); | ||
|
|
||
| describe('broadcasts cancel command', () => { | ||
| const restoreEnv = captureTestEnv(); | ||
| let spies: ReturnType<typeof setupOutputSpies> | undefined; | ||
| let errorSpy: MockInstance | undefined; | ||
| let stderrSpy: MockInstance | undefined; | ||
| let exitSpy: MockInstance | undefined; | ||
|
|
||
| beforeEach(() => { | ||
| process.env.RESEND_API_KEY = 're_test_key'; | ||
| mockCancel.mockClear(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| restoreEnv(); | ||
| errorSpy?.mockRestore(); | ||
| stderrSpy?.mockRestore(); | ||
| exitSpy?.mockRestore(); | ||
| spies = undefined; | ||
| errorSpy = undefined; | ||
| stderrSpy = undefined; | ||
| exitSpy = undefined; | ||
| }); | ||
|
|
||
| it('cancels broadcast by id', async () => { | ||
| spies = setupOutputSpies(); | ||
|
|
||
| const { cancelBroadcastCommand } = await import( | ||
| '../../../src/commands/broadcasts/cancel' | ||
| ); | ||
| await cancelBroadcastCommand.parseAsync( | ||
| ['d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6'], | ||
| { from: 'user' }, | ||
| ); | ||
|
|
||
| expect(mockCancel).toHaveBeenCalledTimes(1); | ||
| expect(mockCancel.mock.calls[0][0]).toBe( | ||
| 'd1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6', | ||
| ); | ||
| }); | ||
|
|
||
| it('outputs JSON id when non-interactive', async () => { | ||
| spies = setupOutputSpies(); | ||
|
|
||
| const { cancelBroadcastCommand } = await import( | ||
| '../../../src/commands/broadcasts/cancel' | ||
| ); | ||
| await cancelBroadcastCommand.parseAsync( | ||
| ['d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6'], | ||
| { from: 'user' }, | ||
| ); | ||
|
|
||
| const output = spies.logSpy.mock.calls[0][0] as string; | ||
| const parsed = JSON.parse(output); | ||
| expect(parsed.id).toBe('d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6'); | ||
| expect(parsed.object).toBe('broadcast'); | ||
| }); | ||
|
|
||
| it('errors with auth_error when no API key', async () => { | ||
| setNonInteractive(); | ||
| delete process.env.RESEND_API_KEY; | ||
| process.env.XDG_CONFIG_HOME = '/tmp/nonexistent-resend'; | ||
| errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
| exitSpy = mockExitThrow(); | ||
|
|
||
| const { cancelBroadcastCommand } = await import( | ||
| '../../../src/commands/broadcasts/cancel' | ||
| ); | ||
| await expectExit1(() => | ||
| cancelBroadcastCommand.parseAsync( | ||
| ['d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6'], | ||
| { from: 'user' }, | ||
| ), | ||
| ); | ||
|
|
||
| const output = errorSpy.mock.calls.map((c) => c[0]).join(' '); | ||
| expect(output).toContain('auth_error'); | ||
| }); | ||
|
|
||
| it('errors with cancel_error when SDK returns an error', async () => { | ||
| setNonInteractive(); | ||
| mockCancel.mockResolvedValueOnce( | ||
| mockSdkError( | ||
| 'Only queued or scheduled broadcasts can be canceled', | ||
| 'validation_error', | ||
| ), | ||
| ); | ||
| errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
| stderrSpy = vi | ||
| .spyOn(process.stderr, 'write') | ||
| .mockImplementation(() => true); | ||
| exitSpy = mockExitThrow(); | ||
|
|
||
| const { cancelBroadcastCommand } = await import( | ||
| '../../../src/commands/broadcasts/cancel' | ||
| ); | ||
| await expectExit1(() => | ||
| cancelBroadcastCommand.parseAsync( | ||
| ['00000000-0000-0000-0000-00000000bad0'], | ||
| { from: 'user' }, | ||
| ), | ||
| ); | ||
|
|
||
| const output = errorSpy.mock.calls.map((c) => c[0]).join(' '); | ||
| expect(output).toContain('cancel_error'); | ||
| }); | ||
| }); |
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.