# Copyright (C) 2010-2024 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
import pytest

from rhodecode.apps.file_store import utils as store_utils
from rhodecode.apps.file_store import config_keys
from rhodecode.apps.file_store.tests import generate_random_filename


@pytest.fixture()
def file_store_objectstore_instance(ini_settings):
    config = ini_settings
    config[config_keys.backend_type] = config_keys.backend_objectstore
    f_store = store_utils.get_filestore_backend(config=config, always_init=True)
    return f_store


class TestFileStoreObjectStoreBackend:
    @pytest.mark.parametrize("filename", [generate_random_filename() for _ in range(10)])
    def test_get_shard_number(self, filename, file_store_objectstore_instance):
        shard_number = file_store_objectstore_instance.get_shard_index(
            filename, len(file_store_objectstore_instance._shards)
        )
        # Check that the shard number is between 0 and shards
        assert 0 <= shard_number <= len(file_store_objectstore_instance._shards)

    @pytest.mark.parametrize(
        "filename, expected_shard_num",
        [
            ("my-name-1", 3),
            ("my-name-2", 2),
            ("my-name-3", 4),
            ("my-name-4", 1),
            ("rhodecode-enterprise-ce", 5),
            ("rhodecode-enterprise-ee", 6),
        ],
    )
    def test_get_shard_number_consistency(self, filename, expected_shard_num, file_store_objectstore_instance):
        shard_number = file_store_objectstore_instance.get_shard_index(
            filename, len(file_store_objectstore_instance._shards)
        )
        assert expected_shard_num == shard_number
