import { describe, expect, it } from 'vitest'
import { buildAiInstructionBlock, copilotExportsGate, fictionalDataDisclaimer } from '../internal/ai-instruction-block'

describe('copilotExportsGate', () => {
  it('rejects when the flag is disabled', () => {
    const result = copilotExportsGate(false)
    expect(result.ok).toBe(false)
    if (!result.ok) expect(result.error.length).toBeGreaterThan(0)
  })

  it('allows when the flag is enabled', () => {
    expect(copilotExportsGate(true)).toEqual({ ok: true })
  })
})

describe('fictionalDataDisclaimer', () => {
  it('returns null for a non-demo project', () => {
    expect(fictionalDataDisclaimer(false)).toBeNull()
  })

  it('returns a non-empty disclaimer for a demo project', () => {
    const text = fictionalDataDisclaimer(true)
    expect(text).not.toBeNull()
    expect(text).toContain('fiktive')
    expect(text).toContain('Demo-Projekt')
  })
})

describe('buildAiInstructionBlock', () => {
  it('carries the LSC/QAF/VSM/FA glossary and the requested about-text/usage hints', () => {
    const block = buildAiInstructionBlock({
      exportTitle: 'Test-Export',
      aboutText: 'Testbeschreibung.',
      usageHints: ['Hinweis eins.', 'Hinweis zwei.'],
      isDemo: false,
    })
    expect(block.heading).toContain('Test-Export')
    expect(block.aboutText).toBe('Testbeschreibung.')
    expect(block.usageHints).toEqual(['Hinweis eins.', 'Hinweis zwei.'])
    expect(block.disclaimer).toBeNull()

    const terms = block.glossary.map((g) => g.term)
    expect(terms).toContain('LSC')
    expect(terms).toContain('QAF')
    expect(terms.some((t) => t.includes('VSM'))).toBe(true)
    expect(terms.some((t) => t.includes('FA'))).toBe(true)
    for (const entry of block.glossary) {
      expect(entry.definition.length).toBeGreaterThan(0)
    }
  })

  it('propagates the demo disclaimer when isDemo is true', () => {
    const block = buildAiInstructionBlock({
      exportTitle: 'Test-Export',
      aboutText: 'Testbeschreibung.',
      usageHints: [],
      isDemo: true,
    })
    expect(block.disclaimer).not.toBeNull()
  })

  it('never mentions a specific customer name (portability, scripts/check-forbidden-strings.mjs)', () => {
    const block = buildAiInstructionBlock({ exportTitle: 'T', aboutText: 'T', usageHints: ['T'], isDemo: true })
    const haystack = JSON.stringify(block).toLowerCase()
    expect(haystack).not.toContain('bmw') // allow-customer-string — asserts absence, not a customer-coupling
  })
})
