Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
[codex] Fix self-hosted snips idmux fallback (#3864)
Gergő Móricz6b5cfe0b644f
diff --git a/apps/api/src/__tests__/snips/lib.ts b/apps/api/src/__tests__/snips/lib.tsindex 2755956fa..7aacb2550 100644--- a/apps/api/src/__tests__/snips/lib.ts+++ b/apps/api/src/__tests__/snips/lib.ts@@ -78,15 +78,25 @@ export type IdmuxRequest = {teamId?: string;};+function fallbackIdentity(): Identity {+ if (!config.TEST_API_KEY || !config.TEST_TEAM_ID) {+ throw new Error(+ "TEST_API_KEY and TEST_TEAM_ID must be set to use self-hosted idmux fallback",+ );+ }++ return {+ apiKey: config.TEST_API_KEY,+ teamId: config.TEST_TEAM_ID,+ };+}+export async function idmux(req: IdmuxRequest): Promise<Identity> {if (!config.IDMUX_URL) {if (TEST_PRODUCTION) {console.warn("IDMUX_URL is not set, using test API key and team ID");}- return {- apiKey: config.TEST_API_KEY!,- teamId: config.TEST_TEAM_ID!,- };+ return fallbackIdentity();}let runNumber = parseInt(config.GITHUB_RUN_NUMBER!);@@ -94,18 +104,31 @@ export async function idmux(req: IdmuxRequest): Promise<Identity> {runNumber = 0;}- const res = await fetch(config.IDMUX_URL + "/", {- method: "POST",- body: JSON.stringify({- refName: config.GITHUB_REF_NAME!,- runNumber,- concurrency: req.concurrency ?? 100,- ...req,- }),- headers: {- "Content-Type": "application/json",- },- });+ let res: Response;+ try {+ res = await fetch(config.IDMUX_URL + "/", {+ method: "POST",+ body: JSON.stringify({+ refName: config.GITHUB_REF_NAME!,+ runNumber,+ concurrency: req.concurrency ?? 100,+ ...req,+ }),+ headers: {+ "Content-Type": "application/json",+ },+ });+ } catch (error) {+ if (config.TEST_SUITE_SELF_HOSTED) {+ const reason = error instanceof Error ? error.message : String(error);+ console.warn(+ `IDMUX_URL is unreachable in self-hosted snips, using test API key and team ID: ${reason}`,+ );+ return fallbackIdentity();+ }++ throw error;+ }if (!res.ok) {console.error(await res.text());diff --git a/apps/api/src/__tests__/snips/v2/idmux.test.ts b/apps/api/src/__tests__/snips/v2/idmux.test.tsnew file mode 100644index 000000000..076e0f1b2--- /dev/null+++ b/apps/api/src/__tests__/snips/v2/idmux.test.ts@@ -0,0 +1,49 @@+import { afterEach, describe, expect, it, vi } from "vitest";+import { config } from "../../../config";+import { idmux } from "../lib";++const originalConfig = {+ IDMUX_URL: config.IDMUX_URL,+ TEST_API_KEY: config.TEST_API_KEY,+ TEST_TEAM_ID: config.TEST_TEAM_ID,+ TEST_SUITE_SELF_HOSTED: config.TEST_SUITE_SELF_HOSTED,+};++afterEach(() => {+ config.IDMUX_URL = originalConfig.IDMUX_URL;+ config.TEST_API_KEY = originalConfig.TEST_API_KEY;+ config.TEST_TEAM_ID = originalConfig.TEST_TEAM_ID;+ config.TEST_SUITE_SELF_HOSTED = originalConfig.TEST_SUITE_SELF_HOSTED;+ vi.restoreAllMocks();+});++describe("idmux", () => {+ it("falls back to the configured test identity when self-hosted idmux is unreachable", async () => {+ config.IDMUX_URL = "https://idmux.invalid";+ config.TEST_API_KEY = "test-api-key";+ config.TEST_TEAM_ID = "test-team-id";+ config.TEST_SUITE_SELF_HOSTED = true;++ vi.spyOn(globalThis, "fetch").mockRejectedValueOnce(+ new TypeError("fetch failed"),+ );++ await expect(idmux({ name: "self-hosted-fallback" })).resolves.toEqual({+ apiKey: "test-api-key",+ teamId: "test-team-id",+ });+ });++ it("keeps production idmux failures strict", async () => {+ config.IDMUX_URL = "https://idmux.invalid";+ config.TEST_SUITE_SELF_HOSTED = undefined;++ vi.spyOn(globalThis, "fetch").mockRejectedValueOnce(+ new TypeError("fetch failed"),+ );++ await expect(idmux({ name: "production-strict" })).rejects.toThrow(+ "fetch failed",+ );+ });+});diff --git a/apps/api/src/__tests__/snips/v2/scrape-browser.test.ts b/apps/api/src/__tests__/snips/v2/scrape-browser.test.tsindex 1ad307b56..390d5ace9 100644--- a/apps/api/src/__tests__/snips/v2/scrape-browser.test.ts+++ b/apps/api/src/__tests__/snips/v2/scrape-browser.test.ts@@ -232,6 +232,10 @@ describe("Scrape browser interact replay", () => {itIf(ALLOW_TEST_SUITE_WEBSITE && !!config.IDMUX_URL)("returns 403 when scrape job belongs to another team",async () => {+ if (identity.teamId === otherIdentity.teamId) {+ return;+ }+const scrapeResponse = await scrapeRaw({url: `${TEST_SUITE_WEBSITE}?testId=${crypto.randomUUID()}`,