Files
strukto-ai--mirage/python/tests/core/dify/test_tree.py
T
wehub-resource-sync bcbd1bdb22
Integ / changes (push) Has been skipped
Pre-commit / pre-commit (push) Failing after 1s
CLI exit codes / changes (push) Has been skipped
Test (Install) / changes (push) Has been skipped
Test (Python) / changes (push) Has been skipped
Test (TypeScript) / changes (push) Has been skipped
CLI exit codes / cli-gate (push) Has been cancelled
Test (Install) / test-install-gate (push) Has been cancelled
Integ / integ-gate (push) Has been cancelled
Test (Python) / test-python-gate (push) Has been cancelled
Test (TypeScript) / test-typescript-gate (push) Has been cancelled
Test (Install) / python-minimal (3.12) (push) Has been cancelled
Test (Install) / python-minimal (3.11) (push) Has been cancelled
Test (Install) / python-extra (agno, mirage.agents.agno) (push) Has been cancelled
Test (Install) / python-extra (chroma, mirage.resource.chroma) (push) Has been cancelled
Test (Install) / python-extra (pdf, mirage.core.filetype.pdf) (push) Has been cancelled
Integ / integ (push) Has been cancelled
Integ / integ-database (push) Has been cancelled
Integ / integ-database-ts (push) Has been cancelled
Integ / integ-data (push) Has been cancelled
Integ / integ-ssh (push) Has been cancelled
Integ / integ-ssh-ts (push) Has been cancelled
Test (Python) / audit (push) Has been cancelled
Test (TypeScript) / test (push) Has been cancelled
Test (TypeScript) / python-fs-shim (push) Has been cancelled
CLI exit codes / Python CLI (push) Has been cancelled
CLI exit codes / TypeScript CLI (push) Has been cancelled
CLI exit codes / Cross-language snapshot interop (push) Has been cancelled
Test (Python) / test (push) Has been cancelled
Test (Python) / import-isolation (deepagents, openai, mirage.agents.openai_agents) (push) Has been cancelled
Test (Python) / import-isolation (deepagents, pydantic-ai, mirage.agents.pydantic_ai) (push) Has been cancelled
Integ / integ-ts (push) Has been cancelled
Integ / integ-fuse (push) Has been cancelled
Test (Install) / python-extra (databricks, mirage.resource.databricks_volume) (push) Has been cancelled
Test (Install) / python-extra (deepagents, mirage.agents.langchain) (push) Has been cancelled
Test (Install) / python-extra (email, mirage.resource.email) (push) Has been cancelled
Test (Install) / python-extra (fuse, mirage.fuse.mount) (push) Has been cancelled
Test (Install) / python-extra (hdf5, mirage.core.filetype.hdf5) (push) Has been cancelled
Test (Install) / python-extra (hf, mirage.resource.hf_buckets) (push) Has been cancelled
Test (Install) / python-extra (lancedb, mirage.resource.lancedb) (push) Has been cancelled
Test (Install) / python-extra (langfuse, mirage.resource.langfuse) (push) Has been cancelled
Test (Install) / python-extra (mongodb, mirage.resource.mongodb) (push) Has been cancelled
Test (Install) / python-extra (nextcloud, mirage.resource.nextcloud) (push) Has been cancelled
Test (Install) / python-extra (openai, mirage.agents.openai_agents) (push) Has been cancelled
Test (Install) / python-extra (openhands, mirage.agents.openhands, 3.12) (push) Has been cancelled
Test (Install) / python-extra (parquet, mirage.core.filetype.parquet) (push) Has been cancelled
Test (Install) / python-extra (postgres, mirage.resource.postgres) (push) Has been cancelled
Test (Install) / python-extra (pydantic-ai, mirage.agents.pydantic_ai) (push) Has been cancelled
Test (Install) / python-extra (qdrant, mirage.resource.qdrant) (push) Has been cancelled
Test (Install) / python-extra (redis, mirage.resource.redis) (push) Has been cancelled
Test (Install) / python-extra (s3, mirage.resource.s3) (push) Has been cancelled
Test (Install) / python-extra (ssh, mirage.resource.ssh) (push) Has been cancelled
Test (Install) / ts-minimal (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:30:44 +08:00

126 lines
4.3 KiB
Python

import pytest
from mirage.core.dify import tree
from mirage.core.dify._client import is_visible_document
from .conftest import document
tree_calls = {"documents": 0}
async def list_documents_with_hidden_and_fallbacks(config):
# Mirrors the list_all_documents contract: hidden documents (archived,
# disabled, still indexing) never leave the client.
documents = [
document("doc-1", "Quickstart", slug="guides/quickstart", size=333),
document("doc-2", "API", slug="api", size=444, archived=True),
document("doc-3", "Draft", slug="draft", indexing_status="indexing"),
document("doc-4", "Disabled", slug="disabled", enabled=False),
document("doc-5", "Archived", slug="archived", archived=True),
document("doc-6", "README.md", size=None),
]
return [doc for doc in documents if is_visible_document(doc)]
async def counted_documents(config):
tree_calls["documents"] += 1
return await list_documents_with_hidden_and_fallbacks(config)
async def duplicate_documents(config):
return [
document("doc-1", "one", slug="same"),
document("doc-2", "two", slug="same"),
]
async def collision_documents(config):
return [
document("doc-1", "foo", slug="foo"),
document("doc-2", "bar", slug="foo/bar"),
]
@pytest.mark.asyncio
async def test_ensure_tree_builds_prefixed_entries_and_uses_api_size(
monkeypatch, dify_accessor, dify_index):
tree_calls["documents"] = 0
monkeypatch.setattr(tree, "list_all_documents", counted_documents)
await tree.ensure_tree(dify_accessor, dify_index, "/knowledge/")
root = await dify_index.list_dir("/knowledge")
guides = await dify_index.list_dir("/knowledge/guides")
quickstart = await dify_index.get("/knowledge/guides/quickstart")
readme = await dify_index.get("/knowledge/README.md")
assert root.entries == ["/knowledge/README.md", "/knowledge/guides"]
assert guides.entries == ["/knowledge/guides/quickstart"]
assert quickstart.entry.id == "doc-1"
assert quickstart.entry.size == 333
assert quickstart.entry.extra["slug"] == "guides/quickstart"
assert quickstart.entry.extra["raw_slug"] == "guides/quickstart"
assert quickstart.entry.extra["has_slug"] is True
assert readme.entry.id == "doc-6"
assert readme.entry.size is None
assert readme.entry.extra["raw_slug"] == "README.md"
assert readme.entry.extra["has_slug"] is False
await tree.ensure_tree(dify_accessor, dify_index, "/knowledge/")
assert tree_calls["documents"] == 1
@pytest.mark.asyncio
async def test_ensure_tree_rejects_duplicate_and_path_collision(
monkeypatch, dify_accessor, dify_index):
monkeypatch.setattr(tree, "list_all_documents", duplicate_documents)
with pytest.raises(ValueError, match="Duplicate slug 'same'"):
await tree.ensure_tree(dify_accessor, dify_index, "")
await dify_index.clear()
monkeypatch.setattr(tree, "list_all_documents", collision_documents)
with pytest.raises(ValueError, match="Path collision"):
await tree.ensure_tree(dify_accessor, dify_index, "")
def test_tree_slug_and_timestamp_helpers():
assert tree.extract_slug({
"doc_metadata": {
"slug": "a/b"
},
"name": "fallback"
}) == ("a/b", True)
assert tree.extract_slug(
{
"doc_metadata": [{
"name": "path",
"value": "docs/start"
}],
"name": "fallback"
}, "path") == ("docs/start", True)
assert tree.extract_slug(
{
"doc_metadata": {
"path": "docs/map"
},
"name": "fallback"
}, "path") == ("docs/map", True)
assert tree.normalize_slug("/a//b/") == "/a/b"
assert tree.extract_document_size(
{"data_source_info": {
"upload_file": {
"size": 7
}
}}) == 7
assert tree.timestamp_to_iso(None) == ""
assert tree.virtual_path("/a", "/knowledge/") == "/knowledge/a"
assert tree.parent("/a/b") == "/a"
assert tree.gnu_basename("/a/b") == "b"
def test_normalize_slug_rejects_invalid_segments():
with pytest.raises(ValueError, match="Invalid empty"):
tree.normalize_slug("/")
with pytest.raises(ValueError, match="Invalid Dify document slug segment"):
tree.normalize_slug("../x")