Skip to content
This repository was archived by the owner on Aug 12, 2026. It is now read-only.
Open
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
31 changes: 25 additions & 6 deletions src/api/v2/agents/handlers/join.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type { Request, Response } from "express";
import { z } from "zod";
import { AGENT_POOL_API_KEY, AGENT_POOL_URL, XMTP_ENV } from "@/config";
import {
AGENT_POOL_API_KEY,
AGENT_POOL_URL,
shouldUseDevBehavior,
XMTP_ENV,
type XmtpEnv,
} from "@/config";

const bodySchema = z.object({
slug: z.string().min(1, "Slug is required").max(2048),
Expand All @@ -27,12 +33,24 @@ const ERRORS = {
},
} as const;

function buildInviteUrl(slug: string): string {
const domain =
XMTP_ENV === "production" ? "popup.convos.org" : "dev.convos.org";
export function buildInviteUrl(
slug: string,
xmtpEnv: XmtpEnv = XMTP_ENV,
): string {
const domainByEnv: Record<XmtpEnv, string> = {
production: "popup.convos.org",
testnet: "testnet.convos.org",
dev: "dev.convos.org",
local: "dev.convos.org",
};
const domain = domainByEnv[xmtpEnv];
return `https://${domain}/v2?i=${encodeURIComponent(slug)}`;
}

export function shouldAllowForcedErrors(xmtpEnv: XmtpEnv = XMTP_ENV): boolean {
return shouldUseDevBehavior(xmtpEnv);
}

/**
* Handler for POST /api/v2/agents/join
*
Expand Down Expand Up @@ -64,8 +82,9 @@ function buildInviteUrl(slug: string): string {
*/
export async function joinHandler(req: Request, res: Response) {
// Force error responses for testing (non-production XMTP env only) — see JSDoc above for usage
const forceError =
XMTP_ENV !== "production" ? req.headers["x-force-error"] : undefined;
const forceError = shouldAllowForcedErrors()
? req.headers["x-force-error"]
: undefined;
const forcedError = Object.values(ERRORS).find(
(e) => String(e.status) === forceError,
);
Expand Down
41 changes: 41 additions & 0 deletions tests/agent-join.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, test } from "bun:test";
import {
buildInviteUrl,
shouldAllowForcedErrors,
} from "@/api/v2/agents/handlers/join";

describe("buildInviteUrl", () => {
test("uses production domain for production", () => {
expect(buildInviteUrl("abc123", "production")).toBe(
"https://popup.convos.org/v2?i=abc123",
);
});

test("uses testnet domain for testnet", () => {
expect(buildInviteUrl("abc123", "testnet")).toBe(
"https://testnet.convos.org/v2?i=abc123",
);
});

test("uses dev domain for all other environments", () => {
expect(buildInviteUrl("abc123", "dev")).toBe(
"https://dev.convos.org/v2?i=abc123",
);
expect(buildInviteUrl("abc123", "local")).toBe(
"https://dev.convos.org/v2?i=abc123",
);
});

test("encodes invite slugs", () => {
expect(buildInviteUrl("abc 123/?", "testnet")).toBe(
"https://testnet.convos.org/v2?i=abc%20123%2F%3F",
);
});

test("allows forced errors in dev-like environments only", () => {
expect(shouldAllowForcedErrors("dev")).toBe(true);
expect(shouldAllowForcedErrors("testnet")).toBe(true);
expect(shouldAllowForcedErrors("local")).toBe(true);
expect(shouldAllowForcedErrors("production")).toBe(false);
});
});
Loading