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

Code before:
      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);

      // Mock dripCampaign.create to throw an error
      const createSpy = vi
        .spyOn(fastify.prisma.dripCampaign, 'create')
        .mockRejectedValueOnce(new Error('Database error'));

      const user = await findOrCreateUser(fastify, email);

      expect(user).toBeDefined();
      expect(user.id).toBeTruthy();

      // Verify error was captured by Sentry
      expect(captureException).toHaveBeenCalledTimes(1);
      expect(captureException).toHaveBeenCalledWith(
        expect.objectContaining({
          message: 'Database error'
        })
      );

      createSpy.mockRestore();
    });

    test('should not create drip campaign for existing users', async () => {
      fastify.gb.isOn = vi.fn(() => true);


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

Code after:
      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 }
      });

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

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

      // Mock dripCampaign.create to throw an error
      const createSpy = vi
        .spyOn(fastify.prisma.dripCampaign, 'create')
        .mockRejectedValueOnce(new Error('Database error'));

      const user = await findOrCreateUser(fastify, email);

      expect(user).toBeDefined();
      expect(user.id).toBeTruthy();

      // Verify error was captured by Sentry
      expect(captureException).toHaveBeenCalledTimes(1);
      expect(captureException).toHaveBeenCalledWith(
        expect.objectContaining({
          message: 'Database error'
        })
      );