forked from opawg/user-agents-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patterns_test.ts
135 lines (117 loc) · 7.64 KB
/
patterns_test.ts
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
import { fail } from 'https://deno.land/[email protected]/testing/asserts.ts';
import { join, fromFileUrl } from 'https://deno.land/[email protected]/path/mod.ts';
type Type = 'apps' | 'bots' | 'browsers' | 'devices' |'libraries' | 'referrers';
type Entry = { name: string, pattern: string, examples?: string[] };
Deno.test({
name: 'patterns',
fn: async () => {
const entriesByType = new Map<Type, Entry[]>();
// first, read and perform basic parsing/validation on each entries file
for (const type of [ 'apps', 'bots', 'browsers', 'devices', 'libraries', 'referrers' ] as Type[]) {
const filepath = join(fromFileUrl(import.meta.url), `../../../src/${type}.json`);
const txt = await Deno.readTextFile(filepath);
const obj = JSON.parse(txt);
if (!Array.isArray(obj.entries)) fail(`Bad top-level object: missing 'entries' array.`);
const names = new Set<string>();
const entries: Entry[] = [];
let i = 0;
for (const entry of obj.entries) {
const tag = `${type}.entry[${i}]`;
if (typeof entry !== 'object' || entry === null) fail(`Bad ${tag}: expected an object, found ${JSON.stringify(entry)}`);
const { name, pattern, description, examples, svg, comments, category, urls } = entry as Record<string, unknown>;
// name
if (typeof name !== 'string') fail(`Bad ${tag}.name: expected a string property, found ${JSON.stringify(entry)}`);
if (name.trim() !== name) fail(`Bad ${tag}.name: expected no leading or trailing whitespace, found ${name}`);
if (name === '') fail(`Bad ${tag}.name: expected a non-blank string`);
if (names.has(name.toLowerCase())) fail(`Bad ${tag}.name: expected a unique value, found ${name}`);
names.add(name.toLowerCase());
// pattern
if (typeof pattern !== 'string') fail(`Bad ${tag}.pattern: expected a string property, found ${JSON.stringify(entry)}`);
if (/^\s+$/.test(pattern)) fail(`Bad ${tag}.pattern: expected a non-blank string`);
if (pattern.includes('(?:')) fail(`Bad ${tag}.pattern: non-capturing groups are not supported in all environments`);
if (pattern.includes('(?=') || pattern.includes('(?!')) fail(`Bad ${tag}.pattern: lookaheads are not supported in all environments`);
if (pattern.includes('(?<=') || pattern.includes('(?<!')) fail(`Bad ${tag}.pattern: lookbehinds are not supported in all environments`);
if (pattern.includes('\\A')) fail(`Bad ${tag}.pattern: \\A (beginning of string) is not supported in all environments, use ^`);
if (pattern.includes('\\Z')) fail(`Bad ${tag}.pattern: \\Z (end of string or before trailing newline) is not supported in all environments, use $`);
if (pattern.includes('\\z')) fail(`Bad ${tag}.pattern: \\z (end of string) is not supported in all environments, use $`);
const regex = new RegExp(pattern);
// description
if (description !== undefined && typeof description !== 'string') fail(`Bad ${tag}.description: expected an optional string property, found ${JSON.stringify(entry)}`);
if (typeof description === 'string') {
if (description.trim() !== description) fail(`Bad ${tag}.description: expected no leading or trailing whitespace, found ${description}`);
if (description === '') fail(`Bad ${tag}.description: expected a non-blank string`);
}
// svg
if (svg !== undefined && typeof svg !== 'string') fail(`Bad ${tag}.svg: expected an optional string property, found ${JSON.stringify(entry)}`);
if (typeof svg === 'string') {
if (!/^[a-z]+(-[a-z]+)*\.svg$/.test(svg)) fail(`Bad ${tag}.svg: unexpected value ${JSON.stringify(svg)}`);
await Deno.stat(join(fromFileUrl(import.meta.url), `../../../svg/${svg}`));
}
// comments
if (comments !== undefined && typeof comments !== 'string') fail(`Bad ${tag}.comments: expected an optional string property, found ${JSON.stringify(entry)}`);
if (typeof comments === 'string') {
if (comments.trim() !== comments) fail(`Bad ${tag}.comments: expected no leading or trailing whitespace, found ${comments}`);
if (comments === '') fail(`Bad ${tag}.comments: expected a non-blank string`);
}
// examples
if (examples !== undefined) {
if (!Array.isArray(examples)) fail(`Bad ${tag}.examples: expected an array, found ${JSON.stringify(examples)}`);
examples.forEach((example: unknown, j: number) => {
if (typeof example !== 'string') fail(`Bad ${tag}.examples[${j}]: expected a string, found ${JSON.stringify(example)}`);
if (!regex.test(example)) fail(`Bad ${tag}.examples[${j}]: "${example}" does not match pattern "${pattern}"`);
});
}
// urls
if (urls !== undefined) {
if (!Array.isArray(urls)) fail(`Bad ${tag}.urls: expected an array, found ${JSON.stringify(urls)}`);
urls.forEach((url: unknown, j: number) => {
if (typeof url !== 'string') fail(`Bad ${tag}.urls[${j}]: expected a string, found ${JSON.stringify(url)}`);
if (!isValidUrl(url)) fail(`Bad ${tag}.urls[${j}]: expected url, found "${url}"`);
});
}
// category
if (category !== undefined && typeof category !== 'string') fail(`Bad ${tag}.category: expected an optional string property, found ${JSON.stringify(entry)}`);
if (typeof category === 'string') {
if (!/^[a-z]+(_[a-z]+)*$/.test(category)) fail(`Bad ${tag}.category: unexpected value ${JSON.stringify(category)}`);
}
entries.push({ name, pattern, examples });
i++;
}
entriesByType.set(type, entries);
}
// now that we know all files are valid, check deterministic match for each example
for (const [ type, entries ] of entriesByType) {
if (type === 'devices' || type === 'referrers') continue;
let i = 0;
for (const { name, examples } of entries) {
const tag = `${type}.entry[${i}]`;
let j = 0;
for (const example of examples ?? []) {
const match = computeDeterministicMatch(example, entriesByType);
if (!match || match.name !== name || match.type !== type) {
fail(`Bad ${tag}.examples[${j}]: ${name} "${example}" does not match itself, deterministic match ${JSON.stringify(match)}`);
}
j++;
}
i++;
}
}
}
});
function isValidUrl(url: string): boolean {
try {
const u = new URL(url);
return u.protocol === 'http:' || u.protocol === 'https:'
} catch {
return false;
}
}
function computeDeterministicMatch(userAgent: string, entriesByType: Map<Type, Entry[]>): { type: Type, name: string } | undefined {
for (const type of [ 'bots', 'apps', 'libraries', 'browsers' ] as Type[]) {
for (const { name, pattern } of entriesByType.get(type) ?? []) {
if (new RegExp(pattern).test(userAgent)) {
return { type, name };
}
}
}
}