115 lines
4.6 KiB
Python
115 lines
4.6 KiB
Python
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import json
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def load_script(name: str):
|
|
spec = importlib.util.spec_from_file_location(name, ROOT / "skills-scripts" / f"{name}.py")
|
|
module = importlib.util.module_from_spec(spec)
|
|
sys.modules[name] = module
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
discovery = load_script("build_discovery_files")
|
|
publisher = load_script("publish_hugo_output")
|
|
|
|
|
|
class PoppyDiscoveryTests(unittest.TestCase):
|
|
def test_ordinary_web_manifest(self):
|
|
manifest = json.loads(discovery.build_poppy_json())
|
|
self.assertEqual(manifest, {
|
|
"protocol_version": "0.1",
|
|
"organization": {"name": "iNDX Earth", "domain": "indx.earth"},
|
|
"web": {},
|
|
})
|
|
|
|
def test_rebranding_uses_website_identity(self):
|
|
with patch.object(discovery, "SITE_URL", "https://www.example.com/"), patch.object(
|
|
discovery, "SITE_TITLE", "Example Company"
|
|
):
|
|
manifest = json.loads(discovery.build_poppy_json())
|
|
self.assertEqual(manifest["organization"], {
|
|
"name": "Example Company", "domain": "www.example.com"
|
|
})
|
|
|
|
def test_invalid_site_configuration_fails_before_writes(self):
|
|
invalid_urls = [
|
|
"http://example.com", "https:///", "https://user:[email protected]",
|
|
"https://example.com:8443", "https://example.com/site/",
|
|
"https://example.com?token=placeholder", "https://example.com#home",
|
|
]
|
|
for url in invalid_urls:
|
|
with self.subTest(url=url), patch.object(discovery, "SITE_URL", url), patch.object(
|
|
discovery, "write_text"
|
|
) as write:
|
|
with self.assertRaises(ValueError):
|
|
discovery.main()
|
|
write.assert_not_called()
|
|
with patch.object(discovery, "SITE_TITLE", " "):
|
|
with self.assertRaises(ValueError):
|
|
discovery.build_poppy_json()
|
|
|
|
def test_validation_rejects_stale_or_unimplemented_claims(self):
|
|
mutations = [
|
|
{"protocol_version": "1.0"},
|
|
{"organization": {"name": "Old Brand", "domain": "old.example"}},
|
|
{"web": {"browser_session_endpoint": "https://indx.earth/session"}},
|
|
{"auth": {"issuer": "https://auth.example.com"}},
|
|
{"apis": [{"type": "mcp", "url": "https://example.com/mcp"}]},
|
|
{"agent": {"protocols": []}},
|
|
{"extensions": {"operations": {"version": "1"}}},
|
|
]
|
|
for mutation in mutations:
|
|
with self.subTest(mutation=mutation):
|
|
manifest = json.loads(discovery.build_poppy_json())
|
|
manifest.update(mutation)
|
|
with self.assertRaises(RuntimeError):
|
|
discovery.validate_poppy_json(manifest)
|
|
|
|
def test_discovery_generation_validation_and_copy(self):
|
|
with tempfile.TemporaryDirectory(prefix="poppy-test-", dir=ROOT) as directory:
|
|
root = Path(directory)
|
|
public = root / "public"
|
|
hugo = root / "hugo"
|
|
hugo.mkdir()
|
|
with patch.object(discovery, "PUBLIC_ROOT", public), patch.object(
|
|
discovery, "HUGO_PUBLIC", hugo
|
|
), patch.object(publisher, "PUBLIC_ROOT", public), patch.object(
|
|
publisher, "HUGO_PUBLIC", hugo
|
|
):
|
|
discovery.main()
|
|
target = public / ".well-known/poppy.json"
|
|
expected = target.read_bytes()
|
|
for name in ("llms.txt", "agents.md", ".well-known/agent-card.json"):
|
|
self.assertIn("/.well-known/poppy.json", (public / name).read_text())
|
|
stale = hugo / ".well-known/poppy.json"
|
|
stale.parent.mkdir()
|
|
stale.write_text('{"stale": true}')
|
|
(hugo / "index.html").write_text("<html>Example</html>")
|
|
copied, skipped = publisher.copy_hugo_output()
|
|
self.assertEqual(1, copied)
|
|
self.assertIn(".well-known/poppy.json", skipped)
|
|
self.assertEqual(expected, target.read_bytes())
|
|
discovery.main()
|
|
self.assertEqual(expected, target.read_bytes())
|
|
target.write_text('{"protocol_version": "9.0"}')
|
|
with self.assertRaises(RuntimeError):
|
|
discovery.validate_outputs()
|
|
target.unlink()
|
|
with self.assertRaises(RuntimeError):
|
|
discovery.validate_outputs()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|