about summary refs log tree commit diff
path: root/__tests__
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2023-08-16 10:22:50 -0700
committerGitHub <noreply@github.com>2023-08-16 10:22:50 -0700
commit819340dd3c34e89e8cd7126c6f1172aba7a8ebec (patch)
tree91a1a4e3f45d7a0e7c32f530319c6349c778ccfc /__tests__
parent5379561934f6249fbbecf33ed0cd10d2d30128f0 (diff)
downloadvoidsky-819340dd3c34e89e8cd7126c6f1172aba7a8ebec.tar.zst
Shorten links in composer to reduce char usage (#1188)
* Modify toShortUrl() to always include the full domain

* Shorten links in the composer to save on characters

* Apply some limits to the link card suggester
Diffstat (limited to '__tests__')
-rw-r--r--__tests__/lib/string.test.ts58
1 files changed, 57 insertions, 1 deletions
diff --git a/__tests__/lib/string.test.ts b/__tests__/lib/string.test.ts
index 936708cf2..726c9be94 100644
--- a/__tests__/lib/string.test.ts
+++ b/__tests__/lib/string.test.ts
@@ -1,3 +1,4 @@
+import {RichText} from '@atproto/api'
 import {
   getYoutubeVideoId,
   makeRecordUri,
@@ -8,6 +9,7 @@ import {
 import {pluralize, enforceLen} from '../../src/lib/strings/helpers'
 import {ago} from '../../src/lib/strings/time'
 import {detectLinkables} from '../../src/lib/strings/rich-text-detection'
+import {shortenLinks} from '../../src/lib/strings/rich-text-manip'
 import {makeValidHandle, createFullHandle} from '../../src/lib/strings/handles'
 import {cleanError} from '../../src/lib/strings/errors'
 
@@ -296,11 +298,15 @@ describe('toShortUrl', () => {
     'https://bsky.app',
     'https://bsky.app/3jk7x4irgv52r',
     'https://bsky.app/3jk7x4irgv52r2313y182h9',
+    'https://very-long-domain-name.com/foo',
+    'https://very-long-domain-name.com/foo?bar=baz#andsomemore',
   ]
   const outputs = [
     'bsky.app',
     'bsky.app/3jk7x4irgv52r',
-    'bsky.app/3jk7x4irgv52r2313y...',
+    'bsky.app/3jk7x4irgv52...',
+    'very-long-domain-name.com/foo',
+    'very-long-domain-name.com/foo?bar=baz#...',
   ]
 
   it('shortens the url', () => {
@@ -352,3 +358,53 @@ describe('getYoutubeVideoId', () => {
     expect(getYoutubeVideoId('https://youtu.be/videoId')).toBe('videoId')
   })
 })
+
+describe('shortenLinks', () => {
+  const inputs = [
+    'start https://middle.com/foo/bar?baz=bux#hash end',
+    'https://start.com/foo/bar?baz=bux#hash middle end',
+    'start middle https://end.com/foo/bar?baz=bux#hash',
+    'https://newline1.com/very/long/url/here\nhttps://newline2.com/very/long/url/here',
+    'Classic article https://socket3.wordpress.com/2018/02/03/designing-windows-95s-user-interface/',
+  ]
+  const outputs = [
+    [
+      'start middle.com/foo/bar?baz=... end',
+      ['https://middle.com/foo/bar?baz=bux#hash'],
+    ],
+    [
+      'start.com/foo/bar?baz=... middle end',
+      ['https://start.com/foo/bar?baz=bux#hash'],
+    ],
+    [
+      'start middle end.com/foo/bar?baz=...',
+      ['https://end.com/foo/bar?baz=bux#hash'],
+    ],
+    [
+      'newline1.com/very/long/ur...\nnewline2.com/very/long/ur...',
+      [
+        'https://newline1.com/very/long/url/here',
+        'https://newline2.com/very/long/url/here',
+      ],
+    ],
+    [
+      'Classic article socket3.wordpress.com/2018/02/03/d...',
+      [
+        'https://socket3.wordpress.com/2018/02/03/designing-windows-95s-user-interface/',
+      ],
+    ],
+  ]
+  it('correctly shortens rich text while preserving facet URIs', () => {
+    for (let i = 0; i < inputs.length; i++) {
+      const input = inputs[i]
+      const inputRT = new RichText({text: input})
+      inputRT.detectFacetsWithoutResolution()
+      const outputRT = shortenLinks(inputRT)
+      expect(outputRT.text).toEqual(outputs[i][0])
+      expect(outputRT.facets?.length).toEqual(outputs[i][1].length)
+      for (let j = 0; j < outputs[i][1].length; j++) {
+        expect(outputRT.facets![j].features[0].uri).toEqual(outputs[i][1][j])
+      }
+    }
+  })
+})