|
2 | 2 |
|
3 | 3 | from pathlib import Path |
4 | 4 |
|
5 | | -from dory_core.semantic_write import SubjectResolver, build_semantic_write_plan |
| 5 | +from dory_core.semantic_write import SubjectMatch, SubjectResolver, build_semantic_write_plan |
6 | 6 | from dory_core.types import MemoryWriteReq, MemoryWriteResp |
7 | 7 |
|
8 | 8 |
|
@@ -124,3 +124,97 @@ def test_build_semantic_write_plan_routes_to_canonical_targets(tmp_path: Path) - |
124 | 124 | assert core_plan.subject_ref == "core:user" |
125 | 125 | assert core_plan.target_path == "core/user.md" |
126 | 126 | assert core_plan.resolved_mode == "replace" |
| 127 | + |
| 128 | + |
| 129 | +def test_build_semantic_write_plan_creates_new_project_from_explicit_scope(tmp_path: Path) -> None: |
| 130 | + plan = build_semantic_write_plan( |
| 131 | + tmp_path, |
| 132 | + MemoryWriteReq( |
| 133 | + action="write", |
| 134 | + kind="state", |
| 135 | + subject="Open Privacy Filter", |
| 136 | + content="Open Privacy Filter is active.", |
| 137 | + scope="project", |
| 138 | + ), |
| 139 | + ) |
| 140 | + |
| 141 | + assert plan.subject_ref == "project:open-privacy-filter" |
| 142 | + assert plan.target_subject_ref == "project:open-privacy-filter" |
| 143 | + assert plan.target_path == "projects/open-privacy-filter/state.md" |
| 144 | + assert plan.matched_by == "explicit_scope" |
| 145 | + assert plan.target_exists is False |
| 146 | + |
| 147 | + |
| 148 | +def test_build_semantic_write_plan_does_not_create_people_from_explicit_scope(tmp_path: Path) -> None: |
| 149 | + try: |
| 150 | + build_semantic_write_plan( |
| 151 | + tmp_path, |
| 152 | + MemoryWriteReq( |
| 153 | + action="write", |
| 154 | + kind="preference", |
| 155 | + subject="active", |
| 156 | + content="Bad scoped proposal should not create a person.", |
| 157 | + scope="person", |
| 158 | + ), |
| 159 | + ) |
| 160 | + except ValueError as err: |
| 161 | + assert "could not resolve semantic subject: active" in str(err) |
| 162 | + else: |
| 163 | + raise AssertionError("expected unresolved person subject to be rejected") |
| 164 | + |
| 165 | + |
| 166 | +def test_build_semantic_write_plan_creates_new_dream_project_over_alias_match(tmp_path: Path) -> None: |
| 167 | + class _AliasResolver: |
| 168 | + def resolve(self, subject: str, *, scope: str | None = None) -> SubjectMatch | None: |
| 169 | + return SubjectMatch( |
| 170 | + subject_ref="project:privacy-filter-lab", |
| 171 | + family="project", |
| 172 | + title="Privacy Filter Lab", |
| 173 | + target_path="projects/privacy-filter-lab/state.md", |
| 174 | + matched_by="alias", |
| 175 | + confidence="high", |
| 176 | + ) |
| 177 | + |
| 178 | + plan = build_semantic_write_plan( |
| 179 | + tmp_path, |
| 180 | + MemoryWriteReq( |
| 181 | + action="write", |
| 182 | + kind="state", |
| 183 | + subject="open-privacy-filter", |
| 184 | + content="Open Privacy Filter is active.", |
| 185 | + scope="project", |
| 186 | + source="/var/lib/dory/digests/daily/2026-04-23.md", |
| 187 | + ), |
| 188 | + resolver=_AliasResolver(), |
| 189 | + ) |
| 190 | + |
| 191 | + assert plan.subject_ref == "project:open-privacy-filter" |
| 192 | + assert plan.target_path == "projects/open-privacy-filter/state.md" |
| 193 | + |
| 194 | + |
| 195 | +def test_build_semantic_write_plan_keeps_alias_match_for_non_dream_write(tmp_path: Path) -> None: |
| 196 | + class _AliasResolver: |
| 197 | + def resolve(self, subject: str, *, scope: str | None = None) -> SubjectMatch | None: |
| 198 | + return SubjectMatch( |
| 199 | + subject_ref="project:privacy-filter-lab", |
| 200 | + family="project", |
| 201 | + title="Privacy Filter Lab", |
| 202 | + target_path="projects/privacy-filter-lab/state.md", |
| 203 | + matched_by="alias", |
| 204 | + confidence="high", |
| 205 | + ) |
| 206 | + |
| 207 | + plan = build_semantic_write_plan( |
| 208 | + tmp_path, |
| 209 | + MemoryWriteReq( |
| 210 | + action="write", |
| 211 | + kind="state", |
| 212 | + subject="open-privacy-filter", |
| 213 | + content="Open Privacy Filter is active.", |
| 214 | + scope="project", |
| 215 | + ), |
| 216 | + resolver=_AliasResolver(), |
| 217 | + ) |
| 218 | + |
| 219 | + assert plan.subject_ref == "project:privacy-filter-lab" |
| 220 | + assert plan.target_path == "projects/privacy-filter-lab/state.md" |
0 commit comments