#!/usr/bin/env python3 """ Tests for the pure parts of reels-scrape.py -- everything except the actual CDP session, which needs a live signed-in Chrome and is exercised by hand. python3 -m unittest discover -s scripts -p 'test_*.py' """ import importlib.util import sys import unittest from pathlib import Path _spec = importlib.util.spec_from_file_location( "reels_scrape", Path(__file__).with_name("reels-scrape.py")) scrape = importlib.util.module_from_spec(_spec) sys.modules["reels_scrape"] = scrape _spec.loader.exec_module(scrape) class ExtractShortcodes(unittest.TestCase): def test_reads_relative_and_absolute_hrefs(self): hrefs = [ "/someuser/reel/ABC123/", "https://www.instagram.com/someuser/reel/DEF456/", "/reel/GHI789/?img_index=1", ] self.assertEqual(scrape.extract_shortcodes(hrefs), ["ABC123", "DEF456", "GHI789"]) def test_ignores_non_reel_links(self): hrefs = ["/someuser/", "/someuser/p/ABC123/", "/explore/tags/foo/"] self.assertEqual(scrape.extract_shortcodes(hrefs), []) def test_empty_input(self): self.assertEqual(scrape.extract_shortcodes([]), []) if __name__ == "__main__": unittest.main(verbosity=2)