import * as assert from 'remix/assert'
import { describe, it } from 'remix/test'
import { crawl } from './crawl.ts'
import { createRouter } from 'remix/router'
function html(content: string): Response {
return new Response(content, { headers: { 'Content-Type': 'text/html' } })
}
function css(content = ''): Response {
return new Response(content, { headers: { 'Content-Type': 'text/css' } })
}
function js(content = ''): Response {
return new Response(content, { headers: { 'Content-Type': 'application/javascript' } })
}
describe('crawl(router)', () => {
it('visits the root path by default', async () => {
let router = createRouter()
router.get('/', () => html('hello'))
let visited: [string, string, string][] = []
for await (let { pathname, filepath, response } of crawl(router)) {
visited.push([pathname, filepath, await response.text()])
}
assert.deepEqual(visited, [['/', '/index.html', 'hello']])
})
it('visits custom initial paths', async () => {
let router = createRouter()
router.get('/a', () => html('A'))
router.get('/b', () => html('B'))
let visited: [string, string, string][] = []
for await (let { pathname, filepath, response } of crawl(router, {
paths: ['/a', '/b'],
})) {
visited.push([pathname, filepath, await response.text()])
}
assert.deepEqual(visited, [
['/a', '/a/index.html', 'A'],
['/b', '/b/index.html', 'B'],
])
})
it('uses correct file paths based on content type', async () => {
let router = createRouter()
router.get('/', () => html(''))
router.get('/about', () => html(''))
router.get('/about/', () => html(''))
router.get('/style.css', () => css('body {}'))
router.get('/app.js', () => js('console.log()'))
let filePaths: string[] = []
for await (let { filepath } of crawl(router, {
paths: ['/', '/about', '/about/', '/style.css', '/app.js'],
})) {
filePaths.push(filepath)
}
assert.deepEqual(filePaths, [
'/index.html',
'/about/index.html',
'/about/index.html',
'/style.css',
'/app.js',
])
})
it('does not follow links when spider is false', async () => {
let router = createRouter()
router.get('/', () => html('About'))
router.get('/about', () => html(''))
let visited: string[] = []
for await (let { pathname } of crawl(router, { spider: false })) {
visited.push(pathname)
}
assert.deepEqual(visited, ['/'])
})
it('follows links by default (spider is true)', async () => {
let router = createRouter()
router.get('/', () => html('About'))
router.get('/about', () => html(''))
let visited: string[] = []
for await (let { pathname } of crawl(router)) {
visited.push(pathname)
}
assert.deepEqual(visited, ['/', '/about'])
})
it('always queues CSS, JS, and image assets regardless of spider mode', async () => {
let router = createRouter()
router.get('/', () =>
html(`
`),
)
router.get('/style.css', () => css('body {}'))
router.get('/app.js', () => js())
router.get('/logo.png', () => new Response('', { headers: { 'Content-Type': 'image/png' } }))
let visited: string[] = []
for await (let { pathname } of crawl(router)) {
visited.push(pathname)
}
assert.deepEqual(visited, ['/', '/style.css', '/app.js', '/logo.png'])
})
it('skips absolute http/https URLs by default', async () => {
let router = createRouter()
router.get('/', () => html('External'))
let visited: string[] = []
for await (let { pathname } of crawl(router)) {
visited.push(pathname)
}
assert.deepEqual(visited, ['/'])
})
it('skips protocol-relative URLs by default', async () => {
let router = createRouter()
router.get('/', () => html('External'))
let visited: string[] = []
for await (let { pathname } of crawl(router)) {
visited.push(pathname)
}
assert.deepEqual(visited, ['/'])
})
it('skips non-navigable href schemes', async () => {
let router = createRouter()
router.get('/', () =>
html(`
Anchor
Email
Phone
JS
Data
Real
`),
)
router.get('/real', () => html(''))
let visited: string[] = []
for await (let { pathname } of crawl(router)) {
visited.push(pathname)
}
assert.deepEqual(visited, ['/', '/real'])
})
it('does not follow links with rel="nofollow"', async () => {
let router = createRouter()
router.get('/', () =>
html('SkipFollow'),
)
router.get('/follow', () => html(''))
let visited: string[] = []
for await (let { pathname } of crawl(router)) {
visited.push(pathname)
}
assert.deepEqual(visited, ['/', '/follow'])
})
it('queues preload/prefetch link elements', async () => {
let router = createRouter()
router.get('/', () =>
html(`
`),
)
router.get('/preload.css', () => css())
router.get('/prefetch.js', () => js())
router.get('/module.js', () => js())
router.get('/real.css', () => css())
let visited: string[] = []
for await (let { pathname } of crawl(router)) {
visited.push(pathname)
}
assert.deepEqual(visited, ['/', '/preload.css', '/prefetch.js', '/module.js', '/real.css'])
})
it('resolves relative hrefs against the current page URL', async () => {
let router = createRouter()
router.get('/blog/', () => html('Post 1'))
router.get('/blog/post-1', () => html(''))
let visited: string[] = []
for await (let { pathname } of crawl(router, { paths: ['/blog/'] })) {
visited.push(pathname)
}
assert.deepEqual(visited, ['/blog/', '/blog/post-1'])
})
it('removes query strings and fragments from crawled paths', async () => {
let router = createRouter()
router.get('/', () => html('About'))
router.get('/about', () => html(''))
let visited: string[] = []
for await (let { pathname } of crawl(router)) {
visited.push(pathname)
}
assert.deepEqual(visited, ['/', '/about'])
})
it('throws on non-2xx responses', async () => {
let router = createRouter()
router.get('/', () => new Response('Not Found', { status: 404, statusText: 'Not Found' }))
await assert.rejects(
async () => {
for await (let _ of crawl(router)) {
}
},
{ message: 'Crawl failed: 404 Not Found (/)' },
)
})
it('writes static redirect pages and visits same-site destinations', async () => {
let router = createRouter()
router.get(
'/old',
() =>
new Response(null, {
status: 308,
headers: { Location: '/new?from=old&mode=fast' },
}),
)
router.get('/new', () => html('