Repository: xenova/transformers.js
File: tests/utils/utils.test.js (JavaScript)

Code before:
    it("Can split image into separate channels", async () => {
      const url = './examples/demo-site/public/images/cats.jpg';
      const image = await RawImage.fromURL(url);
      // Rather than test the entire image, we'll just test the first 3 pixels;
      // ensuring that these match.
      const image_data = image.toChannels().map(c => c.slice(0, 3));

      const target = [
        new Uint8Array([140, 144, 145]), // Reds
        new Uint8Array([25, 25, 25]),    // Greens
        new Uint8Array([56, 67, 73]),    // Blues
      ];

      compare (image_data, target);
    });

    it("Can splits channels for grayscale", async () => {
      const url = './examples/demo-site/public/images/cats.jpg';
      const image = (await RawImage.fromURL(url)).grayscale();

      const image_data = image.toChannels().map(c => c.slice(0, 3));
      const target = [new Uint8Array([63, 65, 66])];

      compare (image_data, target);
    });
  });
});


Review comment (suggestion):
```suggestion
  describe("Image utilities", () => {
    const [width, height, channels] = [2, 2, 3];
    const data = Uint8Array.from({ length: width * height * channels }, (_, i) => i % 5);
    const image = new RawImage(data, width, height, channels);

    it("Can split image into separate channels", async () => {
      const image_data = image.split()

      const target = [
        new Uint8Array([0, 3, 1, 4]),  // Reds
        new Uint8Array([1, 4, 2, 0]), // Greens
        new Uint8Array([2, 0, 3, 1]), // Blues
      ];

      compare(image_data, target);
    });

    it("Can splits channels for grayscale", async () => {
      const image_data = image.grayscale().split();
      const target = [new Uint8Array([1, 3, 2, 1])];

      compare(image_data, target);
    });
  });
```

Code after:
    const [width, height, channels] = [2, 2, 3];
    const data = Uint8Array.from({ length: width * height * channels }, (_, i) => i % 5);
    const tiny_image = new RawImage(data, width, height, channels);
    
    let image;
    beforeAll(async () => {
      image = await RawImage.fromURL("https://picsum.photos/300/200");
    });

    it("Can split image into separate channels", async () => {
      const image_data = tiny_image.split().map(x => x.data);

      const target = [
        new Uint8Array([0, 3, 1, 4]), // Reds
        new Uint8Array([1, 4, 2, 0]), // Greens
        new Uint8Array([2, 0, 3, 1]), // Blues
      ];

      compare(image_data, target);
    });

    it("Can splits channels for grayscale", async () => {
      const image_data = tiny_image.grayscale().split().map(x => x.data);
      const target = [new Uint8Array([1, 3, 2, 1])];

      compare(image_data, target);
    });

    it("Read image from URL", async () => {
      expect(image.width).toBe(300);
      expect(image.height).toBe(200);
      expect(image.channels).toBe(3);
    });

    it("Can resize image", async () => {
      const resized = await image.resize(150, 100);
      expect(resized.width).toBe(150);
      expect(resized.height).toBe(100);
    });

    it("Can resize with aspect ratio", async () => {
      const resized = await image.resize(150, null);
      expect(resized.width).toBe(150);
      expect(resized.height).toBe(100);
    });

    it("Returns original image if width and height are null", async () => {
      const resized = await image.resize(null, null);
      expect(resized.width).toBe(300);
      expect(resized.height).toBe(200);
    });