2023-08-31 21:55:17 +02:00
|
|
|
import os
|
2023-08-10 00:44:40 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
2023-11-21 21:30:43 +01:00
|
|
|
from khoj.database.models import KhojUser, LocalPlaintextConfig
|
2023-11-22 07:11:32 +01:00
|
|
|
from khoj.processor.content.plaintext.plaintext_to_entries import PlaintextToEntries
|
2023-08-31 21:55:17 +02:00
|
|
|
from khoj.utils.fs_syncer import get_plaintext_files
|
|
|
|
from khoj.utils.rawconfig import TextContentConfig
|
2023-08-10 00:44:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_plaintext_file(tmp_path):
|
|
|
|
"Convert files with no heading to jsonl."
|
|
|
|
# Arrange
|
2024-02-09 11:34:41 +01:00
|
|
|
raw_entry = f"""
|
2023-08-10 00:44:40 +02:00
|
|
|
Hi, I am a plaintext file and I have some plaintext words.
|
|
|
|
"""
|
2024-02-09 11:34:41 +01:00
|
|
|
plaintextfile = create_file(tmp_path, raw_entry)
|
2023-08-10 00:44:40 +02:00
|
|
|
|
|
|
|
filename = plaintextfile.stem
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# Extract Entries from specified plaintext files
|
|
|
|
|
2023-08-31 21:55:17 +02:00
|
|
|
data = {
|
2024-02-09 11:34:41 +01:00
|
|
|
f"{plaintextfile}": raw_entry,
|
2023-08-31 21:55:17 +02:00
|
|
|
}
|
|
|
|
|
2024-02-09 11:34:41 +01:00
|
|
|
entries = PlaintextToEntries.extract_plaintext_entries(entry_to_file_map=data)
|
2023-08-10 00:44:40 +02:00
|
|
|
|
|
|
|
# Convert each entry.file to absolute path to make them JSON serializable
|
2024-02-09 11:34:41 +01:00
|
|
|
for entry in entries:
|
|
|
|
entry.file = str(Path(entry.file).absolute())
|
2023-08-10 00:44:40 +02:00
|
|
|
|
|
|
|
# Assert
|
2024-02-09 12:33:36 +01:00
|
|
|
assert len(entries) == 1
|
2023-08-10 00:44:40 +02:00
|
|
|
# Ensure raw entry with no headings do not get heading prefix prepended
|
2024-02-09 12:33:36 +01:00
|
|
|
assert not entries[0].raw.startswith("#")
|
2023-08-10 00:44:40 +02:00
|
|
|
# Ensure compiled entry has filename prepended as top level heading
|
2024-02-09 12:33:36 +01:00
|
|
|
assert entries[0].compiled == f"{filename}\n{raw_entry}"
|
2023-08-10 00:44:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_plaintext_files(tmp_path):
|
|
|
|
"Ensure Plaintext files specified via input-filter, input-files extracted"
|
|
|
|
# Arrange
|
|
|
|
# Include via input-filter globs
|
|
|
|
group1_file1 = create_file(tmp_path, filename="group1-file1.md")
|
|
|
|
group1_file2 = create_file(tmp_path, filename="group1-file2.md")
|
|
|
|
|
|
|
|
group2_file1 = create_file(tmp_path, filename="group2-file1.markdown")
|
|
|
|
group2_file2 = create_file(tmp_path, filename="group2-file2.markdown")
|
|
|
|
group2_file3 = create_file(tmp_path, filename="group2-file3.mbox")
|
|
|
|
group2_file4 = create_file(tmp_path, filename="group2-file4.html")
|
|
|
|
# Include via input-file field
|
|
|
|
file1 = create_file(tmp_path, filename="notes.txt")
|
|
|
|
# Include unsupported file types
|
|
|
|
create_file(tmp_path, filename="group2-unincluded.py")
|
|
|
|
create_file(tmp_path, filename="group2-unincluded.csv")
|
|
|
|
create_file(tmp_path, filename="group2-unincluded.csv")
|
|
|
|
# Not included by any filter
|
|
|
|
create_file(tmp_path, filename="not-included-markdown.md")
|
|
|
|
create_file(tmp_path, filename="not-included-text.txt")
|
|
|
|
|
2023-08-31 21:55:17 +02:00
|
|
|
expected_files = set(
|
|
|
|
[
|
|
|
|
os.path.join(tmp_path, file.name)
|
|
|
|
for file in [group1_file1, group1_file2, group2_file1, group2_file2, group2_file3, group2_file4, file1]
|
|
|
|
]
|
2023-08-10 00:44:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Setup input-files, input-filters
|
|
|
|
input_files = [tmp_path / "notes.txt"]
|
|
|
|
input_filter = [tmp_path / "group1*.md", tmp_path / "group2*.*"]
|
|
|
|
|
2023-08-31 21:55:17 +02:00
|
|
|
plaintext_config = TextContentConfig(
|
|
|
|
input_files=input_files,
|
|
|
|
input_filter=[str(filter) for filter in input_filter],
|
|
|
|
compressed_jsonl=tmp_path / "test.jsonl",
|
|
|
|
embeddings_file=tmp_path / "test_embeddings.jsonl",
|
|
|
|
)
|
|
|
|
|
2023-08-10 00:44:40 +02:00
|
|
|
# Act
|
2023-08-31 21:55:17 +02:00
|
|
|
extracted_plaintext_files = get_plaintext_files(plaintext_config)
|
2023-08-10 00:44:40 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(extracted_plaintext_files) == 7
|
2023-08-31 21:55:17 +02:00
|
|
|
assert set(extracted_plaintext_files.keys()) == set(expected_files)
|
2023-08-10 00:44:40 +02:00
|
|
|
|
|
|
|
|
2023-10-26 18:42:29 +02:00
|
|
|
def test_parse_html_plaintext_file(content_config, default_user: KhojUser):
|
2023-08-27 20:24:30 +02:00
|
|
|
"Ensure HTML files are parsed correctly"
|
|
|
|
# Arrange
|
|
|
|
# Setup input-files, input-filters
|
2023-10-26 18:42:29 +02:00
|
|
|
config = LocalPlaintextConfig.objects.filter(user=default_user).first()
|
|
|
|
extracted_plaintext_files = get_plaintext_files(config=config)
|
2023-08-27 20:24:30 +02:00
|
|
|
|
|
|
|
# Act
|
2024-02-09 11:34:41 +01:00
|
|
|
entries = PlaintextToEntries.extract_plaintext_entries(extracted_plaintext_files)
|
2023-08-27 20:24:30 +02:00
|
|
|
|
|
|
|
# Assert
|
2024-02-09 11:34:41 +01:00
|
|
|
assert len(entries) == 1
|
|
|
|
assert "<div>" not in entries[0].raw
|
2023-08-27 20:24:30 +02:00
|
|
|
|
|
|
|
|
2023-08-10 00:44:40 +02:00
|
|
|
# Helper Functions
|
|
|
|
def create_file(tmp_path: Path, entry=None, filename="test.md"):
|
|
|
|
file_ = tmp_path / filename
|
|
|
|
file_.touch()
|
|
|
|
if entry:
|
|
|
|
file_.write_text(entry)
|
|
|
|
return file_
|