aboutsummaryrefslogtreecommitdiff
path: root/test/unit/specs/services/matcher/matcher.spec.js
blob: c6e9719d3d0b66a902ee624e3aaf5d9895e7065c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import * as MatcherService from 'src/services/matcher/matcher.service.js'

const localAttn = () => ({
  id: 123,
  is_local: true,
  name: 'Guy',
  screen_name: 'person',
  statusnet_profile_url: 'https://instance.com/users/person'
})

const externalAttn = () => ({
  id: 123,
  is_local: false,
  name: 'Guy',
  screen_name: 'person@instance.com',
  statusnet_profile_url: 'https://instance.com/users/person'
})

describe('MatcherService', () => {
  describe('mentionMatchesUrl', () => {
    it('should match local mention', () => {
      const attention = localAttn()
      const url = 'https://instance.com/users/person'

      expect(MatcherService.mentionMatchesUrl(attention, url)).to.eql(true)
    })

    it('should not match a local mention with same name but different instance', () => {
      const attention = localAttn()
      const url = 'https://website.com/users/person'

      expect(MatcherService.mentionMatchesUrl(attention, url)).to.eql(false)
    })

    it('should match external pleroma mention', () => {
      const attention = externalAttn()
      const url = 'https://instance.com/users/person'

      expect(MatcherService.mentionMatchesUrl(attention, url)).to.eql(true)
    })

    it('should not match external pleroma mention with same name but different instance', () => {
      const attention = externalAttn()
      const url = 'https://website.com/users/person'

      expect(MatcherService.mentionMatchesUrl(attention, url)).to.eql(false)
    })

    it('should match external mastodon mention', () => {
      const attention = externalAttn()
      const url = 'https://instance.com/@person'

      expect(MatcherService.mentionMatchesUrl(attention, url)).to.eql(true)
    })

    it('should not match external mastodon mention with same name but different instance', () => {
      const attention = externalAttn()
      const url = 'https://website.com/@person'

      expect(MatcherService.mentionMatchesUrl(attention, url)).to.eql(false)
    })
  })
  describe('extractTagFromUrl', () => {
    it('should return tag name from valid pleroma url', () => {
      const url = 'https://website.com/tag/photo'

      expect(MatcherService.extractTagFromUrl(url)).to.eql('photo')
    })

    it('should return tag name from valid mastodon url', () => {
      const url = 'https://website.com/tags/sky'

      expect(MatcherService.extractTagFromUrl(url)).to.eql('sky')
    })

    it('should not return string but false if invalid url', () => {
      const url = 'https://website.com/users/sky'

      expect(MatcherService.extractTagFromUrl(url)).to.eql(false)
    })

    it('should return tag name from non-ascii tags', () => {
      const url = encodeURI('https://website.com/tag/喵喵喵')

      expect(MatcherService.extractTagFromUrl(url)).to.eql('喵喵喵')
    })
  })
})