import pytest
from src.core import WordCounter
from src.file_io import save_results
import csv

@pytest.fixture
def sample_data(tmp_path):
    data_dir = tmp_path / "data"
    data_dir.mkdir()
    (data_dir / "test1.txt").write_text("apple banana apple")
    (data_dir / "test2.txt").write_text("banana cherry")
    return data_dir

def test_full_pipeline(sample_data, tmp_path):
    config = {
        "data_dir": str(sample_data),
        "stop_words_file": str(tmp_path / "stopwords.txt"),
        "output_file": str(tmp_path / "results.csv"),
        "top_n": 2,
        "tokenizer": "simple"
    }
    
    # 生成停用词文件
    (tmp_path / "stopwords.txt").write_text("cherry")
    
    # 执行完整流程
    counter = WordCounter()
    counter.config = config
    results = counter.process_files()
    save_results(results, config['output_file'])
    
    # 验证输出
    with open(config['output_file']) as f:
        reader = csv.reader(f)
        next(reader)  # Skip header
        assert list(reader) == [["apple", "2"], ["banana", "2"]]