-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add service-level auth token to email inbound flow #229
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
sweetmantech
merged 1 commit into
test
from
sweetmantech/myc-4270-generate-a-service-level-auth-token-for-the-email-flow-so
Feb 18, 2026
Merged
Changes from all commits
Commits
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
116 changes: 116 additions & 0 deletions
116
lib/emails/inbound/__tests__/validateNewEmailMemory.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,116 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { validateNewEmailMemory } from "../validateNewEmailMemory"; | ||
| import type { ResendEmailReceivedEvent } from "@/lib/emails/validateInboundEmailEvent"; | ||
| import { NextResponse } from "next/server"; | ||
|
|
||
| vi.mock("@/lib/supabase/account_emails/selectAccountEmails", () => ({ | ||
| default: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/emails/inbound/getEmailContent", () => ({ | ||
| getEmailContent: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/emails/inbound/getEmailRoomId", () => ({ | ||
| getEmailRoomId: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/emails/inbound/trimRepliedContext", () => ({ | ||
| trimRepliedContext: vi.fn((html: string) => html), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/chat/setupConversation", () => ({ | ||
| setupConversation: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/supabase/memory_emails/insertMemoryEmail", () => ({ | ||
| default: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/messages/getMessages", () => ({ | ||
| getMessages: vi.fn((text: string) => [{ role: "user", content: text }]), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/const", () => ({ | ||
| RECOUP_API_KEY: "test-recoup-api-key", | ||
| })); | ||
|
|
||
| import selectAccountEmails from "@/lib/supabase/account_emails/selectAccountEmails"; | ||
| import { getEmailContent } from "@/lib/emails/inbound/getEmailContent"; | ||
| import { getEmailRoomId } from "@/lib/emails/inbound/getEmailRoomId"; | ||
| import { setupConversation } from "@/lib/chat/setupConversation"; | ||
|
|
||
| const MOCK_ACCOUNT_ID = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"; | ||
| const MOCK_ROOM_ID = "11111111-2222-3333-4444-555555555555"; | ||
| const MOCK_EMAIL_ID = "email-123"; | ||
| const MOCK_MESSAGE_ID = "msg-456"; | ||
|
|
||
| function createMockEvent(overrides?: Partial<ResendEmailReceivedEvent["data"]>): ResendEmailReceivedEvent { | ||
| return { | ||
| type: "email.received", | ||
| created_at: "2024-01-01T00:00:00.000Z", | ||
| data: { | ||
| email_id: MOCK_EMAIL_ID, | ||
| from: "artist@example.com", | ||
| to: ["agent@mail.recoupable.com"], | ||
| subject: "Test email", | ||
| message_id: MOCK_MESSAGE_ID, | ||
| created_at: "2024-01-01T00:00:00.000Z", | ||
| ...overrides, | ||
| }, | ||
| } as ResendEmailReceivedEvent; | ||
| } | ||
|
|
||
| describe("validateNewEmailMemory", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
|
|
||
| vi.mocked(selectAccountEmails).mockResolvedValue([ | ||
| { account_id: MOCK_ACCOUNT_ID } as Awaited<ReturnType<typeof selectAccountEmails>>[0], | ||
| ]); | ||
|
|
||
| vi.mocked(getEmailContent).mockResolvedValue({ | ||
| html: "<p>Hello from email</p>", | ||
| headers: {}, | ||
| } as Awaited<ReturnType<typeof getEmailContent>>); | ||
|
|
||
| vi.mocked(getEmailRoomId).mockResolvedValue(undefined); | ||
|
|
||
| vi.mocked(setupConversation).mockResolvedValue({ roomId: MOCK_ROOM_ID }); | ||
| }); | ||
|
|
||
| it("includes authToken from RECOUP_API_KEY in chatRequestBody", async () => { | ||
| const event = createMockEvent(); | ||
|
|
||
| const result = await validateNewEmailMemory(event); | ||
|
|
||
| // Should not be a response (duplicate) | ||
| expect(result).not.toHaveProperty("response"); | ||
|
|
||
| const { chatRequestBody } = result as { chatRequestBody: { authToken?: string }; emailText: string }; | ||
| expect(chatRequestBody.authToken).toBe("test-recoup-api-key"); | ||
| }); | ||
|
|
||
| it("returns chatRequestBody with correct accountId, orgId, messages, and roomId", async () => { | ||
| const event = createMockEvent(); | ||
|
|
||
| const result = await validateNewEmailMemory(event); | ||
| const { chatRequestBody } = result as { chatRequestBody: Record<string, unknown>; emailText: string }; | ||
|
|
||
| expect(chatRequestBody.accountId).toBe(MOCK_ACCOUNT_ID); | ||
| expect(chatRequestBody.orgId).toBeNull(); | ||
| expect(chatRequestBody.roomId).toBe(MOCK_ROOM_ID); | ||
| expect(chatRequestBody.messages).toBeDefined(); | ||
| }); | ||
|
|
||
| it("returns duplicate response when setupConversation throws unique constraint error", async () => { | ||
| vi.mocked(setupConversation).mockRejectedValue({ code: "23505" }); | ||
|
|
||
| const event = createMockEvent(); | ||
| const result = await validateNewEmailMemory(event); | ||
|
|
||
| expect(result).toHaveProperty("response"); | ||
| const { response } = result as { response: NextResponse }; | ||
| expect(response.status).toBe(200); | ||
| }); | ||
| }); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RECOUP_API_KEYshould fail fast when unset, not silently fall back to"".PRIVY_PROJECT_SECRETon lines 3–5 guards against a missing env var at startup with an explicitthrow.RECOUP_API_KEYis equally load-bearing in production — it's the service-level auth token that gates MCP tool access in the email inbound flow. Falling back to""means the app starts up successfully even if this env var is never configured in Vercel, and every inbound email request will carry an emptyauthTokenwith no error signal — silently regressing to pre-PR, tool-less behavior. The PR description itself calls out configuring this env var as a manual step, making the silent-failure risk concrete.Apply the same startup-guard pattern used by
PRIVY_PROJECT_SECRET:🛡️ Proposed fix: fail fast on missing RECOUP_API_KEY
📝 Committable suggestion
🤖 Prompt for AI Agents