"""Article-format, reference, source-group and failure-path checks without a large model.""" import copy import hashlib import json from pathlib import Path import unittest import tempfile from unittest.mock import patch from .articles import MODES, article_messages, behavior_examples, normalize_sources, render_draft, validate_response from .create_article_examples import build from .data import encode_example, normalize_messages, prompt_key from . import test_pipeline as pipeline_checks from .runtime import CONFIG from .train import read_data class ArticleTests(unittest.TestCase): def setUp(self): self.packet = build()["packets"][0] self.sources = normalize_sources(self.packet["sources"]) self.gold = self.packet["article_target"] def test_all_authored_examples_validate_and_source_groups_do_not_leak(self): examples = behavior_examples() self.assertEqual({s: len(rows) for s, rows in examples.items()}, {"train": 12, "validation": 4, "test": 4}) groups = {split: {row["group_id"] for row in rows} for split, rows in examples.items()} self.assertFalse(groups["train"] & groups["test"]) self.assertFalse(groups["train"] & groups["validation"]) self.assertFalse(groups["validation"] & groups["test"]) on_disk = json.loads(Path(__file__).with_name("article_examples.json").read_text(encoding="utf-8")) self.assertEqual(on_disk, build()) def test_source_packet_cannot_be_empty_incomplete_or_credentialed(self): for rows in [[], [["title", "https://example.org", ""]], [["title", "javascript:alert(1)", "fact"]], [["title", "https://user:password@example.org", "fact"]]]: with self.subTest(rows=rows), self.assertRaises(ValueError): normalize_sources(rows) with self.assertRaises(ValueError): normalize_sources(self.packet["sources"] * 2) def test_unknown_or_missing_citations_are_rejected(self): for refs in [[], ["S999"], [123], "S1"]: value = copy.deepcopy(self.gold) value["lead"][0]["sources"] = refs with self.subTest(refs=refs), self.assertRaises(ValueError): validate_response(json.dumps(value), self.sources, MODES[0]) def test_invented_url_or_raw_reference_markup_is_rejected(self): for text in ["See https://invented.example/source", "Claim Invented source", "{{cite web|url=made-up}}"]: value = copy.deepcopy(self.gold) value["lead"][0]["text"] = text with self.subTest(text=text), self.assertRaises(ValueError): validate_response(json.dumps(value), self.sources, MODES[0]) def test_truncated_json_and_wrong_task_schema_are_rejected(self): with self.assertRaises(ValueError): validate_response('{"lead": [', self.sources, MODES[0]) with self.assertRaises(ValueError): validate_response(json.dumps(self.gold), self.sources, MODES[1]) result = validate_response(json.dumps(self.packet["outline_target"]), self.sources, MODES[1]) self.assertNotIn("lead", result) self.assertNotIn("paragraphs", result["sections"][0]) def test_reference_urls_come_from_supplied_sources_and_markup_is_escaped(self): result = validate_response(json.dumps(self.gold), self.sources, MODES[0]) result["lead"][0]["text"] = " [arbitrary](javascript:example)" rendered = render_draft(self.packet["topic"], result, self.sources, MODES[0]) self.assertIn(self.sources[0]["url"], rendered) self.assertIn("[^S1]", rendered) self.assertNotIn("