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')

/**
 * Regression: multiple <span> text nodes under the same <h1> must not share one field id
 * (old getJsxPath skipped consecutive same tag names and used only the last two levels).
 */
test('from-jsx style field naming: distinct RichText per sibling span in h1', () => {
  const src = `export function HomeHero() {
  return (
    <section>
      <h1>
        <span className="block">Ship high performance</span>
        <span className="row">
          <span className="text-white">experiences.</span>
          <span className="g">Faster</span>
        </span>
      </h1>
    </section>
  );
}
export default HomeHero;
`
  const d = fs.mkdtempSync(path.join(os.tmpdir(), 'bild-sibling-span-'))
  const f = path.join(d, 'H.tsx')
  fs.writeFileSync(f, src)
  const { output, fieldReport } = runCodemod({
    source: src,
    filePath: f,
    cssMode: 'hybrid',
    namingStrategy: 'camelComponent',
    depsFilePath: null,
    fieldMapPath: path.join(d, '.bild', 'm.field-map.json'),
    groupingStyle: 'jsdoc',
  })
  assert.strictEqual(fieldReport.richTextCount, 3)
  const names = new Set()
  for (const m of output.matchAll(/data-bildit-var-name=["']([^"']+)["']/g)) {
    names.add(m[1])
  }
  assert.strictEqual(names.size, 3, 'expected three unique data-bildit-var-name values')
})
