import { describe, it } from 'bun:test'; import { expect } from 'chai'; import { PathPattern } from './path-pattern'; describe('PathPattern', () => { // What url-pattern 1.0.3 returned. const cases: [string, string, unknown][] = [ ['/users/:id', '/users/42', { id: '42' }], ['/users/:id', '/users/42/x', null], ['/*', '/a/b', { _: 'a/b' }], ['/*', '/', { _: '' }], ['/api(/:v)/x', '/api/x', {}], ['/api(/:v)/x', '/api/v1/x', { v: 'v1' }], ['/a/:x/:x', '/a/1/2', { x: ['1', '2'] }], ['/login/oauth2/code/*', '/login/oauth2/code/github', { _: 'github' }], ['/health', '/health', {}], ['/health', '/health/', null], ['/health(/:indicatorName)', '/health/db', { indicatorName: 'db' }], ['/u/:id', '/u/a%20b', { id: 'a%20b' }], ['/u/:id', '/u/a.b', null], ['/f\\*', '/f*', {}], ['/f\\*', '/fx', null], ['api', 'api', {}], ['/a/*/b/*', '/a/1/b/2/3', { _: ['1', '2/3'] }], ['/a.b', '/aXb', null] ]; for (const [pattern, path, expected] of cases) { it(`${pattern} on ${path}`, () => { expect(new PathPattern(pattern).match(path)).to.deep.equal(expected); }); } it('returns the groups of a RegExp', () => { expect(new PathPattern(/^\/api\/(\d+)$/).match('/api/12')).to.deep.equal(['12']); expect(new PathPattern(/^\/api\/(\d+)$/).match('/api/x')).to.equal(null); }); it('refuses patterns it cannot read', () => { for (const pattern of ['', '/a b', '/a(/b', '/a)', '/:', '/()']) { expect(() => new PathPattern(pattern), pattern).to.throw(); } }); });