feat(phase6): 자산 경로 구조 옵션(UI/미리보기/Export) + 폰트 preconnect 토글 + extras 자동 보강(robots/manifest) + 문서화
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { exportPage } from '@/lib/exporter/html'
|
||||
import { pageSchema, type Page } from '@/lib/schema/page'
|
||||
import { AssetManager } from '@/lib/assets/assetManager'
|
||||
|
||||
function makePage(): Page {
|
||||
return pageSchema.parse({
|
||||
title: 'Path Strategy',
|
||||
locale: 'en',
|
||||
theme: { primaryColor: '#2563eb', fontFamily: 'Inter' },
|
||||
sections: [ { type: 'hero', props: { heading: 'Hello', subheading: 'World', imageUrl: 'local:hero.png' } } ],
|
||||
form: { actionUrl: '', fields: [], spamProtection: { honeypotFieldName: '_hp', minSubmitSeconds: 2 } },
|
||||
seo: { title: 't', description: 'd' },
|
||||
})
|
||||
}
|
||||
|
||||
describe('자산 경로 구조 옵션', () => {
|
||||
it('grouped 전략일 때 이미지가 assets/images/ 경로로 렌더된다', async () => {
|
||||
const page = makePage()
|
||||
const am = new AssetManager({ maxBytes: 1024 * 1024, allowedExts: ['png'], pathStrategy: 'grouped' })
|
||||
// add hero image asset
|
||||
const bytes = new Uint8Array([1,2,3,4,5])
|
||||
await am.add({ name: 'hero.png', type: 'image/png', bytes })
|
||||
const out = exportPage(page, { assetManager: am })
|
||||
expect(out.html).toMatch(/assets\/images\/[a-f0-9]{8}\.png/i)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,44 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { buildExtrasForPage } from '@/lib/exporter/extras'
|
||||
import { pageSchema, type Page } from '@/lib/schema/page'
|
||||
|
||||
function makePage(): Page {
|
||||
return pageSchema.parse({
|
||||
title: 'My Product',
|
||||
locale: 'en',
|
||||
theme: { primaryColor: '#0ea5e9', fontFamily: 'Inter' },
|
||||
sections: [ { type: 'hero', props: { heading: 'Hello', subheading: '', imageUrl: 'https://example.com/hero.png' } } ],
|
||||
form: { actionUrl: '', fields: [], spamProtection: { honeypotFieldName: '_hp', minSubmitSeconds: 2 } },
|
||||
seo: { title: 't', description: 'd' },
|
||||
})
|
||||
}
|
||||
|
||||
function toStr(bytes: Uint8Array) {
|
||||
return new TextDecoder().decode(bytes)
|
||||
}
|
||||
|
||||
describe('extras 자동 보강', () => {
|
||||
it('manifest: 잘못된 theme_color는 무시하고 기본(primaryColor) 사용', () => {
|
||||
const page = makePage()
|
||||
const out = buildExtrasForPage(page, { manifest: { theme_color: 'not-a-hex' } })
|
||||
const manifest = JSON.parse(toStr(out['site.webmanifest']))
|
||||
expect(manifest.theme_color).toBe('#0ea5e9')
|
||||
})
|
||||
|
||||
it('manifest: short_name은 12자 이내로 자동 절단', () => {
|
||||
const page = makePage()
|
||||
const out = buildExtrasForPage(page, { manifest: { name: 'Super Long Product Name' } })
|
||||
const manifest = JSON.parse(toStr(out['site.webmanifest']))
|
||||
expect(manifest.short_name.length).toBeLessThanOrEqual(12)
|
||||
})
|
||||
|
||||
it('robots: 주어진 plain 경로 목록을 자동으로 User-agent 헤더와 Disallow 라인으로 보강', () => {
|
||||
const page = makePage()
|
||||
const robotsPlain = "/admin\n/private"
|
||||
const out = buildExtrasForPage(page, { robotsText: robotsPlain })
|
||||
const robots = toStr(out['robots.txt'])
|
||||
expect(robots).toMatch(/^User-agent: \*/)
|
||||
expect(robots).toMatch(/Disallow: \/admin/)
|
||||
expect(robots).toMatch(/Disallow: \/private/)
|
||||
})
|
||||
})
|
||||
+19
-3
@@ -17,7 +17,14 @@ function toBytes(s: string): Uint8Array {
|
||||
}
|
||||
|
||||
function buildRobots(base?: string): string {
|
||||
if (base && base.trim().length > 0) return base
|
||||
if (base && base.trim().length > 0) {
|
||||
const s = base.trim()
|
||||
if (/^User-agent:/i.test(s)) return s
|
||||
const lines = s.split(/\n+/).map((l) => l.trim()).filter((l) => l.length > 0)
|
||||
if (lines.length === 0) return 'User-agent: *\n'
|
||||
const disallowLines = lines.map((l) => (l.startsWith('Disallow:') ? l : `Disallow: ${l}`))
|
||||
return `User-agent: *\n${disallowLines.join('\n')}`
|
||||
}
|
||||
return 'User-agent: *\n'
|
||||
}
|
||||
|
||||
@@ -37,9 +44,18 @@ export function buildExtrasForPage(page: Page, overrides?: ExtrasOverrides): Rec
|
||||
const robotsText = buildRobots(overrides?.robotsText)
|
||||
out['robots.txt'] = toBytes(robotsText)
|
||||
|
||||
// site.webmanifest
|
||||
// site.webmanifest (auto-enrich and safe-merge)
|
||||
const manifestBase = deriveManifest(page)
|
||||
const manifestMerged = { ...manifestBase, ...(overrides?.manifest || {}) }
|
||||
const ov = overrides?.manifest || {}
|
||||
const name = ov.name && ov.name.trim().length > 0 ? ov.name : manifestBase.name
|
||||
const short_name = ov.short_name && ov.short_name.trim().length > 0
|
||||
? ov.short_name
|
||||
: (name.length > 12 ? name.slice(0, 12) : name)
|
||||
const start_url = ov.start_url && ov.start_url.trim().length > 0 ? ov.start_url : manifestBase.start_url
|
||||
const display = ov.display && ov.display.trim().length > 0 ? ov.display : manifestBase.display
|
||||
const hexOk = ov.theme_color ? /^#([0-9a-fA-F]{3}){1,2}$/.test(ov.theme_color) : false
|
||||
const theme_color = hexOk ? (ov.theme_color as string) : manifestBase.theme_color
|
||||
const manifestMerged = { name, short_name, start_url, display, theme_color }
|
||||
const manifestJson = JSON.stringify(manifestMerged, null, 2)
|
||||
out['site.webmanifest'] = toBytes(manifestJson)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user