Skip to content

Commit

Permalink
chore: add route matcher to tests (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed May 2, 2023
1 parent 9151b5f commit 585ac53
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
12 changes: 11 additions & 1 deletion node/manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { env } from 'process'

import { test, expect, vi } from 'vitest'

import { getRouteMatcher } from '../test/util.js'

import { BundleFormat } from './bundle.js'
import { Cache, FunctionConfig } from './config.js'
import { Declaration } from './declaration.js'
Expand Down Expand Up @@ -73,24 +75,32 @@ test('Generates a manifest with excluded paths and patterns', () => {
const functions = [
{ name: 'func-1', path: '/path/to/func-1.ts' },
{ name: 'func-2', path: '/path/to/func-2.ts' },
{ name: 'func-3', path: '/path/to/func-3.ts' },
]
const declarations: Declaration[] = [
{ function: 'func-1', path: '/f1/*', excludedPath: '/f1/exclude' },
{ function: 'func-2', pattern: '^/f2/.*/?$', excludedPattern: '^/f2/exclude$' },
{ function: 'func-3', path: '/*', excludedPath: '/**/*.html' },
]
const manifest = generateManifest({ bundles: [], declarations, functions })

const expectedRoutes = [
{ function: 'func-1', pattern: '^/f1/.*/?$' },
{ function: 'func-2', pattern: '^/f2/.*/?$' },
{ function: 'func-3', pattern: '^/.*/?$' },
]

expect(manifest.routes).toEqual(expectedRoutes)
expect(manifest.function_config).toEqual({
'func-1': { excluded_patterns: ['^/f1/exclude/?$'] },
'func-2': { excluded_patterns: ['^/f2/exclude$'] },
'func-3': { excluded_patterns: ['^/.*/.*\\.html/?$'] },
})
expect(manifest.bundler_version).toBe(env.npm_package_version as string)

const matcher = getRouteMatcher(manifest)

expect(matcher('/f1/hello')?.function).toBe('func-1')
expect(matcher('/grandparent/parent/child/grandchild.html')?.function).toBeUndefined()
})

test('TOML-defined paths can be combined with ISC-defined excluded paths', () => {
Expand Down
2 changes: 1 addition & 1 deletion node/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,4 @@ const writeManifest = async ({ distDirectory, ...rest }: WriteManifestOptions) =
return manifest
}

export { generateManifest, Manifest, writeManifest }
export { generateManifest, Manifest, Route, writeManifest }
17 changes: 16 additions & 1 deletion test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { execa } from 'execa'
import tmp from 'tmp-promise'

import { getLogger } from '../node/logger.js'
import type { Manifest } from '../node/manifest.js'

const testLogger = getLogger(() => {
// no-op
Expand Down Expand Up @@ -43,6 +44,20 @@ const inspectFunction = (path: string) => `
console.log(JSON.stringify(responses));
`

const getRouteMatcher = (manifest: Manifest) => (candidate: string) =>
manifest.routes.find((route) => {
const regex = new RegExp(route.pattern)

if (!regex.test(candidate)) {
return false
}

const excludedPattern = manifest.function_config[route.function].excluded_patterns
const isExcluded = excludedPattern.some((pattern) => new RegExp(pattern).test(candidate))

return !isExcluded
})

const runESZIP = async (eszipPath: string) => {
const tmpDir = await tmp.dir({ unsafeCleanup: true })

Expand Down Expand Up @@ -86,4 +101,4 @@ const runESZIP = async (eszipPath: string) => {
return JSON.parse(result.stdout)
}

export { fixturesDir, testLogger, runESZIP, useFixture }
export { fixturesDir, getRouteMatcher, testLogger, runESZIP, useFixture }

0 comments on commit 585ac53

Please sign in to comment.