Repository: freeCodeCamp/freeCodeCamp
File: api/src/exam-environment/routes/exam-environment.test.ts (TypeScript)

Code before:
          }
        ]);

        expect(res2.status).toBe(200);
      });

      it('should indicate an exam may be taken if the user has no prior attempts', async () => {
        const res = await superGet('/exam-environment/exams').set(
          'exam-environment-authorization-token',
          examEnvironmentAuthorizationToken
        );

        expect(res.body).toStrictEqual([
          {
            canTake: true,
            config: {
              name: mock.exam.config.name,
              note: mock.exam.config.note,
              passingPercent: mock.exam.config.passingPercent,
              totalTimeInMS: mock.exam.config.totalTimeInMS,
              retakeTimeInMS: mock.exam.config.retakeTimeInMS
            },
            id: mock.examId
          }
        ]);

        expect(res.status).toBe(200);
      });

      it("should indicate an exam's availability based on the last attempt's start time, and the exam retake time", async () => {
        // Create a recent exam attempt that's within the retake time
        const recentExamAttempt = {
          ...mock.examAttempt,
          userId: defaultUserId,
          startTimeInMS: Date.now() - mock.exam.config.totalTimeInMS
        };
        await fastifyTestInstance.prisma.examEnvironmentExamAttempt.create({
          data: recentExamAttempt
        });

        const res = await superGet('/exam-environment/exams').set(
          'exam-environment-authorization-token',
          examEnvironmentAuthorizationToken
        );

        expect(res.body).toStrictEqual([
          {
            canTake: false,
            config: {
              name: mock.exam.config.name,
              note: mock.exam.config.note,

Review comment (suggestion):
```suggestion
        expect(res.body).toMatchObject([{ canTake: true }]);
```


Code after:
            id: mock.examId
          }
        ]);
        expect(res.body).toMatchObject([{ canTake: true }]);
        expect(res.status).toBe(200);
      });

      it("should indicate an exam's availability based on the last attempt's start time, and the exam retake time", async () => {
        // Create a recent exam attempt that's within the retake time
        const recentExamAttempt = {
          ...mock.examAttempt,
          userId: defaultUserId,
          startTimeInMS: Date.now() - mock.exam.config.totalTimeInMS
        };
        await fastifyTestInstance.prisma.examEnvironmentExamAttempt.create({
          data: recentExamAttempt
        });

        const res = await superGet('/exam-environment/exams').set(
          'exam-environment-authorization-token',
          examEnvironmentAuthorizationToken
        );

        expect(res.body).toMatchObject([{ canTake: false }]);
        expect(res.status).toBe(200);

        // Update the attempt to be outside the retake time
        await fastifyTestInstance.prisma.examEnvironmentExamAttempt.update({
          where: { id: recentExamAttempt.id },
          data: {
            startTimeInMS:
              Date.now() -
              (mock.exam.config.totalTimeInMS +
                mock.exam.config.retakeTimeInMS +
                1000)
          }
        });

        const res2 = await superGet('/exam-environment/exams').set(
          'exam-environment-authorization-token',
          examEnvironmentAuthorizationToken
        );

        expect(res2.body).toMatchObject([{ canTake: true }]);

        expect(res2.status).toBe(200);
      });

      it('should indicate an exam is unavailable if there are any pending moderation records for the exam attempts', async () => {
        // Create an exam attempt that's outside the retake time
        const examAttempt = {