Files
summercms/admin/tests/app/runtime.test.ts

75 lines
2.4 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { DEFAULT_BASE, readBase, readBuild, runtime } from '../../src/app/runtime'
function docWith(content: string | null): Document {
const doc = document.implementation.createHTMLDocument('test')
if (content !== null) {
const meta = doc.createElement('meta')
meta.setAttribute('name', 'summer-admin-base')
meta.setAttribute('content', content)
doc.head.appendChild(meta)
}
return doc
}
describe('runtime base', () => {
it('derives the router base and the API base from the served meta', () => {
expect(runtime.base).toBe('/admin-test')
expect(runtime.api).toBe('/admin-test/api/v1')
})
it('reads a custom prefix and trims trailing slashes and whitespace', () => {
expect(readBase(docWith('/acp'))).toBe('/acp')
expect(readBase(docWith(' /acp/panel// '))).toBe('/acp/panel')
})
it.each([
['a missing meta', null],
['an empty value', ''],
['the unreplaced build token', '__SUMMER_ADMIN_BASE__'],
['a relative path', 'backend'],
['an absolute URL', 'https://evil.example/backend'],
['the root alone', '/'],
['slashes only', '///'],
])('falls back to the default for %s', (_label, content) => {
expect(readBase(docWith(content))).toBe(DEFAULT_BASE)
})
it('uses /backend as the default', () => {
expect(DEFAULT_BASE).toBe('/backend')
})
})
function docWithBuild(content: string | null): Document {
const doc = document.implementation.createHTMLDocument('test')
if (content !== null) {
const meta = doc.createElement('meta')
meta.setAttribute('name', 'summer-admin-build')
meta.setAttribute('content', content)
doc.head.appendChild(meta)
}
return doc
}
describe('runtime build', () => {
it('reads a hex build id from the served meta', () => {
expect(readBuild(docWithBuild('0123456789ab'))).toBe('0123456789ab')
expect(readBuild(docWithBuild(' 0123456789ab '))).toBe('0123456789ab')
})
it.each([
['a missing meta', null],
['an empty value', ''],
['the unreplaced build token', '__SUMMER_ADMIN_BUILD__'],
['uppercase hex', '0123456789AB'],
['a value with markup', '<b>0123456789ab</b>'],
['a 65-character hex value', 'a'.repeat(65)],
])('is empty for %s', (_label, content) => {
expect(readBuild(docWithBuild(content))).toBe('')
})
it('is empty in the test document, which carries no build', () => {
expect(runtime.build).toBe('')
})
})