import { describe, expect, test } from 'bun:test'
import { mergeSectionIntoCodeMd, assertWithinSize } from './mergeSection.js'
import { buildInitialCodeMdTemplate } from './codeMdTemplate.js'

describe('mergeSection', () => {
  test('upsert architecture section', () => {
    const base = buildInitialCodeMdTemplate()
    const out = mergeSectionIntoCodeMd(
      base,
      'architecture',
      '模块 A 在 src/a',
      'upsert',
    )
    expect(out).toContain('## architecture')
    expect(out).toContain('模块 A 在 src/a')
  })

  test('append adds dated line', () => {
    const base = '## gotchas\n\n'
    const out = mergeSectionIntoCodeMd(base, 'gotchas', 'need env', 'append')
    expect(out).toContain('need env')
    expect(out).toMatch(/-\s+\d{4}-\d{2}-\d{2}:/)
  })

  test('assertWithinSize throws when too large', () => {
    expect(() => assertWithinSize('x'.repeat(50_000))).toThrow()
  })
})
