# tests/test_pack_exclude_dirs.py
from __future__ import annotations

from pathlib import Path

import oceansguard.cli as cli
from tests.helpers import read_text, write_aiguard_yml, write_text as _write


def test_pack_excludes_node_modules_files(tmp_path: Path, ux, patch_subprocess_for_pack):
    """
    誤検知しないテスト：
    - pack出力に「node_modules配下のファイルパス」や「その内容」が入っていないことを検証する
    - ".aiguard.yml の exclude_dirs に node_modules が書かれている" のような設定文字列は許容する
    """
    repo = tmp_path / "repo"
    repo.mkdir()

    # 最小構成
    _write(repo / "backend" / "app" / "main.py", "print('x')\n")
    _write(repo / "frontend" / "package.json", '{"name":"frontend"}\n')

    # 本来 pack に入ってはいけない領域（node_modules）
    nm_file = repo / "frontend" / "node_modules" / "react" / "index.js"
    nm_payload = "/* should be excluded */\n"
    _write(nm_file, nm_payload)

    _write(
        repo / "contracts" / "openapi.json",
        '{"openapi":"3.0.0","info":{"title":"t","version":"1"}}\n',
    )
    _write(repo / "README.md", "# r\n")

    # roots に frontend を入れた状態でも node_modules は除外されるべき
    write_aiguard_yml(repo, include_frontend_in_roots=True, max_files=200, max_kb_each=64)

    cli.cmd_pack(ux, repo, strict=False)

    txt = read_text(repo / "ai_context_pack.md")

    # node_modules配下の「パス」が混入していない（これが本命の検証）
    assert "frontend/node_modules/react/index.js" not in txt

    # node_modules配下の「内容」が混入していない（これが本命の検証）
    assert nm_payload.strip() not in txt


def test_pack_excludes_dist_build_artifacts(tmp_path: Path, ux, patch_subprocess_for_pack):
    """
    追加の典型ケース：
    - dist/build など生成物が roots 配下にあっても混入しない
    """
    repo = tmp_path / "repo"
    repo.mkdir()

    _write(repo / "backend" / "app" / "main.py", "print('x')\n")
    _write(repo / "frontend" / "package.json", '{"name":"frontend"}\n')

    dist_file = repo / "frontend" / "dist" / "bundle.js"
    dist_payload = "/* generated artifact */\n"
    _write(dist_file, dist_payload)

    _write(
        repo / "contracts" / "openapi.json",
        '{"openapi":"3.0.0","info":{"title":"t","version":"1"}}\n',
    )
    _write(repo / "README.md", "# r\n")

    write_aiguard_yml(repo, include_frontend_in_roots=True, max_files=200, max_kb_each=64)

    cli.cmd_pack(ux, repo, strict=False)
    txt = read_text(repo / "ai_context_pack.md")

    assert "frontend/dist/bundle.js" not in txt
    assert dist_payload.strip() not in txt
