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

Code before:

      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);

      // 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);

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

Code after:

  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 }
      });

      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);