- lib/product-doc-parser.ts: parseProductDocMd(md) → {ok, frontmatter, body}
| {ok:false, errors[]} met line-info bij YAML-fouten. Pattern gespiegeld
uit lib/idea-plan-parser.ts.
- lib/product-doc-frontmatter.ts: setProductDocFrontmatterFields(md, patch)
laat de server `last_updated` server-side normaliseren (P2-review-fix
uit docs/recommendations/product-docs-storage-system-review-2026-05-16).
Gebruikt yaml.parseDocument om field-ordering best-effort te behouden.
- todayIsoDate() helper voor 'yyyy-mm-dd' string.
- __tests__: 19 nieuwe tests groen — parse-success/fail-paden, en
expliciete P2-coverage (vervangen + toevoegen last_updated, behoud
overige velden + body).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
108 lines
2.7 KiB
TypeScript
108 lines
2.7 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
|
|
import { parseProductDocMd } from '@/lib/product-doc-parser'
|
|
import {
|
|
setProductDocFrontmatterFields,
|
|
todayIsoDate,
|
|
} from '@/lib/product-doc-frontmatter'
|
|
|
|
const baseMd = `---
|
|
title: "Deploy"
|
|
status: draft
|
|
audience: maintainer
|
|
last_updated: 2020-01-01
|
|
---
|
|
|
|
# Body
|
|
|
|
inhoud
|
|
`
|
|
|
|
describe('setProductDocFrontmatterFields — P2-coverage', () => {
|
|
it('vervangt bestaand last_updated', () => {
|
|
const out = setProductDocFrontmatterFields(baseMd, {
|
|
last_updated: '2026-05-16',
|
|
})
|
|
const parsed = parseProductDocMd(out)
|
|
expect(parsed.ok).toBe(true)
|
|
if (!parsed.ok) return
|
|
expect(parsed.frontmatter.last_updated).toBe('2026-05-16')
|
|
})
|
|
|
|
it('voegt last_updated toe als afwezig', () => {
|
|
const md = `---
|
|
title: "Deploy"
|
|
status: draft
|
|
---
|
|
|
|
body
|
|
`
|
|
const out = setProductDocFrontmatterFields(md, {
|
|
last_updated: '2026-05-16',
|
|
})
|
|
const parsed = parseProductDocMd(out)
|
|
expect(parsed.ok).toBe(true)
|
|
if (!parsed.ok) return
|
|
expect(parsed.frontmatter.last_updated).toBe('2026-05-16')
|
|
})
|
|
|
|
it('behoudt overige frontmatter-velden', () => {
|
|
const out = setProductDocFrontmatterFields(baseMd, {
|
|
last_updated: '2026-05-16',
|
|
})
|
|
const parsed = parseProductDocMd(out)
|
|
expect(parsed.ok).toBe(true)
|
|
if (!parsed.ok) return
|
|
expect(parsed.frontmatter.title).toBe('Deploy')
|
|
expect(parsed.frontmatter.status).toBe('draft')
|
|
expect(parsed.frontmatter.audience).toBe('maintainer')
|
|
})
|
|
|
|
it('behoudt body-inhoud onveranderd', () => {
|
|
const out = setProductDocFrontmatterFields(baseMd, {
|
|
last_updated: '2026-05-16',
|
|
})
|
|
expect(out).toContain('# Body')
|
|
expect(out).toContain('inhoud')
|
|
})
|
|
|
|
it('kan meerdere velden tegelijk patchen', () => {
|
|
const out = setProductDocFrontmatterFields(baseMd, {
|
|
last_updated: '2026-05-16',
|
|
status: 'active',
|
|
})
|
|
const parsed = parseProductDocMd(out)
|
|
expect(parsed.ok).toBe(true)
|
|
if (!parsed.ok) return
|
|
expect(parsed.frontmatter.status).toBe('active')
|
|
expect(parsed.frontmatter.last_updated).toBe('2026-05-16')
|
|
})
|
|
|
|
it('throwed bij ontbrekende frontmatter', () => {
|
|
expect(() =>
|
|
setProductDocFrontmatterFields('# alleen body', { last_updated: 'x' }),
|
|
).toThrow(/yaml-frontmatter/i)
|
|
})
|
|
|
|
it('throwed bij broken yaml', () => {
|
|
const broken = `---
|
|
title: "open quote
|
|
status: draft
|
|
---
|
|
|
|
body`
|
|
expect(() =>
|
|
setProductDocFrontmatterFields(broken, { last_updated: 'x' }),
|
|
).toThrow()
|
|
})
|
|
})
|
|
|
|
describe('todayIsoDate', () => {
|
|
it('returnt yyyy-mm-dd format', () => {
|
|
expect(todayIsoDate()).toMatch(/^\d{4}-\d{2}-\d{2}$/)
|
|
})
|
|
|
|
it('respecteert de meegegeven Date', () => {
|
|
expect(todayIsoDate(new Date('2026-05-16T12:34:56Z'))).toBe('2026-05-16')
|
|
})
|
|
})
|