Files
seo-geo-research-OPEN/tests/test_discovery.py
T
2026-10-14 10:45:26 -06:00

41 lines
2.7 KiB
Python

import copy, json, sys, tempfile, unittest
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT/'scripts'))
from build_content_index import build, validate_index
from validate_discovery import validate
from rollup import build as rollup
class DiscoveryTests(unittest.TestCase):
def setUp(self):
self.temp=tempfile.TemporaryDirectory(dir=Path(__file__).resolve().parent); self.root=Path(self.temp.name)
import shutil
for name in ['repository.json','repository-variables.json','VERSION','CITATION.cff','AUTHOR.md','LICENSE','CHANGELOG.md','README.md','AGENTS.md','skills.md','CONTRIBUTING.md','SECURITY.md','UPDATES.md']:
shutil.copyfile(ROOT/name,self.root/name)
for folder in ['scripts','schemas','skills','templates','evals','examples','reports','tests']:
shutil.copytree(ROOT/folder,self.root/folder,ignore=shutil.ignore_patterns('__pycache__','tmp*'))
build(self.root)
def tearDown(self): self.temp.cleanup()
def test_stable_and_idempotent(self):
first=(self.root/'content-index.json').read_bytes(); build(self.root); self.assertEqual(first,(self.root/'content-index.json').read_bytes())
def test_duplicate_id_and_path(self):
index=build(self.root); index['items'].append(copy.deepcopy(index['items'][0]))
with self.assertRaisesRegex(ValueError,'Duplicate'): validate_index(self.root,index)
def test_missing_path_preserves_index(self):
before=(self.root/'content-index.json').read_bytes(); (self.root/'templates/report-template.md').rename(self.root/'templates/missing.md')
with self.assertRaisesRegex(ValueError,'Missing'): build(self.root)
self.assertEqual(before,(self.root/'content-index.json').read_bytes())
def test_stale_then_rollup_refresh(self):
(self.root/'reports/2026-10-19-geo-ai-search-intelligence.md').write_text('# Synthetic report\nStatus: partial\n')
with self.assertRaisesRegex(ValueError,'Stale'): validate(self.root,False)
rollup(self.root); result=validate(self.root,False); self.assertEqual(result['status'],'PASS')
index=json.loads((self.root/'content-index.json').read_text()); item=next(i for i in index['items'] if '2026-10-19' in i['path']); self.assertEqual(item['status'],'draft')
def test_version_mismatch(self):
(self.root/'VERSION').write_text('9.9.9')
with self.assertRaisesRegex(ValueError,'version mismatch'): validate(self.root,False)
def test_publication_status_preserved(self):
p=self.root/'content-index.json'; index=json.loads(p.read_text()); index['items'][0]['status']='published'; index['items'][0]['published_at']='2026-10-14'; p.write_text(json.dumps(index))
result=build(self.root); self.assertEqual(result['items'][0]['status'],'published'); self.assertEqual(result['items'][0]['published_at'],'2026-10-14')
if __name__=='__main__': unittest.main()