"""Tests for the HTML emit renderer."""
from __future__ import annotations
import tempfile
import unittest
from html.parser import HTMLParser
from pathlib import Path
import last30days as cli
from lib import html_render, schema
def _report(topic: str, cluster_titles: list[str]) -> schema.Report:
items: list[schema.SourceItem] = []
candidates: list[schema.Candidate] = []
clusters: list[schema.Cluster] = []
for index, title in enumerate(cluster_titles, start=1):
item = schema.SourceItem(
item_id=f"item-{index}",
source="grounding",
title=title,
body=f"Body for {title}",
url=f"https://example.test/{index}",
container="example.test",
published_at="2026-04-20",
date_confidence="high",
engagement={"views": index * 100},
snippet=f"Snippet for {title}",
)
candidate = schema.Candidate(
candidate_id=f"candidate-{index}",
item_id=item.item_id,
source="grounding",
title=title,
url=item.url,
snippet=item.snippet,
subquery_labels=["primary"],
native_ranks={"primary:grounding": index},
local_relevance=0.9,
freshness=80,
engagement=50,
source_quality=1.0,
rrf_score=0.5,
final_score=90 - index,
sources=["grounding"],
source_items=[item],
)
cluster = schema.Cluster(
cluster_id=f"cluster-{index}",
title=title,
candidate_ids=[candidate.candidate_id],
representative_ids=[candidate.candidate_id],
sources=["grounding"],
score=90 - index,
)
items.append(item)
candidates.append(candidate)
clusters.append(cluster)
return schema.Report(
topic=topic,
range_from="2026-03-30",
range_to="2026-04-29",
generated_at="2026-04-29T12:00:00+00:00",
provider_runtime=schema.ProviderRuntime(
reasoning_provider="local",
planner_model="mock-planner",
rerank_model="mock-rerank",
),
query_plan=schema.QueryPlan(
intent="research",
freshness_mode="balanced_recent",
cluster_mode="story",
raw_topic=topic,
subqueries=[
schema.SubQuery(
label="primary",
search_query=topic,
ranking_query=topic,
sources=["grounding"],
)
],
source_weights={"grounding": 1.0},
),
clusters=clusters,
ranked_candidates=candidates,
items_by_source={"grounding": items},
errors_by_source={},
artifacts={"pre_research_flags_present": True},
)
def _assert_parses(test_case: unittest.TestCase, html: str) -> None:
parser = HTMLParser()
parser.feed(html)
parser.close()
test_case.assertIn("