import importlib.util
import tempfile
import unittest
from pathlib import Path
SCRIPT_PATH = (
Path(__file__).resolve().parents[2]
/ "skills"
/ "agentfeed-posting"
/ "scripts"
/ "agentfeed_post.py"
)
def _load_module():
spec = importlib.util.spec_from_file_location("agentfeed_post", SCRIPT_PATH)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
agentfeed_post = _load_module()
class SniffTextKindTestCase(unittest.TestCase):
def test_markdown_suffixes(self):
for suffix in (".md", ".markdown", ".txt"):
self.assertEqual(agentfeed_post.sniff_text_kind(suffix, "anything"), "markdown")
def test_json_suffix(self):
self.assertEqual(agentfeed_post.sniff_text_kind(".json", '{"a": 1}'), "json")
def test_csv_suffix(self):
self.assertEqual(agentfeed_post.sniff_text_kind(".csv", "a,b\n1,2"), "csv")
def test_html_full_sniffed_from_content(self):
self.assertEqual(
agentfeed_post.sniff_text_kind(".html", ""), "html_full"
)
def test_deck_sniffed_from_content(self):
self.assertEqual(agentfeed_post.sniff_text_kind(".html", "
hi
"), "html_fragment") class ValidatePayloadTestCase(unittest.TestCase): def test_valid_json_passes(self): agentfeed_post.validate_payload({"kind": "json", "content": '{"a": 1}'}, None) # no raise def test_invalid_json_raises(self): with self.assertRaises(ValueError): agentfeed_post.validate_payload({"kind": "json", "content": "{not json"}, None) def test_deck_without_section_raises(self): with self.assertRaises(ValueError): agentfeed_post.validate_payload({"kind": "deck", "content": "x
"}, None) def test_blank_markdown_raises(self): with self.assertRaises(ValueError): agentfeed_post.validate_payload({"kind": "markdown", "content": " "}, None) def test_csv_has_no_shape_check(self): agentfeed_post.validate_payload({"kind": "csv", "content": "anything at all"}, None) # no raise def test_upload_path_skips_all_checks(self): agentfeed_post.validate_payload({"upload_path": Path("whatever.png")}, None) # no raise class ResolveAutoPathTestCase(unittest.TestCase): def _args(self, path): class Args: pass args = Args() args.path = str(path) args.as_markdown = False args.title = None return args def test_json_file_infers_json_kind(self): with tempfile.TemporaryDirectory() as tmp: path = Path(tmp) / "data.json" path.write_text('{"a": 1}', encoding="utf-8") args = agentfeed_post.resolve_auto_path(self._args(path)) self.assertEqual(args.kind, "json") self.assertEqual(args.content_file, str(path)) def test_csv_file_infers_csv_kind(self): with tempfile.TemporaryDirectory() as tmp: path = Path(tmp) / "data.csv" path.write_text("a,b\n1,2", encoding="utf-8") args = agentfeed_post.resolve_auto_path(self._args(path)) self.assertEqual(args.kind, "csv") def test_image_file_becomes_multipart_upload(self): with tempfile.TemporaryDirectory() as tmp: path = Path(tmp) / "chart.png" path.write_bytes(b"\x89PNG\r\n") args = agentfeed_post.resolve_auto_path(self._args(path)) self.assertIsNone(args.kind) self.assertEqual(args.file, str(path)) if __name__ == "__main__": unittest.main()