#!/usr/bin/env python3 from __future__ import annotations import tempfile import unittest from pathlib import Path from redmine_ticket_entities import extract_entities, extract_entities_from_issue from redmine_ticket_control_export import export_control_tag_jsonl from redmine_ticket_control_tags import add_tag, load_store from redmine_tickets import KNOWN_COMMANDS, build_parser, inject_default_sync_command class EntityExtractTest(unittest.TestCase): def test_patterns(self): text = ( "Order 123-4567890-1234567 UPS 1Z999AA10123456784 " "Item # SKU-ABC-99 USPS 9400111899223344556677" ) types = {e["type"] for e in extract_entities(text)} self.assertIn("tracking_ups", types) self.assertIn("tracking_usps", types) self.assertIn("order_amazon", types) self.assertIn("item_or_sku", types) def test_noise_suppressed(self): htmlish = "order-mode responsive-title order-text" hits = extract_entities(htmlish) self.assertEqual([], hits) def test_from_issue(self): issue = { "subject": "RET 1ZY228K59066639253", "description": "Order ID: 1013179 shipped", "journals": [], } ents = extract_entities_from_issue(issue) types = {e["type"] for e in ents} self.assertIn("tracking_ups", types) self.assertIn("order_id", types) class ControlExportTest(unittest.TestCase): def test_export(self): with tempfile.TemporaryDirectory() as td: cache = Path(td) add_tag(cache, tag="spammy", issue_id=7, properties=["subject"], annotation="x") store = load_store(cache) out = cache / "out.jsonl" n = export_control_tag_jsonl( cache_dir=cache, tags_payload=store, issues_by_id={7: {"id": 7, "subject": "hello", "status": {"name": "New"}}}, out_path=out, ) self.assertEqual(1, n) line = out.read_text(encoding="utf-8").strip() self.assertIn("spammy", line) self.assertIn('"issue_id": 7', line) class CliRegistrationTest(unittest.TestCase): def test_known_commands(self): self.assertIn("entities", KNOWN_COMMANDS) self.assertIn("browse", KNOWN_COMMANDS) args = build_parser().parse_args( inject_default_sync_command(["entities", "show", "--issue-id", "1"]) ) self.assertEqual("entities", args.command) self.assertEqual("show", args.entities_command) if __name__ == "__main__": unittest.main()