const test = require('node:test')
const assert = require('node:assert')
const fs = require('fs')
const os = require('os')
const path = require('path')

const { runCodemod } = require('../src/lib/codemod')

test('from-jsx emits RichText as .text in JSX (object shape)', () => {
  const src = `export default function F() {
  return (
    <p>Hello</p>
  )
}
`
  const d = fs.mkdtempSync(path.join(os.tmpdir(), 'bild-rtext-'))
  const f = path.join(d, 'C.jsx')
  fs.writeFileSync(f, src)
  const { output, fieldReport } = runCodemod({
    source: src,
    filePath: f,
    cssMode: 'hybrid',
    namingStrategy: 'camelComponent',
    depsFilePath: null,
    fieldMapPath: path.join(d, '.bild', 'c.field-map.json'),
    groupingStyle: 'lineComments',
  })
  assert.ok(fieldReport.richTextCount >= 1)
  assert.match(output, /\{helloText\.text\}/, 'RichText value should render .text, not the object')
  assert.match(
    output,
    /RichText="Hello"|RichText=\{\s*text:\s*"Hello"/,
    'text-only default is quoted or object with text:',
  )
})
