63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
"""Tests for persisting edkb 04_chunks.jsonl into KB version directories."""
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from app.services.knowledge_cleaning import edkb_adapter
|
|
|
|
|
|
def _fake_build_knowledge(file_path: Path, output_dir: Path, config, cache_dir):
|
|
out_sub = output_dir / 'sample'
|
|
out_sub.mkdir(parents=True, exist_ok=True)
|
|
(out_sub / '03_clean.md').write_text(
|
|
'## Sheet: Data\n\n| A | B |\n| --- | --- |\n\n| 1 | 2 |\n',
|
|
encoding='utf-8',
|
|
)
|
|
(out_sub / '04_chunks.jsonl').write_text(
|
|
'{"id":"chunk-0","text":"row one"}\n{"id":"chunk-1","text":"row two"}\n',
|
|
encoding='utf-8',
|
|
)
|
|
chunks_dir = out_sub / 'chunks'
|
|
chunks_dir.mkdir()
|
|
(chunks_dir / 'chunk_0000.md').write_text('row one\n', encoding='utf-8')
|
|
(chunks_dir / 'chunk_0001.md').write_text('row two\n', encoding='utf-8')
|
|
return [{'status': 'success', 'file': str(file_path)}]
|
|
|
|
|
|
def test_build_clean_result_persists_chunks_jsonl(monkeypatch, tmp_path):
|
|
source_path = tmp_path / 'sample.xlsx'
|
|
source_path.write_bytes(b'fake-xlsx-content')
|
|
target_dir = tmp_path / 'output'
|
|
|
|
monkeypatch.setattr('edkb_system.controller.build_knowledge', _fake_build_knowledge)
|
|
|
|
class _FakeConfig:
|
|
@staticmethod
|
|
def load(_path):
|
|
return _FakeConfig()
|
|
|
|
monkeypatch.setattr('edkb_system.config.SystemConfig', _FakeConfig)
|
|
|
|
result = edkb_adapter.build_clean_result(source_path, target_dir)
|
|
|
|
chunks_path = target_dir / '04_chunks.jsonl'
|
|
assert chunks_path.exists()
|
|
assert (target_dir / 'document.md').exists()
|
|
assert (target_dir / 'chunks' / 'chunk_0000.md').exists()
|
|
assert (target_dir / 'chunks' / 'chunk_0001.md').exists()
|
|
assert result['chunks_file_path'] == str(chunks_path)
|
|
assert result['chunks_count'] == 2
|
|
|
|
|
|
def test_persist_debug_chunks_skips_missing_jsonl(tmp_path):
|
|
output_root = tmp_path / 'output'
|
|
output_root.mkdir()
|
|
target_dir = tmp_path / 'target'
|
|
target_dir.mkdir()
|
|
|
|
chunks_file_path, chunks_count = edkb_adapter._persist_debug_chunks(output_root, target_dir)
|
|
|
|
assert chunks_file_path is None
|
|
assert chunks_count is None
|
|
assert not (target_dir / '04_chunks.jsonl').exists()
|