|
1 | | -import { assert, test } from 'vitest'; |
2 | | -import { get_pathname, pattern_to_src } from '../utils.js'; |
| 1 | +import { assert, test, describe } from 'vitest'; |
| 2 | +import { get_pathname, parse_isr_expiration, pattern_to_src } from '../utils.js'; |
3 | 3 |
|
4 | 4 | // workaround so that TypeScript doesn't follow that import which makes it pick up that file and then error on missing import aliases |
5 | 5 | const { parse_route_id } = await import('../../kit/src/' + 'utils/routing.js'); |
@@ -130,3 +130,44 @@ test('pattern_to_src for route with rest parameter', () => { |
130 | 130 | test('pattern_to_src for route with rest parameter in the middle', () => { |
131 | 131 | run_pattern_to_src_test('/foo/[...bar]/baz', '^/foo(/[^]*)?/baz/?'); |
132 | 132 | }); |
| 133 | + |
| 134 | +describe('parse_isr_expiration', () => { |
| 135 | + test.each( |
| 136 | + /** @type {const} */ ([ |
| 137 | + [1, 1], |
| 138 | + ['1', 1], |
| 139 | + [false, false], |
| 140 | + ['false', false] |
| 141 | + ]) |
| 142 | + )('works for valid inputs ($0)', (input, output) => { |
| 143 | + const result = parse_isr_expiration(input, '/isr'); |
| 144 | + assert.equal(result, output); |
| 145 | + }); |
| 146 | + |
| 147 | + test('does not allow floats', () => { |
| 148 | + assert.throws(() => parse_isr_expiration(1.5, '/isr'), /should be an integer, in \/isr/); |
| 149 | + }); |
| 150 | + |
| 151 | + test('does not allow `true`', () => { |
| 152 | + const val = /** @type {false} */ (true); |
| 153 | + assert.throws(() => parse_isr_expiration(val, '/isr'), /should be an integer, in \/isr/); |
| 154 | + }); |
| 155 | + |
| 156 | + test('does not allow negative numbers', () => { |
| 157 | + assert.throws(() => parse_isr_expiration(-1, '/isr'), /should be non-negative, in \/isr/); |
| 158 | + }); |
| 159 | + |
| 160 | + test('does not allow strings that do not parse to valid numbers', () => { |
| 161 | + assert.throws( |
| 162 | + () => parse_isr_expiration('foo', '/isr'), |
| 163 | + /value was a string but could not be parsed as an integer, in \/isr/ |
| 164 | + ); |
| 165 | + }); |
| 166 | + |
| 167 | + test('does not allow strings that parse to floats', () => { |
| 168 | + assert.throws( |
| 169 | + () => parse_isr_expiration('1.1', '/isr'), |
| 170 | + /value was a string but could not be parsed as an integer, in \/isr/ |
| 171 | + ); |
| 172 | + }); |
| 173 | +}); |
0 commit comments