Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

19.7 KB
Raw Blame
import { testFeatureFlags } from "@/src/__tests__/fixtures/feature-flags";
import type { Session } from "next-auth";
import { randomUUID } from "crypto";
import { env } from "@/src/env.mjs";
import { appRouter } from "@/src/server/api/root";
import { createInnerTRPCContext } from "@/src/server/api/trpc";
import { createProjectMembershipsOnSignup } from "@/src/features/auth/lib/createProjectMembershipsOnSignup";
import { V4_DEFAULT_ENABLED_FROM_AT } from "@/src/features/events/lib/v4Rollout";
import { createProjectRoute } from "@/src/features/setup/setupRoutes";
import { prisma, Role } from "@langfuse/shared/src/db";

const makeSession = ({
  userId,
  email,
  name = "Test User",
  canCreateOrganizations = true,
  organizations = [],
}: {
  userId: string;
  email: string;
  name?: string;
  canCreateOrganizations?: boolean;
  organizations?: NonNullable<Session["user"]>["organizations"];
}) =>
  ({
    expires: "1",
    user: {
      id: userId,
      email,
      name,
      canCreateOrganizations,
      organizations,
      featureFlags: testFeatureFlags(),
      admin: false,
    },
    environment: {} as Session["environment"],
  }) as Session;

describe("onboarding router", () => {
  const createdUserIds: string[] = [];
  const createdOrgIds: string[] = [];

  afterEach(async () => {
    if (createdOrgIds.length > 0) {
      await prisma.organization.deleteMany({
        where: {
          id: {
            in: createdOrgIds,
          },
        },
      });
      createdOrgIds.length = 0;
    }

    if (createdUserIds.length > 0) {
      await prisma.user.deleteMany({
        where: {
          id: {
            in: createdUserIds,
          },
        },
      });
      createdUserIds.length = 0;
    }
  });

  it("resolves the starter org and project provisioned for first-time users", async () => {
    const userId = randomUUID();
    const email = `starter-${userId}@example.com`;
    createdUserIds.push(userId);

    const user = await prisma.user.create({
      data: {
        id: userId,
        email,
        name: "Taylor Test",
      },
    });

    await createProjectMembershipsOnSignup(user, {
      userWasJustCreated: true,
    });

    const organizationMembership =
      await prisma.organizationMembership.findFirst({
        where: {
          userId,
        },
        include: {
          organization: {
            include: {
              projects: true,
            },
          },
        },
      });

    if (!organizationMembership) {
      throw new Error("Expected starter organization membership to exist");
    }
    createdOrgIds.push(organizationMembership.organization.id);
    const starterProjectId =
      organizationMembership.organization.projects[0]?.id;
    if (!starterProjectId) {
      throw new Error("Expected starter project to exist");
    }

    await prisma.organization.update({
      where: {
        id: organizationMembership.organization.id,
      },
      data: {
        name: `Renamed Starter Org ${userId}`,
      },
    });

    await prisma.project.update({
      where: {
        id: starterProjectId,
      },
      data: {
        name: `Renamed Starter Project ${userId}`,
      },
    });

    const caller = appRouter.createCaller({
      ...createInnerTRPCContext({
        session: makeSession({
          userId,
          email,
          name: "Taylor Test",
        }),
        headers: {},
      }),
      prisma,
    });

    const result = await caller.onboarding.complete();

    expect(result).toEqual({
      redirectTo: `/project/${starterProjectId}/traces`,
      surveyCreated: true,
    });
    expect(organizationMembership.role).toBe(Role.OWNER);
    expect(organizationMembership.organization.projects).toHaveLength(1);
  });

  it("falls back to manual org creation when no real org exists", async () => {
    const userId = randomUUID();
    const email = `no-org-${userId}@example.com`;
    createdUserIds.push(userId);

    await prisma.user.create({
      data: {
        id: userId,
        email,
      },
    });

    const caller = appRouter.createCaller({
      ...createInnerTRPCContext({
        session: makeSession({
          userId,
          email,
        }),
        headers: {},
      }),
      prisma,
    });

    const result = await caller.onboarding.complete();

    expect(result).toEqual({
      redirectTo: "/setup",
      surveyCreated: true,
    });
  });

  it("reuses an existing real project instead of creating starter resources", async () => {
    const userId = randomUUID();
    const email = `existing-${userId}@example.com`;
    createdUserIds.push(userId);

    await prisma.user.create({
      data: {
        id: userId,
        email,
      },
    });

    const organization = await prisma.organization.create({
      data: {
        name: `Existing Org ${userId}`,
        organizationMemberships: {
          create: {
            userId,
            role: Role.OWNER,
          },
        },
      },
      include: {
        projects: true,
      },
    });
    createdOrgIds.push(organization.id);

    const project = await prisma.project.create({
      data: {
        name: `Existing Project ${userId}`,
        orgId: organization.id,
      },
    });

    const caller = appRouter.createCaller({
      ...createInnerTRPCContext({
        session: makeSession({
          userId,
          email,
        }),
        headers: {},
      }),
      prisma,
    });

    const result = await caller.onboarding.complete();

    expect(result).toEqual({
      redirectTo: `/project/${project.id}`,
      surveyCreated: true,
    });

    const projectCount = await prisma.project.count({
      where: {
        orgId: organization.id,
        deletedAt: null,
      },
    });

    expect(projectCount).toBe(1);
  });

  it("does not create starter resources when signup consumes a real invitation", async () => {
    const userId = randomUUID();
    const email = `invited-${userId}@example.com`;
    createdUserIds.push(userId);

    const user = await prisma.user.create({
      data: {
        id: userId,
        email,
      },
    });

    const organization = await prisma.organization.create({
      data: {
        name: `Invited Org ${userId}`,
        createdAt: new Date(V4_DEFAULT_ENABLED_FROM_AT.getTime() + 1_000),
      },
    });
    createdOrgIds.push(organization.id);

    const project = await prisma.project.create({
      data: {
        name: `Invited Project ${userId}`,
        orgId: organization.id,
      },
    });

    await prisma.membershipInvitation.create({
      data: {
        orgId: organization.id,
        projectId: project.id,
        email,
        orgRole: Role.NONE,
        projectRole: Role.MEMBER,
      },
    });

    await createProjectMembershipsOnSignup(user, {
      userWasJustCreated: true,
    });

    const organizationMembership =
      await prisma.organizationMembership.findFirst({
        where: {
          userId,
        },
        include: {
          organization: {
            include: {
              projects: true,
            },
          },
        },
      });

    if (!organizationMembership) {
      throw new Error("Expected starter organization membership to exist");
    }
    createdOrgIds.push(organizationMembership.organization.id);

    const caller = appRouter.createCaller({
      ...createInnerTRPCContext({
        session: makeSession({
          userId,
          email,
        }),
        headers: {},
      }),
      prisma,
    });

    const result = await caller.onboarding.complete();

    expect(result).toEqual({
      redirectTo: `/project/${project.id}`,
      surveyCreated: true,
    });

    const organizationMemberships =
      await prisma.organizationMembership.findMany({
        where: {
          userId,
        },
        select: {
          orgId: true,
        },
      });

    const realOrganizationIds = organizationMemberships
      .map((membership) => membership.orgId)
      .filter((orgId) => orgId !== env.NEXT_PUBLIC_DEMO_ORG_ID);

    expect(realOrganizationIds).toEqual([organization.id]);

    const invitationCount = await prisma.membershipInvitation.count({
      where: {
        email,
      },
    });

    expect(invitationCount).toBe(0);

    const updatedUser = await prisma.user.findUniqueOrThrow({
      where: {
        id: userId,
      },
      select: {
        v4BetaEnabled: true,
      },
    });

    expect(updatedUser.v4BetaEnabled).toBe(true);
  });

  it("creates starter resources when a consumed real invitation leaves no usable workspace", async () => {
    const userId = randomUUID();
    const email = `invited-none-${userId}@example.com`;
    createdUserIds.push(userId);

    const user = await prisma.user.create({
      data: {
        id: userId,
        email,
        name: "Jordan Test",
      },
    });

    const organization = await prisma.organization.create({
      data: {
        name: `Restricted Org ${userId}`,
        createdAt: new Date(V4_DEFAULT_ENABLED_FROM_AT.getTime() + 1_000),
      },
    });
    createdOrgIds.push(organization.id);

    await prisma.membershipInvitation.create({
      data: {
        orgId: organization.id,
        email,
        orgRole: Role.NONE,
      },
    });

    await createProjectMembershipsOnSignup(user, {
      userWasJustCreated: true,
    });

    const caller = appRouter.createCaller({
      ...createInnerTRPCContext({
        session: makeSession({
          userId,
          email,
          name: "Jordan Test",
        }),
        headers: {},
      }),
      prisma,
    });

    const result = await caller.onboarding.complete();

    const organizationMemberships =
      await prisma.organizationMembership.findMany({
        where: {
          userId,
        },
        include: {
          organization: {
            include: {
              projects: {
                where: {
                  deletedAt: null,
                },
              },
            },
          },
        },
        orderBy: {
          createdAt: "asc",
        },
      });

    const realOrganizationMemberships = organizationMemberships.filter(
      (membership) => membership.orgId !== env.NEXT_PUBLIC_DEMO_ORG_ID,
    );

    expect(realOrganizationMemberships).toHaveLength(2);

    const starterOrganizationMembership = realOrganizationMemberships.find(
      (membership) => membership.role === Role.OWNER,
    );

    expect(starterOrganizationMembership?.organization.projects).toHaveLength(
      1,
    );
    expect(starterOrganizationMembership?.organization.projects[0]?.name).toBe(
      "My Project",
    );
    expect(result).toEqual({
      redirectTo: `/project/${starterOrganizationMembership?.organization.projects[0]?.id}/traces`,
      surveyCreated: true,
    });
  });

  describe("AI Gateway invitations", () => {
    const originalGatewayAvailability = {
      NODE_ENV: env.NODE_ENV,
      NEXT_PUBLIC_PREVIEW_PR_URL: env.NEXT_PUBLIC_PREVIEW_PR_URL,
      LANGFUSE_AI_GATEWAY_DEPLOYED: env.LANGFUSE_AI_GATEWAY_DEPLOYED,
      LANGFUSE_AI_GATEWAY_ENABLED: env.LANGFUSE_AI_GATEWAY_ENABLED,
      LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST:
        env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST,
    };

    afterEach(() => {
      Object.assign(env, originalGatewayAvailability);
    });

    it("lands invitees on the overview instead of creating starter resources", async () => {
      const { organization, memberships, complete } =
        await signUpWithGatewayInvitation({ gatewayEnabled: true });

      expect(memberships).toEqual([
        { orgId: organization.id, role: Role.AI_GATEWAY },
      ]);
      expect(await complete()).toEqual({
        redirectTo: "/",
        surveyCreated: true,
      });
    });

    it("creates starter resources when the gateway was turned off for the inviting org", async () => {
      const { organization, memberships } = await signUpWithGatewayInvitation({
        gatewayEnabled: false,
      });

      expect(memberships).toHaveLength(2);
      expect(memberships).toContainEqual({
        orgId: organization.id,
        role: Role.AI_GATEWAY,
      });
      expect(memberships).toContainEqual(
        expect.objectContaining({ role: Role.OWNER }),
      );
    });

    /** signUpWithGatewayInvitation signs up a user holding an AI_GATEWAY invitation for a fresh org */
    async function signUpWithGatewayInvitation({
      gatewayEnabled,
    }: {
      gatewayEnabled: boolean;
    }) {
      const userId = randomUUID();
      const email = `invited-gateway-${userId}@example.com`;
      createdUserIds.push(userId);

      const user = await prisma.user.create({
        data: { id: userId, email, name: "Gale Test" },
      });
      const organization = await prisma.organization.create({
        data: { name: `Gateway Org ${userId}` },
      });
      createdOrgIds.push(organization.id);
      await prisma.project.create({
        data: { name: `Gateway Org Project ${userId}`, orgId: organization.id },
      });
      await prisma.membershipInvitation.create({
        data: { orgId: organization.id, email, orgRole: Role.AI_GATEWAY },
      });

      // Development and preview enable the gateway everywhere, so only the
      // org allowlist decides here.
      Object.assign(env, {
        NODE_ENV: "test",
        NEXT_PUBLIC_PREVIEW_PR_URL: undefined,
        LANGFUSE_AI_GATEWAY_DEPLOYED: "true",
        LANGFUSE_AI_GATEWAY_ENABLED: "false",
        LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST: gatewayEnabled
          ? [organization.id]
          : [],
      });
      await createProjectMembershipsOnSignup(user, {
        userWasJustCreated: true,
      });

      const memberships = await prisma.organizationMembership.findMany({
        where: {
          userId,
          ...(env.NEXT_PUBLIC_DEMO_ORG_ID
            ? { orgId: { not: env.NEXT_PUBLIC_DEMO_ORG_ID } }
            : {}),
        },
        select: { orgId: true, role: true },
      });
      createdOrgIds.push(
        ...memberships
          .map((membership) => membership.orgId)
          .filter((orgId) => orgId !== organization.id),
      );

      const caller = appRouter.createCaller({
        ...createInnerTRPCContext({
          session: makeSession({ userId, email, name: "Gale Test" }),
          headers: {},
        }),
        prisma,
      });

      return {
        organization,
        memberships,
        complete: () => caller.onboarding.complete(),
      };
    }
  });

  it("falls back to manual project creation when a real org exists without a project", async () => {
    const userId = randomUUID();
    const email = `org-only-${userId}@example.com`;
    createdUserIds.push(userId);

    await prisma.user.create({
      data: {
        id: userId,
        email,
      },
    });

    const organization = await prisma.organization.create({
      data: {
        name: `Org Only ${userId}`,
        organizationMemberships: {
          create: {
            userId,
            role: Role.OWNER,
          },
        },
      },
    });
    createdOrgIds.push(organization.id);

    const caller = appRouter.createCaller({
      ...createInnerTRPCContext({
        session: makeSession({
          userId,
          email,
        }),
        headers: {},
      }),
      prisma,
    });

    const result = await caller.onboarding.complete();

    expect(result).toEqual({
      redirectTo: createProjectRoute(organization.id),
      surveyCreated: true,
    });
  });
  describe("survey answers", () => {
    const createCallerForNewUser = async () => {
      const userId = randomUUID();
      const email = `build-intent-${userId}@example.com`;
      createdUserIds.push(userId);
      await prisma.user.create({ data: { id: userId, email } });

      const caller = appRouter.createCaller({
        ...createInnerTRPCContext({
          session: makeSession({ userId, email }),
          headers: {},
        }),
        prisma,
      });

      return { caller, userId };
    };

    it("stores trimmed answers with their shown positions", async () => {
      const { caller, userId } = await createCallerForNewUser();

      await caller.onboarding.complete({
        buildIntents: ["rag", "other"],
        buildIntentPositions: [2, 6],
        buildIntentOther: "  voice agent  ",
      });

      const survey = await prisma.survey.findFirstOrThrow({
        where: { userId },
      });
      expect(survey.response).toEqual({
        buildIntents: ["rag", "other"],
        buildIntentOther: "voice agent",
        buildIntentPositions: [2, 6],
      });
    });

    describe("newsletter result", () => {
      const originalCloudRegion = env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION;

      afterEach(() => {
        Object.assign(env, {
          NEXT_PUBLIC_LANGFUSE_CLOUD_REGION: originalCloudRegion,
        });
      });

      const completeAsOrganizationMember = async ({
        role,
        cloudRegion,
        clientLists,
      }: {
        role: Role;
        cloudRegion?: string;
        clientLists: string[];
      }) => {
        Object.assign(env, { NEXT_PUBLIC_LANGFUSE_CLOUD_REGION: cloudRegion });
        const { caller, userId } = await createCallerForNewUser();
        const organization = await prisma.organization.create({
          data: {
            name: `Newsletter Org ${userId}`,
            organizationMemberships: { create: { userId, role } },
          },
        });
        createdOrgIds.push(organization.id);

        await caller.onboarding.complete({
          newsletter: { outcome: "subscribed", lists: clientLists },
        } as Parameters<typeof caller.onboarding.complete>[0]);

        const survey = await prisma.survey.findFirstOrThrow({
          where: { userId },
        });
        return survey.response;
      };

      it.each([
        {
          role: Role.VIEWER,
          clientLists: ["product", "oss"],
          storedLists: ["product"],
        },
        {
          role: Role.OWNER,
          clientLists: ["product"],
          storedLists: ["product", "oss"],
        },
      ])(
        "stores the lists offered to a $role on self-hosted, not the client's",
        async ({ role, clientLists, storedLists }) => {
          await expect(
            completeAsOrganizationMember({ role, clientLists }),
          ).resolves.toEqual({
            newsletter: { lists: storedLists, outcome: "subscribed" },
          });
        },
      );

      it("stores no newsletter result in Cloud", async () => {
        await expect(
          completeAsOrganizationMember({
            role: Role.OWNER,
            cloudRegion: "US",
            clientLists: ["product", "oss"],
          }),
        ).resolves.toEqual({});
      });
    });

    it.each([
      [
        "duplicate picks",
        { buildIntents: ["rag", "rag"], buildIntentPositions: [0, 0] },
      ],
      [
        "just exploring with another pick",
        {
          buildIntents: ["just_exploring", "rag"],
          buildIntentPositions: [5, 0],
        },
      ],
      [
        "more than three picks",
        {
          buildIntents: ["rag", "chat_agent", "coding_agents", "other"],
          buildIntentPositions: [0, 1, 2, 6],
        },
      ],
      [
        "an unknown id",
        { buildIntents: ["unknown"], buildIntentPositions: [0] },
      ],
      [
        "other text over 500 characters",
        {
          buildIntents: ["other"],
          buildIntentPositions: [6],
          buildIntentOther: "x".repeat(501),
        },
      ],
      [
        "positions that don't match the picks",
        { buildIntents: ["rag", "other"], buildIntentPositions: [2] },
      ],
      [
        "a newsletter result with an unknown outcome",
        { newsletter: { outcome: "bounced" } },
      ],
    ])("rejects %s", async (_case, input) => {
      const { caller, userId } = await createCallerForNewUser();

      await expect(
        caller.onboarding.complete(
          input as Parameters<typeof caller.onboarding.complete>[0],
        ),
      ).rejects.toThrow();
      expect(await prisma.survey.count({ where: { userId } })).toBe(0);
    });
  });
});