diff --git a/haiku_rag_slim/haiku/rag/app.py b/haiku_rag_slim/haiku/rag/app.py index e9beb7bf..b82e271d 100644 --- a/haiku_rag_slim/haiku/rag/app.py +++ b/haiku_rag_slim/haiku/rag/app.py @@ -855,22 +855,22 @@ class HaikuRAGApp: content = Markdown(content) else: content = Markdown(doc.content) - title_part = ( - f" [repr.attrib_name]title[/repr.attrib_name]: {doc.title}" - if doc.title - else "" - ) - self.console.print( - f"[repr.attrib_name]id[/repr.attrib_name]: {doc.id} " - f"[repr.attrib_name]uri[/repr.attrib_name]: {doc.uri}" - + title_part - + f" [repr.attrib_name]meta[/repr.attrib_name]: {doc.metadata}" - ) + parts = [f"[repr.attrib_name]id[/repr.attrib_name]: {doc.id}"] + if doc.uri: + parts.append(f"[repr.attrib_name]uri[/repr.attrib_name]: {doc.uri}") + if doc.title: + parts.append(f"[repr.attrib_name]title[/repr.attrib_name]: {doc.title}") + if doc.metadata: + parts.append(f"[repr.attrib_name]meta[/repr.attrib_name]: {doc.metadata}") + self.console.print(" ".join(parts)) self.console.print( f"[repr.attrib_name]created at[/repr.attrib_name]: {doc.created_at} [repr.attrib_name]updated at[/repr.attrib_name]: {doc.updated_at}" ) - self.console.print("[repr.attrib_name]content[/repr.attrib_name]:") - self.console.print(content) + # `list` does not load content, which is where the docling blobs live, so + # the header would otherwise announce a field the command declined to fetch. + if doc.content: + self.console.print("[repr.attrib_name]content[/repr.attrib_name]:") + self.console.print(content) self.console.rule() def _rich_print_search_result(self, result: "SearchResult"): diff --git a/tests/test_app.py b/tests/test_app.py index e1371f4c..ae9e5f26 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -59,6 +59,36 @@ def _doc(content: str = "body text", **kwargs) -> Document: return doc +async def test_a_listing_omits_the_fields_it_did_not_fetch(app, client): + """`list` does not load content, and a document need not carry a uri, a + title or metadata. Printing a header for each regardless announced fields + the command declined to fetch or the document never had.""" + bare = Document(content="", uri=None) + bare.id = "doc-bare" + client.list_documents.return_value = [bare] + + await app.list_documents() + + printed = out(app) + assert "doc-bare" in printed + for absent in ("uri:", "title:", "meta:", "content:"): + assert absent not in printed, absent + + +async def test_a_listing_prints_the_fields_it_has(app, client): + client.list_documents.return_value = [ + _doc("body text", title="A title", metadata={"k": "v"}) + ] + + await app.list_documents() + + printed = out(app) + assert "uri: test://doc" in printed + assert "title: A title" in printed + assert "meta:" in printed + assert "content:" in printed + + async def test_list_documents_prints_each_document(app, client): client.list_documents.return_value = [_doc("first"), _doc("second")]