const test = require('node:test')
const assert = require('node:assert/strict')
const {
  normalizeMetaTagsForPut,
  normalizePreviewDescriptionForPut,
  pagePutPayloadFromExisting,
} = require('../src/lib/pages-cli')

test('normalizeMetaTagsForPut merges seo/meta/meta_tags/metaTags', () => {
  const merged = normalizeMetaTagsForPut({
    seo: { ogTitle: 'from seo' },
    meta: { description: 'from meta' },
    meta_tags: { keywords: 'a,b' },
    metaTags: { title: 'canonical title', ogTitle: 'from metaTags wins' },
  })
  assert.ok(merged)
  assert.equal(merged.title, 'canonical title')
  assert.equal(merged.description, 'from meta')
  assert.equal(merged.keywords, 'a,b')
  assert.equal(merged.ogTitle, 'from metaTags wins')
})

test('normalizePreviewDescriptionForPut prefers previewDescription', () => {
  assert.equal(
    normalizePreviewDescriptionForPut({
      previewDescription: ' A ',
      excerpt: 'ignored',
    }),
    'A',
  )
})

test('pagePutPayloadFromExisting carries merged metaTags and preview', () => {
  const body = pagePutPayloadFromExisting({
    slug: 'x',
    name: 'N',
    contentId: 'c',
    published: true,
    archived: false,
    deleted: false,
    seo: { ogImage: 'https://example.com/o.jpg' },
    preview_description: 'Excerpt line',
  })
  assert.ok(body.metaTags)
  assert.equal(body.metaTags.ogImage, 'https://example.com/o.jpg')
  assert.equal(body.previewDescription, 'Excerpt line')
})
