fix(contacts): surface ORG/TITLE/NOTE/URL/CATEGORIES/PHOTO on read (refs #716)

PR #719 fixed the contact-create path so all documented fields persist to the
vCard, but the read path (list/search via MCP) still returned ``organization:
null`` / ``note: null`` / ``title: null`` because pythonvCard4 has no typed
parser for ORG/TITLE — they land in ``Contact.custom`` — and the server-side
mapper never read ``note``/``urls``/``categories``/``photo`` even when present.

Reads now surface what the write side persisted:

- ``client/contacts.py``: new ``_first_custom`` helper pulls raw values from
  ``Contact.custom`` for ORG/TITLE/unencoded PHOTO. ``list_contacts``
  extends its per-contact dict with org/title/note/url/categories/photo.
- ``server/contacts.py``: ``_raw_contact_to_model`` maps the new keys onto
  ``Contact.organization`` / ``.title`` / ``.note`` / ``.urls`` / ``.categories``
  / ``.photo``. URL accepts both list and plain-string shapes; categories
  accepts comma-separated strings for forward-compat.

Coverage:

- Unit: ``TestFirstCustom`` (five cases incl. bare-string library shape) and
  three new ``_raw_contact_to_model`` cases covering the full field set,
  plain-string URL, and comma-string categories.
- Integration: ``test_mcp_contacts_workflow`` now decodes the
  ``nc_contacts_search_contacts`` response and asserts
  ``organization`` / ``note`` round-trip — direct regression coverage for
  elvisdragonmao's report on issue #716.

Verified end-to-end against the local single-user docker stack: creating a
contact with ``{organization, title, note, url, categories}`` and reading it
back via ``nc_contacts_search_contacts`` returns every field populated.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-05-21 10:07:47 +02:00
co-authored by Claude Opus 4.7
parent 04a6294cc5
commit 688a03f00a
5 changed files with 179 additions and 3 deletions
+67
View File
@@ -388,6 +388,73 @@ def test_contact_mapping_missing_optional_fields():
assert contact.custom_fields == {}
@pytest.mark.unit
def test_contact_mapping_surfaces_org_title_note_url_categories_photo():
"""Issue #716 follow-up: ORG / TITLE / NOTE / URL / CATEGORIES / PHOTO must
round-trip from the raw client dict onto the response model.
Before this fix, the server-side mapper ignored these keys even when the
client supplied them, so MCP responses returned ``organization: null`` /
``note: null`` for contacts that did have the fields set in their vCard.
"""
raw_contact = {
"vcard_id": "full-1",
"getetag": '"etag"',
"contact": {
"fullname": "Alice",
"org": "Acme Corp",
"title": "Engineer",
"note": "Met at conference",
"url": ["https://acme.example.com"],
"categories": ["vip", "customer"],
"photo": "https://photos.example.com/alice.jpg",
},
}
contact = _map_contact(raw_contact)
assert contact.organization == "Acme Corp"
assert contact.title == "Engineer"
assert contact.note == "Met at conference"
assert len(contact.urls) == 1
assert contact.urls[0].value == "https://acme.example.com"
assert contact.urls[0].type == "url"
assert contact.categories == ["vip", "customer"]
assert contact.photo == "https://photos.example.com/alice.jpg"
@pytest.mark.unit
def test_contact_mapping_accepts_url_as_plain_string():
"""The client dict normally carries ``url`` as a list, but accept a plain
string for forward-compat with library changes that might collapse single
entries.
"""
raw_contact = {
"vcard_id": "url-str-1",
"contact": {"fullname": "Bob", "url": "https://bob.example.com"},
}
contact = _map_contact(raw_contact)
assert len(contact.urls) == 1
assert contact.urls[0].value == "https://bob.example.com"
@pytest.mark.unit
def test_contact_mapping_categories_accepts_comma_string():
"""A comma-separated string is split into discrete categories on the read
side, matching how the write side parses ``categories``.
"""
raw_contact = {
"vcard_id": "cat-1",
"contact": {"fullname": "Carol", "categories": "vip, customer, archived"},
}
contact = _map_contact(raw_contact)
assert contact.categories == ["vip", "customer", "archived"]
@pytest.mark.unit
def test_list_contacts_response_wraps_contacts():
"""Test ListContactsResponse wraps contacts correctly for MCP output."""