forked from ChelseaKR/queer-the-stacks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_share.py
More file actions
196 lines (162 loc) · 6.46 KB
/
Copy pathtest_share.py
File metadata and controls
196 lines (162 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"""Share cards — local-only composition, privacy-safe content, accessible page."""
from __future__ import annotations
from pathlib import Path
from app.a11y_check import check_html
from app.color_contrast import contrast_ratio, meets_aa
from app.share import (
MAX_POST_CHARS,
SVG_BG,
SVG_BODY,
SVG_BORDER,
SVG_HEADING,
build_share_cards,
finished_book_card,
render_share_page,
render_share_svg,
year_in_books_card,
)
from app.view import demo_view
from app.wrapped import Wrapped
from ingest.models import (
Author,
Book,
ReadingStat,
ReadingState,
ReadingStatus,
Source,
SourceKind,
ThemeTag,
)
def _wrapped() -> Wrapped:
return Wrapped(
year=2024,
books_finished=12,
pages_read=3400,
read_time_seconds=180_000,
days_read=140,
theme_breakdown=(("trans", 5), ("speculative", 4), ("queer", 3)),
standout_reads=(),
)
def _finished_state() -> ReadingState:
src = Source(SourceKind.OPENLIBRARY_SUBJECT, "https://openlibrary.org/subjects/trans", "x", "t")
book = Book(
book_id="b",
title="Nevada",
authors=(Author("Imogen Binnie"),),
theme_tags=(ThemeTag("trans", src), ThemeTag("queer", src)),
)
stat = ReadingStat("b", "Nevada", ("Imogen Binnie",), 250, 250, 36000, 1_700_000_000, 6)
return ReadingState(
title="Nevada",
authors=("Imogen Binnie",),
status=ReadingStatus.FINISHED,
book=book,
stat=stat,
)
def test_year_card_exposes_only_aggregates() -> None:
card = year_in_books_card(_wrapped())
text = card.post_text()
assert "12 books" in text
assert "2024" in card.title
assert "Top themes: trans, speculative, queer" in text
# Hashtags suitable for the fediverse.
assert "#bookwyrm" in text
# No device names, timestamps, or per-day history leak into the card.
assert "Kobo" not in text and "last_read" not in text
def test_finished_card_uses_sourced_themes_only() -> None:
card = finished_book_card(_finished_state())
text = card.post_text()
assert "Nevada" in text
assert "Imogen Binnie" in text
assert "Themes (sourced): trans, queer" in text
def test_post_text_capped_to_post_limit() -> None:
w = _wrapped()
huge = Wrapped(
year=w.year,
books_finished=w.books_finished,
pages_read=w.pages_read,
read_time_seconds=w.read_time_seconds,
days_read=w.days_read,
theme_breakdown=tuple((f"theme-with-a-long-name-{i}", 1) for i in range(50)),
standout_reads=(),
)
card = year_in_books_card(huge)
assert len(card.post_text()) <= MAX_POST_CHARS
def test_svg_is_self_contained_and_labelled() -> None:
svg = render_share_svg(year_in_books_card(_wrapped()))
assert svg.startswith("<svg")
assert 'role="img"' in svg
assert "<title>" in svg
# Self-contained: no external fetch, so no egress when a client renders it.
assert "http://www.w3.org/2000/svg" in svg # the xmlns is the only http token
assert "<image" not in svg and "xlink:href" not in svg
def test_share_page_is_accessible_and_says_nothing_auto_posts() -> None:
cards = (year_in_books_card(_wrapped()), finished_book_card(_finished_state()))
html = render_share_page(cards, user="demo")
assert check_html(html) == []
assert "Nothing is posted automatically" in html
# The postable text sits in a labelled control the reader copies by hand.
assert "Postable text" in html
assert "<textarea" in html
def test_copy_js_has_no_angle_brackets_inside() -> None:
from app.share import _COPY_JS
inner = _COPY_JS.replace("<script>", "").replace("</script>", "")
assert "<" not in inner # keeps the static a11y parser happy
def test_share_page_escapes_user_content() -> None:
src = Source(SourceKind.CALIBRE_TAG, "calibre:local", "2026-06-05", "x")
book = Book(
book_id="b",
title="<script>alert(1)</script>",
authors=(Author("A & B"),),
theme_tags=(ThemeTag("queer", src),),
)
state = ReadingState(
title=book.title, authors=("A & B",), status=ReadingStatus.FINISHED, book=book
)
html = render_share_page((finished_book_card(state),))
assert "<script>alert(1)</script>" not in html
assert "<script>" in html
def test_build_share_cards_from_demo_view(tmp_path: Path) -> None:
cards = build_share_cards(demo_view(tmp_path))
kinds = {c.kind for c in cards}
assert "year" in kinds and "finished" in kinds
def test_no_empty_cards_page_is_accessible() -> None:
html = render_share_page(())
assert check_html(html) == []
assert "No share cards yet" in html
def test_share_svg_palette_meets_aa() -> None:
"""The deterministic contrast gate: the actual SVG palette must pass AA.
Body text (32px, normal size) needs >=4.5:1; the heading (44px) and the
tags/border color (used at 26px, still large text per WCAG's >=18pt / 24px
definition) need only >=3.0:1.
"""
assert contrast_ratio(SVG_BODY, SVG_BG) >= 4.5
assert meets_aa(SVG_BODY, SVG_BG, large=False)
assert contrast_ratio(SVG_HEADING, SVG_BG) >= 3.0
assert meets_aa(SVG_HEADING, SVG_BG, large=True)
assert contrast_ratio(SVG_BORDER, SVG_BG) >= 3.0
assert meets_aa(SVG_BORDER, SVG_BG, large=True)
def test_contrast_helper_flags_violation() -> None:
"""Acceptance criterion: CI fails on an injected contrast violation."""
# Known-bad: light gray on white fails even the large-text AA threshold.
assert meets_aa("#aaaaaa", "#ffffff", large=False) is False
assert meets_aa("#aaaaaa", "#ffffff", large=True) is False
# Known-good: near-black on white comfortably passes both thresholds.
assert meets_aa("#111111", "#ffffff", large=False) is True
assert meets_aa("#111111", "#ffffff", large=True) is True
def test_svg_truncates_a_very_long_title() -> None:
from ingest.models import Author, Book, ReadingState, ReadingStatus
book = Book(
book_id="b",
title="A" * 200,
authors=(Author("Someone"),),
)
state = ReadingState(
title=book.title, authors=("Someone",), status=ReadingStatus.FINISHED, book=book
)
svg = render_share_svg(finished_book_card(state))
# The accessible aria-label/<title> (alt_text) keep the full, untruncated
# title as a text equivalent; only the visible heading <text> is truncated.
after_title = svg.split("</title>", 1)[1]
assert "…" in after_title
assert ("A" * 200) not in after_title