Skip to content
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
47 changes: 47 additions & 0 deletions library/agent/realtime/getRealtimeURL.test.ts
Original file line number Diff line number Diff line change
@@ -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/");
});
12 changes: 12 additions & 0 deletions library/agent/realtime/getRealtimeURL.ts
Original file line number Diff line number Diff line change
@@ -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");
}
Loading