# 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_legacy_instance(ini_settings):
    config = ini_settings
    config[config_keys.backend_type] = config_keys.backend_legacy_filesystem
    f_store = store_utils.get_filestore_backend(config=config, always_init=True)
    return f_store


class TestFileStoreLegacyBackend:
    @pytest.mark.parametrize("filename", [generate_random_filename() for _ in range(10)])
    def test_get_shard_number(self, filename, file_store_legacy_instance):
        shard_number = file_store_legacy_instance.get_shard_index(filename, len(file_store_legacy_instance._shards))
        # Check that the shard number is 0 for legacy filesystem store we don't use shards
        assert shard_number == 0

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