from pathlib import Path import pytest from infospace_bench import ( InfospaceError, add_artifact, create_infospace, load_infospace, ) def test_create_infospace_writes_layout_and_loadable_config(tmp_path: Path) -> None: infospace = create_infospace( tmp_path, "wealth-vsm", name="Wealth of Nations through VSM", topic_domain="Classical Economics", ) root = tmp_path / "infospaces" / "wealth-vsm" assert infospace.root == root assert (root / "infospace.yaml").is_file() assert (root / "artifacts" / "sources").is_dir() assert (root / "artifacts" / "generated").is_dir() assert (root / "output" / "evaluations").is_dir() assert (root / "output" / "metrics").is_dir() assert (root / "reports").is_dir() assert (root / "exports").is_dir() loaded = load_infospace(root) assert loaded.config.slug == "wealth-vsm" assert loaded.config.name == "Wealth of Nations through VSM" assert loaded.config.topic.domain == "Classical Economics" assert loaded.config.topic.sources == "artifacts/sources" assert loaded.artifacts == [] def test_create_infospace_rejects_unsafe_slug_with_structured_error(tmp_path: Path) -> None: with pytest.raises(InfospaceError) as raised: create_infospace(tmp_path, "../outside", name="Nope") assert raised.value.code == "invalid_slug" assert raised.value.detail["slug"] == "../outside" def test_load_infospace_reports_missing_config(tmp_path: Path) -> None: root = tmp_path / "infospaces" / "empty" root.mkdir(parents=True) with pytest.raises(InfospaceError) as raised: load_infospace(root) assert raised.value.code == "missing_config" assert "infospace.yaml" in raised.value.message def test_add_artifact_copies_file_and_updates_manifest(tmp_path: Path) -> None: create_infospace(tmp_path, "pilot", name="Pilot Infospace") source = tmp_path / "chapter.md" source.write_text("# Chapter\n\nSource text.\n", encoding="utf-8") artifact = add_artifact( tmp_path / "infospaces" / "pilot", source, kind="source", title="Chapter 1", ) assert artifact.id == "source/chapter.md" assert artifact.path == "artifacts/sources/chapter.md" assert (tmp_path / "infospaces" / "pilot" / artifact.path).read_text( encoding="utf-8" ).startswith("# Chapter") loaded = load_infospace(tmp_path / "infospaces" / "pilot") assert [item.id for item in loaded.artifacts] == ["source/chapter.md"] assert loaded.artifacts[0].title == "Chapter 1" def test_add_artifact_rejects_duplicate_manifest_entry(tmp_path: Path) -> None: create_infospace(tmp_path, "pilot", name="Pilot Infospace") source = tmp_path / "chapter.md" source.write_text("# Chapter\n", encoding="utf-8") add_artifact(tmp_path / "infospaces" / "pilot", source, kind="source") with pytest.raises(InfospaceError) as raised: add_artifact(tmp_path / "infospaces" / "pilot", source, kind="source") assert raised.value.code == "duplicate_artifact"