-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
194 lines (164 loc) · 6.03 KB
/
test.js
File metadata and controls
194 lines (164 loc) · 6.03 KB
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
const test = require('ava');
const shortener = require('./');
const TEST_URLS = [
'google.com',
'https://example.com',
'https://test.com/path/to/something',
'https://special-chars.com/!@#$%^&*()',
'localhost:3000',
'very.very.long.domain.with.subdomain.example.com',
'http://user:pass@domain.com',
'https://domain.com/?query=123¶m=456'
];
// Snapshot: hardcoded expected outputs to guarantee backward compatibility
const SNAPSHOTS = {
'https://google.com': 'Qhq1TQ2n'
};
// ===== gen() basic tests =====
test('gen: output length and format', t => {
for (const url of TEST_URLS) {
const shortened = shortener.gen(url);
t.is(shortened.length, 8, `Short URL length should be 8 for ${url}`);
t.regex(shortened, /^[0-9A-Za-z]+$/, `Output should be alphanumeric for ${url}`);
}
});
test('gen: consistency (same input → same output)', t => {
for (const url of TEST_URLS) {
const first = shortener.gen(url);
const second = shortener.gen(url);
t.is(first, second, `Same URL should generate same code: ${url}`);
}
});
test('gen: uniqueness', t => {
const results = new Set();
for (const url of TEST_URLS) {
const shortened = shortener.gen(url);
t.false(results.has(shortened), `Generated code should be unique: ${shortened} for ${url}`);
results.add(shortened);
}
});
test('gen: snapshot — backward compatibility', t => {
for (const [input, expected] of Object.entries(SNAPSHOTS)) {
t.is(shortener.gen(input), expected, `Snapshot mismatch for ${input}`);
}
});
// ===== gen() error cases =====
test('gen: missing arguments', t => {
t.throws(() => shortener.gen(), {
instanceOf: TypeError,
message: 'Wrong number of arguments'
});
});
test('gen: empty string', t => {
t.throws(() => shortener.gen(''), {
instanceOf: TypeError,
message: 'Invalid URL'
});
});
test('gen: non-string input', t => {
t.throws(() => shortener.gen(123), { instanceOf: TypeError });
t.throws(() => shortener.gen(null), { instanceOf: TypeError });
t.throws(() => shortener.gen(undefined), { instanceOf: TypeError });
t.throws(() => shortener.gen({}), { instanceOf: TypeError });
t.throws(() => shortener.gen(true), { instanceOf: TypeError });
t.throws(() => shortener.gen([]), { instanceOf: TypeError });
});
test('gen: input exceeding max size', t => {
const huge = 'x'.repeat(65537);
t.throws(() => shortener.gen(huge), {
instanceOf: RangeError
});
});
// ===== gen() edge cases =====
test('gen: unicode input', t => {
const unicodeInputs = [
'🎉🎊🎈',
'你好世界',
'مرحبا بالعالم',
'こんにちは',
'Héllo Wörld',
'\u0000\u0001\u0002'
];
for (const input of unicodeInputs) {
const result = shortener.gen(input);
t.is(result.length, 8, `Should produce 8-char code for Unicode: ${input}`);
t.regex(result, /^[0-9A-Za-z]+$/);
}
});
test('gen: large input within limits', t => {
const large = 'a'.repeat(65536);
const result = shortener.gen(large);
t.is(result.length, 8);
t.regex(result, /^[0-9A-Za-z]+$/);
});
// ===== gen() with custom length =====
test('gen: custom length', t => {
const input = 'https://google.com';
const len6 = shortener.gen(input, 6);
t.is(len6.length, 6);
t.regex(len6, /^[0-9A-Za-z]+$/);
const len12 = shortener.gen(input, 12);
t.is(len12.length, 12);
t.regex(len12, /^[0-9A-Za-z]+$/);
const len1 = shortener.gen(input, 1);
t.is(len1.length, 1);
const len22 = shortener.gen(input, 22);
t.is(len22.length, 22);
t.regex(len22, /^[0-9A-Za-z]+$/);
});
test('gen: default length is 8 when omitted', t => {
const a = shortener.gen('test');
const b = shortener.gen('test', 8);
t.is(a, b);
t.is(a.length, 8);
});
test('gen: shorter length is prefix of default', t => {
const full = shortener.gen('https://google.com');
const short = shortener.gen('https://google.com', 6);
t.is(short, full.substring(0, 6));
});
test('gen: custom length consistency', t => {
const input = 'https://example.com/test';
for (let len = 1; len <= 22; len++) {
const a = shortener.gen(input, len);
const b = shortener.gen(input, len);
t.is(a, b, `Consistency failed for length ${len}`);
t.is(a.length, len, `Length mismatch for requested ${len}`);
}
});
test('gen: invalid length', t => {
t.throws(() => shortener.gen('test', 0), { instanceOf: RangeError });
t.throws(() => shortener.gen('test', 23), { instanceOf: RangeError });
t.throws(() => shortener.gen('test', -1), { instanceOf: RangeError });
t.throws(() => shortener.gen('test', 'abc'), { instanceOf: TypeError });
});
// ===== genAsync() tests =====
test('genAsync: basic usage', async t => {
const result = await shortener.genAsync('https://google.com');
t.is(result, 'Qhq1TQ2n');
t.is(result.length, 8);
});
test('genAsync: matches gen() output', async t => {
for (const url of TEST_URLS) {
const sync = shortener.gen(url);
const async_ = await shortener.genAsync(url);
t.is(sync, async_, `Async should match sync for ${url}`);
}
});
test('genAsync: custom length', async t => {
const result = await shortener.genAsync('https://google.com', 12);
t.is(result.length, 12);
t.regex(result, /^[0-9A-Za-z]+$/);
});
test('genAsync: error on invalid input', async t => {
// These throw synchronously since validation happens before the async work
t.throws(() => shortener.genAsync(), { instanceOf: TypeError });
t.throws(() => shortener.genAsync(''), { instanceOf: TypeError });
t.throws(() => shortener.genAsync(123), { instanceOf: TypeError });
});
test('genAsync: concurrent calls', async t => {
const promises = TEST_URLS.map(url => shortener.genAsync(url));
const results = await Promise.all(promises);
const unique = new Set(results);
t.is(unique.size, TEST_URLS.length, 'All concurrent results should be unique');
});