from __future__ import annotations

import asyncio

from thread_downloader import DownloadConfig, Downloader, DownloadTask

from conftest import run_test_server


def test_downloader_instance_can_be_reused(tmp_path, sample_data: bytes) -> None:
    downloader = Downloader(DownloadConfig(workers=4))

    with run_test_server(support_range=True, data=sample_data) as (url, _state):
        first_file = tmp_path / "first.bin"
        second_file = tmp_path / "second.bin"

        result1 = asyncio.run(downloader.download(DownloadTask(url, str(first_file))))
        result2 = asyncio.run(downloader.download(DownloadTask(url, str(second_file))))

    assert first_file.read_bytes() == sample_data
    assert second_file.read_bytes() == sample_data
    assert result1.bytes_written == len(sample_data)
    assert result2.bytes_written == len(sample_data)
