diff --git a/scheduler/.env.example b/scheduler/.env.example index c62d6b89..b01c6d68 100644 --- a/scheduler/.env.example +++ b/scheduler/.env.example @@ -60,6 +60,12 @@ SCHEDULER_ENCRYPTION_KEY= # Generate the 48-character lowercase hexadecimal secret with: openssl rand -hex 24 # MUSE_REVIEW_LOGIN_PASSWORD= +# Optional publish failure fixture. Set to the connected account ID of the test profile. +# Use only that profile and the exact message "SimplePost QA failure test." +# to record a failed post and emit post.failed without publishing to the platform. +# Unset after testing. +# QA_PUBLISH_FAILURE_ACCOUNT_ID= + # Price IDs must be recurring monthly prices with USD default and EUR currency options. STRIPE_SECRET_KEY=sk_test_your_stripe_secret_key STRIPE_WEBHOOK_SECRET=whsec_your_stripe_webhook_secret diff --git a/scheduler/lib/env.ts b/scheduler/lib/env.ts index 43367745..6caddafd 100644 --- a/scheduler/lib/env.ts +++ b/scheduler/lib/env.ts @@ -63,6 +63,9 @@ export const env = { get BILLING_EMAIL_MONITOR_BCC() { return getRuntimeEnvironmentVariable("BILLING_EMAIL_MONITOR_BCC")?.trim() || undefined; }, + get QA_PUBLISH_FAILURE_ACCOUNT_ID() { + return getRuntimeEnvironmentVariable("QA_PUBLISH_FAILURE_ACCOUNT_ID")?.trim() || undefined; + }, /** * Self-hosted mode: disables all billing and Stripe integration. Every user * gets unrestricted access, billing UI is hidden, and Stripe environment diff --git a/scheduler/lib/posting/index.ts b/scheduler/lib/posting/index.ts index 35b13c49..b9357f20 100644 --- a/scheduler/lib/posting/index.ts +++ b/scheduler/lib/posting/index.ts @@ -13,6 +13,7 @@ import { } from "@simple-post/sdk"; import { generatePostUrl, mapPlatformName } from "@simple-post/sdk/platform-names"; +import { env } from "@/lib/env"; import { postingLogger, serializeError, redact } from "@/lib/logger"; import { acquireConnectedAccountCredentialLock, @@ -332,6 +333,8 @@ interface AccountSegment { mediaFiles: MediaFile[]; } +const QA_PUBLISH_FAILURE_MESSAGE = "SimplePost QA failure test."; + async function postSegmentsToAccount( segments: AccountSegment[], account: ConnectedAccount, @@ -341,6 +344,17 @@ async function postSegmentsToAccount( postId: string = randomUUID(), tiktokRetryApproved = false, ): Promise { + if (account.id === env.QA_PUBLISH_FAILURE_ACCOUNT_ID && segments[0]?.message === QA_PUBLISH_FAILURE_MESSAGE) { + // Return a terminal result so the caller persists the failure and sends the normal webhook. + return { + accountId: account.id, + platform: account.platform, + success: false, + error: "QA_PUBLISH_FAILURE", + message: "Intentional test publish failure.", + }; + } + const log = postingLogger.child({ fn: "postSegmentsToAccount", accountId: account.id, diff --git a/scheduler/tests/lib/posting/batch-failures.test.ts b/scheduler/tests/lib/posting/batch-failures.test.ts index 3f666879..4afd1197 100644 --- a/scheduler/tests/lib/posting/batch-failures.test.ts +++ b/scheduler/tests/lib/posting/batch-failures.test.ts @@ -41,9 +41,61 @@ beforeEach(() => { }); afterEach(() => { + jest.restoreAllMocks(); jest.useRealTimers(); }); +const qaMessage = "SimplePost QA failure test."; + +it("reports the configured QA failure without preparing media or publishing", async () => { + jest.replaceProperty(process, "env", { ...process.env, QA_PUBLISH_FAILURE_ACCOUNT_ID: "edmund-qa" }); + jest.mocked(prisma.connectedAccount.findMany).mockResolvedValue([{ id: "edmund-qa", platform: "bluesky" }] as never); + const onResult = jest.fn(); + + const results = await postToAccounts( + "u1", + qaMessage, + [], + ["edmund-qa"], + undefined, + undefined, + undefined, + undefined, + onResult, + ); + + expect(results).toEqual([ + { + accountId: "edmund-qa", + platform: "bluesky", + success: false, + error: "QA_PUBLISH_FAILURE", + message: "Intentional test publish failure.", + }, + ]); + expect(onResult).toHaveBeenCalledWith(results[0]); + expect(sdkPost).not.toHaveBeenCalled(); + expect(mockResolve).not.toHaveBeenCalled(); +}); + +it.each([ + { id: "edmund-qa", message: qaMessage, accountSetting: undefined }, + { id: "edmund-qa", message: `${qaMessage} Extra text`, accountSetting: "edmund-qa" }, + { id: "other-account", message: qaMessage, accountSetting: "edmund-qa" }, +])( + "publishes normally when the QA setting, account, or exact message does not match ($id, $message)", + async ({ id, message, accountSetting }) => { + jest.replaceProperty(process, "env", { ...process.env, QA_PUBLISH_FAILURE_ACCOUNT_ID: accountSetting }); + jest.mocked(prisma.connectedAccount.findMany).mockResolvedValue([{ id, platform: "bluesky" }] as never); + jest.mocked(sdkPost).mockResolvedValue(new Map([["bluesky", { id: "published", error: "NO_ERROR" }]]) as never); + + await expect(postToAccounts("u1", message, [], [id])).resolves.toEqual([ + expect.objectContaining({ accountId: id, success: true, postId: "published" }), + ]); + expect(sdkPost).toHaveBeenCalledTimes(1); + }, +); + it("preserves other results and waits to clean up shared media after one account's download fails", async () => { jest.mocked(prisma.connectedAccount.findMany).mockResolvedValue([ { id: "good", platform: "x" },