about summary refs log tree commit diff
path: root/__tests__/lib/link-meta.test.ts
blob: f0ca7a9d4b7ecf630e4c972dc6c58123cec59d74 (plain) (blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import {
  LikelyType,
  getLinkMeta,
  getLikelyType,
} from '../../src/lib/link-meta/link-meta'
import {exampleComHtml} from './__mocks__/exampleComHtml'
import {BskyAgent} from '@atproto/api'
import {DEFAULT_SERVICE, RootStoreModel} from '../../src/state'

describe('getLinkMeta', () => {
  let rootStore: RootStoreModel

  beforeEach(() => {
    rootStore = new RootStoreModel(new BskyAgent({service: DEFAULT_SERVICE}))
  })

  const inputs = [
    '',
    'httpbadurl',
    'https://example.com',
    'https://example.com/index.html',
    'https://example.com/image.png',
    'https://example.com/video.avi',
    'https://example.com/audio.ogg',
    'https://example.com/text.txt',
    'https://example.com/javascript.js',
    'https://bsky.app/',
    'https://bsky.app/index.html',
  ]
  const outputs = [
    {
      error: 'Invalid URL',
      likelyType: LikelyType.Other,
      url: '',
    },
    {
      error: 'Invalid URL',
      likelyType: LikelyType.Other,
      url: 'httpbadurl',
    },
    {
      likelyType: LikelyType.HTML,
      url: 'https://example.com',
      title: 'Example Domain',
      description: 'An example website',
    },
    {
      likelyType: LikelyType.HTML,
      url: 'https://example.com/index.html',
      title: 'Example Domain',
      description: 'An example website',
    },
    {
      likelyType: LikelyType.Image,
      url: 'https://example.com/image.png',
    },
    {
      likelyType: LikelyType.Video,
      url: 'https://example.com/video.avi',
    },
    {
      likelyType: LikelyType.Audio,
      url: 'https://example.com/audio.ogg',
    },
    {
      likelyType: LikelyType.Text,
      url: 'https://example.com/text.txt',
    },
    {
      likelyType: LikelyType.Other,
      url: 'https://example.com/javascript.js',
    },
    {
      likelyType: LikelyType.AtpData,
      url: '/',
    },
    {
      likelyType: LikelyType.AtpData,
      url: '/index.html',
    },
    {
      likelyType: LikelyType.Other,
      url: '',
      title: '',
    },
  ]
  it('correctly handles a set of text inputs', async () => {
    for (let i = 0; i < inputs.length; i++) {
      global.fetch = jest.fn().mockImplementationOnce(() => {
        return new Promise((resolve, _reject) => {
          resolve({
            ok: true,
            status: 200,
            text: () => exampleComHtml,
          })
        })
      })
      const input = inputs[i]
      const output = await getLinkMeta(rootStore, input)
      expect(output).toEqual(outputs[i])
    }
  })
})

describe('getLikelyType', () => {
  it('correctly handles non-parsed url', async () => {
    const output = await getLikelyType('https://example.com')
    expect(output).toEqual(LikelyType.HTML)
  })

  it('handles non-string urls without crashing', async () => {
    const output = await getLikelyType('123')
    expect(output).toEqual(LikelyType.Other)
  })
})