diff --git a/library/agent/realtime/getRealtimeURL.test.ts b/library/agent/realtime/getRealtimeURL.test.ts new file mode 100644 index 000000000..68b1c5c95 --- /dev/null +++ b/library/agent/realtime/getRealtimeURL.test.ts @@ -0,0 +1,47 @@ +import * as t from "tap"; +import { getRealtimeURL } from "./getRealtimeURL"; + +t.afterEach(() => { + delete process.env.AIKIDO_TOKEN; + delete process.env.AIKIDO_REALTIME_ENDPOINT; +}); + +t.test("should return EU if no token set", async (t) => { + delete process.env.AIKIDO_TOKEN; + t.equal(getRealtimeURL().href, "https://runtime.aikido.dev/"); +}); + +t.test("should return EU for invalid token", async (t) => { + process.env.AIKIDO_TOKEN = "invalid-token"; + t.equal(getRealtimeURL().href, "https://runtime.aikido.dev/"); +}); + +t.test("should return EU for old format token without region", async (t) => { + process.env.AIKIDO_TOKEN = "AIK_RUNTIME_123_456_randomstring"; + t.equal(getRealtimeURL().href, "https://runtime.aikido.dev/"); +}); + +t.test("should return US for new format token with US region", async (t) => { + process.env.AIKIDO_TOKEN = "AIK_RUNTIME_123_456_US_randomstring"; + t.equal(getRealtimeURL().href, "https://runtime.us.aikido.dev/"); +}); + +t.test("should return ME for new format token with ME region", async (t) => { + process.env.AIKIDO_TOKEN = "AIK_RUNTIME_123_456_ME_randomstring"; + t.equal(getRealtimeURL().href, "https://runtime.me.aikido.dev/"); +}); + +t.test("should return EU for new format token with EU region", async (t) => { + process.env.AIKIDO_TOKEN = "AIK_RUNTIME_123_456_EU_randomstring"; + t.equal(getRealtimeURL().href, "https://runtime.aikido.dev/"); +}); + +t.test("should not return unsupported region", async (t) => { + process.env.AIKIDO_TOKEN = "AIK_RUNTIME_123_456_INVALID_randomstring"; + t.equal(getRealtimeURL().href, "https://runtime.aikido.dev/"); +}); + +t.test("should respect AIKIDO_REALTIME_ENDPOINT if set", async (t) => { + process.env.AIKIDO_REALTIME_ENDPOINT = "https://custom-runtime.example.com"; + t.equal(getRealtimeURL().href, "https://custom-runtime.example.com/"); +}); diff --git a/library/agent/realtime/getRealtimeURL.ts b/library/agent/realtime/getRealtimeURL.ts index 6f2cc465d..3bf5c687b 100644 --- a/library/agent/realtime/getRealtimeURL.ts +++ b/library/agent/realtime/getRealtimeURL.ts @@ -1,7 +1,19 @@ +import { extractRegionFromToken } from "../extractRegionFromToken"; + export function getRealtimeURL() { if (process.env.AIKIDO_REALTIME_ENDPOINT) { return new URL(process.env.AIKIDO_REALTIME_ENDPOINT); } + const region = extractRegionFromToken(process.env.AIKIDO_TOKEN || ""); + + if (region === "US") { + return new URL("https://runtime.us.aikido.dev"); + } + + if (region === "ME") { + return new URL("https://runtime.me.aikido.dev"); + } + return new URL("https://runtime.aikido.dev"); }