from __future__ import annotations

from pathlib import Path

import pytest

from sync_state_machine.pipeline.factory import _prepare_remote_jsonl_dir


def test_prepare_remote_jsonl_dir_autocreates_inside_project_root(tmp_path: Path) -> None:
    project_root = tmp_path / "project"
    project_root.mkdir(parents=True, exist_ok=True)

    remote_dir = project_root / "remote_datasource" / "datasource"
    assert not remote_dir.exists()

    prepared = _prepare_remote_jsonl_dir(remote_dir, project_root=project_root)

    assert prepared == remote_dir
    assert remote_dir.exists()
    assert remote_dir.is_dir()


def test_prepare_remote_jsonl_dir_rejects_outside_project_root(tmp_path: Path) -> None:
    project_root = tmp_path / "project"
    outside_dir = tmp_path / "outside" / "remote_datasource" / "datasource"
    project_root.mkdir(parents=True, exist_ok=True)

    with pytest.raises(FileNotFoundError, match="auto-create is only allowed"):
        _prepare_remote_jsonl_dir(outside_dir, project_root=project_root)
