@@ -2017,3 +2017,151 @@ async def test_create_or_update_entity_fuzzy_search_bug(
20172017 assert "Original content for Node A" not in content_c , (
20182018 "Node C.md should not contain Node A content"
20192019 )
2020+
2021+
2022+ # --- User Tracking (created_by / last_updated_by) ---
2023+
2024+
2025+ @pytest .mark .asyncio
2026+ async def test_created_by_null_by_default (entity_service : EntityService ):
2027+ """created_by and last_updated_by are NULL when get_user_id returns None (local/CLI usage)."""
2028+ schema = EntitySchema (
2029+ title = "Local Entity" ,
2030+ directory = "test" ,
2031+ entity_type = "note" ,
2032+ )
2033+ entity = await entity_service .create_entity (schema )
2034+ assert entity .created_by is None
2035+ assert entity .last_updated_by is None
2036+
2037+
2038+ @pytest .mark .asyncio
2039+ async def test_created_by_set_when_get_user_id_returns_value (entity_service : EntityService ):
2040+ """created_by and last_updated_by are set when get_user_id returns a user ID."""
2041+ user_id = str (uuid .uuid4 ())
2042+ entity_service .get_user_id = lambda : user_id
2043+
2044+ schema = EntitySchema (
2045+ title = "Cloud Entity" ,
2046+ directory = "test" ,
2047+ entity_type = "note" ,
2048+ )
2049+ entity = await entity_service .create_entity (schema )
2050+ assert entity .created_by == user_id
2051+ assert entity .last_updated_by == user_id
2052+
2053+
2054+ @pytest .mark .asyncio
2055+ async def test_update_preserves_created_by (entity_service : EntityService ):
2056+ """Updating an entity preserves created_by and updates last_updated_by."""
2057+ creator_id = str (uuid .uuid4 ())
2058+ editor_id = str (uuid .uuid4 ())
2059+
2060+ # Create as creator
2061+ entity_service .get_user_id = lambda : creator_id
2062+ schema = EntitySchema (
2063+ title = "Owned Entity" ,
2064+ directory = "test" ,
2065+ entity_type = "note" ,
2066+ content = "Original content" ,
2067+ )
2068+ entity = await entity_service .create_entity (schema )
2069+ assert entity .created_by == creator_id
2070+
2071+ # Update as editor
2072+ entity_service .get_user_id = lambda : editor_id
2073+ update_schema = EntitySchema (
2074+ title = "Owned Entity" ,
2075+ directory = "test" ,
2076+ entity_type = "note" ,
2077+ content = "Updated content" ,
2078+ )
2079+ updated = await entity_service .update_entity (entity , update_schema )
2080+ assert updated .created_by == creator_id # preserved
2081+ assert updated .last_updated_by == editor_id # updated
2082+
2083+
2084+ @pytest .mark .asyncio
2085+ async def test_fast_write_entity_sets_user_tracking (entity_service : EntityService ):
2086+ """fast_write_entity sets created_by and last_updated_by on create."""
2087+ user_id = str (uuid .uuid4 ())
2088+ entity_service .get_user_id = lambda : user_id
2089+
2090+ schema = EntitySchema (
2091+ title = "Fast Write Tracked" ,
2092+ directory = "test" ,
2093+ entity_type = "note" ,
2094+ )
2095+ entity = await entity_service .fast_write_entity (schema , external_id = str (uuid .uuid4 ()))
2096+ assert entity .created_by == user_id
2097+ assert entity .last_updated_by == user_id
2098+
2099+
2100+ @pytest .mark .asyncio
2101+ async def test_fast_write_entity_update_preserves_created_by (entity_service : EntityService ):
2102+ """fast_write_entity update path preserves created_by, sets last_updated_by."""
2103+ creator_id = str (uuid .uuid4 ())
2104+ editor_id = str (uuid .uuid4 ())
2105+ external_id = str (uuid .uuid4 ())
2106+
2107+ # Create
2108+ entity_service .get_user_id = lambda : creator_id
2109+ schema = EntitySchema (
2110+ title = "Fast Write Update" ,
2111+ directory = "test" ,
2112+ entity_type = "note" ,
2113+ )
2114+ entity = await entity_service .fast_write_entity (schema , external_id = external_id )
2115+ assert entity .created_by == creator_id
2116+
2117+ # Update (same external_id triggers update path)
2118+ entity_service .get_user_id = lambda : editor_id
2119+ update_schema = EntitySchema (
2120+ title = "Fast Write Update" ,
2121+ directory = "test" ,
2122+ entity_type = "note" ,
2123+ content = "Updated" ,
2124+ )
2125+ updated = await entity_service .fast_write_entity (update_schema , external_id = external_id )
2126+ assert updated .created_by == creator_id # preserved
2127+ assert updated .last_updated_by == editor_id # updated
2128+
2129+
2130+ @pytest .mark .asyncio
2131+ async def test_fast_edit_entity_sets_last_updated_by (entity_service : EntityService ):
2132+ """fast_edit_entity sets last_updated_by on edit."""
2133+ creator_id = str (uuid .uuid4 ())
2134+ editor_id = str (uuid .uuid4 ())
2135+
2136+ # Create entity first
2137+ entity_service .get_user_id = lambda : creator_id
2138+ schema = EntitySchema (
2139+ title = "Fast Edit Tracked" ,
2140+ directory = "test" ,
2141+ entity_type = "note" ,
2142+ content = "Original content" ,
2143+ )
2144+ entity = await entity_service .fast_write_entity (schema , external_id = str (uuid .uuid4 ()))
2145+
2146+ # Edit as different user
2147+ entity_service .get_user_id = lambda : editor_id
2148+ edited = await entity_service .fast_edit_entity (
2149+ entity = entity ,
2150+ operation = "append" ,
2151+ content = "\n Appended content" ,
2152+ )
2153+ assert edited .created_by == creator_id # preserved
2154+ assert edited .last_updated_by == editor_id # updated
2155+
2156+
2157+ @pytest .mark .asyncio
2158+ async def test_fast_write_entity_null_user_id (entity_service : EntityService ):
2159+ """fast_write_entity with default get_user_id (None) leaves tracking fields null."""
2160+ schema = EntitySchema (
2161+ title = "No User Tracking" ,
2162+ directory = "test" ,
2163+ entity_type = "note" ,
2164+ )
2165+ entity = await entity_service .fast_write_entity (schema , external_id = str (uuid .uuid4 ()))
2166+ assert entity .created_by is None
2167+ assert entity .last_updated_by is None
0 commit comments