-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathutils.spec.js
More file actions
97 lines (80 loc) · 3.04 KB
/
Copy pathutils.spec.js
File metadata and controls
97 lines (80 loc) · 3.04 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
import { assert, test, describe } from 'vitest';
import { parse_isr_expiration, pattern_to_src, resolve_runtime } from './utils.js';
// workaround so that TypeScript doesn't follow that import which makes it pick up that file and then error on missing import aliases
const { parse_route_id } = await import(
new URL('../kit/src/' + 'utils/routing.js', import.meta.url).href
);
/**
* @param {string} route_id
* @param {string} expected
*/
function run_pattern_to_src_test(route_id, expected) {
const { pattern } = parse_route_id(route_id);
assert.equal(pattern_to_src(pattern.toString()), expected);
}
test('pattern_to_src for simple route', () => {
run_pattern_to_src_test('/', '^/?');
});
test('pattern_to_src for route with parameters', () => {
run_pattern_to_src_test('/foo/[bar]', '^/foo/([^/]+?)/?');
});
test('pattern_to_src for route with optional parameters', () => {
run_pattern_to_src_test('/foo/[[bar]]', '^/foo(?:/([^/]+))?/?');
});
test('pattern_to_src for route with optional parameter in the middle', () => {
run_pattern_to_src_test('/foo/[[bar]]/baz', '^/foo(?:/([^/]+))?/baz/?');
});
test('pattern_to_src for route with rest parameter', () => {
run_pattern_to_src_test('/foo/[...bar]', '^/foo(?:/([^]*))?/?');
});
test('pattern_to_src for route with rest parameter in the middle', () => {
run_pattern_to_src_test('/foo/[...bar]/baz', '^/foo(?:/([^]*))?/baz/?');
});
describe('parse_isr_expiration', () => {
test.each(
/** @type {const} */ ([
[1, 1],
['1', 1],
[false, false],
['false', false]
])
)('works for valid inputs ($0)', (input, output) => {
const result = parse_isr_expiration(input, '/isr');
assert.equal(result, output);
});
test('does not allow floats', () => {
assert.throws(() => parse_isr_expiration(1.5, '/isr'), /should be an integer, in \/isr/);
});
test('does not allow `true`', () => {
const val = /** @type {false} */ (true);
assert.throws(() => parse_isr_expiration(val, '/isr'), /should be an integer, in \/isr/);
});
test('does not allow negative numbers', () => {
assert.throws(() => parse_isr_expiration(-1, '/isr'), /should be non-negative, in \/isr/);
});
test('does not allow strings that do not parse to valid numbers', () => {
assert.throws(
() => parse_isr_expiration('foo', '/isr'),
/value was a string but could not be parsed as an integer, in \/isr/
);
});
test('does not allow strings that parse to floats', () => {
assert.throws(
() => parse_isr_expiration('1.1', '/isr'),
/value was a string but could not be parsed as an integer, in \/isr/
);
});
});
describe('resolve_runtime', () => {
test('prefers override_key over default_key', () => {
const result = resolve_runtime('nodejs20.x', 'bun1.x');
assert.equal(result, 'bun1.x');
});
test('uses default_key when override_key is undefined', () => {
const result = resolve_runtime('bun1.x');
assert.equal(result, 'bun1.x');
});
test('throws an error when resolving to an invalid runtime', () => {
assert.throws(() => resolve_runtime('node18.x', undefined), /Unsupported runtime: node18.x/);
});
});