Repository: freeCodeCamp/freeCodeCamp
File: tools/scripts/build/build-external-curricula-data-v2.test.ts (TypeScript)

Code before:
        }
      })
    ).map(file => file.path);

    superBlockFiles.forEach(file => {
      const fileContentJson = fs.readFileSync(
        `${clientStaticPath}/curriculum-data/${VERSION}/${file}`,
        'utf-8'
      );

      const fileContent = JSON.parse(
        fileContentJson
      ) as Curriculum<GeneratedCurriculumProps>;

      const superBlock = Object.keys(fileContent)[0] as SuperBlocks;
      const superBlockData = fileContent[
        superBlock
      ] as GeneratedBlockBasedCurriculumProps;

      // Randomly pick a block to check its data.
      const blocks = superBlockData.blocks;
      const randomBlockIndex = Math.floor(Math.random() * blocks.length);

      expect(superBlockData.intro).toEqual(intros[superBlock].intro);
      expect(superBlockData.blocks[randomBlockIndex].intro).toEqual(
        intros[superBlock].blocks[randomBlockIndex].intro
      );
      expect(superBlockData.blocks[randomBlockIndex].meta.name).toEqual(
        intros[superBlock].blocks[randomBlockIndex].title
      );
    });
  });

  test('chapter-based super blocks and blocks should have the correct data', async () => {
    const superBlockFiles = (
      await readdirp.promise(`${clientStaticPath}/curriculum-data/${VERSION}`, {
        directoryFilter: ['!challenges'],
        fileFilter: entry => {
          // The directory contains super block files and other curriculum-related files.
          // We're only interested in super block ones.
          const superBlocks = Object.values(SuperBlocks);
          return (
            superBlocks.includes(entry.basename) &&
            chapterBasedSuperBlocks.includes(entry.basename)
          );
        }
      })
    ).map(file => file.path);

    superBlockFiles.forEach(file => {
      const fileContentJson = fs.readFileSync(

Review comment (bug):
We're comparing the intro in the generated data against the intro in `intro.json`.

The look up here is incorrect as `blocks` in `intro.json` is an object rather than an array.

Code after:
        directoryFilter: ['!challenges'],
        fileFilter: entry => {
          // The directory contains super block files and other curriculum-related files.
          // We're only interested in super block ones.
          const isSuperBlock = superBlocks.some(superBlock =>
            entry.basename.includes(superBlock)
          );

          const isChapterBasedSuperBlock = chapterBasedSuperBlocks.some(
            chapterBasedSuperBlock =>
              entry.basename.includes(chapterBasedSuperBlock)
          );

          return isSuperBlock && !isChapterBasedSuperBlock;
        }
      })
    ).map(file => file.path);

    expect(superBlockFiles.length).toBeGreaterThan(0);

    superBlockFiles.forEach(file => {
      const fileContentJson = fs.readFileSync(
        `${clientStaticPath}/curriculum-data/${VERSION}/${file}`,
        'utf-8'
      );

      const fileContent = JSON.parse(
        fileContentJson
      ) as Curriculum<GeneratedCurriculumProps>;

      const superBlock = Object.keys(fileContent)[0] as SuperBlocks;
      const superBlockData = fileContent[
        superBlock
      ] as GeneratedBlockBasedCurriculumProps;

      // Randomly pick a block to check its data.
      const blocks = superBlockData.blocks;
      const randomBlockIndex = Math.floor(Math.random() * blocks.length);
      const randomBlock = blocks[randomBlockIndex];

      expect(superBlockData.intro).toEqual(intros[superBlock].intro);
      expect(superBlockData.blocks[randomBlockIndex].intro).toEqual(
        intros[superBlock].blocks[randomBlock.meta.dashedName as string].intro
      );
      expect(superBlockData.blocks[randomBlockIndex].meta.name).toEqual(
        intros[superBlock].blocks[randomBlock.meta.dashedName as string].title
      );
    });
  });

  test('chapter-based super blocks and blocks should have the correct data', async () => {