/
index.test.js
202 lines (180 loc) · 6.67 KB
/
index.test.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const fs = require('fs')
const path = require('path')
const {entities, getRootDomain, getEntity, getProduct} = require('./index.js')
describe('getRootDomain', () => {
it('works for IP addresses', () => {
expect(getRootDomain('8.8.8.8')).toEqual('8.8.8.8')
expect(getRootDomain('192.168.0.1')).toEqual('192.168.0.1')
})
it('works for basic domains', () => {
expect(getRootDomain('cdn.cnn.com')).toEqual('cnn.com')
expect(getRootDomain('www.hulce.photography')).toEqual('hulce.photography')
expect(getRootDomain('api.supercool.io')).toEqual('supercool.io')
})
it('works for country-tlds', () => {
expect(getRootDomain('content.yahoo.co.jp')).toEqual('yahoo.co.jp')
expect(getRootDomain('go.visit.gov.in')).toEqual('visit.gov.in')
})
it('works for URLs', () => {
expect(getRootDomain('https://content.yahoo.co.jp/path/?query=param')).toEqual('yahoo.co.jp')
expect(getRootDomain('https://a.b.c.it/path/?query=param&two=2')).toEqual('c.it')
expect(getRootDomain('https://foo.bar:433/path/?query=param&two=2')).toEqual('foo.bar')
})
it('works for localhost', () => {
expect(getRootDomain('https://localhost:8080/path/?query=param')).toEqual('localhost')
expect(getRootDomain('https://localhost/path/?query=param&two=2')).toEqual('localhost')
expect(getRootDomain('localhost:9000/path/?query=param&two=2')).toEqual('localhost')
expect(getRootDomain('localhost:1200')).toEqual('localhost')
})
it('works for wildcard domains', () => {
expect(getRootDomain('*.google.com')).toEqual('google.com')
expect(getRootDomain('*.yahoo.co.jp')).toEqual('yahoo.co.jp')
expect(getRootDomain('*.hulce.photography')).toEqual('hulce.photography')
})
it('runs on *massive* inputs', () => {
const massiveInput = '123456789'.repeat(100e3)
expect(getRootDomain(massiveInput)).toEqual(null)
})
it('runs on data URIs', () => {
const dataUri = 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs='
expect(getRootDomain(dataUri)).toEqual(null)
})
it('returns null on invalid inputs', () => {
expect(getRootDomain('this is not a domain')).toEqual(null)
expect(getRootDomain('neither-is-this')).toEqual(null)
expect(getRootDomain('http://nor this')).toEqual(null)
})
})
describe('getEntity', () => {
it('works for direct domain usage', () => {
expect(getEntity('https://js.connect.facebook.net/lib.js')).toMatchInlineSnapshot(`
Object {
"averageExecutionTime": 228.82690369259257,
"categories": Array [
"social",
],
"company": "Facebook",
"domains": Array [
"*.atlassbx.com",
"*.facebook.com",
"*.fbsbx.com",
"fbcdn-photos-e-a.akamaihd.net",
"*.facebook.net",
"*.fbcdn.net",
],
"examples": Array [
"www.facebook.com",
"connect.facebook.net",
"staticxx.facebook.com",
"static.xx.fbcdn.net",
"m.facebook.com",
"an.facebook.com",
"platform-lookaside.fbsbx.com",
],
"homepage": "https://www.facebook.com",
"name": "Facebook",
"products": Array [
Object {
"facades": Array [
Object {
"name": "React Live Chat Loader",
"repo": "https://github.com/calibreapp/react-live-chat-loader",
},
],
"name": "Facebook Messenger Customer Chat",
"urlPatterns": Array [
/connect\\\\\\.facebook\\\\\\.net\\\\/\\.\\*\\\\/sdk\\\\/xfbml\\\\\\.customerchat\\\\\\.js/,
],
},
],
"totalExecutionTime": 334391848,
"totalOccurrences": 1461331,
}
`)
})
it('works for inferred domain usage', () => {
expect(getEntity('https://unknown.typekit.net/fonts.css')).toMatchInlineSnapshot(`
Object {
"averageExecutionTime": 94.28264200503448,
"categories": Array [
"cdn",
],
"company": "Adobe",
"domains": Array [
"*.typekit.com",
"*.typekit.net",
],
"examples": Array [
"use.typekit.net",
"p.typekit.net",
],
"homepage": "https://fonts.adobe.com/",
"name": "Adobe TypeKit",
"products": Array [],
"totalExecutionTime": 1722921,
"totalOccurrences": 18274,
}
`)
})
it('does not over-infer', () => {
expect(getEntity('https://unknown.gstatic.com/what')).toEqual(undefined)
})
it('only infers as a fallback', () => {
expect(getEntity('http://fbcdn-photos-e-a.akamaihd.net/1234.jpg').name).toEqual('Facebook')
expect(getEntity('http://unknown.akamaihd.net/1234.jpg').name).toEqual('Akamai')
})
it('runs on *massive* inputs', () => {
const massiveInput = '123456789'.repeat(100e3)
expect(getEntity(massiveInput)).toEqual(undefined)
})
it('runs on data URIs', () => {
const dataUri = 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs='
expect(getEntity(dataUri)).toEqual(undefined)
})
})
describe('getProduct', () => {
it('works on basic url', () => {
expect(getProduct('https://www.youtube.com/embed/alGcULGtiv8')).toMatchObject({
name: 'YouTube Embedded Player',
facades: [
{
name: 'Lite YouTube',
repo: 'https://github.com/paulirish/lite-youtube-embed',
},
],
})
})
it('works on regex based', () => {
expect(
getProduct('https://connect.facebook.net/en_US/sdk/xfbml.customerchat.js')
).toMatchObject({
name: 'Facebook Messenger Customer Chat',
facades: [
{
name: 'React Live Chat Loader',
repo: 'https://github.com/calibreapp/react-live-chat-loader',
},
],
})
})
it('returns undefined when product does not match', () => {
expect(getProduct('https://js.connect.facebook.net/lib.js')).toEqual(undefined)
})
it('returns undefined with no products', () => {
expect(getProduct('https://unknown.typekit.net/fonts.css')).toEqual(undefined)
})
})
describe('build state', () => {
it('should use the complete entities set', () => {
const sourceOfTruthEntities = require('../data/entities.js')
expect(entities).toHaveLength(sourceOfTruthEntities.length)
})
it('should have all the same subsets in root as lib', () => {
const srcSizes = fs.readdirSync(path.join(__dirname, 'subsets'))
const dstSizes = fs.readdirSync(path.join(__dirname, '../')).filter(f => f.includes('-subset'))
expect(dstSizes).toHaveLength(srcSizes.length) // run `yarn build` if this fails
for (const file of dstSizes) {
if (file.endsWith('.js')) require(path.join(__dirname, '../', file))
}
})
})