# Copyright (C) 2021-2022 by the FEM on Colab authors
#
# This file is part of FEM on Colab-related actions.
#
# SPDX-License-Identifier: MIT
"""Tests for the open_in_colab_workflow.glob_images package."""

import os

from open_in_colab_workflow.glob_images import glob_images


def test_glob_images(root_directory: str) -> None:
    """Test that images are correctly transformed to their base64 representation."""
    nb_directory = os.path.join(root_directory, "tests", "data", "replace_images_in_markdown")
    black = os.path.join(nb_directory, "images", "black.png")
    black_base64_trail = "DUOgAAAABJRU5ErkJggg=="
    red = os.path.join(nb_directory, "images", "red.jpg")
    red_converted = red.replace(".jpg", ".png")
    red_base64_trail = "BJQwAAAABJRU5ErkJggg=="
    blue = os.path.join(nb_directory, "images", "blue.svg")
    blue_converted = blue.replace(".svg", ".png")
    blue_base64_trail = ["Zm1CLDAAAAAElFTkSuQmCC", "bUIsMAAAAASUVORK5CYII="]

    # Clean up converted images from previous runs
    if os.path.isfile(red_converted):
        os.remove(red_converted)
    if os.path.isfile(blue_converted):
        os.remove(blue_converted)

    # Run test on clean repository
    images_as_base64 = glob_images(nb_directory)
    assert len(images_as_base64) == 3
    assert black in images_as_base64
    assert black_base64_trail in images_as_base64[black]
    assert red in images_as_base64
    assert red_base64_trail in images_as_base64[red]
    assert blue in images_as_base64
    assert any([blue_base64_trail_ in images_as_base64[blue] for blue_base64_trail_ in blue_base64_trail])

    # Expected values *_base64_trail can be regenerated by uncommenting the following lines
    """
    print()
    print(images_as_base64[black][-22:])
    print(images_as_base64[red][-22:])
    print(images_as_base64[blue][-22:])
    """

    # Run test on a dirty repository. The red and blue images are found twice, once with their original
    # extension and once with the png extension due to the conversion in the previous run
    images_as_base64 = glob_images(nb_directory)
    assert len(images_as_base64) == 5
    assert black in images_as_base64
    assert black_base64_trail in images_as_base64[black]
    assert red in images_as_base64
    assert red_base64_trail in images_as_base64[red]
    assert red_converted in images_as_base64
    assert red_base64_trail in images_as_base64[red_converted]
    assert blue in images_as_base64
    assert any([blue_base64_trail_ in images_as_base64[blue] for blue_base64_trail_ in blue_base64_trail])
    assert blue_converted in images_as_base64
    assert any([blue_base64_trail_ in images_as_base64[blue_converted] for blue_base64_trail_ in blue_base64_trail])

# Copyright (C) 2021-2022 by the FEM on Colab authors
#
# This file is part of FEM on Colab-related actions.
#
# SPDX-License-Identifier: MIT
"""Tests for the open_in_colab_workflow.glob_images package."""

import os

from open_in_colab_workflow.glob_images import glob_images


def test_glob_images(root_directory: str) -> None:
    """Test that images are correctly transformed to their base64 representation."""
    nb_directory = os.path.join(root_directory, "tests", "data", "replace_images_in_markdown")
    black = os.path.join(nb_directory, "images", "black.png")
    black_base64_trail = "DUOgAAAABJRU5ErkJggg=="
    red = os.path.join(nb_directory, "images", "red.jpg")
    red_converted = red.replace(".jpg", ".png")
    red_base64_trail = "BJQwAAAABJRU5ErkJggg=="
    blue = os.path.join(nb_directory, "images", "blue.svg")
    blue_converted = blue.replace(".svg", ".png")
    blue_base64_trail = ["Zm1CLDAAAAAElFTkSuQmCC", "bUIsMAAAAASUVORK5CYII="]

    # Clean up converted images from previous runs
    if os.path.isfile(red_converted):
        os.remove(red_converted)
    if os.path.isfile(blue_converted):
        os.remove(blue_converted)

    # Run test on clean repository
    images_as_base64 = glob_images(nb_directory)
    assert len(images_as_base64) == 3
    assert black in images_as_base64
    assert black_base64_trail in images_as_base64[black]
    assert red in images_as_base64
    assert red_base64_trail in images_as_base64[red]
    assert blue in images_as_base64
    assert any([blue_base64_trail_ in images_as_base64[blue] for blue_base64_trail_ in blue_base64_trail])

    # Expected values *_base64_trail can be regenerated by uncommenting the following lines
    """
    print()
    print(images_as_base64[black][-22:])
    print(images_as_base64[red][-22:])
    print(images_as_base64[blue][-22:])
    """

    # Run test on a dirty repository. The red and blue images are found twice, once with their original
    # extension and once with the png extension due to the conversion in the previous run
    images_as_base64 = glob_images(nb_directory)
    assert len(images_as_base64) == 5
    assert black in images_as_base64
    assert black_base64_trail in images_as_base64[black]
    assert red in images_as_base64
    assert red_base64_trail in images_as_base64[red]
    assert red_converted in images_as_base64
    assert red_base64_trail in images_as_base64[red_converted]
    assert blue in images_as_base64
    assert any([blue_base64_trail_ in images_as_base64[blue] for blue_base64_trail_ in blue_base64_trail])
    assert blue_converted in images_as_base64
    assert any([blue_base64_trail_ in images_as_base64[blue_converted] for blue_base64_trail_ in blue_base64_trail])

# Copyright (C) 2021-2022 by the FEM on Colab authors # # This file is part of FEM on Colab-related actions. # # SPDX-License-Identifier: MIT Tests for the open_in_colab_workflow.glob_images package. Test that images are correctly transformed to their base64 representation. # Clean up converted images from previous runs # Run test on clean repository # Expected values *_base64_trail can be regenerated by uncommenting the following lines print()     print(images_as_base64[black][-22:])     print(images_as_base64[red][-22:])     print(images_as_base64[blue][-22:]) # Run test on a dirty repository. The red and blue images are found twice, once with their original # extension and once with the png extension due to the conversion in the previous run