import pytest

from infrastructure.s3_uri import parse_s3_uri


def test_parses_simple_s3_uri() -> None:
    # act / assert
    assert parse_s3_uri("s3://my-bucket/file.csv") == ("my-bucket", "file.csv")


def test_parses_s3_uri_with_nested_key() -> None:
    # act
    bucket, key = parse_s3_uri("s3://my-bucket/nested/path/to/file.csv")
    # assert
    assert (bucket, key) == ("my-bucket", "nested/path/to/file.csv")


def test_rejects_s3_uri_without_key() -> None:
    # act / assert
    with pytest.raises(ValueError, match="bucket and a key"):
        parse_s3_uri("s3://my-bucket")


def test_rejects_s3_uri_with_empty_key() -> None:
    # act / assert
    with pytest.raises(ValueError, match="bucket and a key"):
        parse_s3_uri("s3://my-bucket/")


def test_parses_console_url_prefix() -> None:
    # arrange
    url = "https://eu-west-2.console.aws.amazon.com/s3/object/my-bucket?prefix=nested%2Ffile.csv"
    # act / assert
    assert parse_s3_uri(url) == ("my-bucket", "nested/file.csv")


def test_rejects_unparseable_string() -> None:
    # act / assert
    with pytest.raises(ValueError):
        parse_s3_uri("not-a-uri-at-all")
