test_hermes_import.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """Read-only import of an existing Hermes home into the embedded home."""
  2. import hashlib
  3. import json
  4. import sqlite3
  5. import subprocess
  6. import sys
  7. import tempfile
  8. import unittest
  9. from pathlib import Path
  10. ROOT = Path(__file__).resolve().parents[2]
  11. SCRIPT = ROOT / "runtime" / "hermes_import.py"
  12. def tree_digest(root: Path) -> str:
  13. h = hashlib.sha256()
  14. for path in sorted(p for p in root.rglob("*") if p.is_file() and not p.name.endswith(("-wal", "-shm"))):
  15. h.update(str(path.relative_to(root)).encode())
  16. h.update(path.read_bytes())
  17. return h.hexdigest()
  18. class HermesImportTest(unittest.TestCase):
  19. def test_copies_data_and_leaves_source_untouched(self):
  20. with tempfile.TemporaryDirectory() as tmp:
  21. source, staging = Path(tmp, "hermes"), Path(tmp, "staging")
  22. (source / "profiles" / "writer").mkdir(parents=True)
  23. (source / "config.yaml").write_text("model:\n provider: openrouter\n")
  24. (source / "auth.json").write_text('{"providers": {}}')
  25. (source / ".env").write_text("OPENROUTER_API_KEY=sk-test\n")
  26. (source / "profiles" / "writer" / "config.yaml").write_text("model:\n default: x\n")
  27. (source / "skills" / "demo").mkdir(parents=True)
  28. (source / "skills" / "demo" / "SKILL.md").write_text("# demo\n")
  29. for skipped in ("hermes-agent", "logs", "plugins/yaoyao-bot-bridge"):
  30. (source / skipped).mkdir(parents=True)
  31. (source / skipped / "file.txt").write_text("skip")
  32. (source / "plugins" / "other").mkdir(parents=True)
  33. (source / "plugins" / "other" / "plugin.yaml").write_text("name: other\n")
  34. (source / "gateway.pid").write_text("123")
  35. db = sqlite3.connect(source / "state.db")
  36. db.execute("PRAGMA journal_mode=WAL")
  37. db.execute("CREATE TABLE sessions (id TEXT PRIMARY KEY, title TEXT)")
  38. db.execute("INSERT INTO sessions VALUES ('s1', 'hello')")
  39. db.commit() # keep the connection open: the import must read a live WAL database
  40. before = tree_digest(source)
  41. result = subprocess.run([sys.executable, "-I", str(SCRIPT), str(source), str(staging)],
  42. capture_output=True, text=True, check=True)
  43. after = tree_digest(source)
  44. db.close()
  45. summary = json.loads(result.stdout)
  46. self.assertGreater(summary["files"], 0)
  47. self.assertEqual((staging / "config.yaml").read_text(), "model:\n provider: openrouter\n")
  48. self.assertTrue((staging / "profiles" / "writer" / "config.yaml").exists())
  49. self.assertTrue((staging / "skills" / "demo" / "SKILL.md").exists())
  50. self.assertTrue((staging / "plugins" / "other" / "plugin.yaml").exists())
  51. for skipped in ("hermes-agent", "logs", "plugins/yaoyao-bot-bridge", "gateway.pid"):
  52. self.assertFalse((staging / skipped).exists(), skipped)
  53. copied = sqlite3.connect(staging / "state.db")
  54. self.assertEqual(copied.execute("SELECT title FROM sessions WHERE id='s1'").fetchone()[0], "hello")
  55. copied.close()
  56. manifest = json.loads((staging / ".import.json").read_text())
  57. self.assertEqual(manifest["source"], str(source.resolve()))
  58. self.assertEqual(oct((staging / ".env").stat().st_mode & 0o777), "0o600")
  59. # The legacy home is read-only input (WAL files from our own writer aside).
  60. self.assertEqual(after, before)
  61. def test_refuses_existing_staging_directory(self):
  62. with tempfile.TemporaryDirectory() as tmp:
  63. source, staging = Path(tmp, "hermes"), Path(tmp, "staging")
  64. source.mkdir()
  65. (source / "config.yaml").write_text("{}")
  66. staging.mkdir()
  67. result = subprocess.run([sys.executable, "-I", str(SCRIPT), str(source), str(staging)], capture_output=True, text=True)
  68. self.assertNotEqual(result.returncode, 0)
  69. if __name__ == "__main__":
  70. unittest.main()