#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试add_docs_to_mkdocs.py脚本的基本功能
"""

import os
import yaml

# 测试get_all_md_files函数
def test_get_all_md_files():
    print("测试get_all_md_files函数...")
    md_files = []
    for root, dirs, files in os.walk('docs'):
        for file in files:
            if file.endswith('.md'):
                rel_path = os.path.relpath(os.path.join(root, file), 'docs')
                rel_path = rel_path.replace('\\', '/')
                md_files.append(rel_path)
    
    print(f"找到 {len(md_files)} 个.md文件")
    print("前10个文件:")
    for i, f in enumerate(sorted(md_files)[:10], 1):
        print(f"  {i:2d}. {f}")
    
    return sorted(md_files)

# 测试categorize_file函数
def test_categorize_file():
    print("\n测试categorize_file函数...")
    test_cases = [
        ('技术文档/API文档.md', '技术文档'),
        ('开发指南/快速开始.md', '开发指南'),
        ('学习笔记/Java学习.md', '学习笔记'),
        ('DevOps平台/Gerrit使用指南.md', 'DevOps平台'),
        ('cursor/cursor.md', 'Cursor工具'),
        ('Obsidian/高频命令.md', 'Obsidian笔记'),
        ('其他文件.md', '其他'),
    ]
    
    for file_path, expected in test_cases:
        # 模拟categorize_file函数
        path_parts = file_path.split('/')
        if len(path_parts) == 1:
            category = "其他"
        else:
            category_map = {
                '技术文档': '技术文档',
                '开发指南': '开发指南',
                '学习笔记': '学习笔记',
                'DevOps平台': 'DevOps平台',
                'cursor': 'Cursor工具',
                'Obsidian': 'Obsidian笔记'
            }
            first_dir = path_parts[0]
            category = category_map.get(first_dir, first_dir)
        
        status = "✓" if category == expected else "✗"
        print(f"  {status} {file_path:40} -> {category:15} (期望: {expected})")

# 主测试
def main():
    print("=" * 60)
    print("测试add_docs_to_mkdocs.py脚本功能")
    print("=" * 60)
    
    # 测试1: 获取所有md文件
    all_md_files = test_get_all_md_files()
    
    # 测试2: 分类函数
    test_categorize_file()
    
    # 测试3: 检查当前mkdocs.yml中的文件
    print("\n检查当前mkdocs.yml中的导航文件...")
    try:
        with open('mkdocs.yml', 'r', encoding='utf-8') as f:
            content = f.read()
        
        # 简单统计nav中的文件
        import re
        # 计算nav部分有多少行包含.md
        nav_section = re.search(r'nav:\s*\n(.*?)(?=\n\w+:|$)', content, re.DOTALL)
        if nav_section:
            nav_content = nav_section.group(1)
            md_count = nav_content.count('.md')
            print(f"mkdocs.yml导航中大约有 {md_count} 个.md文件引用")
            
            # 提取所有文件路径
            file_paths = re.findall(r'\s+-\s+(.*?\.md)', nav_content)
            print(f"实际提取到 {len(file_paths)} 个文件路径")
            print("导航中的分类:")
            lines = nav_content.split('\n')
            for line in lines:
                if line.strip() and not line.strip().startswith('-') and ':' in line:
                    print(f"  - {line.strip()}")
    except Exception as e:
        print(f"读取mkdocs.yml时出错: {e}")
    
    print("\n" + "=" * 60)
    print("测试完成")

if __name__ == '__main__':
    main()
