about summary refs log tree commit diff
path: root/__tests__/lib/strings
diff options
context:
space:
mode:
Diffstat (limited to '__tests__/lib/strings')
-rw-r--r--__tests__/lib/strings/mention-manip.test.ts84
-rw-r--r--__tests__/lib/strings/rich-text-sanitize.ts123
-rw-r--r--__tests__/lib/strings/rich-text.ts123
3 files changed, 330 insertions, 0 deletions
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')
+  })
+})