-
Notifications
You must be signed in to change notification settings - Fork 13
Add batch send sandbox email tool #135
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
4b31646
Add batch send sandbox email tool to reach parity with other SDKs
izikaj f16a265
Merge branch 'main' into add-bulk-send-sandbox-email-tool
izikaj 10f3a9d
Expose sandbox_id instead of the legacy test_inbox_id
izikaj c0591eb
List MAILTRAP_SANDBOX_ID before its legacy alias
izikaj 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
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
144 changes: 144 additions & 0 deletions
144
src/tools/sandbox/__tests__/batchSendSandboxEmail.test.ts
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,144 @@ | ||
| import batchSendSandboxEmail from "../batchSendSandboxEmail"; | ||
| import { getSandboxClient } from "../../../client"; | ||
|
|
||
| const mockClient = { | ||
| batchSend: jest.fn(), | ||
| }; | ||
|
|
||
| jest.mock("../../../client", () => ({ | ||
| getSandboxClient: jest.fn(() => mockClient), | ||
| })); | ||
|
|
||
| describe("batchSendSandboxEmail", () => { | ||
| const originalEnv = process.env; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| (getSandboxClient as jest.Mock).mockReturnValue(mockClient); | ||
| process.env = { ...originalEnv }; | ||
| delete process.env.MAILTRAP_SANDBOX_ID; | ||
| delete process.env.MAILTRAP_TEST_INBOX_ID; | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| process.env = originalEnv; | ||
| }); | ||
|
|
||
| it("uses the sandbox client for the given inbox and forwards the SDK payload", async () => { | ||
| mockClient.batchSend.mockResolvedValue({ | ||
| success: true, | ||
| responses: [{ success: true, message_ids: ["m-1"] }], | ||
| }); | ||
|
|
||
| const result = await batchSendSandboxEmail({ | ||
| sandbox_id: 4242, | ||
| base: { | ||
| from: { email: "sender@example.com", name: "Sender" }, | ||
| subject: "Sandbox hello", | ||
| text: "Hello sandbox", | ||
| }, | ||
| requests: [{ to: "alice@example.com" }], | ||
| }); | ||
|
|
||
| expect(getSandboxClient).toHaveBeenCalledWith(4242); | ||
| expect(mockClient.batchSend).toHaveBeenCalledWith({ | ||
| base: { | ||
| from: { email: "sender@example.com", name: "Sender" }, | ||
| subject: "Sandbox hello", | ||
| text: "Hello sandbox", | ||
| }, | ||
| requests: [{ to: [{ email: "alice@example.com" }] }], | ||
| }); | ||
| expect(result.isError).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("does not forward sandbox_id into the SDK payload", async () => { | ||
| mockClient.batchSend.mockResolvedValue({ success: true, responses: [] }); | ||
|
|
||
| await batchSendSandboxEmail({ | ||
| sandbox_id: 4242, | ||
| base: { from: "sender@example.com", subject: "Hi", text: "x" }, | ||
| requests: [{ to: "alice@example.com" }], | ||
| }); | ||
|
|
||
| const payload = mockClient.batchSend.mock.calls[0][0]; | ||
| expect(payload.base).not.toHaveProperty("sandbox_id"); | ||
| expect(payload).not.toHaveProperty("sandbox_id"); | ||
| }); | ||
|
|
||
| it("falls back to MAILTRAP_SANDBOX_ID when sandbox_id is omitted", async () => { | ||
| process.env.MAILTRAP_SANDBOX_ID = "777"; | ||
| mockClient.batchSend.mockResolvedValue({ success: true, responses: [] }); | ||
|
|
||
| const result = await batchSendSandboxEmail({ | ||
| base: { from: "sender@example.com", subject: "Hi", text: "x" }, | ||
| requests: [{ to: "alice@example.com" }], | ||
| }); | ||
|
|
||
| expect(getSandboxClient).toHaveBeenCalledWith(777); | ||
| expect(result.isError).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("falls back to the legacy MAILTRAP_TEST_INBOX_ID, but MAILTRAP_SANDBOX_ID wins", async () => { | ||
| process.env.MAILTRAP_TEST_INBOX_ID = "777"; | ||
| mockClient.batchSend.mockResolvedValue({ success: true, responses: [] }); | ||
|
|
||
| await batchSendSandboxEmail({ | ||
| base: { from: "sender@example.com", subject: "Hi", text: "x" }, | ||
| requests: [{ to: "alice@example.com" }], | ||
| }); | ||
|
|
||
| expect(getSandboxClient).toHaveBeenCalledWith(777); | ||
|
|
||
| process.env.MAILTRAP_SANDBOX_ID = "888"; | ||
|
|
||
| await batchSendSandboxEmail({ | ||
| base: { from: "sender@example.com", subject: "Hi", text: "x" }, | ||
| requests: [{ to: "alice@example.com" }], | ||
| }); | ||
|
|
||
| expect(getSandboxClient).toHaveBeenLastCalledWith(888); | ||
| }); | ||
|
|
||
| it("errors when no sandbox is configured", async () => { | ||
| const result = await batchSendSandboxEmail({ | ||
| base: { from: "sender@example.com", subject: "Hi", text: "x" }, | ||
| requests: [{ to: "alice@example.com" }], | ||
| }); | ||
|
|
||
| expect(result.isError).toBe(true); | ||
| expect(result.content[0].text).toBe( | ||
| "Failed to batch send sandbox email: Provide sandbox_id or set MAILTRAP_SANDBOX_ID environment variable for sandbox mode" | ||
| ); | ||
| expect(mockClient.batchSend).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("propagates payload validation errors", async () => { | ||
| const result = await batchSendSandboxEmail({ | ||
| sandbox_id: 4242, | ||
| base: { from: "sender@example.com", subject: "Hi", text: "x" }, | ||
| requests: [{}], | ||
| }); | ||
|
|
||
| expect(result.isError).toBe(true); | ||
| expect(result.content[0].text).toContain( | ||
| "Failed to batch send sandbox email: requests[0]: provide at least one recipient" | ||
| ); | ||
| expect(mockClient.batchSend).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("surfaces API errors with a sandbox-specific prefix", async () => { | ||
| mockClient.batchSend.mockRejectedValue(new Error("inbox is full")); | ||
|
|
||
| const result = await batchSendSandboxEmail({ | ||
| sandbox_id: 4242, | ||
| base: { from: "sender@example.com", subject: "Hi", text: "x" }, | ||
| requests: [{ to: "alice@example.com" }], | ||
| }); | ||
|
|
||
| expect(result.isError).toBe(true); | ||
| expect(result.content[0].text).toBe( | ||
| "Failed to batch send sandbox email: inbox is full" | ||
| ); | ||
| }); | ||
| }); |
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,32 @@ | ||
| import { getSandboxClient } from "../../client"; | ||
| import { BatchSendSandboxEmailToolRequest } from "../../types/mailtrap"; | ||
| import buildBatchPayload from "../sendEmail/buildBatchPayload"; | ||
| import { | ||
| buildErrorResponse, | ||
| buildSuccessResponse, | ||
| ToolResponse, | ||
| } from "../utils/responses"; | ||
| import resolveSandboxId from "./utils/resolveSandboxId"; | ||
|
|
||
| async function batchSendSandboxEmail({ | ||
| sandbox_id, | ||
| ...body | ||
| }: BatchSendSandboxEmailToolRequest): Promise<ToolResponse> { | ||
| try { | ||
| const inboxId = resolveSandboxId(sandbox_id); | ||
|
|
||
| const payload = buildBatchPayload(body); | ||
|
|
||
| const mailtrap = getSandboxClient(inboxId); | ||
|
|
||
| const response = await mailtrap.batchSend( | ||
| payload as unknown as Parameters<typeof mailtrap.batchSend>[0] | ||
| ); | ||
|
|
||
| return buildSuccessResponse(JSON.stringify(response, null, 2)); | ||
| } catch (error) { | ||
| return buildErrorResponse("batch send sandbox email", error); | ||
| } | ||
| } | ||
|
|
||
| export default batchSendSandboxEmail; |
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,15 @@ | ||
| import batchSendStreamEmailSchema from "../../sendEmail/schemas/batchSendStreamEmail"; | ||
|
|
||
| const batchSendSandboxEmailSchema = { | ||
| ...batchSendStreamEmailSchema, | ||
| properties: { | ||
| sandbox_id: { | ||
| type: "number", | ||
| description: | ||
| "Mailtrap sandbox (test inbox) ID. Optional if MAILTRAP_SANDBOX_ID env var is set. Use to target a specific sandbox.", | ||
| }, | ||
| ...batchSendStreamEmailSchema.properties, | ||
| }, | ||
| }; | ||
|
|
||
| export default batchSendSandboxEmailSchema; |
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.
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.