Skip to content

Commit 8cfeda0

Browse files
committed
Cover local image upload, autolinks, and formatted headings in e2e
- Add a locally-sourced image to the fixture so the test exercises the real api.get_image() upload path; the test asserts the image was uploaded to a remote URL. The fixture references it via a token resolved to a CWD-relative path (so get_image's os.path.exists check and the leading-slash handling both behave), and from_markdown is now called with api=. - Add an autolink and a heading with inline formatting. - Normalise image dimension attrs (height/width/resizeWidth) alongside src, since Substack fills those in for uploaded images non-deterministically.
1 parent d2d85b4 commit 8cfeda0

4 files changed

Lines changed: 166 additions & 14 deletions

File tree

‎tests/substack/fixtures/full_features.expected.json‎

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,9 +542,9 @@
542542
"src": "<SRC>",
543543
"fullscreen": false,
544544
"imageSize": "normal",
545-
"height": 819,
546-
"width": 1456,
547-
"resizeWidth": 728,
545+
"height": "<DIM>",
546+
"width": "<DIM>",
547+
"resizeWidth": "<DIM>",
548548
"bytes": null,
549549
"alt": "A captioned image",
550550
"title": null,
@@ -565,9 +565,9 @@
565565
"src": "<SRC>",
566566
"fullscreen": false,
567567
"imageSize": "normal",
568-
"height": 819,
569-
"width": 1456,
570-
"resizeWidth": 728,
568+
"height": "<DIM>",
569+
"width": "<DIM>",
570+
"resizeWidth": "<DIM>",
571571
"bytes": null,
572572
"alt": "Linked image alt",
573573
"title": null,
@@ -579,6 +579,29 @@
579579
}
580580
]
581581
},
582+
{
583+
"type": "captionedImage",
584+
"content": [
585+
{
586+
"type": "image2",
587+
"attrs": {
588+
"src": "<SRC>",
589+
"fullscreen": false,
590+
"imageSize": "normal",
591+
"height": "<DIM>",
592+
"width": "<DIM>",
593+
"resizeWidth": "<DIM>",
594+
"bytes": null,
595+
"alt": "A locally uploaded image",
596+
"title": null,
597+
"type": null,
598+
"href": null,
599+
"belowTheFold": false,
600+
"internalRedirect": null
601+
}
602+
}
603+
]
604+
},
582605
{
583606
"type": "paragraph",
584607
"content": [
@@ -593,6 +616,81 @@
593616
}
594617
]
595618
},
619+
{
620+
"type": "heading",
621+
"content": [
622+
{
623+
"type": "text",
624+
"text": "Heading with "
625+
},
626+
{
627+
"type": "text",
628+
"text": "bold",
629+
"marks": [
630+
{
631+
"type": "strong"
632+
}
633+
]
634+
},
635+
{
636+
"type": "text",
637+
"text": ", "
638+
},
639+
{
640+
"type": "text",
641+
"text": "italic",
642+
"marks": [
643+
{
644+
"type": "em"
645+
}
646+
]
647+
},
648+
{
649+
"type": "text",
650+
"text": ", and a "
651+
},
652+
{
653+
"type": "text",
654+
"text": "link",
655+
"marks": [
656+
{
657+
"type": "link",
658+
"attrs": {
659+
"href": "https://example.com"
660+
}
661+
}
662+
]
663+
}
664+
],
665+
"attrs": {
666+
"level": 2
667+
}
668+
},
669+
{
670+
"type": "paragraph",
671+
"content": [
672+
{
673+
"type": "text",
674+
"text": "An autolink to "
675+
},
676+
{
677+
"type": "text",
678+
"text": "https://example.com",
679+
"marks": [
680+
{
681+
"type": "link",
682+
"attrs": {
683+
"href": "https://example.com"
684+
}
685+
}
686+
]
687+
},
688+
{
689+
"type": "text",
690+
"text": " becomes a link."
691+
}
692+
]
693+
},
596694
{
597695
"type": "footnote",
598696
"attrs": {

‎tests/substack/fixtures/full_features.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ plain code block without a language
5757

5858
[![Linked image alt](https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png)](https://example.com/target)
5959

60+
![A locally uploaded image]({{LOCAL_IMAGE}})
61+
6062
`inline code with [^1] inside stays literal`
6163

64+
## Heading with **bold**, *italic*, and a [link](https://example.com)
65+
66+
An autolink to <https://example.com> becomes a link.
67+
6268
[^1]: The first footnote, with a [link](https://example.com).
6369
[^note]: A named footnote whose definition spans
6470
a continuation line in the same paragraph.
75 Bytes
Loading

‎tests/substack/test_end_to_end.py‎

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@
3838
TITLE = "python-substack e2e feature test"
3939
SUBTITLE = "Automated round-trip fixture"
4040

41+
LOCAL_IMAGE_FILE = FIXTURES / "local_image.png"
42+
LOCAL_IMAGE_TOKEN = "{{LOCAL_IMAGE}}"
43+
LOCAL_IMAGE_ALT = "A locally uploaded image"
44+
45+
46+
def _image_srcs_by_alt(content, alt):
47+
"""Collect every image2 src whose alt matches (for upload verification)."""
48+
found = []
49+
50+
def walk(node):
51+
if isinstance(node, dict):
52+
if node.get("type") == "image2" and node.get("attrs", {}).get("alt") == alt:
53+
found.append(node["attrs"].get("src"))
54+
for value in node.values():
55+
walk(value)
56+
elif isinstance(node, list):
57+
for item in node:
58+
walk(item)
59+
60+
walk(content)
61+
return found
62+
4163

4264
def _has_credentials() -> bool:
4365
"""Whether auth is configured, checked without any network calls."""
@@ -66,12 +88,24 @@ def _api_from_env() -> Api:
6688

6789

6890
def _normalize(content):
69-
"""Replace values that legitimately vary between runs (e.g. image URLs)."""
91+
"""Replace values that vary between runs/environments.
92+
93+
Image ``src`` is a per-upload URL, and Substack fills in the dimension
94+
attributes (``height``/``width``/``resizeWidth``) for uploaded images via its
95+
own image pipeline (not deterministically), so all of these are blanked
96+
before comparison.
97+
"""
98+
volatile = {
99+
"src": "<SRC>",
100+
"height": "<DIM>",
101+
"width": "<DIM>",
102+
"resizeWidth": "<DIM>",
103+
}
70104

71105
def walk(node):
72106
if isinstance(node, dict):
73107
return {
74-
key: ("<SRC>" if key == "src" else walk(value))
108+
key: (volatile[key] if key in volatile else walk(value))
75109
for key, value in node.items()
76110
}
77111
if isinstance(node, list):
@@ -82,18 +116,23 @@ def walk(node):
82116

83117

84118
def _roundtrip(api: Api):
85-
"""Post the fixture as a draft, read it back, and return normalised content."""
119+
"""Post the fixture as a draft, read it back, and return its stored content."""
86120
markdown = MARKDOWN_FILE.read_text(encoding="utf-8")
121+
# The local-image path is environment-specific, so it lives as a token in the
122+
# fixture. We resolve it to a path relative to the current directory: get_image()
123+
# uploads any path that os.path.exists(), and a relative path avoids the
124+
# leading-slash handling that from_markdown applies to web-root paths.
125+
markdown = markdown.replace(LOCAL_IMAGE_TOKEN, os.path.relpath(LOCAL_IMAGE_FILE))
87126
post = Post(TITLE, SUBTITLE, user_id=api.get_user_id())
88-
post.from_markdown(markdown)
127+
post.from_markdown(markdown, api=api)
89128
draft = api.post_draft(post.get_draft())
90129
draft_id = draft.get("id")
91130
try:
92131
stored = api.get_draft(draft_id)
93132
body = stored.get("draft_body")
94133
if isinstance(body, str):
95134
body = json.loads(body)
96-
return _normalize(body["content"])
135+
return body["content"]
97136
finally:
98137
try:
99138
api.delete_draft(draft_id)
@@ -104,17 +143,26 @@ def _roundtrip(api: Api):
104143
@pytest.mark.skipif(not _has_credentials(), reason="no Substack credentials configured")
105144
def test_full_features_roundtrip():
106145
api = _api_from_env()
107-
actual = _roundtrip(api)
146+
content = _roundtrip(api)
147+
148+
# The locally-sourced image must have been uploaded to a remote Substack URL
149+
# rather than left as the local file path.
150+
local_srcs = _image_srcs_by_alt(content, LOCAL_IMAGE_ALT)
151+
assert local_srcs, "local image node not found in stored draft"
152+
assert local_srcs[0].startswith("http"), (
153+
f"local image was not uploaded: {local_srcs[0]!r}"
154+
)
155+
108156
expected = json.loads(EXPECTED_FILE.read_text(encoding="utf-8"))
109-
assert actual == expected
157+
assert _normalize(content) == expected
110158

111159

112160
def _generate():
113161
"""Regenerate the golden file from a live round-trip."""
114162
if not _has_credentials():
115163
raise SystemExit("No credentials configured; cannot generate golden file.")
116164
api = _api_from_env()
117-
content = _roundtrip(api)
165+
content = _normalize(_roundtrip(api))
118166
EXPECTED_FILE.write_text(
119167
json.dumps(content, indent=2, ensure_ascii=False) + "\n", encoding="utf-8"
120168
)

0 commit comments

Comments
 (0)