nandi/frqpublic Fork 0
284b59c6810a1b2abace25c071e9d166cd0dafc9
Commits
Clone
git clone https://git.rickub.com/nandi/frq.git
git clone ssh://git@rickub.com/nandi/frq.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

ttextruns.nim · 72 lines · 2.7 KBNim Blame HistoryRaw
The chat screen, in Nim 87a28cc nandi 18h ago1## Link detection in message text — the part of the chat screen that is pure
2## string work, and the part most worth pinning down.
3
4import std/[sequtils, unittest]
5import frq/textruns
6
7proc kinds(s: string): seq[RunKind] = textRuns(s).mapIt(it.kind)
8proc values(s: string): seq[string] = textRuns(s).mapIt(it.value)
9
10suite "textRuns":
11 test "plain text is one run":
12 check kinds("hello there") == @[rkText]
13 check values("hello there") == @["hello there"]
14
15 test "a bare URL is one link":
16 check kinds("https://example.com") == @[rkLink]
17
18 test "text around a link":
19 check kinds("see https://example.com now") == @[rkText, rkLink, rkText]
20 check values("see https://example.com now") ==
21 @["see ", "https://example.com", " now"]
22
23 test "two links in one line":
24 check kinds("a http://x.com b https://y.com") ==
25 @[rkText, rkLink, rkText, rkLink]
26
27 test "http as well as https":
28 check kinds("http://example.com") == @[rkLink]
29
30 test "the message's own ends are trimmed":
31 check values(" hello ") == @["hello"]
32
33 test "but spaces between words are somebody's typing":
34 check values("a b") == @["a b"]
35
36 test "empty text is no runs":
37 check textRuns("").len == 0
38
39 test "whitespace-only text is no runs, not an empty one":
40 check textRuns(" ").len == 0
41
42suite "trimTrailingPunctuation":
43 test "a sentence's full stop is not part of the URL":
44 check trimTrailingPunctuation("https://x.com.") == "https://x.com"
45 check trimTrailingPunctuation("https://x.com,") == "https://x.com"
46 check trimTrailingPunctuation("https://x.com?") == "https://x.com"
47
48 test "several at once":
49 check trimTrailingPunctuation("https://x.com...") == "https://x.com"
50
51 test "a closing bracket goes when the URL opened none":
52 check trimTrailingPunctuation("https://x.com)") == "https://x.com"
53
54 test "but stays when it did — a wikipedia path keeps its brackets":
55 check trimTrailingPunctuation("https://en.wikipedia.org/wiki/Foo_(bar)") ==
56 "https://en.wikipedia.org/wiki/Foo_(bar)"
57
58 test "the trimmed punctuation comes back as text":
59 # Not dropped: somebody typed it.
60 check values("see https://x.com. ok") == @["see ", "https://x.com", ". ok"]
61
62suite "firstImageUrl":
63 test "a png link":
64 check firstImageUrl("look https://x.com/a.png yes") == "https://x.com/a.png"
65 test "a freeq media link":
66 check firstImageUrl("https://irc.freeq.at/api/v1/media/a/b/picture.png") ==
67 "https://irc.freeq.at/api/v1/media/a/b/picture.png"
68 test "no picture at all":
69 check firstImageUrl("just words and https://x.com/page") == ""
70 test "the first one wins":
71 check firstImageUrl("https://a.com/1.png https://b.com/2.png") ==
72 "https://a.com/1.png"