import { afterEach, describe, expect, it } from 'bun:test' import type { MessageEntry, ViewSection } from '../src/types' import type { ViewsDebugbar } from '../src/views' import { info } from '../src/api' import { currentCollection } from '../src/context' import { createViewsDebugbar } from '../src/views' import { LOCAL, PRODUCTION, recordOf, teardown, temporaryDirectory } from './helpers' afterEach(teardown) const loopback = { requestIP: () => ({ address: '127.0.0.1' }) } function boot(config: Record = {}): ViewsDebugbar { const debugbar = createViewsDebugbar({ ...LOCAL, config: { storage: { path: temporaryDirectory() }, ...config } }) if (!debugbar) throw new Error('The views debugbar did not boot.') return debugbar } const page = (headers: Record = {}): Response => new Response('

Cars

', { headers: { 'content-type': 'text/html; charset=utf-8', ...headers } }) describe('the views integration', () => { it('does not exist in production', () => { expect(createViewsDebugbar({ ...PRODUCTION, config: { enabled: true } })).toBeUndefined() }) it('injects the script into a page and records the render', async () => { const debugbar = boot({ signInAs: { tokenStorageKey: 'app-token' } }) const request = new Request('http://localhost:3000/cars') expect(debugbar.onRequest(request, loopback)).toBeUndefined() info('rendered inside the request') const response = debugbar.onResponse(request, page())! const id = response.headers.get('x-debugbar-id')! const html = await response.text() expect(html).toContain(`data-id="${id}"`) expect(html).toContain('/__debugbar/assets/debugbar.js') expect(html).toContain('app-token') expect(html).toMatch(/<\/script><\/body><\/html>$/) const record = await recordOf(debugbar.runtime, id) expect(record.summary).toMatchObject({ kind: 'page', process: 'views', path: '/cars', status: 200 }) expect((record.collectors.messages as MessageEntry[])[0]!.message).toBe('rendered inside the request') expect((record.collectors.views as ViewSection).fragment).toBe(false) expect(((record.collectors.timeline as { measures: Array<{ label: string }> }).measures).map(entry => entry.label)).toContain('render page') }) it('gives a router fragment the id header and no script', async () => { const debugbar = boot() const request = new Request('http://localhost:3000/cars', { headers: { 'x-stx-router': 'true' } }) debugbar.onRequest(request, loopback) const response = debugbar.onResponse(request, new Response('
Cars
', { headers: { 'content-type': 'text/html', 'x-stx-fragment': 'true', 'x-stx-layout': 'default' } }))! const id = response.headers.get('x-debugbar-id')! expect(await response.text()).toBe('
Cars
') const record = await recordOf(debugbar.runtime, id) expect(record.summary.kind).toBe('fragment') expect(record.collectors.views).toMatchObject({ fragment: true, layout: 'default' }) }) it('does not inject into a compressed page, and still records it', async () => { const debugbar = boot() const request = new Request('http://localhost:3000/cars') debugbar.onRequest(request, loopback) const response = debugbar.onResponse(request, page({ 'content-encoding': 'gzip' }))! expect(response.headers.get('x-debugbar-id')).toBeTruthy() expect(await response.text()).not.toContain('data-stacks-debugbar') }) it('ignores assets, static files and requests it never began', () => { const debugbar = boot() const asset = new Request('http://localhost:3000/assets/app.css') expect(debugbar.onRequest(asset, loopback)).toBeUndefined() expect(currentCollection()).toBeUndefined() expect(debugbar.onResponse(asset, new Response('body{}', { headers: { 'content-type': 'text/css' } }))).toBeUndefined() const download = new Request('http://localhost:3000/export') debugbar.onRequest(download, loopback) expect(debugbar.onResponse(download, new Response('a,b', { headers: { 'content-type': 'text/csv' } }))).toBeUndefined() expect(debugbar.onResponse(new Request('http://localhost:3000/unseen'), page())).toBeUndefined() }) it('passes a proxied API response through with the API record\'s id', async () => { const debugbar = boot() const request = new Request('http://localhost:3000/api/cars') debugbar.onRequest(request, loopback) const proxied = new Response('{}', { headers: { 'content-type': 'application/json', 'x-debugbar-id': '0apirecord00000000000000' } }) expect(debugbar.onResponse(request, proxied)).toBeUndefined() expect(proxied.headers.get('x-debugbar-id')).toBe('0apirecord00000000000000') await debugbar.runtime.store.flush() expect(await debugbar.runtime.store.list()).toEqual([]) }) it('collects nothing and injects nothing for a remote client', () => { const debugbar = boot() const remote = new Request('http://192.168.1.5:3000/cars') expect(debugbar.onRequest(remote, { requestIP: () => ({ address: '192.168.1.20' }) })).toBeUndefined() expect(debugbar.onResponse(remote, page())).toBeUndefined() const tunnelled = new Request('http://localhost:3000/cars', { headers: { 'x-forwarded-for': '203.0.113.9' } }) debugbar.onRequest(tunnelled, loopback) expect(debugbar.onResponse(tunnelled, page())).toBeUndefined() }) it('answers /__debugbar itself, for local clients only', async () => { const debugbar = boot() const local = await debugbar.onRequest(new Request('http://localhost:3000/__debugbar/requests'), loopback) expect(local!.status).toBe(200) const remote = await debugbar.onRequest(new Request('http://localhost:3000/__debugbar/requests'), { requestIP: () => ({ address: '10.0.0.9' }) }) expect(remote!.status).toBe(403) const unknown = await debugbar.onRequest(new Request('http://localhost:3000/__debugbar/requests'), undefined) expect(unknown!.status).toBe(403) }) it('falls back to the socket peer when the server cannot name the client', async () => { const debugbar = createViewsDebugbar({ ...LOCAL, config: { storage: { path: temporaryDirectory() } }, clientAddress: () => null })! expect((await debugbar.onRequest(new Request('http://localhost:3000/__debugbar/requests'), loopback))!.status).toBe(200) expect((await debugbar.onRequest(new Request('http://localhost:3000/__debugbar/requests'), { requestIP: () => ({ address: '10.0.0.9' }) }))!.status).toBe(403) expect((await debugbar.onRequest(new Request('http://localhost:3000/__debugbar/requests'), {}))!.status).toBe(403) }) it('uses the server\'s own idea of the client address when given one', async () => { const debugbar = createViewsDebugbar({ ...LOCAL, config: { storage: { path: temporaryDirectory() } }, clientAddress: () => '198.51.100.4' })! const response = await debugbar.onRequest(new Request('http://localhost:3000/__debugbar/requests'), loopback) expect(response!.status).toBe(403) }) })