Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
"""Where a link is, and what a platform owes the whole of it.
A Markdown link is one unit: a label a reader taps and an address the tap goes
to. Every IM dialect spells that unit its own way, and the passes that do it
are line scanners - Slack's ``markdown_to_mrkdwn`` reads a whole line for
emphasis and for brackets alike. Two things went wrong there. It read
``https://a*b*.example/x`` as emphasis and delivered a link to
``https://a_b_.example/x``, a different site; and once the address was held
behind a placeholder, the brackets still in the stream were its to mis-pair -
``[^f]: [p](https://example.com/x)`` went out as one link labelled ``^f]: [p``.
So the unit is located once, in the shared layer, and each platform is asked to
spell it again from the label and the destination a Markdown reader resolves.
Both halves are measured here: which source ranges a link can be found in -
including the ones CommonMark hands to no inline parser, because the consumer
downstream reads them anyway - and what the real Slack client sends for each.
"""
from __future__ import annotations
import html
import json
import re
import sys
import unittest
from pathlib import Path
from unittest import mock
from markdown_it import MarkdownIt
from markdown_it.common.entities import entities
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from config.v2_config import WeChatConfig
from core.citations import (
_END,
_SEP,
_START,
CitationSource,
register_citations,
resolve_citations,
)
from core.message_dispatcher import ConsolidatedMessageDispatcher
from core.reply_enhancer import inline_links
from modules.im import MessageContext
from modules.im.formatters import hold_links, restore_held
from modules.im.slack import SlackBot, SlackMarkdownConverter
from modules.im.wechat import WeChatBot, wechat_api, wechat_cdn
from tests.test_message_dispatcher_result_fallback import _StubController
STAR = "https://a*b*.example/x"
PLAIN = "https://example.com/x"
def located(text: str) -> list[tuple[str, str, str]]:
"""Every link in *text* as ``(whole unit, label, resolved destination)``."""
return [
(
text[link.start : link.end],
text[link.label_start : link.label_end],
link.destination,
)
for link in inline_links(text)
]
def slack(text: str) -> str:
# The conversion is the whole unit under test; the rest of the client is
# network plumbing this never reaches.
bot = SlackBot.__new__(SlackBot)
bot.markdown_converter = SlackMarkdownConverter()
return bot._convert_markdown_to_slack_mrkdwn(text)
# Slack's own grammar for the unit, and the only way to read one honestly:
# structure first. A field stops at a raw ``<``, ``>`` or ``|`` because that is
# where Slack stops reading it, so a destination that let one through does not
# parse into a field here at all - which is the point. Decoding before parsing
# would quietly turn a truncated link into a plausible one and let a broken
# delimiter pass as a passing test.
_SLACK_WRAPPER_RE = re.compile(r"<([^<>|]*)(?:\|([^<>]*))?>")
# The three references Slack resolves on the way in, and nothing else: what the
# reader is shown for a wire string is that string with these three decoded, in
# one pass.
_SLACK_DECODED = {"&": "&", "<": "<", ">": ">"}
_SLACK_REFERENCE_RE = re.compile(r"&(?:amp|lt|gt);")
_ANCHOR_TEXT_RE = re.compile(r"<a [^>]*>(.*)</a>", re.S)
def all_links(wire: str) -> list[tuple[str, str]]:
"""Every ``(destination, label)`` in *wire*, exactly as the wire spells them."""
return _SLACK_WRAPPER_RE.findall(wire)
def one_link(wire: str) -> tuple[str, str]:
"""The ``(destination, label)`` of the single wrapper in *wire*, undecoded.
Asserting there is exactly one is half the contract: a destination that
ended the wrapper early leaves a second ``<...>`` behind it, or none that
parses at all, and either way the count says so before any field is read.
"""
units = all_links(wire)
assert len(units) == 1, wire
return units[0]
def slack_label(markdown: str) -> str:
"""The label half of the one link *markdown* is delivered as."""
return one_link(slack(markdown))[1]
def slack_decodes(wire: str) -> str:
"""What a Slack reader is shown for *wire*."""
return _SLACK_REFERENCE_RE.sub(lambda match: _SLACK_DECODED[match.group()], wire)
def slack_reads(wire: str) -> tuple[str, str]:
"""The ``(destination, label)`` a Slack client resolves out of *wire*.
One pass of the three decodings over each field, once the structure is
settled. The destination here is the address a tap opens, which is the
thing the wire owes the reader - not the bytes it took to spell it.
"""
destination, label = one_link(wire)
return slack_decodes(destination), slack_decodes(label)
def commonmark_shows(label: str) -> str:
"""What a Markdown reader is shown for *label* written as a link's text.
The oracle for every reference case below: CommonMark decides whether a
label means a character or spells one, and Slack has to show the reader
the same thing. Code literals are excluded - their rendering is a ``<code>``
element rather than text - and are asserted directly instead.
"""
rendered = MarkdownIt().renderInline(f"[{label}]({PLAIN})")
match = _ANCHOR_TEXT_RE.search(rendered)
assert match is not None, rendered
return html.unescape(match.group(1))
def one_line(text: str) -> str:
"""A label's documented policy: a break, and the space around it, is a space.
The oracle above answers what a Markdown reader sees; a Slack label answers
that on one line, because a newline inside ``<url|label>`` is not a link.
"""
return re.sub(r"[ \t]*(?:\r\n|\r|\n)[ \t]*", " ", text)
def citation_body(url: str, title: str = "Source") -> str:
"""The delivered text a real citation of *url* produces."""
body, _ = resolve_citations(
f"Cited. {_START}cite{_SEP}turn0view0{_END}",
{"turn0view0": CitationSource(ref_id="turn0view0", url=url, title=title)},
unresolved_label="(source unavailable)",
)
return body
def converter_alone(text: str) -> str:
"""What the third-party converter does with no protection at all.
Used only where the contract is "this text is NOT ours to change" - code and
images, compared against the converter of whatever version is installed. The
mangling that motivated this change is documented in prose rather than
asserted: pinning one vendor version's particular wrong output would make an
upstream fix read as a regression here, and the delivered link is the thing
this module owes the reader either way.
"""
return SlackMarkdownConverter().convert(text)
class TestWhereALinkIs:
"""The source ranges a link has to be findable in.
A block CommonMark parses inline is the easy half. The other half is the
lines it parses inline for nobody - a link reference definition is consumed
whole by the block parser and an HTML block is passed through raw - which
the Web renderer treats its own way but the IM converters read as ordinary
lines of text.
"""
def test_a_link_in_prose_is_located_whole(self):
text = f"Read [the docs]({STAR}) first."
assert located(text) == [(f"[the docs]({STAR})", "the docs", STAR)]
def test_a_link_in_a_footnote_definition_is_found(self):
"""The line CommonMark hands to no inline parser at all.
Nothing found it before, so nothing protected it, and Slack's converter
paired the definition's own bracket with the link's - the reader got
one link labelled ``^f]: [p`` pointing at a rewritten address.
"""
text = f"[^f]: [p]({STAR})\n\nCited. [q]({PLAIN}) [^f]"
assert located(text) == [
(f"[p]({STAR})", "p", STAR),
(f"[q]({PLAIN})", "q", PLAIN),
]
def test_a_link_in_an_html_block_is_found(self):
text = f"<div>\n[x]({STAR})\n</div>"
assert located(text) == [(f"[x]({STAR})", "x", STAR)]
def test_a_link_in_a_table_cell_is_found(self):
text = f"| Source |\n| --- |\n| [t]({STAR}) |"
assert located(text) == [(f"[t]({STAR})", "t", STAR)]
def test_a_link_written_across_two_lines_is_one_unit(self):
text = f"See [the\nguide]({STAR}) here."
assert located(text) == [(f"[the\nguide]({STAR})", "the\nguide", STAR)]
def test_a_link_inside_a_code_span_that_spans_two_lines_is_not_a_link(self):
"""The counterexample to reading each line on its own.
A backtick pair is a code span across the whole paragraph, and the link
it holds is an example rather than an address. A pass that scanned
every line separately - or masked backticks one line at a time - would
find one here, and would then hand a platform a link no reader is
shown. Lines a block already claimed are read as that block.
"""
text = f"Text `code\n[x]({STAR})` more"
assert located(text) == []
def test_a_link_inside_code_on_one_line_is_not_a_link(self):
for text in (
f"An example: `[x]({STAR})`",
f"```\n[x]({STAR})\n```",
f" [x]({STAR})\n",
):
assert located(text) == []
def test_an_image_is_not_a_link(self):
assert located(f"") == []
def test_a_reference_link_spells_no_destination(self):
assert located(f"[a][ref]\n\n[ref]: {STAR}") == []
def test_neighbouring_links_are_each_located_once(self):
"""Two units touching, so a caller may splice on these offsets in one pass."""
text = f"[a]({PLAIN})[b]({STAR})"
links = inline_links(text)
assert located(text) == [(f"[a]({PLAIN})", "a", PLAIN), (f"[b]({STAR})", "b", STAR)]
assert links[0].end == links[1].start
def test_a_title_is_not_part_of_the_address(self):
text = f'[docs]({PLAIN} "T")'
assert located(text) == [(text, "docs", PLAIN)]
class TestHoldingTheUnit:
def test_each_unit_is_replaced_once_and_restored_verbatim(self):
text = f"See [one]({PLAIN}) and [two]({STAR})."
held, tokens = hold_links(text, render=lambda label, url: f"<{url}|{label}>")
assert "](" not in held
assert len(tokens) == 2
assert restore_held(held, tokens) == (
f"See <{PLAIN}|one> and <{STAR}|two>."
)
def test_the_platform_is_handed_the_label_and_the_resolved_destination(self):
"""Resolved: angle brackets and a title are spelling, not address."""
seen: list[tuple[str, str]] = []
hold_links(
f'[a](<{STAR}>) and [b]({PLAIN} "T")',
render=lambda label, url: seen.append((label, url)) or "",
)
assert seen == [("a", STAR), ("b", PLAIN)]
def test_a_text_with_no_link_is_handed_back_untouched(self):
text = "Just *em* and **bold** and `code`."
assert hold_links(text, render=lambda label, url: "") == (text, {})
class TestSlackSendsTheWholeUnit:
"""The real client, on each range the converter reads as a line of text."""
def test_a_footnote_definition_reaches_slack_as_the_link_it_spells(self):
text = f"[^f]: [p]({STAR})"
assert slack(text) == f"[^f]: <{STAR}|p>"
def test_an_html_block_keeps_the_address_it_holds(self):
text = f"<div>\n[x]({STAR})\n</div>"
assert slack(text) == f"<div>\n<{STAR}|x>\n</div>"
def test_a_table_cell_becomes_a_link_instead_of_raw_markdown(self):
text = f"| Source |\n| --- |\n| [t]({STAR}) |"
assert slack(text) == f"*Source*\n<{STAR}|t>"
def test_a_label_written_across_two_lines_arrives_as_one_link(self):
"""A newline inside ``<url|label>`` is not a link, and a soft break is a space.
The converter's link pattern never crosses a line, so on its own it
left the Markdown standing - with the address already rewritten by the
emphasis pass that does not stop at one either.
"""
text = f"See [the\nguide]({STAR}) here."
assert slack(text) == f"See <{STAR}|the guide> here."
def test_a_titled_link_no_longer_sends_the_title_as_part_of_the_address(self):
text = f'[docs]({PLAIN} "T")'
assert slack(text) == f"<{PLAIN}|docs>"
def test_a_formatted_label_is_still_mrkdwn(self):
"""Holding the unit takes the label out of the converter's reach, so the
platform that spells the unit formats the label itself."""
assert slack(f"[**bold**]({PLAIN})") == f"<{PLAIN}|*bold*>"
def test_a_link_with_no_label_is_sent_bare(self):
assert slack(f"[]({PLAIN})") == f"<{PLAIN}>"
def test_prose_around_a_link_is_formatted_as_before(self):
assert slack(f"See [one]({PLAIN}) and *em* text.") == (
f"See <{PLAIN}|one> and _em_ text."
)
def test_a_code_example_keeps_the_behaviour_it_had(self):
"""Code is not a link on any surface, and holding stops at its edge."""
for text in (f"```\n[x]({STAR})\n```", f"Text `code\n[x]({STAR})` more"):
assert slack(text) == converter_alone(text)
def test_an_image_keeps_the_behaviour_it_had(self):
text = f""
assert slack(text) == converter_alone(text)
class TestSlackSpellsTheWholeLabel:
"""What a label owes Slack once the unit is Slack's to serialize.
``<url|label>`` ends at the first ``>``, so a label that means one
literally takes the address down with it: the reader is shown the rest of
the label and the raw URL, with nothing to click. Slack reads ``&`` and
``<`` as markup of its own for the same reason, and a label that came from
a backslash escape only becomes those characters at restore time - after
the wrapper used to be built.
"""
def test_a_label_that_means_a_delimiter_arrives_as_one(self):
assert slack(f"[a > b]({PLAIN})") == f"<{PLAIN}|a > b>"
assert slack(f"[Tom & Jerry]({PLAIN})") == f"<{PLAIN}|Tom & Jerry>"
assert slack(f"[5 < 6]({PLAIN})") == f"<{PLAIN}|5 < 6>"
def test_a_backslash_escaped_delimiter_is_encoded_not_restored_raw(self):
"""The ordering case: these characters come back after the converter ran.
An escape says the character is not syntax, so it is held through the
platform pass and restored afterwards. Restored into a finished
``<url|label>`` it would be a raw delimiter inside the wrapper with
nothing left to encode it - the link would end at the ``>``.
"""
assert slack(rf"[a \> b \& c \< d]({PLAIN})") == f"<{PLAIN}|a > b & c < d>"
def test_a_reference_the_source_spells_arrives_as_the_character_it_names(self):
"""``&`` means ``&`` to the reader, and Slack spells ``&`` that way too."""
assert slack(f"[AT&T]({PLAIN})") == f"<{PLAIN}|AT&T>"
assert slack(f"[a <b> c]({PLAIN})") == f"<{PLAIN}|a <b> c>"
def test_a_label_that_looks_like_a_mention_is_delivered_as_text(self):
"""A label reading ``<@U123>`` is text somebody wrote, not a mention."""
assert slack(f"[<@U123>]({PLAIN})") == f"<{PLAIN}|<@U123>>"
def test_emphasis_in_a_label_survives_the_encoding(self):
assert slack(f"[**bold** & <em>]({PLAIN})") == f"<{PLAIN}|*bold* & <em>>"
def test_the_destination_reaches_the_address_it_named(self):
"""The address is not text - but the wire that carries it is.
Slack reads ``&`` in a text object as the start of one of the three
references it resolves, so a query separator travels spelled the way
that object spells one. What the reader is owed is the address a tap
opens, not the bytes that carried it there: ``?a=1&b=2`` on the
wire is ``?a=1&b=2`` to the client, which is the page the Markdown
named. Percent-encoding that ``&`` instead would survive every decoder
and open a different page - one parameter ``a`` holding ``1&b=2``.
"""
query = "https://example.com/s?a=1&b=2"
wire = slack(f"[q]({query})")
assert wire == "<https://example.com/s?a=1&b=2|q>"
assert slack_reads(wire) == (query, "q")
def test_an_autolink_is_not_this_serializers_unit(self):
"""``<https://...>`` arrives already wrapped, and is left as it was."""
query = "https://example.com/s?a=1&b=2"
assert slack(f"<{query}>") == f"<{query}>"
def test_text_outside_a_link_keeps_the_behaviour_it_had(self):
"""Only the link wrapper is this change's boundary."""
for text in ("a > b & c", "5 < 6", "plain & text"):
assert slack(text) == converter_alone(text)
# The destination half of the unit, as one batch: the Markdown a reader wrote,
# the exact bytes Slack is sent, and the address a tap on it opens. The wire is
# asserted on its own so a permissive decoder cannot make a broken delimiter
# look like a working link, and the address on its own because that - not the
# spelling - is what the reader was promised.
# The bounded matrix both consumers are measured on. Shared with
# ui/src/components/ui/markdown.test.tsx, which renders the same sources
# through the real Markdown component; see the file's own description.
_DESTINATION_MATRIX = json.loads(
(Path(__file__).resolve().parent / "fixtures/link_destination_matrix.json").read_text(
encoding="utf-8"
)
)["cases"]
_DESTINATION_CORPUS = (
# A delimiter of the wrapper, however the source spells it. Raw, escaped
# and written as a reference all resolve to the same address, and all three
# have to leave it whole: a ``>`` ends the link early, a ``|`` starts the
# label early, and a ``<`` opens a second unit inside the first.
(r"[h](https://example.com/a>b)", "<https://example.com/a%3Eb|h>", "https://example.com/a%3Eb"),
(r"[h](https://example.com/a\>b)", "<https://example.com/a%3Eb|h>", "https://example.com/a%3Eb"),
("[h](https://example.com/a>b)", "<https://example.com/a%3Eb|h>", "https://example.com/a%3Eb"),
("[h](https://example.com/a>b)", "<https://example.com/a%3Eb|h>", "https://example.com/a%3Eb"),
(r"[h](https://example.com/a<b)", "<https://example.com/a%3Cb|h>", "https://example.com/a%3Cb"),
(r"[h](https://example.com/a\<b)", "<https://example.com/a%3Cb|h>", "https://example.com/a%3Cb"),
("[h](https://example.com/a<b)", "<https://example.com/a%3Cb|h>", "https://example.com/a%3Cb"),
(r"[h](https://example.com/a|b)", "<https://example.com/a%7Cb|h>", "https://example.com/a%7Cb"),
(r"[h](https://example.com/a\|b)", "<https://example.com/a%7Cb|h>", "https://example.com/a%7Cb"),
# ``&`` is the one Slack control character an address genuinely needs: it
# separates query parameters. So it is spelled, not escaped away, and the
# separator comes back when the client resolves the reference.
("[h](https://example.com/s?a=1&b=2)", "<https://example.com/s?a=1&b=2|h>", "https://example.com/s?a=1&b=2"),
("[h](https://example.com/a&b)", "<https://example.com/a&b|h>", "https://example.com/a&b"),
# A reference that stands for the text of a reference, paired with the
# escape that spells the same thing. Both name an address holding the five
# characters ``&``, and one decoding pass has to leave them standing.
("[h](https://example.com/a&amp;b)", "<https://example.com/a&amp;b|h>", "https://example.com/a&b"),
(r"[h](https://example.com/a\&b)", "<https://example.com/a&amp;b|h>", "https://example.com/a&b"),
# A reference can put a control character in an address. A newline would
# end the one line this wrapper is allowed to be.
("[h](https://example.com/p q)", "<https://example.com/p%0Aq|h>", "https://example.com/p%0Aq"),
("[h](https://example.com/p	q)", "<https://example.com/p%09q|h>", "https://example.com/p%09q"),
("[h](<https://example.com/a b>)", "<https://example.com/a%20b|h>", "https://example.com/a%20b"),
# An escape the address already spells is left spelled once. Encoding the
# ``%`` again would send ``%253E``, which is a path holding the text
# ``%3E`` rather than the one the author wrote.
("[h](https://example.com/a%3Eb)", "<https://example.com/a%3Eb|h>", "https://example.com/a%3Eb"),
("[h](https://example.com/a%26amp%3Bb)", "<https://example.com/a%26amp%3Bb|h>", "https://example.com/a%26amp%3Bb"),
# ``%b`` starts no escape, so that ``%`` is a character of the path and is
# written ``%25``. Leaving it bare is not the gentler answer: the consumer
# on the other side reads a bare ``%`` as data too, so a bare one delivers
# a path nobody asked for. ``%zz`` is two ASCII alphanumerics after a
# ``%``, which the same rule reads as an escape already spelled - so it is
# left standing rather than repaired into something it never said.
("[h](https://example.com/a%b)", "<https://example.com/a%25b|h>", "https://example.com/a%25b"),
("[h](https://example.com/a%zzb)", "<https://example.com/a%zzb|h>", "https://example.com/a%zzb"),
# Brackets are the syntax of an IPv6 host and data everywhere else, and the
# destination has to say which it means. Only the authority keeps them; the
# same two characters in a path, query or fragment are spelled - and
# userinfo, port, query and fragment structure survive either way.
(r"[h](https://[::1]:8443/a\>b)", "<https://[::1]:8443/a%3Eb|h>", "https://[::1]:8443/a%3Eb"),
(
"[h](https://u:p@[::1]:8443/a?x=1&y=2)",
"<https://u:p@[::1]:8443/a?x=1&y=2|h>",
"https://u:p@[::1]:8443/a?x=1&y=2",
),
(
"[h](https://u:p@[::1]:8443/a[b]?x=[c]#f[d])",
"<https://u:p@[::1]:8443/a%5Bb%5D?x=%5Bc%5D#f%5Bd%5D|h>",
"https://u:p@[::1]:8443/a%5Bb%5D?x=%5Bc%5D#f%5Bd%5D",
),
("[h](https://example.com/a[b])", "<https://example.com/a%5Bb%5D|h>", "https://example.com/a%5Bb%5D"),
# A bracketed host no URL parser accepts stays spelled. Putting brackets
# back there would invent an address the source never wrote, and the Web
# renderer refuses the same repair for the same reason.
("[h](https://[nope]/x)", "<https://%5Bnope%5D/x|h>", "https://%5Bnope%5D/x"),
("[h](https://example.com/p?x=1#f%20g)", "<https://example.com/p?x=1#f%20g|h>", "https://example.com/p?x=1#f%20g"),
# Characters with no meaning to a URI and none to Slack either, but every
# one of them outside the set the consumer leaves standing.
(r"[h](https://example.com/a\\b)", "<https://example.com/a%5Cb|h>", "https://example.com/a%5Cb"),
("[h](https://example.com/q=`x`)", "<https://example.com/q=%60x%60|h>", "https://example.com/q=%60x%60"),
# Non-ASCII is spelled, in the host as much as in the path, because that is
# what the consumer writes. A URL parser reads that host back as the
# punycode it was always going to resolve - a third spelling, and the one
# the connection is actually made to.
(
"[h](https://例子.测试/路径?q=a&b=c)",
"<https://%E4%BE%8B%E5%AD%90.%E6%B5%8B%E8%AF%95/%E8%B7%AF%E5%BE%84?q=a&b=c|h>",
"https://%E4%BE%8B%E5%AD%90.%E6%B5%8B%E8%AF%95/%E8%B7%AF%E5%BE%84?q=a&b=c",
),
# A scheme that is not http, already delivered today.
(
r"[h](mailto:a+b@example.com?subject=a\>b)",
"<mailto:[email protected]?subject=a%3Eb|h>",
"mailto:[email protected]?subject=a%3Eb",
),
)
class TestSlackSpellsTheWholeDestination:
"""The other half of the unit, and the half a broken wrapper takes with it.
``<url|label>`` is built out of ``<``, ``|`` and ``>``, and an address is
allowed to contain all three. One of them in the destination ends the unit
where the author did not: the reader is shown a truncated link followed by
the rest of the address as plain text, and the tap goes somewhere else or
nowhere. Escaped or spelled as a reference, it is the same character by the
time the wrapper is built - so the destination is finished before the
wrapper closes over it, the same as the label.
Two jobs, kept apart. First the address is written the way the Markdown
consumer on the other side writes it - ``spell_uri``, so ``<``, ``>`` and
``|`` leave as ``%3C``, ``%3E`` and ``%7C`` along with spaces, controls,
backslashes, backticks, brackets that are data and a ``%`` that starts no
escape, while ``?#&=`` and an escape already spelled are left standing.
Then Slack's own text-object escaping runs over the result, which is what a
query ``&`` needs and percent-encoding would destroy.
The wire is asserted here, exactly, because it is what Slack receives. That
those bytes and the Web renderer's own href reach one address is a separate
claim, and it is measured rather than asserted: see
``test_the_matrix_arrives_at_the_address_the_web_consumer_resolves``.
"""
def test_every_destination_arrives_as_the_address_it_named(self):
for markdown, wire, address in _DESTINATION_CORPUS:
assert slack(markdown) == wire, markdown
assert slack_reads(wire) == (address, "h"), markdown
def test_the_matrix_arrives_at_the_address_the_web_consumer_resolves(self):
"""Half of a relationship, and the half this language can measure.
Listing the Slack spelling beside the Web spelling and calling them
equivalent proves nothing about where either one goes. So one bounded
matrix is shared with the Web suite: this side asserts what the real
adapter decodes out of the wrapper, that side renders the same source
through the real Markdown component and asserts a browser URL parser
resolves its anchor and this address to one URL. Either side drifting
fails one of the two.
"""
for case in _DESTINATION_MATRIX:
wire = slack(case["markdown"])
assert slack_reads(wire)[0] == case["address"], case["markdown"]
def test_a_destination_that_breaks_out_leaves_more_than_one_unit_behind(self):
"""Why the wire is asserted and not just the decoding: the failure shape.
A raw ``>`` in the destination does not corrupt a field, it ends the
wrapper - and what follows is text, not a link. Reading the fields of
whatever parsed first would find a plausible ``(destination, label)``
and say nothing was wrong, so the shape of the wire is checked first.
"""
for markdown, _, _ in _DESTINATION_CORPUS:
rendered = slack(markdown)
assert (rendered.count("<"), rendered.count("|"), rendered.count(">")) == (
1,
1,
1,
), markdown
assert len(all_links(rendered)) == 1, markdown
broken = "<https://example.com/a>b|h>"
assert all_links(broken) == [("https://example.com/a", "")]
assert all_links(slack_decodes(broken)) == [("https://example.com/a", "")]
def test_an_empty_label_leaves_the_bare_wrapper_intact(self):
"""No label, so no ``|`` - and the destination is still finished first."""
wire = slack(r"[](https://example.com/a\>b)")
assert wire == "<https://example.com/a%3Eb>"
assert slack_reads(wire) == ("https://example.com/a%3Eb", "")
def test_two_links_side_by_side_stay_two_links(self):
"""Composition: each unit closes over its own address, and only its own."""
wire = slack(r"[a](https://example.com/1\>x) [b](https://example.com/2|y)")
assert wire == "<https://example.com/1%3Ex|a> <https://example.com/2%7Cy|b>"
assert all_links(wire) == [
("https://example.com/1%3Ex", "a"),
("https://example.com/2%7Cy", "b"),
]
# The brackets half of the same table, measured on this side. See
# tests/fixtures/citation_authority_matrix.json: the citation suite asserts
# ``safe_url`` and the Web suite asserts the anchor, so a repair the three
# surfaces agree on still has to be agreed on three times.
_AUTHORITY_MATRIX = json.loads(
(Path(__file__).resolve().parent / "fixtures/citation_authority_matrix.json").read_text(
encoding="utf-8"
)
)
class TestSlackKeepsOnlyTheBracketsThatWereAHost:
"""A bracket is a host's syntax or it is data, and spelling loses which.
``spell_uri`` writes every bracket it is given as ``%5B``/``%5D``, so by the
time the wrapper is built ``https://[::1]/admin`` and
``https://%5B::1%5D/admin`` are one string. A pass that then restores
brackets to whatever looks like an authority gives the second destination a
loopback address its author never wrote - and a tap on it opens the
reader's own machine. So the destination is qualified before it is spelled,
and only a literal authority a URL parser accepts gets its brackets back.
"""
def test_every_authority_arrives_as_the_destination_it_named(self):
for case in _AUTHORITY_MATRIX["cases"]:
wire = slack(case["markdown"])
assert len(all_links(wire)) == 1, case["markdown"]
assert slack_reads(wire) == (case["slack"], "h"), case["why"]
def test_two_destinations_that_spell_alike_are_still_two_destinations(self):
"""Per occurrence, not per spelling.
Both links reach ``https://%5B::1%5D/admin`` if the answer is looked up
by the finished text, so the body carries one of each and the pair has
to come back apart.
"""
pair = _AUTHORITY_MATRIX["per_occurrence"]
wire = slack(pair["markdown"])
assert [link[0] for link in all_links(wire)] == pair["slack"]
assert [link[1] for link in all_links(wire)] == [
link["label"] for link in pair["links"]
]
class TestSlackTellsAReferenceFromItsSpelling:
"""Two labels, one wire: a reference and the text that spells one.
``&`` in a Markdown label is the character ``&``; ``\\&`` is the
five characters ``&``. The escape is what tells them apart, and it is
held behind a placeholder for the whole platform pass - so the distinction
is still there to use, right up to the moment the label is serialized. A
pass that restores the escape first and then guesses from the finished
string cannot tell the two apart any more, and picked one meaning for both.
A real citation writes both spellings: ``https://a%26amp%3B.example/x`` is
a host that literally contains ``&``, and a reader shown ``a&.example``
is being told the page is on a different site.
"""
def test_every_reference_is_the_character_commonmark_shows(self):
"""The oracle, paired: each reference and the escaped text that spells it."""
for label in (
"a&b",
r"a\&b",
"a<b",
r"a\<b",
"a>b",
r"a\>b",
"a©b",
r"a\©b",
"a&b",
r"a\&b",
"a<b",
"a©b",
"a&nope;b",
):
assert slack_decodes(slack_label(f"[{label}]({PLAIN})")) == commonmark_shows(label), label
def test_the_two_spellings_do_not_arrive_as_the_same_wire_string(self):
"""Encoding is not the same as resolving, and the wire has to say which."""
assert slack_label(f"[a&b]({PLAIN})") == "a&b"
assert slack_label(rf"[a\&b]({PLAIN})") == "a&amp;b"
assert slack_label(f"[a<b]({PLAIN})") == "a<b"
assert slack_label(rf"[a\<b]({PLAIN})") == "a&lt;b"
def test_a_reference_slack_cannot_resolve_is_resolved_here(self):
"""``©`` is a character to a Markdown reader, whatever Slack knows.
Slack resolves only three references, so the character has to arrive
already resolved. The escaped spelling is the literal-text case, and it
arrives as the six characters it names.
"""
assert slack_label(f"[a©b]({PLAIN})") == "a©b"
assert slack_label(rf"[a\©b]({PLAIN})") == "a&copy;b"
def test_a_reference_is_resolved_once_and_not_again(self):
"""``&copy;`` is ``©`` to a reader, not ``©``."""
assert slack_label(f"[a&copy;b]({PLAIN})") == "a&copy;b"
assert slack_decodes(slack_label(f"[a&copy;b]({PLAIN})")) == "a©b"
def test_a_reference_inside_a_code_literal_stays_written_out(self):
"""Code is a literal, so its ``&`` is five characters the reader sees."""
assert slack_label(f"[`&`]({PLAIN})") == "`&amp;`"
assert slack_decodes(slack_label(f"[`&`]({PLAIN})")) == "`&`"
def test_a_citation_host_that_spells_a_reference_reaches_slack_whole(self):
"""The producer end: a real citation of a host that contains ``&``.
The label a citation writes is escaped character by character, so the
host's own ``&`` is literal text. The address arrives from citation
ingestion already percent-encoded and holds no character Slack reads,
so here the wire and the address a tap opens are the same string - and
both are asserted, because a decoder permissive enough to agree with a
broken wire would have agreed with this one too.
"""
for url, shown in (
("https://a%26amp%3B.example/x", "a&.example"),
("https://a%26lt%3B.example/x", "a<.example"),
("https://a%26copy%3B.example/x", "a©.example"),
):
wire = slack(citation_body(url))
destination, label = one_link(wire)
assert destination == url, url
assert slack_reads(wire) == (url, shown), url
def test_every_name_the_parser_knows_reaches_slack_as_that_character(self):
"""The whole entity table, against the parser that owns it.
Which names are references at all, and what each one stands for, is one
table with two thousand rows; a helper that reconstructs it from a
pattern of its own is a second grammar that will disagree somewhere. So
the oracle is the real inline parser, asked for every name it knows.
"""
for name in entities:
label = f"a&{name};b"
assert slack_decodes(slack_label(f"[{label}]({PLAIN})")) == one_line(
commonmark_shows(label)
), label
def test_a_numeric_reference_is_read_at_the_width_the_grammar_allows(self):
"""Seven decimal digits and six hexadecimal ones - the eighth is text.
A decoding utility of the same package accepts eight of either. It is
not the inline rule, and a label is what the inline rule reads: padded
past the limit, ``&`` is the thirteen characters a reader
sees, not ``&``.
"""
widths = [f"a&#{'0' * pad}38;b" for pad in range(0, 7)]
widths += [f"a&#x{'0' * pad}26;b" for pad in range(0, 6)]
for label in widths:
assert slack_decodes(slack_label(f"[{label}]({PLAIN})")) == commonmark_shows(
label
), label
assert slack_label(f"[a&b]({PLAIN})") == "a&#00000038;b"
assert slack_label(f"[a&b]({PLAIN})") == "a&#x0000026;b"
def test_a_code_point_the_parser_cannot_encode_is_the_character_it_substitutes(self):
"""Recognized is not the same as valid, and the rule answers both.
``�``, a C1 control, a surrogate half and anything past the last
code point are all references the grammar recognizes; CommonMark shows
U+FFFD for each. Leaving the spelling standing would show the reader a
different thing than every other Markdown surface does.
"""
for label in (
"a�b",
"a€b",
"a�b",
"a�b",
"a�b",
"a�b",
"ab",
"a�b",
):
assert slack_decodes(slack_label(f"[{label}]({PLAIN})")) == commonmark_shows(
label
), label
assert slack_label(f"[a�b]({PLAIN})") == "a\ufffdb"
def test_a_malformed_or_unknown_reference_stays_the_text_it_is(self):
for label in ("a&#;b", "a&#x;b", "a&#xZZ;b", "a&nope;b", "a& b"):
assert slack_decodes(slack_label(f"[{label}]({PLAIN})")) == commonmark_shows(
label
), label
def test_an_escaped_numeric_reference_is_literal_text(self):
"""The escape still decides, and a numeric spelling is no different."""
assert slack_label(rf"[a\&b]({PLAIN})") == "a&#38;b"
assert slack_label(rf"[a\�b]({PLAIN})") == "a&#0;b"
assert slack_label(rf"[a\&b]({PLAIN})") == "a&#00000038;b"
def test_a_nested_looking_numeric_reference_is_resolved_once(self):
assert slack_decodes(slack_label(f"[a&#38;b]({PLAIN})")) == "a&b"
def test_a_numeric_reference_inside_a_code_literal_stays_written_out(self):
assert slack_decodes(slack_label(f"[`�`]({PLAIN})")) == "`�`"
def test_emphasis_and_a_reference_in_one_label_both_survive(self):
assert slack(f"[**bold** & ©]({PLAIN})") == f"<{PLAIN}|*bold* & ©>"
class TestALabelIsOneLineHoweverTheBreakIsSpelled:
"""``<url|label>`` ends at a newline, and Markdown spells one three ways.
A source line break was already folded to a space before the platform pass,
which was the whole of the invariant while a break could only be written as
one. A character reference writes one too - `` ``, ``
``,
``
``, `` `` - and those become characters only after the
reference is resolved. So the label is measured for being one line once its
characters are decided, not before they are.
"""
def test_a_source_line_break_is_still_a_space(self):
"""The case that already held, restated where the policy now ends."""
assert slack(f"[the\nguide]({PLAIN})") == f"<{PLAIN}|the guide>"
assert slack(f"[the\r\nguide]({PLAIN})") == f"<{PLAIN}|the guide>"
def test_a_reference_that_spells_a_break_is_folded_the_same_way(self):
for label in ("a b", "a
b", "a
b", "a b", "a b"):
assert slack(f"[{label}]({PLAIN})") == f"<{PLAIN}|a b>", label
def test_an_escaped_break_reference_is_literal_text_with_no_break_to_fold(self):
assert slack(rf"[a\
b]({PLAIN})") == f"<{PLAIN}|a&NewLine;b>"
assert slack(rf"[a\ b]({PLAIN})") == f"<{PLAIN}|a&#10;b>"
def test_the_destination_is_not_touched_by_the_fold(self):
"""A percent-encoded break in the address is address, not label."""
url = "https://example.com/a%0Ab"
assert slack(f"[x]({url})") == f"<{url}|x>"
def test_text_outside_a_link_keeps_the_lines_it_had(self):
assert slack("first\nsecond") == converter_alone("first\nsecond")
class _TextOnlyClient:
"""A client that can only send text, and whose sends all succeed.
The success matters: a fragment delivered here is reported as a delivered
result, so nothing downstream would ever fall back to sending the whole
thing another way.
"""
def __init__(self):
self.sent: list[str] = []
self._next_id = 1
def should_use_thread_for_reply(self):
return False
async def send_message(self, context, text, parse_mode=None, reply_to=None, subtext=None):
self.sent.append(text)
message_id = f"msg-{self._next_id}"
self._next_id += 1
return message_id
class _DiscordLikeClient(_TextOnlyClient):
"""Adds the native Markdown upload Discord implements."""
def __init__(self, *, upload_id: str = "discord-file-1"):
super().__init__()
self.uploads: list[tuple[str, str]] = []
self._upload_id = upload_id
async def upload_markdown(self, context, title, content, filetype="markdown"):
self.uploads.append((title, content))
return self._upload_id
def discord_context() -> MessageContext:
return MessageContext(user_id="U1", channel_id="C1", platform="discord")
def wechat_context() -> MessageContext:
# ``context_token`` is spelled here so the adapter resolves the recipient
# from the message instead of reading its on-disk token cache.
return MessageContext(
user_id="wechat-user",
channel_id="wechat-user",
platform="wechat",
platform_specific={"context_token": "ctx-token"},
)
def dispatcher_for(platform: str, client) -> ConsolidatedMessageDispatcher:
return ConsolidatedMessageDispatcher(_StubController(platform=platform, im_client=client))
class TestTheSplitKeepsALinkWhole:
"""Where the shared splitter is allowed to draw a boundary.
A result longer than one message is cut into several, and the cut used to
be taken at whitespace with no idea what it was cutting. A boundary drawn
through ``[label](url)`` delivers neither half: both chunks send fine, so
nothing falls back, and the reader is shown raw Markdown and no way to
reach the source.
"""
def test_a_fitting_link_crossing_a_boundary_moves_whole(self):
link = f"[a b]({PLAIN})"
text = "x" * 1890 + link + " tail"
chunks = dispatcher_for("discord", _DiscordLikeClient())._split_result_text(text, 1900)
assert "".join(chunks) == text
assert sum(chunk.count(link) for chunk in chunks) == 1
assert all(len(chunk) <= 1900 for chunk in chunks)
def test_adjacent_links_are_each_kept_whole(self):
first, second = f"[a]({PLAIN})", f"[b c]({STAR})"
text = "x" * (1900 - len(first) - 3) + first + second + " tail"
chunks = dispatcher_for("discord", _DiscordLikeClient())._split_result_text(text, 1900)
assert "".join(chunks) == text
assert [chunk.count(first) for chunk in chunks] == [1, 0]
assert [chunk.count(second) for chunk in chunks] == [0, 1]
assert all(len(chunk) <= 1900 for chunk in chunks)
def test_a_multibyte_budget_keeps_a_link_whole_and_within_the_limit(self):
"""WeChat counts bytes, so the boundary moves on a different ruler."""
link = f"[文 档]({PLAIN})"
text = "字" * 629 + link + "字" * 100
chunks = dispatcher_for("wechat", _DiscordLikeClient())._split_result_text_by_bytes(text, 1900)
assert "".join(chunks) == text
assert sum(chunk.count(link) for chunk in chunks) == 1
assert all(len(chunk.encode("utf-8")) <= 1900 for chunk in chunks)
def test_a_link_that_fits_the_message_is_not_called_unsplittable(self):
"""Capacity is the message, not the last space before the link ends.
The preferred cut is whitespace, and here every space inside the
budget is inside the label. Pulling the boundary back to the link's
start lands on ``0`` - the link begins the chunk - which said nothing
about whether the link fits: it fits with 426 characters to spare, and
the cut belongs after it.
"""
dispatcher = dispatcher_for("discord", _DiscordLikeClient())
link = "[" + ("word " * 220) + "](https://example.com/" + ("x" * 350) + ")"
text = link + "x" * 1000
assert (len(link), len(text)) == (1474, 2474)
for plan in (
dispatcher._plan_result_split(text, 1900),
dispatcher._plan_result_split_by_bytes(text, 1900),
):
assert plan.links_whole is True
assert "".join(plan.chunks) == text
assert len(plan.chunks) > 1
assert sum(chunk.count(link) for chunk in plan.chunks) == 1
assert all(len(chunk) <= 1900 for chunk in plan.chunks)
assert all(len(chunk.encode("utf-8")) <= 1900 for chunk in plan.chunks)
def test_a_multibyte_link_that_fits_is_measured_on_its_own_ruler(self):
"""Same shape on the byte budget, where capacity is not the character count."""
dispatcher = dispatcher_for("wechat", _DiscordLikeClient())
link = f"[{'文 档 ' * 100}]({PLAIN})"
text = link + "字" * 1000
plan = dispatcher._plan_result_split_by_bytes(text, 1900)
assert plan.links_whole is True
assert "".join(plan.chunks) == text
assert sum(chunk.count(link) for chunk in plan.chunks) == 1
assert all(len(chunk.encode("utf-8")) <= 1900 for chunk in plan.chunks)
def test_two_links_at_the_head_of_a_chunk_are_both_kept_whole(self):
"""The search resumes past the first link, so the second is seen too.
The only whitespace inside the budget is inside the first link's
label. Once that link is allowed to stay, the boundary has to be
looked for again beyond it - and what it finds there is the second
link, which must not be cut either.
"""
dispatcher = dispatcher_for("discord", _DiscordLikeClient())
first = "[" + ("word " * 220) + "](https://example.com/" + ("x" * 350) + ")"
second = f"[ab]({STAR})"
text = first + second + "x" * 1000
plan = dispatcher._plan_result_split(text, 1900)
assert plan.links_whole is True
assert "".join(plan.chunks) == text
assert sum(chunk.count(first) for chunk in plan.chunks) == 1
assert sum(chunk.count(second) for chunk in plan.chunks) == 1
assert all(len(chunk) <= 1900 for chunk in plan.chunks)
def test_a_scan_that_fails_does_not_certify_a_plan(self):
"""No answer is not the answer "there are no links".
The enumeration is the only thing that knows where a link is. When it
cannot run, the plan still has to make progress - the text is longer
than one message either way - but it may not claim the boundaries it
drew kept anything whole.
"""
dispatcher = dispatcher_for("discord", _DiscordLikeClient())
text = "word " * 800
with mock.patch(
"core.message_dispatcher.inline_links", side_effect=RuntimeError("scanner down")
):
plan = dispatcher._plan_result_split(text, 1900)
by_bytes = dispatcher._plan_result_split_by_bytes(text, 1900)
assert plan.links_whole is False
assert by_bytes.links_whole is False
assert "".join(plan.chunks) == text
assert all(len(chunk) <= 1900 for chunk in plan.chunks)
def test_a_link_longer_than_one_message_leaves_no_whole_boundary(self):
"""No cut keeps this one intact, and the plan says so instead of pretending."""
dispatcher = dispatcher_for("discord", _DiscordLikeClient())
oversized = f"[{'label ' * 400}]({PLAIN})"
assert dispatcher._plan_result_split(f"lead\n\n{oversized}", 1900).links_whole is False
assert dispatcher._plan_result_split_by_bytes(f"lead\n\n{oversized}", 1900).links_whole is False
assert dispatcher._plan_result_split("x" * 4000, 1900).links_whole is True
class TestALinkTooLongToSplitIsDeliveredWhole(unittest.IsolatedAsyncioTestCase):
"""The consumer end: what the platform is actually asked to send.
Persistence is stubbed out here - the workbench row is a different contract
and writing one would reach real local state - so what remains is exactly
the delivery the reader gets.
"""
async def test_discord_attaches_the_whole_result_instead_of_fragmenting_a_link(self):
client = _DiscordLikeClient()
dispatcher = dispatcher_for("discord", client)
text = f"Source:\n\n[{'label ' * 400}]({PLAIN})"
with mock.patch("core.message_dispatcher.persist_agent_message"):
message_id = await dispatcher.emit_agent_message(discord_context(), "result", text)
assert client.uploads == [("result.md", text)]
assert message_id == "discord-file-1"
# Nothing of the result went out as text: no fragment, and no claim of
# a delivery the reader could not follow.
assert client.sent == [dispatcher._t("info.resultDeliveredAsAttachment")]
async def test_discord_still_splits_when_every_link_fits(self):
client = _DiscordLikeClient()
dispatcher = dispatcher_for("discord", client)
link = f"[a b]({PLAIN})"
text = "x" * 1890 + link + " tail " + "y" * 1900
with mock.patch("core.message_dispatcher.persist_agent_message"):
message_id = await dispatcher.emit_agent_message(discord_context(), "result", text)
assert message_id == "msg-1"
assert client.uploads == []
assert "".join(client.sent) == text
assert sum(sent.count(link) for sent in client.sent) == 1
async def test_a_fitting_link_is_delivered_as_text_by_a_client_with_no_file_route(self):
"""The case that used to end in a failure notice with nothing sent.
A text-only client has no attachment to fall back to, so a plan that
wrongly reports the link unsplittable costs the reader the whole
message. There is a legal boundary here, and taking it delivers
everything as text.
"""
client = _TextOnlyClient()
dispatcher = dispatcher_for("discord", client)
link = "[" + ("word " * 220) + "](https://example.com/" + ("x" * 350) + ")"
text = link + "x" * 1000
with mock.patch("core.message_dispatcher.persist_agent_message"):
message_id = await dispatcher.emit_agent_message(discord_context(), "result", text)
assert message_id == "msg-1"
assert "".join(client.sent) == text
assert sum(sent.count(link) for sent in client.sent) == 1
assert dispatcher._t("error.resultDeliveryFailed") not in client.sent
assert dispatcher._t("info.resultDeliveredAsAttachment") not in client.sent
async def test_a_fitting_link_does_not_cost_an_attachment(self):
"""A client that CAN attach must not be asked to, for a link that fits."""
client = _DiscordLikeClient()
dispatcher = dispatcher_for("discord", client)
link = "[" + ("word " * 220) + "](https://example.com/" + ("x" * 350) + ")"
text = link + "x" * 1000
with mock.patch("core.message_dispatcher.persist_agent_message"):
message_id = await dispatcher.emit_agent_message(discord_context(), "result", text)
assert message_id == "msg-1"
assert client.uploads == []
assert "".join(client.sent) == text
async def test_a_platform_with_no_file_route_reports_no_delivery(self):
"""Honest failure beats a half link the user is told arrived."""
client = _TextOnlyClient()
dispatcher = dispatcher_for("discord", client)
text = f"Source:\n\n[{'label ' * 400}]({PLAIN})"
with mock.patch("core.message_dispatcher.persist_agent_message"):
message_id = await dispatcher.emit_agent_message(discord_context(), "result", text)
assert message_id is None
assert client.sent == [dispatcher._t("error.resultDeliveryFailed")]
def wechat_dispatcher() -> tuple[WeChatBot, ConsolidatedMessageDispatcher]:
bot = WeChatBot(WeChatConfig(bot_token="token"))
return bot, dispatcher_for("wechat", bot)
def text_items(calls: list) -> list[str]:
return [
item["text_item"]["text"]
for call in calls
for item in call
if item.get("type") == 1
]
async def deliver_through_wechat(
dispatcher,
text: str,
*,
cdn_meta,
message_type: str = "result",
citations=None,
):
"""Emit *text* through the real adapter with only its network replaced."""
calls: list = []
uploaded: dict = {}
async def fake_upload_to_cdn(base_url, token, cdn_base_url, user_id, file_path, proxy=None):
uploaded["path"] = file_path
uploaded["content"] = Path(file_path).read_text(encoding="utf-8")
return cdn_meta
async def fake_send_message(base_url, token, to_user_id, context_token, item_list, proxy=None):
calls.append(item_list)
return {"message_id": f"wc-{len(calls)}"}
with (
mock.patch.object(wechat_cdn, "upload_file_to_cdn", fake_upload_to_cdn),
mock.patch.object(wechat_api, "send_message", fake_send_message),
mock.patch("core.message_dispatcher.persist_agent_message"),
):
message_id = await dispatcher.emit_agent_message(
wechat_context(), message_type, text, citations=citations
)
return message_id, calls, uploaded
class TestTheWeChatAdapterCarriesTheWholeResult(unittest.IsolatedAsyncioTestCase):
"""The real adapter, with only its network calls replaced.
WeChat inherits ``upload_markdown`` from ``BaseIMClient`` without
implementing it - it raises - and implements ``upload_file_from_path``
instead. A fallback that only knows the first name has no route here, which
is exactly how a link too long to split used to go out in halves.
"""
async def test_the_whole_result_rides_the_file_upload_the_adapter_has(self):
bot, dispatcher = wechat_dispatcher()
text = f"来源:\n\n[{'标签' * 600}]({PLAIN})"
message_id, calls, uploaded = await deliver_through_wechat(
dispatcher,
text,
cdn_meta={"encrypt_query_param": "q", "aes_key": "k", "file_size": 12, "file_id": "wc-file-1"},
)
assert message_id == "wc-file-1"
assert uploaded["content"] == text
assert Path(uploaded["path"]).name == "result.md"
# The staging file is the transport's, not the machine's: it is gone
# once the upload returns.
assert not Path(uploaded["path"]).exists()
# One file item, and the only text sent is the notice - no fragment of
# the link was delivered before it.
assert [item["type"] for call in calls for item in call].count(4) == 1
assert text_items(calls) == [
bot.format_markdown(dispatcher._t("info.resultDeliveredAsAttachment"))
]
async def test_a_failed_upload_is_not_reported_as_a_delivery(self):
"""The CDN refuses, so ``upload_file_from_path`` answers with an empty id."""
bot, dispatcher = wechat_dispatcher()
text = f"来源:\n\n[{'标签' * 600}]({PLAIN})"
message_id, calls, uploaded = await deliver_through_wechat(dispatcher, text, cdn_meta=None)
assert message_id is None
assert not Path(uploaded["path"]).exists()
assert text_items(calls) == [
bot.format_markdown(dispatcher._t("error.resultDeliveryFailed"))
]
async def test_a_result_whose_links_all_fit_is_still_split_as_before(self):
"""Still split, and the chunk boundary now falls outside the link.
WeChat renders the unit its own way - ``label (url)`` - so what has to
survive the split is that rendering, whole and in one message.
"""
bot, dispatcher = wechat_dispatcher()
text = "字" * 629 + f"[文 档]({PLAIN})" + "字" * 400
message_id, calls, uploaded = await deliver_through_wechat(
dispatcher,
text,
cdn_meta={"file_id": "wc-file-1"},
)
assert message_id == "wc-1"
assert uploaded == {}
chunks = text_items(calls)
assert len(chunks) > 1
assert sum(chunk.count(bot.format_markdown(f"[文 档]({PLAIN})")) for chunk in chunks) == 1
assert sum(chunk.count("字") for chunk in chunks) == 1029
assert all(len(chunk.encode("utf-8")) <= 1900 for chunk in chunks)
class TestTheLogPathCarriesTheWholeMessage(unittest.IsolatedAsyncioTestCase):
"""The plan's other consumer, on the path an intermediate message takes.
A WeChat message cannot be edited, so an assistant message on the way to a
result is delivered as its own sends rather than by editing one bubble.
That path splits with the same shared planner and used to read only the
chunks out of it - so the link-integrity answer the planner had already
computed was discarded, and a citation whose address outran one message
went out in pieces that all sent successfully.
This is the same narrow whole-document route the result path uses, taken
before any fragment. Nothing else about an intermediate message changes:
it settles no turn, signals no result, and writes no second transcript row.
"""
async def test_an_unsplittable_citation_rides_the_file_instead_of_fragments(self):
bot, dispatcher = wechat_dispatcher()
url = "https://example.com/" + ("x" * 2000)
text, bundle = register_citations(
f"Source: {_START}cite{_SEP}turn0search0{_END}",
{"turn0search0": CitationSource(ref_id="turn0search0", url=url)},
unresolved_label="(source unavailable)",
)
message_id, calls, uploaded = await deliver_through_wechat(
dispatcher,
text,
cdn_meta={"file_id": "wc-file-1"},
message_type="assistant",
citations=bundle,
)
assert message_id == "wc-file-1"
# The document carries the whole address the fragments cut in half.
assert url in uploaded["content"]
assert Path(uploaded["path"]).name == "result.md"
assert not Path(uploaded["path"]).exists()
assert [item["type"] for call in calls for item in call].count(4) == 1
# An intermediate message has no notice of its own, and no fragment of
# it was sent before the document.
assert text_items(calls) == []
async def test_a_failed_upload_is_not_reported_as_a_log_delivery(self):
bot, dispatcher = wechat_dispatcher()
url = "https://example.com/" + ("x" * 2000)
text, bundle = register_citations(
f"Source: {_START}cite{_SEP}turn0search0{_END}",
{"turn0search0": CitationSource(ref_id="turn0search0", url=url)},
unresolved_label="(source unavailable)",
)
message_id, calls, uploaded = await deliver_through_wechat(
dispatcher,
text,
cdn_meta=None,
message_type="assistant",
citations=bundle,
)
assert message_id is None
assert not Path(uploaded["path"]).exists()
assert text_items(calls) == []
async def test_a_log_message_whose_links_fit_is_still_split_as_before(self):
bot, dispatcher = wechat_dispatcher()
text = "字" * 629 + f"[文 档]({PLAIN})" + "字" * 400
message_id, calls, uploaded = await deliver_through_wechat(
dispatcher,
text,
cdn_meta={"file_id": "wc-file-1"},
message_type="assistant",
)
assert message_id == "wc-1"
assert uploaded == {}
chunks = text_items(calls)
assert len(chunks) > 1
assert sum(chunk.count(bot.format_markdown(f"[文 档]({PLAIN})")) for chunk in chunks) == 1
assert sum(chunk.count("字") for chunk in chunks) == 1029
assert all(len(chunk.encode("utf-8")) <= 1900 for chunk in chunks)