"""Tests for erk create command output behavior."""

from datetime import UTC, datetime
from pathlib import Path

from click.testing import CliRunner

from erk.cli.cli import cli
from erk_shared.gateway.git.abc import WorktreeInfo
from erk_shared.gateway.git.fake import FakeGit
from erk_shared.gateway.github.issues.fake import FakeGitHubIssues
from erk_shared.gateway.github.issues.types import IssueInfo
from tests.test_utils.env_helpers import erk_inmem_env, erk_isolated_fs_env


def test_create_from_current_branch_outputs_script_path_to_stdout() -> None:
    """Test that create --from-current-branch outputs script path to stdout, not stderr.

    This test verifies that the shell integration handler can read the script path
    from stdout. If the script path is written to stderr, the handler will miss it
    and display 'no directory change needed' instead of switching to the new worktree.

    See: https://github.com/anthropics/erk/issues/XXX
    """
    runner = CliRunner()
    with erk_inmem_env(runner) as env:
        repo_dir = env.erk_root / "repos" / env.cwd.name

        # Set up git state: in root worktree on feature branch
        git_ops = FakeGit(
            worktrees={
                env.cwd: [
                    WorktreeInfo(path=env.cwd, branch="main"),
                ]
            },
            current_branches={env.cwd: "my-feature"},
            default_branches={env.cwd: "main"},
            git_common_dirs={env.cwd: env.git_dir},
        )

        test_ctx = env.build_context(git=git_ops)

        # Act: Create worktree from current branch with --script flag
        result = runner.invoke(
            cli,
            ["wt", "create", "--from-current-branch", "--script"],
            obj=test_ctx,
            catch_exceptions=False,
        )

        # Assert: Command succeeded
        if result.exit_code != 0:
            print(f"stderr: {result.stderr}")
            print(f"stdout: {result.stdout}")
        assert result.exit_code == 0

        # Assert: Script path is in stdout (for shell integration)
        assert result.stdout.strip() != "", (
            "Script path should be in stdout for shell integration to read. "
            "Currently it's being written to stderr via user_output(), "
            "but should be written to stdout via machine_output()."
        )

        # Assert: Script path is a valid path to activation script
        script_path = Path(result.stdout.strip())
        script_content = env.script_writer.get_script_content(script_path)
        assert script_content is not None, "Script path should reference a valid script"

        # Assert: Script contains cd command to new worktree
        expected_worktree_path = repo_dir / "worktrees" / "my-feature"
        assert str(expected_worktree_path) in script_content, (
            f"Script should cd to {expected_worktree_path}"
        )


def test_create_from_plan_with_valid_issue() -> None:
    """Test erk create --from-plan with valid erk-plan issue."""
    runner = CliRunner()
    with erk_isolated_fs_env(runner) as env:
        repo_dir = env.erk_root / "repos" / env.cwd.name

        # Set up git state
        git_ops = FakeGit(
            worktrees={
                env.cwd: [
                    WorktreeInfo(path=env.cwd, branch="main"),
                ]
            },
            current_branches={env.cwd: "main"},
            default_branches={env.cwd: "main"},
            git_common_dirs={env.cwd: env.git_dir},
        )

        # Set up GitHub state with issue
        now = datetime.now(UTC)
        fake_issues = FakeGitHubIssues(
            issues={
                123: IssueInfo(
                    number=123,
                    title="Add User Authentication",
                    body="## Implementation\n\n- Step 1\n- Step 2",
                    state="OPEN",
                    url="https://github.com/owner/repo/issues/123",
                    labels=["erk-plan"],
                    assignees=[],
                    created_at=now,
                    updated_at=now,
                    author="test-user",
                )
            }
        )

        test_ctx = env.build_context(git=git_ops, issues=fake_issues)

        # Act: Run create --from-plan 123
        result = runner.invoke(
            cli,
            ["wt", "create", "--from-plan", "123"],
            obj=test_ctx,
            catch_exceptions=False,
        )

        # Assert: Command succeeded
        if result.exit_code != 0:
            print(f"stderr: {result.stderr}")
            print(f"stdout: {result.stdout}")
        assert result.exit_code == 0

        # Assert: Worktree created with branch name derived from issue title
        # Branch name is sanitize_worktree_name(...) + timestamp suffix "-01-15-1430"
        # "P123-Add User Authentication" -> "P123-add-user-authentication-01-15-1430"
        worktrees_dir = repo_dir / "worktrees"
        expected_worktree_path = worktrees_dir / "P123-add-user-authentication-01-15-1430"
        assert expected_worktree_path.exists(), (
            f"Expected worktree at {expected_worktree_path}, found: {list(worktrees_dir.glob('*'))}"
        )

        # Assert: .impl/ folder exists with correct content
        impl_path = expected_worktree_path / ".impl"
        assert impl_path.exists()

        # Assert: plan.md has issue body
        plan_path = impl_path / "plan.md"
        assert plan_path.exists()
        plan_content = plan_path.read_text(encoding="utf-8")
        assert "## Implementation" in plan_content

        # Assert: issue.json has metadata
        issue_json_path = impl_path / "issue.json"
        assert issue_json_path.exists()
        import json

        issue_json = json.loads(issue_json_path.read_text(encoding="utf-8"))
        assert issue_json["issue_number"] == 123
        assert issue_json["issue_title"] == "Add User Authentication"
        assert issue_json["issue_url"] == "https://github.com/owner/repo/issues/123"


def test_create_from_plan_missing_label() -> None:
    """Test erk create --from-plan fails if issue lacks erk-plan label."""
    runner = CliRunner()
    with erk_isolated_fs_env(runner) as env:
        # Set up git state
        git_ops = FakeGit(
            worktrees={
                env.cwd: [
                    WorktreeInfo(path=env.cwd, branch="main"),
                ]
            },
            current_branches={env.cwd: "main"},
            default_branches={env.cwd: "main"},
            git_common_dirs={env.cwd: env.git_dir},
        )

        # Set up GitHub state with issue without erk-plan label
        now = datetime.now(UTC)
        fake_issues = FakeGitHubIssues(
            issues={
                456: IssueInfo(
                    number=456,
                    title="Regular Issue",
                    body="Not a plan",
                    state="OPEN",
                    url="https://github.com/owner/repo/issues/456",
                    labels=["bug", "enhancement"],  # No erk-plan label
                    assignees=[],
                    created_at=now,
                    updated_at=now,
                    author="test-user",
                )
            }
        )

        test_ctx = env.build_context(git=git_ops, issues=fake_issues)

        # Act: Run create --from-plan 456
        result = runner.invoke(
            cli,
            ["wt", "create", "--from-plan", "456"],
            obj=test_ctx,
        )

        # Assert: Error message about missing label
        assert result.exit_code == 1
        assert "must have 'erk-plan' label" in result.output
        assert "gh issue edit 456 --add-label erk-plan" in result.output


def test_create_from_plan_url_parsing() -> None:
    """Test erk create --from-plan with GitHub URL."""
    runner = CliRunner()
    with erk_isolated_fs_env(runner) as env:
        # Set up git state
        git_ops = FakeGit(
            worktrees={
                env.cwd: [
                    WorktreeInfo(path=env.cwd, branch="main"),
                ]
            },
            current_branches={env.cwd: "main"},
            default_branches={env.cwd: "main"},
            git_common_dirs={env.cwd: env.git_dir},
        )

        # Set up GitHub state with issue
        now = datetime.now(UTC)
        fake_issues = FakeGitHubIssues(
            issues={
                789: IssueInfo(
                    number=789,
                    title="Feature Request",
                    body="Plan content",
                    state="OPEN",
                    url="https://github.com/owner/repo/issues/789",
                    labels=["erk-plan"],
                    assignees=[],
                    created_at=now,
                    updated_at=now,
                    author="test-user",
                )
            }
        )

        test_ctx = env.build_context(git=git_ops, issues=fake_issues)

        # Act: Run with full GitHub URL
        result = runner.invoke(
            cli,
            ["wt", "create", "--from-plan", "https://github.com/owner/repo/issues/789"],
            obj=test_ctx,
            catch_exceptions=False,
        )

        # Assert: Success (URL parsed correctly)
        assert result.exit_code == 0
        # Branch name derived from issue title: "789-Feature Request" -> "789-feature-request"
        assert "Created worktree" in result.output or "789-feature-request" in result.output


def test_create_from_plan_name_derivation() -> None:
    """Test worktree name derived from issue title via sanitize_worktree_name."""
    runner = CliRunner()
    with erk_isolated_fs_env(runner) as env:
        repo_dir = env.erk_root / "repos" / env.cwd.name

        # Set up git state
        git_ops = FakeGit(
            worktrees={
                env.cwd: [
                    WorktreeInfo(path=env.cwd, branch="main"),
                ]
            },
            current_branches={env.cwd: "main"},
            default_branches={env.cwd: "main"},
            git_common_dirs={env.cwd: env.git_dir},
        )

        # Set up GitHub state with issue with special characters in title
        now = datetime.now(UTC)
        fake_issues = FakeGitHubIssues(
            issues={
                111: IssueInfo(
                    number=111,
                    title="Fix: Database Connection Issues!!!",
                    body="Plan",
                    state="OPEN",
                    url="https://github.com/owner/repo/issues/111",
                    labels=["erk-plan"],
                    assignees=[],
                    created_at=now,
                    updated_at=now,
                    author="test-user",
                )
            }
        )

        test_ctx = env.build_context(git=git_ops, issues=fake_issues)

        # Act
        result = runner.invoke(
            cli,
            ["wt", "create", "--from-plan", "111"],
            obj=test_ctx,
            catch_exceptions=False,
        )

        # Assert: Name = sanitize_worktree_name(...) + timestamp suffix "-01-15-1430"
        # "P111-Fix: Database..." -> "P111-fix-database-connection-is-01-15-1430"
        # Note: truncated to 31 chars before timestamp suffix
        assert result.exit_code == 0
        worktrees_dir = repo_dir / "worktrees"
        expected_worktree_path = worktrees_dir / "P111-fix-database-connection-is-01-15-1430"
        assert expected_worktree_path.exists(), (
            f"Expected worktree at {expected_worktree_path}, found: {list(worktrees_dir.glob('*'))}"
        )


def test_create_from_plan_not_found() -> None:
    """Test erk create --from-plan when issue doesn't exist."""
    runner = CliRunner()
    with erk_isolated_fs_env(runner) as env:
        # Set up git state
        git_ops = FakeGit(
            worktrees={
                env.cwd: [
                    WorktreeInfo(path=env.cwd, branch="main"),
                ]
            },
            current_branches={env.cwd: "main"},
            default_branches={env.cwd: "main"},
            git_common_dirs={env.cwd: env.git_dir},
        )

        # Set up GitHub state with no issues
        fake_issues = FakeGitHubIssues()  # Empty

        test_ctx = env.build_context(git=git_ops, issues=fake_issues)

        # Act: Request non-existent issue
        result = runner.invoke(
            cli,
            ["wt", "create", "--from-plan", "999"],
            obj=test_ctx,
        )

        # Assert: Error from integration layer
        assert result.exit_code == 1
        # FakeGitHubIssues raises RuntimeError with "not found" message
        assert "not found" in result.output.lower() or "Issue #999" in result.output


def test_create_from_plan_readonly_operation() -> None:
    """Test that --from-plan doesn't create/modify issues."""
    runner = CliRunner()
    with erk_isolated_fs_env(runner) as env:
        # Set up git state
        git_ops = FakeGit(
            worktrees={
                env.cwd: [
                    WorktreeInfo(path=env.cwd, branch="main"),
                ]
            },
            current_branches={env.cwd: "main"},
            default_branches={env.cwd: "main"},
            git_common_dirs={env.cwd: env.git_dir},
        )

        # Set up GitHub state with issue
        now = datetime.now(UTC)
        fake_issues = FakeGitHubIssues(
            issues={
                222: IssueInfo(
                    number=222,
                    title="Test",
                    body="Body",
                    state="OPEN",
                    url="https://example.com",
                    labels=["erk-plan"],
                    assignees=[],
                    created_at=now,
                    updated_at=now,
                    author="test-user",
                )
            }
        )

        test_ctx = env.build_context(git=git_ops, issues=fake_issues)

        # Act
        result = runner.invoke(
            cli,
            ["wt", "create", "--from-plan", "222"],
            obj=test_ctx,
            catch_exceptions=False,
        )

        # Assert: Command succeeded
        assert result.exit_code == 0

        # Assert: No issues created
        assert len(fake_issues.created_issues) == 0
        # Assert: No comments added
        assert len(fake_issues.added_comments) == 0


def test_create_from_plan_tracks_branch_with_graphite() -> None:
    """Test erk create --from-plan tracks the branch via BranchManager when use_graphite=True.

    Verifies that when:
    1. use_graphite=True in global config
    2. erk wt create --from-plan <issue> is called
    3. Then ctx.branch_manager.track_branch() is called with the linked branch name
       and trunk as parent (delegates to graphite.track_branch() in GraphiteBranchManager)
    """
    runner = CliRunner()
    with erk_isolated_fs_env(runner) as env:
        repo_dir = env.erk_root / "repos" / env.cwd.name

        # Set up git state
        git_ops = FakeGit(
            worktrees={
                env.cwd: [
                    WorktreeInfo(path=env.cwd, branch="main"),
                ]
            },
            current_branches={env.cwd: "main"},
            default_branches={env.cwd: "main"},
            git_common_dirs={env.cwd: env.git_dir},
        )

        # Set up GitHub state with issue
        now = datetime.now(UTC)
        fake_issues = FakeGitHubIssues(
            issues={
                500: IssueInfo(
                    number=500,
                    title="Test Graphite Tracking",
                    body="## Plan\n\n- Step 1",
                    state="OPEN",
                    url="https://github.com/owner/repo/issues/500",
                    labels=["erk-plan"],
                    assignees=[],
                    created_at=now,
                    updated_at=now,
                    author="test-user",
                )
            }
        )

        # Create FakeGraphite to track calls
        from erk_shared.gateway.graphite.fake import FakeGraphite

        fake_graphite = FakeGraphite()

        # Build context with use_graphite=True
        test_ctx = env.build_context(
            git=git_ops,
            issues=fake_issues,
            graphite=fake_graphite,
            use_graphite=True,
        )

        # Act: Run create --from-plan 500
        result = runner.invoke(
            cli,
            ["wt", "create", "--from-plan", "500"],
            obj=test_ctx,
            catch_exceptions=False,
        )

        # Assert: Command succeeded
        if result.exit_code != 0:
            print(f"stderr: {result.stderr}")
            print(f"stdout: {result.stdout}")
        assert result.exit_code == 0

        # Assert: Worktree was created
        worktrees_dir = repo_dir / "worktrees"
        assert worktrees_dir.exists(), f"Worktrees dir should exist: {worktrees_dir}"

        # Assert: track_branch was called with correct parameters
        # The branch name is derived from issue title with timestamp suffix
        # Parent should be "main" (the trunk branch)
        assert len(fake_graphite.track_branch_calls) == 1, (
            f"Expected 1 track_branch call, got {len(fake_graphite.track_branch_calls)}: "
            f"{fake_graphite.track_branch_calls}"
        )

        call = fake_graphite.track_branch_calls[0]
        cwd_path, branch_name, parent_branch = call

        # Branch name should contain the issue number
        assert "500" in branch_name, f"Branch name should contain issue number: {branch_name}"

        # Parent should be trunk branch (main)
        assert parent_branch == "main", f"Parent branch should be 'main', got: {parent_branch}"


def test_create_from_plan_no_graphite_tracking_when_disabled() -> None:
    """Test erk create --from-plan works when use_graphite=False.

    When Graphite is disabled, the branch_manager uses GitBranchManager which
    has a no-op track_branch method. This test verifies the command succeeds
    and uses the correct branch manager (GitBranchManager via GraphiteDisabled).
    """
    runner = CliRunner()
    with erk_isolated_fs_env(runner) as env:
        # Set up git state
        git_ops = FakeGit(
            worktrees={
                env.cwd: [
                    WorktreeInfo(path=env.cwd, branch="main"),
                ]
            },
            current_branches={env.cwd: "main"},
            default_branches={env.cwd: "main"},
            git_common_dirs={env.cwd: env.git_dir},
        )

        # Set up GitHub state with issue
        now = datetime.now(UTC)
        fake_issues = FakeGitHubIssues(
            issues={
                501: IssueInfo(
                    number=501,
                    title="Test No Graphite",
                    body="## Plan\n\n- Step 1",
                    state="OPEN",
                    url="https://github.com/owner/repo/issues/501",
                    labels=["erk-plan"],
                    assignees=[],
                    created_at=now,
                    updated_at=now,
                    author="test-user",
                )
            }
        )

        # Build context with use_graphite=False (default)
        # Don't pass graphite - let it use GraphiteDisabled automatically
        test_ctx = env.build_context(
            git=git_ops,
            issues=fake_issues,
            use_graphite=False,  # Explicitly disabled - uses GraphiteDisabled
        )

        # Act: Run create --from-plan 501
        result = runner.invoke(
            cli,
            ["wt", "create", "--from-plan", "501"],
            obj=test_ctx,
            catch_exceptions=False,
        )

        # Assert: Command succeeded (branch_manager.track_branch is no-op for GitBranchManager)
        assert result.exit_code == 0

        # Assert: branch_manager is GitBranchManager (not GraphiteBranchManager)
        from erk_shared.gateway.branch_manager.git import GitBranchManager

        assert isinstance(test_ctx.branch_manager, GitBranchManager), (
            f"Expected GitBranchManager when use_graphite=False, "
            f"got: {type(test_ctx.branch_manager).__name__}"
        )


def test_create_with_from_branch_trunk_errors() -> None:
    """Test that create --from-branch prevents creating worktree for trunk branch.

    This test verifies that ensure_worktree_for_branch() validation catches
    attempts to create a worktree for the trunk branch via --from-branch flag.
    The error should match the one from checkout command for consistency.
    """
    runner = CliRunner()
    with erk_inmem_env(runner) as env:
        # Setup: root worktree on a feature branch (NOT trunk)
        # This way we can test creating a worktree for trunk without "already checked out" error
        git_ops = FakeGit(
            worktrees={
                env.cwd: [
                    WorktreeInfo(path=env.cwd, branch="feature-1"),
                ]
            },
            current_branches={env.cwd: "feature-1"},
            git_common_dirs={env.cwd: env.git_dir},
            local_branches={env.cwd: ["main", "feature-1"]},
            default_branches={env.cwd: "main"},
        )

        test_ctx = env.build_context(git=git_ops)

        # Try to create worktree from trunk branch - should error
        result = runner.invoke(
            cli,
            ["wt", "create", "foo", "--from-branch", "main"],
            obj=test_ctx,
            catch_exceptions=False,
        )

        # Should fail with error
        assert result.exit_code == 1

        # Error message should match checkout command for consistency
        assert "Cannot create worktree for trunk branch" in result.stderr
        assert "main" in result.stderr
        assert "erk br co root" in result.stderr
        assert "root worktree" in result.stderr

        # Verify no worktree was created
        assert len(git_ops.added_worktrees) == 0


def test_create_from_current_branch_shows_shell_integration_instructions() -> None:
    """Test that create --from-current-branch shows setup instructions without --script."""
    runner = CliRunner()
    with erk_inmem_env(runner) as env:
        # Set up git state: in root worktree on feature branch
        git_ops = FakeGit(
            worktrees={
                env.cwd: [
                    WorktreeInfo(path=env.cwd, branch="main"),
                ]
            },
            current_branches={env.cwd: "my-feature"},
            default_branches={env.cwd: "main"},
            git_common_dirs={env.cwd: env.git_dir},
        )

        test_ctx = env.build_context(git=git_ops)

        # Act: Create worktree from current branch WITHOUT --script flag
        result = runner.invoke(
            cli,
            ["wt", "create", "--from-current-branch"],
            obj=test_ctx,
            catch_exceptions=False,
        )

        # Assert: Command succeeded
        if result.exit_code != 0:
            print(f"stderr: {result.stderr}")
            print(f"stdout: {result.stdout}")
        assert result.exit_code == 0

        # Assert: Output contains worktree creation message
        assert "Created worktree at" in result.stderr


def test_create_from_current_branch_with_stay_flag() -> None:
    """Test that create --from-current-branch --stay shows minimal output."""
    runner = CliRunner()
    with erk_inmem_env(runner) as env:
        # Set up git state: in root worktree on feature branch
        git_ops = FakeGit(
            worktrees={
                env.cwd: [
                    WorktreeInfo(path=env.cwd, branch="main"),
                ]
            },
            current_branches={env.cwd: "my-feature"},
            default_branches={env.cwd: "main"},
            git_common_dirs={env.cwd: env.git_dir},
        )

        test_ctx = env.build_context(git=git_ops)

        # Act: Create worktree with --stay flag
        result = runner.invoke(
            cli,
            ["wt", "create", "--from-current-branch", "--stay"],
            obj=test_ctx,
            catch_exceptions=False,
        )

        # Assert: Command succeeded
        if result.exit_code != 0:
            print(f"stderr: {result.stderr}")
            print(f"stdout: {result.stdout}")
        assert result.exit_code == 0

        # Assert: Output contains creation message, no shell integration instructions
        assert "Created worktree at" in result.stderr
        assert "Shell integration not detected" not in result.stderr
        assert "erk init --shell" not in result.stderr
        # Note: "source" IS present for activation instructions,
        # but not "source <(" for shell integration
        assert "source <(" not in result.stderr


def test_create_prints_activation_instructions() -> None:
    """Test that create command prints activation script instructions.

    Part of objective #4954, Phase 5: Activation output for create commands.
    Verifies that erk wt create prints the activation path after worktree creation.
    """
    runner = CliRunner()
    with erk_inmem_env(runner) as env:
        repo_dir = env.erk_root / "repos" / env.cwd.name

        # Set up git state: in root worktree on feature branch
        git_ops = FakeGit(
            worktrees={
                env.cwd: [
                    WorktreeInfo(path=env.cwd, branch="main"),
                ]
            },
            current_branches={env.cwd: "my-feature"},
            default_branches={env.cwd: "main"},
            git_common_dirs={env.cwd: env.git_dir},
        )

        test_ctx = env.build_context(git=git_ops)

        # Act: Create worktree from current branch (without --script flag)
        result = runner.invoke(
            cli,
            ["wt", "create", "--from-current-branch"],
            obj=test_ctx,
            catch_exceptions=False,
        )

        # Assert: Command succeeded
        if result.exit_code != 0:
            print(f"stderr: {result.stderr}")
            print(f"stdout: {result.stdout}")
        assert result.exit_code == 0

        # Assert: Output contains activation instructions
        assert "To activate the worktree environment:" in result.stderr
        assert "source" in result.stderr
        assert ".erk/bin/activate.sh" in result.stderr
        # Should NOT contain implement hint (only shown for up/down navigation)
        assert "erk implement --here" not in result.stderr

        # Assert: Activation script file was created
        expected_worktree_path = repo_dir / "worktrees" / "my-feature"
        activate_script = expected_worktree_path / ".erk" / "bin" / "activate.sh"
        assert activate_script.exists()


def test_create_with_stay_flag_prints_activation_instructions() -> None:
    """Test that create --stay prints activation script instructions.

    Part of objective #4954, Phase 5: Activation output for create commands.
    Verifies that --stay mode still prints activation instructions.
    """
    runner = CliRunner()
    with erk_inmem_env(runner) as env:
        repo_dir = env.erk_root / "repos" / env.cwd.name

        # Set up git state: in root worktree on feature branch
        git_ops = FakeGit(
            worktrees={
                env.cwd: [
                    WorktreeInfo(path=env.cwd, branch="main"),
                ]
            },
            current_branches={env.cwd: "my-feature"},
            default_branches={env.cwd: "main"},
            git_common_dirs={env.cwd: env.git_dir},
        )

        test_ctx = env.build_context(git=git_ops)

        # Act: Create worktree with --stay flag
        result = runner.invoke(
            cli,
            ["wt", "create", "--from-current-branch", "--stay"],
            obj=test_ctx,
            catch_exceptions=False,
        )

        # Assert: Command succeeded
        assert result.exit_code == 0

        # Assert: Output contains activation instructions
        assert "To activate the worktree environment:" in result.stderr
        assert "source" in result.stderr
        assert ".erk/bin/activate.sh" in result.stderr
        # Should NOT contain implement hint (only shown for up/down navigation)
        assert "erk implement --here" not in result.stderr

        # Assert: Activation script file was created
        expected_worktree_path = repo_dir / "worktrees" / "my-feature"
        activate_script = expected_worktree_path / ".erk" / "bin" / "activate.sh"
        assert activate_script.exists()


def test_create_script_mode_does_not_print_activation_instructions() -> None:
    """Test that create --script does NOT print activation instructions.

    Part of objective #4954, Phase 5: Activation output for create commands.
    In script mode, shell integration handles navigation automatically,
    so activation instructions would be redundant.
    """
    runner = CliRunner()
    with erk_inmem_env(runner) as env:
        # Set up git state: in root worktree on feature branch
        git_ops = FakeGit(
            worktrees={
                env.cwd: [
                    WorktreeInfo(path=env.cwd, branch="main"),
                ]
            },
            current_branches={env.cwd: "my-feature"},
            default_branches={env.cwd: "main"},
            git_common_dirs={env.cwd: env.git_dir},
        )

        test_ctx = env.build_context(git=git_ops)

        # Act: Create worktree with --script flag
        result = runner.invoke(
            cli,
            ["wt", "create", "--from-current-branch", "--script"],
            obj=test_ctx,
            catch_exceptions=False,
        )

        # Assert: Command succeeded
        assert result.exit_code == 0

        # Assert: Output does NOT contain activation instructions
        # (shell integration handles navigation)
        assert "To activate the worktree environment:" not in result.stderr
        assert "erk implement --here" not in result.stderr
