"""Regression tests for the TUI gateway's `complete.path` handler.

Reported during the TUI v2 blitz retest:
  - typing `@folder:` (and `@folder` with no colon yet) surfaced files
    alongside directories — the gateway-side completion lives in
    `tui_gateway/server.py` and was never touched by the earlier fix to
    `hermes_cli/commands.py`.
  - typing `@appChrome` required the full `@ui-tui/src/components/app…`
    path to find the file — users expect Cmd-P-style fuzzy basename
    matching across the repo, not a strict directory prefix filter.

Covers:
  - `@folder:` only yields directories
  - `@file:` only yields regular files
  - Bare `@folder` / `@file` (no colon) lists cwd directly
  - Explicit prefix is preserved in the completion text
  - `@<name>` with no slash fuzzy-matches basenames anywhere in the tree
"""

from __future__ import annotations

from pathlib import Path

import pytest

from tui_gateway import server


def _fixture(tmp_path: Path):
    (tmp_path / "readme.md").write_text("x")
    (tmp_path / ".env").write_text("x")
    (tmp_path / "src").mkdir()
    (tmp_path / "docs").mkdir()


def _items(word: str):
    resp = server.handle_request({"id": "1", "method": "complete.path", "params": {"word": word}})

    return [(it["text"], it["display"], it.get("meta", "")) for it in resp["result"]["items"]]


@pytest.fixture(autouse=True)
def _reset_fuzzy_cache(monkeypatch):
    # Each test walks a fresh tmp dir; clear the cached listing so prior
    # roots can't leak through the TTL window.
    server._fuzzy_cache.clear()
    # #70041: _launch_configured_cwd() reads the launch profile's config.yaml
    # via _load_cfg(), which resolves through _hermes_home captured at module
    # import time — before the per-test HERMES_HOME redirect applies. When the
    # developer's real config sets terminal.cwd, _completion_cwd() returns that
    # directory instead of the test's tmp_path (from monkeypatch.chdir). Patch
    # it to None so _completion_cwd falls through to os.getcwd(), which
    # monkeypatch.chdir controls.
    monkeypatch.setattr(server, "_launch_configured_cwd", lambda: None)
    yield
    server._fuzzy_cache.clear()


def test_at_folder_colon_only_dirs(tmp_path, monkeypatch):
    monkeypatch.chdir(tmp_path)
    _fixture(tmp_path)

    texts = [t for t, _, _ in _items("@folder:")]

    assert all(t.startswith("@folder:") for t in texts), texts
    assert any(t == "@folder:src/" for t in texts)
    assert any(t == "@folder:docs/" for t in texts)
    assert not any(t == "@folder:readme.md" for t in texts)
    assert not any(t == "@folder:.env" for t in texts)


def test_at_file_colon_only_files(tmp_path, monkeypatch):
    monkeypatch.chdir(tmp_path)
    _fixture(tmp_path)

    texts = [t for t, _, _ in _items("@file:")]

    assert all(t.startswith("@file:") for t in texts), texts
    assert any(t == "@file:readme.md" for t in texts)
    assert not any(t == "@file:src/" for t in texts)
    assert not any(t == "@file:docs/" for t in texts)


def test_bare_at_still_shows_static_refs(tmp_path, monkeypatch):
    """`@` alone should list the static references so users discover the
    available prefixes.  (Unchanged behaviour; regression guard.)
    """
    monkeypatch.chdir(tmp_path)

    texts = [t for t, _, _ in _items("@")]

    for expected in ("@diff", "@staged", "@file:", "@folder:", "@url:", "@git:"):
        assert expected in texts, f"missing static ref {expected!r} in {texts!r}"


# ── Fuzzy basename matching ──────────────────────────────────────────────
# Users shouldn't have to know the full path — typing `@appChrome` should
# find `ui-tui/src/components/appChrome.tsx`.


def _nested_fixture(tmp_path: Path):
    (tmp_path / "readme.md").write_text("x")
    (tmp_path / ".env").write_text("x")
    (tmp_path / "ui-tui/src/components").mkdir(parents=True)
    (tmp_path / "ui-tui/src/components/appChrome.tsx").write_text("x")
    (tmp_path / "ui-tui/src/components/appLayout.tsx").write_text("x")
    (tmp_path / "ui-tui/src/components/thinking.tsx").write_text("x")
    (tmp_path / "ui-tui/src/hooks").mkdir(parents=True)
    (tmp_path / "ui-tui/src/hooks/useCompletion.ts").write_text("x")
    (tmp_path / "tui_gateway").mkdir()
    (tmp_path / "tui_gateway/server.py").write_text("x")


def test_fuzzy_at_finds_file_without_directory_prefix(tmp_path, monkeypatch):
    """`@appChrome` — with no slash — should surface the nested file."""
    monkeypatch.chdir(tmp_path)
    _nested_fixture(tmp_path)

    entries = _items("@appChrome")
    texts = [t for t, _, _ in entries]

    assert "@file:ui-tui/src/components/appChrome.tsx" in texts, texts

    # Display is the basename, meta is the containing directory, so the
    # picker can show `appChrome.tsx  ui-tui/src/components` on one row.
    row = next(r for r in entries if r[0] == "@file:ui-tui/src/components/appChrome.tsx")
    assert row[1] == "appChrome.tsx"
    assert row[2] == "ui-tui/src/components"


def test_fuzzy_paths_relative_to_cwd_inside_subdir(tmp_path, monkeypatch):
    """When the gateway runs from a subdirectory of a git repo, fuzzy
    completion paths must resolve under that cwd — not under the repo root.

    Without this, `@appChrome` from inside `apps/web/` would suggest
    `@file:apps/web/src/foo.tsx` but the agent (resolving from cwd) would
    look for `apps/web/apps/web/src/foo.tsx` and fail. We translate every
    `git ls-files` result back to a `relpath(root)` and drop anything
    outside `root` so the completion contract stays "paths are cwd-relative".
    """
    import subprocess

    subprocess.run(["git", "init", "-q"], cwd=tmp_path, check=True)
    subprocess.run(["git", "config", "user.email", "test@example.com"], cwd=tmp_path, check=True)
    subprocess.run(["git", "config", "user.name", "test"], cwd=tmp_path, check=True)

    (tmp_path / "apps" / "web" / "src").mkdir(parents=True)
    (tmp_path / "apps" / "web" / "src" / "appChrome.tsx").write_text("x")
    (tmp_path / "apps" / "api" / "src").mkdir(parents=True)
    (tmp_path / "apps" / "api" / "src" / "server.ts").write_text("x")
    (tmp_path / "README.md").write_text("x")

    subprocess.run(["git", "add", "."], cwd=tmp_path, check=True)
    subprocess.run(["git", "commit", "-q", "-m", "init"], cwd=tmp_path, check=True)

    # Run from `apps/web/` — completions should be relative to here, and
    # files outside this subtree (apps/api, README.md at root) shouldn't
    # appear at all.
    monkeypatch.chdir(tmp_path / "apps" / "web")

    texts = [t for t, _, _ in _items("@appChrome")]

    assert "@file:src/appChrome.tsx" in texts, texts
    assert not any("apps/web/" in t for t in texts), texts

    server._fuzzy_cache.clear()
    other_texts = [t for t, _, _ in _items("@server")]

    assert not any("server.ts" in t for t in other_texts), other_texts

    server._fuzzy_cache.clear()
    readme_texts = [t for t, _, _ in _items("@README")]

    assert not any("README.md" in t for t in readme_texts), readme_texts


# ── Fuzzy DIRECTORY matching ─────────────────────────────────────────────
# `@Desktop` used to return nothing: the fuzzy scanner ranks basenames from
# `_list_repo_files`, which lists FILES only, so a directory whose name no
# file inside it happens to match was unreachable without typing a `/`.


def test_fuzzy_finds_top_level_entries_outside_a_git_repo(tmp_path, monkeypatch):
    """Outside a repo the fallback walk can exhaust its file budget on one
    deep subtree before reaching a sibling, hiding top-level folders. The
    root listdir seed guarantees immediate children are always candidates.
    """
    monkeypatch.chdir(tmp_path)
    monkeypatch.setattr(server, "_FUZZY_CACHE_MAX_FILES", 5)

    # A deep subtree that soaks up the entire (patched) file budget...
    deep = tmp_path / "aaa_hog"
    deep.mkdir()
    for i in range(40):
        (deep / f"f{i:03d}.txt").write_text("x")

    # ...and the folder the user actually wants, sorted after it.
    (tmp_path / "Desktop").mkdir()
    (tmp_path / "Desktop" / "note.txt").write_text("x")

    assert "@folder:Desktop/" in [t for t, _, _ in _items("@Desktop")]


# ── Leading slash is a separator, not necessarily an absolute path ───────
# `@/Desktop` used to dead-end: it was read as the absolute `/Desktop`,
# which doesn't exist. People type the slash out of habit — the `@` already
# announced "this is a path" — so it should mean the same as `@Desktop`
# unless a real absolute path is there.


def test_leading_slash_matches_the_bare_form(tmp_path, monkeypatch):
    """`@/foo` and `@foo` return the same thing when `/foo` doesn't exist."""
    monkeypatch.chdir(tmp_path)
    (tmp_path / "Desktop").mkdir()
    (tmp_path / "Desktop" / "note.txt").write_text("x")

    server._fuzzy_cache.clear()
    bare = [t for t, _, _ in _items("@Desktop")]
    server._fuzzy_cache.clear()
    slashed = [t for t, _, _ in _items("@/Desktop")]

    assert "@folder:Desktop/" in bare
    assert slashed == bare


def test_leading_slash_prefers_a_real_absolute_path(tmp_path, monkeypatch):
    """When the absolute reading resolves, it wins — no silent rewrite.

    A cwd-relative `usr/` must not shadow the real `/usr`, or typing an
    absolute path in a repo that happens to mirror those names breaks.
    """
    monkeypatch.chdir(tmp_path)
    # A decoy that would win if the slash were stripped unconditionally.
    (tmp_path / "etc").mkdir()
    (tmp_path / "etc" / "decoy.conf").write_text("x")

    texts = [t for t, _, _ in _items("@/etc/")]

    # `/etc` exists on any POSIX box, so the absolute reading must hold.
    assert not any("decoy.conf" in t for t in texts), texts


def test_completion_ignores_real_terminal_cwd(tmp_path, monkeypatch):
    """#70041: _completion_cwd must not read the developer's real config.yaml
    terminal.cwd when running under hermetic tests.

    The autouse _reset_fuzzy_cache fixture patches _launch_configured_cwd
    to None so _completion_cwd falls through to os.getcwd() (controlled
    by monkeypatch.chdir). This test verifies the fixture holds: with the
    patch in place, a configured terminal.cwd from the launch profile's
    real config can never leak into completion resolution.
    """
    _fixture(tmp_path)
    monkeypatch.chdir(tmp_path)
    monkeypatch.delenv("TERMINAL_CWD", raising=False)

    # _completion_cwd should resolve to tmp_path (via os.getcwd),
    # not to any configured terminal.cwd from the real config.
    resolved = server._completion_cwd({})
    assert resolved == str(tmp_path), (
        f"_completion_cwd resolved to {resolved} instead of {tmp_path} — "
        f"the autouse fixture may not be patching _launch_configured_cwd"
    )


