Repository: freeCodeCamp/freeCodeCamp
File: api/src/routes/helpers/auth-helpers.test.ts (TypeScript)

Code before:
  });

  test('should NOT send a message if there are no users with the email', async () => {
    await findOrCreateUser(fastify, email);

    expect(captureException).not.toHaveBeenCalled();
  });

  describe('drip campaign logic', () => {
    test('should create a drip campaign record when a new user is created and feature flag is enabled', async () => {
      fastify.gb.isOn = vi.fn(() => true);

      const user = await findOrCreateUser(fastify, email);

      const dripCampaign = await fastify.prisma.dripCampaign.findFirst({
        where: { userId: user.id }
      });

      expect(dripCampaign).toBeDefined();
      expect(dripCampaign?.userId).toBe(user.id);
      expect(dripCampaign?.email).toBe(email);
      expect(['A', 'B']).toContain(dripCampaign?.variant);
    });

    test('should assign a consistent variant based on userId', async () => {
      fastify.gb.isOn = vi.fn(() => true);

      const user = await findOrCreateUser(fastify, email);
      const expectedVariant = assignVariantBucket(user.id);

      const dripCampaign = await fastify.prisma.dripCampaign.findFirst({
        where: { userId: user.id }
      });

      expect(dripCampaign?.variant).toBe(expectedVariant);
    });

    test('should not create a drip campaign record when feature flag is disabled', async () => {
      fastify.gb.isOn = vi.fn(() => false);

      const user = await findOrCreateUser(fastify, email);

      const dripCampaign = await fastify.prisma.dripCampaign.findFirst({
        where: { userId: user.id }
      });

      expect(dripCampaign).toBeNull();
    });

    test('should not prevent user creation if drip campaign record creation fails', async () => {
      fastify.gb.isOn = vi.fn(() => true);

Review comment (suggestion):
```suggestion
      vi.spyOn(fastify.gb, 'isOn').mockImplementationOnce(() => true);
```

Code after:
  test('should NOT send a message if there is only one user with the email', async () => {
    await fastify.prisma.user.create({ data: createUserInput(email) });

    await findOrCreateUser(fastify, email);

    expect(captureException).not.toHaveBeenCalled();
  });

  test('should NOT send a message if there are no users with the email', async () => {
    await findOrCreateUser(fastify, email);

    expect(captureException).not.toHaveBeenCalled();
  });

  describe('drip campaign logic', () => {
    test('should create a drip campaign record when a new user is created and feature flag is enabled', async () => {
      vi.spyOn(fastify.gb, 'isOn').mockImplementationOnce(() => true);

      const user = await findOrCreateUser(fastify, email);

      const dripCampaign = await fastify.prisma.dripCampaign.findFirst({
        where: { userId: user.id }
      });

      expect(dripCampaign).toBeDefined();
      expect(dripCampaign?.userId).toBe(user.id);
      expect(dripCampaign?.email).toBe(email);
      expect(['A', 'B']).toContain(dripCampaign?.variant);
    });

    test('should assign a consistent variant based on userId', async () => {
      vi.spyOn(fastify.gb, 'isOn').mockImplementationOnce(() => true);

      const user = await findOrCreateUser(fastify, email);
      const expectedVariant = assignVariantBucket(user.id);

      const dripCampaign = await fastify.prisma.dripCampaign.findFirst({
        where: { userId: user.id }
      });

      expect(dripCampaign?.variant).toBe(expectedVariant);
    });

    test('should not create a drip campaign record when feature flag is disabled', async () => {
      vi.spyOn(fastify.gb, 'isOn').mockImplementationOnce(() => false);

      const user = await findOrCreateUser(fastify, email);

      const dripCampaign = await fastify.prisma.dripCampaign.findFirst({
        where: { userId: user.id }
      });