From f28334739b107f3e9f7b6ca2670778dba280600d Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Wed, 22 Feb 2023 14:23:57 -0600 Subject: Merge main into the Web PR (#230) * Update to RN 71.1.0 (#100) * Update to RN 71 * Adds missing lint plugin * Add missing native changes * Bump @atproto/api@0.0.7 (#112) * Image not loading on swipe (#114) * Adds prefetching to images * Adds image prefetch * bugfix for images not showing on swipe * Fixes prefetch bug * Update src/view/com/util/PostEmbeds.tsx --------- Co-authored-by: Paul Frazee * Fixes to session management (#117) * Update session-management to solve incorrectly dropped sessions * Reset the nav on account switch * Reset the feed on me.load() * Update tests to reflect new account-switching behavior * Increase max image resolutions and sizes (#118) * Slightly increase the hitslop for post controls * Fix character counter color in dark mode * Update login to use new session.create api, which enables email login (close #93) (#119) * Replaces the alert with dropdown for profile image and banner (#123) * replaces the alert with dropdown for profile image and banner * lint * Fix to ordering of images in the embed grid (#121) * Add explicit link-embed controls to the composer (#120) * Add explicit link-embed controls * Update the target rez/size of link embed thumbs * Remove the alert before publishing without a link card * [Draft] Fixes image failing on reupload issue (#128) * Fixes image failing on reupload issue * Use tmp folder instead of documents * lint * Image performance improvements (#126) * Switch out most images for FastImage * Add image loading placeholders * Fix tests * Collection of fixes to list rendering (#127) * Fix bug that caused endless spinners in profile feeds * Bundle fetches of suggested actors into one update * Fixes to suggested follow rendering * Fix missing replacement of flex:1 to height:100 * Fixes to navigation swipes (#129) * Nav swipe: increase the distance traveled in response to gesture movement. This causes swipes to feel faster and more responsive. * Fix: fully clamp the swipe against the edge * Improve the performance of swipes by skipping the interaction manager * Adds dark mode to the edit screen (#130) * Adds dark mode to edit screen * lint * lint * lint * Reduce render cost of post controls and improve perceived responsiveness (#132) * Move post control animations into conditional render and increase perceived responsiveness * Remove log * Adds dark mode to the dropdown (#131) * Adds dark mode to the bottom sheet * Make background button lighter (like before) * lint * Fix bug in lightbox rendering (#133) * Fix layout in onboarding to not overflow the footer * Configure feed FlatList (removeClippedSubviews=true) to improve scroll performance (#136) * Disable like/repost animations to see if theyre causing #135 (#137) * Composer: mention tagging now works in middle of text (close #105) (#139) * Implement account deletion (#141) * Fix photo & camera permission management (#140) * Check photo & camera perms and alert the user if not available (close #64) - Adds perms checks with a prompt to update settings if needed - Moves initial access of photos in the composer so that the initial prompt occurs at an intuitive time. * Add react-native-permissions test mock * Fix issue causing multiple access requests * Use longer var names * Update podfile.lock * Lint fix * Move photo perm request in composer to the gallery btn instead of when the carousel is opened * Adds more tracking all around the app (#142) * Adds more tracking all around the app * more events * lint * using better analytics naming * missed file * more fixes * Calculate image aspect ratio on load (#146) * Calculate image aspect ratio on load * Move aspect ratio bounds to constants * Adds detox testing and instructions (#147) * Adds detox testing and instructions * lint * lint * Error cleanup (close #79) (#148) * Avoid surfacing errors to the user when it's not critical * Remove now-unused GetAssertionsView * Apply cleanError() consistently * Give a better error message for Upstream Failures (http status 502) * Hide errors in notifications because they're not useful * More e2e tests (create account) (#150) * Adds respots under the 'post' tab under profile (#158) * Adds dark mode to delete account screen (#159) * 87 dark mode edit profile (#162) * Adds dark mode to delete account screen * Adds one more missed darkmode * more fixes * Remove fallback gradient on external links without thumbs (#164) * Remove fallback gradient on external links without thumbs * Remove fallback gradient on external links without thumbs in the composer preview * Fix refresh behavior around a series of models (repost, graph, vote) (#163) * Fix refresh behavior around a series of models (repost, graph, vote) * Fix cursor behavior in reposted-by view * Fixes issue where retrying on image upload fails (#166) * Fixes issue where retrying on image upload fails * Lint, longer test time * Longer waitfor time in tests * even longer timeout * longer timeout * missed file * Update src/view/com/composer/ComposePost.tsx Co-authored-by: Paul Frazee * Update src/view/com/composer/ComposePost.tsx Co-authored-by: Paul Frazee --------- Co-authored-by: Paul Frazee * 154 cached image profile (#167) * Fixes issue where retrying on image upload fails * Lint, longer test time * Longer waitfor time in tests * even longer timeout * longer timeout * missed file * Fixes image cache error on second try for profile screen * lint * lint * lint * Refactor session management to use a new "Agent" API (#165) * Add the atp-agent implementation (temporarily in this repo) * Rewrite all session & API management to use the new atp-agent * Update tests for the atp-agent refactor * Refactor management of session-related state. Includes: - More careful management of when state is cleared or fetched - Debug logging to help trace future issues - Clearer APIs overall * Bubble session-expiration events to the user and display a toast to explain * Switch to the new @atproto/api@0.1.0 * Minor aesthetic cleanup in SessionModel * Wire up ReportAccount and ReportPost (#168) * Fixes embeds for youtube channels (#169) * Bump app ios version to 1.1 (needed after app store submission) * Fix potential issues with promise guards when an error occurs (#170) * Refactor models to use bundleAsync and lock regions (#171) * Fix to an edge case with feed re-ordering for threads (#172) * 151 fix youtube channel embed (#173) * Fixes embeds for youtube channels * Tests for youtube extract meta * lint * Add 'doesnt use non-exempt encryption' to ios config * Rework the search UI and add (#174) * Add search tab and move icon to footer * Remove subtitles from view header * Remove unused code * Clean up UI of search screen * Search: give better user feedback to UI state and add a cancel button * Add WhoToFollow section to search * Add a temporary SuggestedPosts solution using the patented 'bsky team algo' * Trigger reload of suggested content in search on open * Wait five min between reloading discovery content * Reduce weight of solid search icon in footer * Fix lint * Fix tests * 151 feat youtube embed iframe (#176) * youtube embed iframe temp commit * Fixes styling and code cleanup * lint * Now clicking between the pause and settings button doesn't trigger the parent * use modest branding (less yt logos) * Stop playing the video once there's a navigation event * Make sure the iframe is unmounted on any navigation event * fixes tests * lint * Add scroll-to-top for all screens (#177) * Adds hardcoded suggested list (#178) * Adds hardcoded suggested list * Update suggested-actors-view to support page sizes smaller than the hardcoded list --------- Co-authored-by: Paul Frazee * more robust centering of the play button (#181) Co-authored-by: Aryan Goharzad * Bundle of UI modifications (#175) * Adjust visual balance of SuggestedPosts and WhoToFollow * Fix bug in the discovery load trigger * Adjust search header aesthetic and have it scroll away * More visual balance tweaks on the search page * Even more visual balance tweaks on the search page * Hide the footer on scroll in search * Ditch the composer prompt buttons in the home feed * Center the view header title * Hide header on scroll on the home feed * Fix e2e tests * Fix home feed positioning (closes #189) (#195) * Fix home feed positioning for floating header * Fix positioning of errors in home feed * Fix lint * Don't show new-content notification for reposts (close #179) (#197) * Show the splash screen during session resumption (close #186) (#199) * Fix to suggested follows: chunk the hardcoded fetches to 25 at a time (close #196) (#198) * UI updates to the floating action button (#201) * Update FAB to use a plus icon and not drop shadow * Update FAB positioning to be more consistent in different shell modes * Animate the FAB's repositioning * Remove the 'loading' placeholder from images as it degraded feed perf (#202) * Remove the 'loading' placeholder from images as it degraded feed perf * Remove references * Fix RN bug that causes home feed not to load more; also fix home feed load view. (#208) RN has a bug where rendering a flatlist with an empty array appears to break its virtual list windowing behaviors. See https://stackoverflow.com/a/67873596 * Only give the loading spinner on the home feed during PTR (#207) (cherry picked from commit b7a5da12fdfacef74873b5cf6d75f20d259bde0e) * Implement our own lifecycle tracking to ensure it never fires while the app is backgrounded (close #193) (#211) * Push notification fixes (#210) * Fix to when screen analytics events are firing * Fix: dont trigger update state when backgrounded * Small fix to notifee API usage * Fix: properly load notification info for push card * Add feedback link to main menu (close #191) (#212) * Add "follows you" information and sync follow state between views (#215) * Bump @atproto/api@0.1.2 and update API usage * Add 'follows you' pill to profile header (close #110) * Add 'follows you' to followers and follows (close #103) * Update reposted-by and liked-by views to use the same components as followers and following * Create a local follows cache MyFollowsModel to keep views in sync (close #205) * Add incremental hydration to the MyFollows model * Fix tests * Update deps * Fix lint * Fix to paginated fetches * Fix reference * Fix potential state-desync issue * Fixes to notifications (#216) * Improve push-notification for follows * Refresh notifications on screen open (close #214) * Avoid showing loader more than needed in post threads * Refactor notification polling to handle view-state more effectively * Delete a bunch of tests taht werent adding value * Remove the accounts integration test; we'll use the e2e test instead * Load latest in notifications when the screen is open rather than full refresh * Randomize hard-coded suggested follows (#226) * Ensure follows are loaded before filtering hardcoded suggestions * Randomize hard-coded suggested profiles (close #219) * Sanitizes posts on publish and render (#217) * Sanatizes posts on publish and render * lint * lint and added sanitize to thread view as well * adjusts indices based on replaced text * Woops, fixes a bug * bugfix + cleanup * comment * lint * move sanitize text to later in the flow * undo changes to compose post * Add RichText library building upon the sanitizePost library method * Add lodash.clonedeep dep * Switch to RichText processing on record load & render * Fix lint --------- Co-authored-by: Paul Frazee * A group of notifications fixes (#227) * Fix: don't group together notifications that can't visually be grouped (close #221) * Mark all notifications read on PTR * Small optimization: useCallback and useMemo in posts feed * Add loading spinner to footer of notifications (close #222) * Fix to scrolling to posts within a thread (#228) * Fix: render the entire thread at start so that scrollToIndex works always (close #270) * Visual fixes to thread 'load more' * A few small perf improvements to thread rendering * Fix lint * 1.2 * Remove unused logger lib * Remove state-mock * Type fixes * Reorganize the folder structure for lib and switch to typescript path aliases * Move build-flags into lib * Move to the state path alias * Add view path alias * Fix lint * iOS build fixes * Wrap analytics in native/web splitter and re-enable in all view code * Add web version of react-native-webview * Add web split for version number * Fix BlurView import for web * Add web split for fastimage * Create web split for permissions lib * Fix for web high priority images --------- Co-authored-by: Aryan Goharzad --- __tests__/lib/__mocks__/youtubeChannelHtml.ts | 63 +++++++++++++ __tests__/lib/async/bundle.test.ts | 47 ++++++++++ __tests__/lib/errors.test.ts | 2 +- __tests__/lib/extractHtmlMeta.test.ts | 16 +++- __tests__/lib/images.test.ts | 29 +++++- __tests__/lib/link-meta.test.ts | 17 +++- __tests__/lib/string.test.ts | 45 ++++++++-- __tests__/lib/strings/mention-manip.test.ts | 84 ++++++++++++++++++ __tests__/lib/strings/rich-text-sanitize.ts | 123 ++++++++++++++++++++++++++ __tests__/lib/strings/rich-text.ts | 123 ++++++++++++++++++++++++++ 10 files changed, 533 insertions(+), 16 deletions(-) create mode 100644 __tests__/lib/__mocks__/youtubeChannelHtml.ts create mode 100644 __tests__/lib/async/bundle.test.ts create mode 100644 __tests__/lib/strings/mention-manip.test.ts create mode 100644 __tests__/lib/strings/rich-text-sanitize.ts create mode 100644 __tests__/lib/strings/rich-text.ts (limited to '__tests__/lib') diff --git a/__tests__/lib/__mocks__/youtubeChannelHtml.ts b/__tests__/lib/__mocks__/youtubeChannelHtml.ts new file mode 100644 index 000000000..cc71995c4 --- /dev/null +++ b/__tests__/lib/__mocks__/youtubeChannelHtml.ts @@ -0,0 +1,63 @@ +export const youtubeChannelHtml = ` + +
AboutPressCopyrightContact usCreatorsAdvertiseDevelopersTermsPrivacyPolicy & SafetyHow YouTube worksTest new features
penguinz0 - YouTube
+ +` diff --git a/__tests__/lib/async/bundle.test.ts b/__tests__/lib/async/bundle.test.ts new file mode 100644 index 000000000..8718f54a6 --- /dev/null +++ b/__tests__/lib/async/bundle.test.ts @@ -0,0 +1,47 @@ +import {bundleAsync} from '../../../src/lib/async/bundle' + +describe('bundle', () => { + it('bundles multiple simultaneous calls into one execution', async () => { + let calls = 0 + const fn = bundleAsync(async () => { + calls++ + await new Promise(r => setTimeout(r, 1)) + return 'hello' + }) + const [res1, res2, res3] = await Promise.all([fn(), fn(), fn()]) + expect(calls).toEqual(1) + expect(res1).toEqual('hello') + expect(res2).toEqual('hello') + expect(res3).toEqual('hello') + }) + it('does not bundle non-simultaneous calls', async () => { + let calls = 0 + const fn = bundleAsync(async () => { + calls++ + await new Promise(r => setTimeout(r, 1)) + return 'hello' + }) + const res1 = await fn() + const res2 = await fn() + const res3 = await fn() + expect(calls).toEqual(3) + expect(res1).toEqual('hello') + expect(res2).toEqual('hello') + expect(res3).toEqual('hello') + }) + it('is not affected by rejections', async () => { + let calls = 0 + const fn = bundleAsync(async () => { + calls++ + await new Promise(r => setTimeout(r, 1)) + throw new Error() + }) + const res1 = await fn().catch(() => 'reject') + const res2 = await fn().catch(() => 'reject') + const res3 = await fn().catch(() => 'reject') + expect(calls).toEqual(3) + expect(res1).toEqual('reject') + expect(res2).toEqual('reject') + expect(res3).toEqual('reject') + }) +}) diff --git a/__tests__/lib/errors.test.ts b/__tests__/lib/errors.test.ts index b9549e6d8..39e8d189e 100644 --- a/__tests__/lib/errors.test.ts +++ b/__tests__/lib/errors.test.ts @@ -1,4 +1,4 @@ -import {isNetworkError} from '../../src/lib/errors' +import {isNetworkError} from '../../src/lib/strings/errors' describe('isNetworkError', () => { const inputs = [ diff --git a/__tests__/lib/extractHtmlMeta.test.ts b/__tests__/lib/extractHtmlMeta.test.ts index 18a0df4c7..cdd2a3384 100644 --- a/__tests__/lib/extractHtmlMeta.test.ts +++ b/__tests__/lib/extractHtmlMeta.test.ts @@ -1,7 +1,8 @@ -import {extractHtmlMeta} from '../../src/lib/extractHtmlMeta' +import {extractHtmlMeta} from '../../src/lib/link-meta/html' import {exampleComHtml} from './__mocks__/exampleComHtml' import {youtubeHTML} from './__mocks__/youtubeHtml' import {tiktokHtml} from './__mocks__/tiktokHtml' +import {youtubeChannelHtml} from './__mocks__/youtubeChannelHtml' describe('extractHtmlMeta', () => { const cases = [ @@ -82,6 +83,19 @@ describe('extractHtmlMeta', () => { expect(output).toEqual(expectedOutput) }) + it('extracts avatar from a youtube channel', () => { + const input = youtubeChannelHtml + const expectedOutput = { + title: 'penguinz0', + description: + 'Clips channel: https://www.youtube.com/channel/UC4EQHfzIbkL_Skit_iKt1aA\n\nTwitter: https://twitter.com/MoistCr1TiKaL\n\nInstagram: https://www.instagram.com/bigmoistcr1tikal/?hl=en\n\nTwitch: https://www.twitch.tv/moistcr1tikal\n\nSnapchat: Hugecharles\n\nTik Tok: Hugecharles\n\nI don't have any other public accounts.', + image: + 'https://yt3.googleusercontent.com/ytc/AL5GRJWOhJOuUC6C2b7gP-5D2q6ypXbcOOckyAE1En4RUQ=s176-c-k-c0x00ffffff-no-rj', + } + const output = extractHtmlMeta({html: input, hostname: 'youtube.com'}) + expect(output).toEqual(expectedOutput) + }) + it('extracts username from the url a twitter profile page', () => { const expectedOutput = { title: '@bluesky on Twitter', diff --git a/__tests__/lib/images.test.ts b/__tests__/lib/images.test.ts index 952b0ca48..ad8556efe 100644 --- a/__tests__/lib/images.test.ts +++ b/__tests__/lib/images.test.ts @@ -78,8 +78,14 @@ describe('downloadAndResize', () => { }) it('should return undefined for unsupported file type', async () => { + const mockedFetch = RNFetchBlob.fetch as jest.Mock + mockedFetch.mockResolvedValueOnce({ + path: jest.fn().mockReturnValue('file://downloaded-image'), + flush: jest.fn(), + }) + const opts: DownloadAndResizeOpts = { - uri: 'https://example.com/image.bmp', + uri: 'https://example.com/image', width: 100, height: 100, maxSize: 500000, @@ -88,6 +94,25 @@ describe('downloadAndResize', () => { } const result = await downloadAndResize(opts) - expect(result).toBeUndefined() + expect(result).toEqual(mockResizedImage) + expect(RNFetchBlob.config).toHaveBeenCalledWith({ + fileCache: true, + appendExt: 'jpeg', + }) + expect(RNFetchBlob.fetch).toHaveBeenCalledWith( + 'GET', + 'https://example.com/image', + ) + expect(ImageResizer.createResizedImage).toHaveBeenCalledWith( + 'file://downloaded-image', + 100, + 100, + 'JPEG', + 100, + undefined, + undefined, + undefined, + {mode: 'cover'}, + ) }) }) diff --git a/__tests__/lib/link-meta.test.ts b/__tests__/lib/link-meta.test.ts index eaf8732b2..ce7da4152 100644 --- a/__tests__/lib/link-meta.test.ts +++ b/__tests__/lib/link-meta.test.ts @@ -1,8 +1,19 @@ -import {LikelyType, getLinkMeta, getLikelyType} from '../../src/lib/link-meta' +import { + LikelyType, + getLinkMeta, + getLikelyType, +} from '../../src/lib/link-meta/link-meta' import {exampleComHtml} from './__mocks__/exampleComHtml' -import {mockedRootStore} from '../../__mocks__/state-mock' +import AtpAgent from '@atproto/api' +import {DEFAULT_SERVICE, RootStoreModel} from '../../src/state' describe('getLinkMeta', () => { + let rootStore: RootStoreModel + + beforeEach(() => { + rootStore = new RootStoreModel(new AtpAgent({service: DEFAULT_SERVICE})) + }) + const inputs = [ '', 'httpbadurl', @@ -88,7 +99,7 @@ describe('getLinkMeta', () => { }) }) const input = inputs[i] - const output = await getLinkMeta(mockedRootStore, input) + const output = await getLinkMeta(rootStore, input) expect(output).toEqual(outputs[i]) } }) diff --git a/__tests__/lib/string.test.ts b/__tests__/lib/string.test.ts index 0032ebf31..4f6fd62d6 100644 --- a/__tests__/lib/string.test.ts +++ b/__tests__/lib/string.test.ts @@ -1,17 +1,18 @@ import { - extractEntities, - detectLinkables, - pluralize, + getYoutubeVideoId, makeRecordUri, - ago, - makeValidHandle, - createFullHandle, - enforceLen, - cleanError, toNiceDomain, toShortUrl, toShareUrl, -} from '../../src/lib/strings' +} from '../../src/lib/strings/url-helpers' +import {pluralize, enforceLen} from '../../src/lib/strings/helpers' +import {ago} from '../../src/lib/strings/time' +import { + extractEntities, + detectLinkables, +} from '../../src/lib/strings/rich-text-detection' +import {makeValidHandle, createFullHandle} from '../../src/lib/strings/handles' +import {cleanError} from '../../src/lib/strings/errors' describe('extractEntities', () => { const knownHandles = new Set(['handle.com', 'full123.test-of-chars']) @@ -487,3 +488,29 @@ describe('toShareUrl', () => { } }) }) + +describe('getYoutubeVideoId', () => { + it(' should return undefined for invalid youtube links', () => { + expect(getYoutubeVideoId('')).toBeUndefined() + expect(getYoutubeVideoId('https://www.google.com')).toBeUndefined() + expect(getYoutubeVideoId('https://www.youtube.com')).toBeUndefined() + expect( + getYoutubeVideoId('https://www.youtube.com/channelName'), + ).toBeUndefined() + expect( + getYoutubeVideoId('https://www.youtube.com/channel/channelName'), + ).toBeUndefined() + }) + + it('getYoutubeVideoId should return video id for valid youtube links', () => { + expect(getYoutubeVideoId('https://www.youtube.com/watch?v=videoId')).toBe( + 'videoId', + ) + expect( + getYoutubeVideoId( + 'https://www.youtube.com/watch?v=videoId&feature=share', + ), + ).toBe('videoId') + expect(getYoutubeVideoId('https://youtu.be/videoId')).toBe('videoId') + }) +}) diff --git a/__tests__/lib/strings/mention-manip.test.ts b/__tests__/lib/strings/mention-manip.test.ts new file mode 100644 index 000000000..f9075763e --- /dev/null +++ b/__tests__/lib/strings/mention-manip.test.ts @@ -0,0 +1,84 @@ +import { + getMentionAt, + insertMentionAt, +} from '../../../src/lib/strings/mention-manip' + +describe('getMentionAt', () => { + type Case = [string, number, string | undefined] + const cases: Case[] = [ + ['hello @alice goodbye', 0, undefined], + ['hello @alice goodbye', 1, undefined], + ['hello @alice goodbye', 2, undefined], + ['hello @alice goodbye', 3, undefined], + ['hello @alice goodbye', 4, undefined], + ['hello @alice goodbye', 5, undefined], + ['hello @alice goodbye', 6, 'alice'], + ['hello @alice goodbye', 7, 'alice'], + ['hello @alice goodbye', 8, 'alice'], + ['hello @alice goodbye', 9, 'alice'], + ['hello @alice goodbye', 10, 'alice'], + ['hello @alice goodbye', 11, 'alice'], + ['hello @alice goodbye', 12, 'alice'], + ['hello @alice goodbye', 13, undefined], + ['hello @alice goodbye', 14, undefined], + ['@alice', 0, 'alice'], + ['@alice hello', 0, 'alice'], + ['@alice hello', 1, 'alice'], + ['@alice hello', 2, 'alice'], + ['@alice hello', 3, 'alice'], + ['@alice hello', 4, 'alice'], + ['@alice hello', 5, 'alice'], + ['@alice hello', 6, 'alice'], + ['@alice hello', 7, undefined], + ['alice@alice', 0, undefined], + ['alice@alice', 6, undefined], + ] + + it.each(cases)( + 'given input string %p and cursor position %p, returns %p', + (str, cursorPos, expected) => { + const output = getMentionAt(str, cursorPos) + expect(output?.value).toEqual(expected) + }, + ) +}) + +describe('insertMentionAt', () => { + type Case = [string, number, string] + const cases: Case[] = [ + ['hello @alice goodbye', 0, 'hello @alice goodbye'], + ['hello @alice goodbye', 1, 'hello @alice goodbye'], + ['hello @alice goodbye', 2, 'hello @alice goodbye'], + ['hello @alice goodbye', 3, 'hello @alice goodbye'], + ['hello @alice goodbye', 4, 'hello @alice goodbye'], + ['hello @alice goodbye', 5, 'hello @alice goodbye'], + ['hello @alice goodbye', 6, 'hello @alice.com goodbye'], + ['hello @alice goodbye', 7, 'hello @alice.com goodbye'], + ['hello @alice goodbye', 8, 'hello @alice.com goodbye'], + ['hello @alice goodbye', 9, 'hello @alice.com goodbye'], + ['hello @alice goodbye', 10, 'hello @alice.com goodbye'], + ['hello @alice goodbye', 11, 'hello @alice.com goodbye'], + ['hello @alice goodbye', 12, 'hello @alice.com goodbye'], + ['hello @alice goodbye', 13, 'hello @alice goodbye'], + ['hello @alice goodbye', 14, 'hello @alice goodbye'], + ['@alice', 0, '@alice.com '], + ['@alice hello', 0, '@alice.com hello'], + ['@alice hello', 1, '@alice.com hello'], + ['@alice hello', 2, '@alice.com hello'], + ['@alice hello', 3, '@alice.com hello'], + ['@alice hello', 4, '@alice.com hello'], + ['@alice hello', 5, '@alice.com hello'], + ['@alice hello', 6, '@alice.com hello'], + ['@alice hello', 7, '@alice hello'], + ['alice@alice', 0, 'alice@alice'], + ['alice@alice', 6, 'alice@alice'], + ] + + it.each(cases)( + 'given input string %p and cursor position %p, returns %p', + (str, cursorPos, expected) => { + const output = insertMentionAt(str, cursorPos, 'alice.com') + expect(output).toEqual(expected) + }, + ) +}) diff --git a/__tests__/lib/strings/rich-text-sanitize.ts b/__tests__/lib/strings/rich-text-sanitize.ts new file mode 100644 index 000000000..d0bbae5e8 --- /dev/null +++ b/__tests__/lib/strings/rich-text-sanitize.ts @@ -0,0 +1,123 @@ +import {AppBskyFeedPost} from '@atproto/api' +type Entity = AppBskyFeedPost.Entity +import {RichText} from '../../../src/lib/strings/rich-text' +import {removeExcessNewlines} from '../../../src/lib/strings/rich-text-sanitize' + +describe('removeExcessNewlines', () => { + it('removes more than two consecutive new lines', () => { + const input = new RichText( + 'test\n\n\n\n\ntest\n\n\n\n\n\n\ntest\n\n\n\n\n\n\ntest\n\n\n\n\n\n\ntest', + ) + const output = removeExcessNewlines(input) + expect(output.text).toEqual('test\n\ntest\n\ntest\n\ntest\n\ntest') + }) + + it('removes more than two consecutive new lines with spaces', () => { + const input = new RichText( + 'test\n\n\n\n\ntest\n \n \n \n \n\n\ntest\n\n\n\n\n\n\ntest\n\n\n\n\n \n\ntest', + ) + const output = removeExcessNewlines(input) + expect(output.text).toEqual('test\n\ntest\n\ntest\n\ntest\n\ntest') + }) + + it('returns original string if there are no consecutive new lines', () => { + const input = new RichText('test\n\ntest\n\ntest\n\ntest\n\ntest') + const output = removeExcessNewlines(input) + expect(output.text).toEqual(input.text) + }) + + it('returns original string if there are no new lines', () => { + const input = new RichText('test test test test test') + const output = removeExcessNewlines(input) + expect(output.text).toEqual(input.text) + }) + + it('returns empty string if input is empty', () => { + const input = new RichText('') + const output = removeExcessNewlines(input) + expect(output.text).toEqual('') + }) + + it('works with different types of new line characters', () => { + const input = new RichText( + 'test\r\ntest\n\rtest\rtest\n\n\n\ntest\n\r \n \n \n \n\n\ntest', + ) + const output = removeExcessNewlines(input) + expect(output.text).toEqual('test\r\ntest\n\rtest\rtest\n\ntest\n\ntest') + }) + + it('removes more than two consecutive new lines with zero width space', () => { + const input = new RichText( + 'test\n\n\n\n\ntest\n\u200B\u200B\n\n\n\ntest\n \u200B\u200B \n\n\n\ntest\n\n\n\n\n\n\ntest', + ) + const output = removeExcessNewlines(input) + expect(output.text).toEqual('test\n\ntest\n\ntest\n\ntest\n\ntest') + }) + + it('removes more than two consecutive new lines with zero width non-joiner', () => { + const input = new RichText( + 'test\n\n\n\n\ntest\n\u200C\u200C\n\n\n\ntest\n \u200C\u200C \n\n\n\ntest\n\n\n\n\n\n\ntest', + ) + const output = removeExcessNewlines(input) + expect(output.text).toEqual('test\n\ntest\n\ntest\n\ntest\n\ntest') + }) + + it('removes more than two consecutive new lines with zero width joiner', () => { + const input = new RichText( + 'test\n\n\n\n\ntest\n\u200D\u200D\n\n\n\ntest\n \u200D\u200D \n\n\n\ntest\n\n\n\n\n\n\ntest', + ) + const output = removeExcessNewlines(input) + expect(output.text).toEqual('test\n\ntest\n\ntest\n\ntest\n\ntest') + }) + + it('removes more than two consecutive new lines with soft hyphen', () => { + const input = new RichText( + 'test\n\n\n\n\ntest\n\u00AD\u00AD\n\n\n\ntest\n \u00AD\u00AD \n\n\n\ntest\n\n\n\n\n\n\ntest', + ) + const output = removeExcessNewlines(input) + expect(output.text).toEqual('test\n\ntest\n\ntest\n\ntest\n\ntest') + }) + + it('removes more than two consecutive new lines with word joiner', () => { + const input = new RichText( + 'test\n\n\n\n\ntest\n\u2060\u2060\n\n\n\ntest\n \u2060\u2060 \n\n\n\ntest\n\n\n\n\n\n\ntest', + ) + const output = removeExcessNewlines(input) + expect(output.text).toEqual('test\n\ntest\n\ntest\n\ntest\n\ntest') + }) +}) + +describe('removeExcessNewlines w/entities', () => { + it('preserves entities as expected', () => { + const input = new RichText( + 'test\n\n\n\n\ntest\n\n\n\n\n\n\ntest\n\n\n\n\n\n\ntest\n\n\n\n\n\n\ntest', + [ + {index: {start: 0, end: 13}, type: '', value: ''}, + {index: {start: 13, end: 24}, type: '', value: ''}, + {index: {start: 9, end: 15}, type: '', value: ''}, + {index: {start: 4, end: 9}, type: '', value: ''}, + ], + ) + const output = removeExcessNewlines(input) + expect(entToStr(input.text, input.entities?.[0])).toEqual( + 'test\n\n\n\n\ntest', + ) + expect(entToStr(input.text, input.entities?.[1])).toEqual( + '\n\n\n\n\n\n\ntest', + ) + expect(entToStr(input.text, input.entities?.[2])).toEqual('test\n\n') + expect(entToStr(input.text, input.entities?.[3])).toEqual('\n\n\n\n\n') + expect(output.text).toEqual('test\n\ntest\n\ntest\n\ntest\n\ntest') + expect(entToStr(output.text, output.entities?.[0])).toEqual('test\n\ntest') + expect(entToStr(output.text, output.entities?.[1])).toEqual('test') + expect(entToStr(output.text, output.entities?.[2])).toEqual('test') + expect(output.entities?.[3]).toEqual(undefined) + }) +}) + +function entToStr(str: string, ent?: Entity) { + if (!ent) { + return '' + } + return str.slice(ent.index.start, ent.index.end) +} diff --git a/__tests__/lib/strings/rich-text.ts b/__tests__/lib/strings/rich-text.ts new file mode 100644 index 000000000..e52ac6cec --- /dev/null +++ b/__tests__/lib/strings/rich-text.ts @@ -0,0 +1,123 @@ +import {RichText} from '../../../src/lib/strings/rich-text' + +describe('richText.insert', () => { + const input = new RichText('hello world', [ + {index: {start: 2, end: 7}, type: '', value: ''}, + ]) + + it('correctly adjusts entities (scenario A - before)', () => { + const output = input.clone().insert(0, 'test') + expect(output.text).toEqual('testhello world') + expect(output.entities?.[0].index.start).toEqual(6) + expect(output.entities?.[0].index.end).toEqual(11) + expect( + output.text.slice( + output.entities?.[0].index.start, + output.entities?.[0].index.end, + ), + ).toEqual('llo w') + }) + + it('correctly adjusts entities (scenario B - inner)', () => { + const output = input.clone().insert(4, 'test') + expect(output.text).toEqual('helltesto world') + expect(output.entities?.[0].index.start).toEqual(2) + expect(output.entities?.[0].index.end).toEqual(11) + expect( + output.text.slice( + output.entities?.[0].index.start, + output.entities?.[0].index.end, + ), + ).toEqual('lltesto w') + }) + + it('correctly adjusts entities (scenario C - after)', () => { + const output = input.clone().insert(8, 'test') + expect(output.text).toEqual('hello wotestrld') + expect(output.entities?.[0].index.start).toEqual(2) + expect(output.entities?.[0].index.end).toEqual(7) + expect( + output.text.slice( + output.entities?.[0].index.start, + output.entities?.[0].index.end, + ), + ).toEqual('llo w') + }) +}) + +describe('richText.delete', () => { + const input = new RichText('hello world', [ + {index: {start: 2, end: 7}, type: '', value: ''}, + ]) + + it('correctly adjusts entities (scenario A - entirely outer)', () => { + const output = input.clone().delete(0, 9) + expect(output.text).toEqual('ld') + expect(output.entities?.length).toEqual(0) + }) + + it('correctly adjusts entities (scenario B - entirely after)', () => { + const output = input.clone().delete(7, 11) + expect(output.text).toEqual('hello w') + expect(output.entities?.[0].index.start).toEqual(2) + expect(output.entities?.[0].index.end).toEqual(7) + expect( + output.text.slice( + output.entities?.[0].index.start, + output.entities?.[0].index.end, + ), + ).toEqual('llo w') + }) + + it('correctly adjusts entities (scenario C - partially after)', () => { + const output = input.clone().delete(4, 11) + expect(output.text).toEqual('hell') + expect(output.entities?.[0].index.start).toEqual(2) + expect(output.entities?.[0].index.end).toEqual(4) + expect( + output.text.slice( + output.entities?.[0].index.start, + output.entities?.[0].index.end, + ), + ).toEqual('ll') + }) + + it('correctly adjusts entities (scenario D - entirely inner)', () => { + const output = input.clone().delete(3, 5) + expect(output.text).toEqual('hel world') + expect(output.entities?.[0].index.start).toEqual(2) + expect(output.entities?.[0].index.end).toEqual(5) + expect( + output.text.slice( + output.entities?.[0].index.start, + output.entities?.[0].index.end, + ), + ).toEqual('l w') + }) + + it('correctly adjusts entities (scenario E - partially before)', () => { + const output = input.clone().delete(1, 5) + expect(output.text).toEqual('h world') + expect(output.entities?.[0].index.start).toEqual(1) + expect(output.entities?.[0].index.end).toEqual(3) + expect( + output.text.slice( + output.entities?.[0].index.start, + output.entities?.[0].index.end, + ), + ).toEqual(' w') + }) + + it('correctly adjusts entities (scenario F - entirely before)', () => { + const output = input.clone().delete(0, 2) + expect(output.text).toEqual('llo world') + expect(output.entities?.[0].index.start).toEqual(0) + expect(output.entities?.[0].index.end).toEqual(5) + expect( + output.text.slice( + output.entities?.[0].index.start, + output.entities?.[0].index.end, + ), + ).toEqual('llo w') + }) +}) -- cgit 1.4.1