import pathlib
import sys
from unittest.mock import Mock


SCRIPTS_DIR = pathlib.Path(__file__).resolve().parents[1] / "scripts"
sys.path.insert(0, str(SCRIPTS_DIR))

import criterion_evaluator


def test_main_threads_course_id_to_get_config(monkeypatch, capsys):
    course_id = "course-xyz"
    mock_get_config = Mock(return_value={
        "domain": "canvas.test.edu",
        "course_id": course_id,
        "course_url": f"https://canvas.test.edu/api/v1/courses/{course_id}",
        "headers": {"Authorization": "Bearer fake"},
    })

    def fake_api_get(path, params=None, course_id=None):
        assert course_id == "course-xyz"
        if path == "":
            return {"name": "Test Course", "syllabus_body": "<p>Ready</p>"}
        if path == "tabs":
            return []
        return None

    def fake_api_get_all(path, params=None, course_id=None):
        assert course_id == "course-xyz"
        return []

    monkeypatch.setattr(criterion_evaluator._canvas_api, "get_config",
                        mock_get_config)
    monkeypatch.setattr(criterion_evaluator, "_config", None)
    monkeypatch.setattr(criterion_evaluator, "_link_validation_result", None)
    monkeypatch.setattr(criterion_evaluator, "_api_get", fake_api_get)
    monkeypatch.setattr(criterion_evaluator, "_api_get_all", fake_api_get_all)
    monkeypatch.setattr(criterion_evaluator, "evaluate_all",
                        lambda cd, filter_standard=None, profile=None: [])
    monkeypatch.setattr(
        sys, "argv",
        ["criterion_evaluator.py", "--json", "--course-id", course_id,
         "--skip-link-validation"],
    )

    criterion_evaluator.main()

    mock_get_config.assert_called_once_with(course_id=course_id)
    captured = capsys.readouterr()
    assert course_id in captured.out
