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

Code before:
        await fastifyTestInstance.prisma.examEnvironmentExamModeration.create({
          data: {
            examAttemptId: createdAttempt.id,
            status: ExamEnvironmentExamModerationStatus.Pending
          }
        });

        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,
              passingPercent: mock.exam.config.passingPercent,
              totalTimeInMS: mock.exam.config.totalTimeInMS,
              retakeTimeInMS: mock.exam.config.retakeTimeInMS
            },
            id: mock.examId
          }
        ]);

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

    describe('GET /exam-environment/exam/attempt/:attemptId', () => {
      afterEach(async () => {
        // If attempt is deleted, moderation record should cascade
        await fastifyTestInstance.prisma.examEnvironmentExamAttempt.deleteMany();
        const moderationRecords =
          await fastifyTestInstance.prisma.examEnvironmentExamModeration.findMany(
            {}
          );
        expect(moderationRecords).toHaveLength(0);
      });

      it('should return 404 if the attempt does not exist', async () => {
        const attemptId = mock.oid();
        const res = await superGet(
          `/exam-environment/exam/attempt/${attemptId}`
        ).set(
          'exam-environment-authorization-token',
          examEnvironmentAuthorizationToken
        );

        expect(res.body).toStrictEqual({

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

Code after:
          await fastifyTestInstance.prisma.examEnvironmentExamAttempt.create({
            data: { ...mock.examAttempt, userId: mock.oid() }
          });
        const res = await superGet(
          `/exam-environment/exam/attempt/${otherUserAttempt.id}`
        ).set(
          'exam-environment-authorization-token',
          examEnvironmentAuthorizationToken
        );

        expect(res.body).toStrictEqual({
          code: 'FCC_ENOENT_EXAM_ENVIRONMENT_EXAM_ATTEMPT', // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
          message: expect.any(String)
        });
        expect(res.status).toBe(404);
      });

      it('should return 200 with the examEnvironmentExamAttempt if the attempt exists and belongs to the user', async () => {
        const attempt =
          await fastifyTestInstance.prisma.examEnvironmentExamAttempt.create({
            data: { ...mock.examAttempt, userId: defaultUserId }
          });
        await fastifyTestInstance.prisma.examEnvironmentExamModeration.create({
          data: {
            examAttemptId: attempt.id,
            status: ExamEnvironmentExamModerationStatus.Pending
          }
        });

        const res = await superGet(
          `/exam-environment/exam/attempt/${attempt.id}`
        ).set(
          'exam-environment-authorization-token',
          examEnvironmentAuthorizationToken
        );

        const examEnvironmentExamAttempt = {
          result: null,
          startTimeInMS: attempt.startTimeInMS,
          questionSets: attempt.questionSets
        };

        expect(res.body).toEqual(examEnvironmentExamAttempt);
        expect(res.status).toBe(200);
      });

      xit('TODO: (once serialization is serializable) should return 400 if no attempt id is given', async () => {
        const res = await superGet('/exam-environment/exam/attempt/').set(
          'exam-environment-authorization-token',
          examEnvironmentAuthorizationToken
        );