Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

test(frontend): cover LD context shape with buildLDContext helper (#13046)

Zamil Majdyeb6b62cfcbed
diff --git a/autogpt_platform/frontend/src/services/feature-flags/__tests__/helpers.test.ts b/autogpt_platform/frontend/src/services/feature-flags/__tests__/helpers.test.ts
new file mode 100644
index 0000000000..65ae23445c
--- /dev/null
+++ b/autogpt_platform/frontend/src/services/feature-flags/__tests__/helpers.test.ts
@@ -0,0 +1,80 @@
+import type { User } from "@supabase/supabase-js";
+import { describe, expect, it } from "vitest";
+import { buildLDContext } from "../helpers";
+
+function userFixture(overrides: Partial<User> = {}): User {
+ return {
+ id: "00000000-0000-0000-0000-000000000001",
+ aud: "authenticated",
+ app_metadata: {},
+ user_metadata: {},
+ created_at: "2026-05-08T12:00:00Z",
+ email: "[email protected]",
+ role: "authenticated",
+ ...overrides,
+ } as User;
+}
+
+describe("buildLDContext", () => {
+ it("returns anonymous context when no user", () => {
+ expect(buildLDContext(null)).toEqual({
+ kind: "user",
+ key: "anonymous",
+ anonymous: true,
+ });
+ });
+
+ it("includes email, email_domain, role, created_at, and custom.role for a full user", () => {
+ const ctx = buildLDContext(userFixture());
+
+ expect(ctx).toEqual({
+ kind: "user",
+ key: "00000000-0000-0000-0000-000000000001",
+ anonymous: false,
+ email: "[email protected]",
+ email_domain: "example.com",
+ role: "authenticated",
+ created_at: "2026-05-08T12:00:00Z",
+ custom: { role: "authenticated" },
+ });
+ });
+
+ it("preserves the exact created_at string from Supabase (no normalization)", () => {
+ const ctx = buildLDContext(
+ userFixture({ created_at: "2025-01-15T08:30:45.123456Z" }),
+ );
+
+ expect("created_at" in ctx && ctx.created_at).toBe(
+ "2025-01-15T08:30:45.123456Z",
+ );
+ });
+
+ it("omits created_at when missing — falsy spread skips the key", () => {
+ const ctx = buildLDContext(
+ userFixture({ created_at: undefined as unknown as string }),
+ );
+
+ expect(ctx).not.toHaveProperty("created_at");
+ expect("email" in ctx && ctx.email).toBe("[email protected]");
+ });
+
+ it("omits role and leaves custom empty when role missing", () => {
+ const ctx = buildLDContext(userFixture({ role: undefined }));
+
+ expect(ctx).not.toHaveProperty("role");
+ expect("custom" in ctx && ctx.custom).toEqual({});
+ });
+
+ it("omits email and email_domain when email missing", () => {
+ const ctx = buildLDContext(userFixture({ email: undefined }));
+
+ expect(ctx).not.toHaveProperty("email");
+ expect(ctx).not.toHaveProperty("email_domain");
+ });
+
+ it("derives email_domain from the last @ segment of email", () => {
+ const ctx = buildLDContext(userFixture({ email: "[email protected]" }));
+
+ expect("email_domain" in ctx && ctx.email_domain).toBe("sub.example.com");
+ });
+});
diff --git a/autogpt_platform/frontend/src/services/feature-flags/feature-flag-provider.tsx b/autogpt_platform/frontend/src/services/feature-flags/feature-flag-provider.tsx
index 670a2deacc..5df4b5a24a 100644
--- a/autogpt_platform/frontend/src/services/feature-flags/feature-flag-provider.tsx
+++ b/autogpt_platform/frontend/src/services/feature-flags/feature-flag-provider.tsx
@@ -7,6 +7,7 @@ import { LDProvider } from "launchdarkly-react-client-sdk";
import type { ReactNode } from "react";
import { useMemo } from "react";
import { environment } from "../environment";
+import { buildLDContext } from "./helpers";
const LAUNCHDARKLY_INIT_TIMEOUT_MS = 5000;
@@ -17,33 +18,7 @@ export function LaunchDarklyProvider({ children }: { children: ReactNode }) {
const context = useMemo(() => {
if (isUserLoading) return;
-
- if (!user) {
- return {
- kind: "user" as const,
- key: "anonymous",
- anonymous: true,
- };
- }
-
- // Mirror the context built by the backend
- // (feature_flag.py:_fetch_user_context_data) so LaunchDarkly targeting
- // rules evaluate identically on both sides.
- return {
- kind: "user" as const,
- key: user.id,
- anonymous: false,
- ...(user.email && {
- email: user.email,
- email_domain: user.email.split("@").at(-1),
- }),
- ...(user.role && { role: user.role }),
- // Supabase JS emits `Z`-suffixed ISO; backend emits `+00:00` — LD date matchers accept both.
- ...(user.created_at && { created_at: user.created_at }),
- custom: {
- ...(user.role && { role: user.role }),
- },
- };
+ return buildLDContext(user);
}, [user, isUserLoading]);
if (!envEnabled) {
diff --git a/autogpt_platform/frontend/src/services/feature-flags/helpers.ts b/autogpt_platform/frontend/src/services/feature-flags/helpers.ts
new file mode 100644
index 0000000000..033039c66b
--- /dev/null
+++ b/autogpt_platform/frontend/src/services/feature-flags/helpers.ts
@@ -0,0 +1,44 @@
+import type { User } from "@supabase/supabase-js";
+
+export type LDUserContext =
+ | {
+ kind: "user";
+ key: string;
+ anonymous: true;
+ }
+ | {
+ kind: "user";
+ key: string;
+ anonymous: false;
+ email?: string;
+ email_domain?: string;
+ role?: string;
+ created_at?: string;
+ custom: { role?: string };
+ };
+
+// Mirror the context built by the backend
+// (feature_flag.py:_fetch_user_context_data) so LaunchDarkly targeting
+// rules evaluate identically on both sides.
+//
+// Supabase JS emits `Z`-suffixed ISO; backend emits `+00:00` — LD date matchers accept both.
+export function buildLDContext(user: User | null): LDUserContext {
+ if (!user) {
+ return { kind: "user", key: "anonymous", anonymous: true };
+ }
+
+ return {
+ kind: "user",
+ key: user.id,
+ anonymous: false,
+ ...(user.email && {
+ email: user.email,
+ email_domain: user.email.split("@").at(-1),
+ }),
+ ...(user.role && { role: user.role }),
+ ...(user.created_at && { created_at: user.created_at }),
+ custom: {
+ ...(user.role && { role: user.role }),
+ },
+ };
+}