Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions scheduler/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions scheduler/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions scheduler/lib/posting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -332,6 +333,8 @@ interface AccountSegment {
mediaFiles: MediaFile[];
}

const QA_PUBLISH_FAILURE_MESSAGE = "SimplePost QA failure test.";

async function postSegmentsToAccount(
segments: AccountSegment[],
account: ConnectedAccount,
Expand All @@ -341,6 +344,17 @@ async function postSegmentsToAccount(
postId: string = randomUUID(),
tiktokRetryApproved = false,
): Promise<PostingResult> {
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,
Expand Down
52 changes: 52 additions & 0 deletions scheduler/tests/lib/posting/batch-failures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
Loading