from pathlib import Path

# worktree 내 스크립트 경로 사용 (메인 workspace와 독립적)
WORKSPACE = Path("/home/jay/workspace/.worktrees/task-2461-dev6")
WT_MGR = WORKSPACE / "scripts/worktree_manager.py"


def test_main_function_includes_sentinel_print():
    """task-2461 P0-2: main() 에 @@WORKTREE_FINISH_RESULT@@ sentinel 출력이 포함되어야 함"""
    src = WT_MGR.read_text(encoding="utf-8")
    assert "@@WORKTREE_FINISH_RESULT@@" in src, "sentinel 출력 누락"


def test_exit_on_block_option_exists():
    """--exit-on-block 옵션이 finish 서브커맨드에 정의되어야 함"""
    src = WT_MGR.read_text(encoding="utf-8")
    assert "--exit-on-block" in src or "exit_on_block" in src


def test_sentinel_is_single_line():
    """sentinel 출력은 단일 라인 (개행 없는 JSON)이어야 함"""
    src = WT_MGR.read_text(encoding="utf-8")
    # sentinel print문이 indent=2 없이 json.dumps 사용하는지 확인
    sentinel_lines = [l for l in src.splitlines() if "@@WORKTREE_FINISH_RESULT@@" in l]
    assert len(sentinel_lines) >= 1, "sentinel print 문 미존재"
    # indent 파라미터 없이 호출되어야 함 (단일 라인 JSON)
    for line in sentinel_lines:
        if "print(" in line:
            assert "indent=" not in line, "sentinel에 indent= 파라미터가 있으면 안 됨 (멀티라인 금지)"


def test_exit_on_block_logic_in_main():
    """main() 함수에 exit_on_block 분기 로직이 존재해야 함"""
    src = WT_MGR.read_text(encoding="utf-8")
    assert "exit_on_block" in src
    # non-merged 시 sys.exit(1) 호출 확인
    assert 'wt_status != "merged"' in src or "wt_status != 'merged'" in src
