// Teilbare Verweise auf einen Befund (Kap. 25, Invariante U-07).

import { describe, it, expect } from 'vitest'
import { buildDeepLink, parseDeepLink, sectionForDifference, VIEW_SECTIONS, isViewSection } from '../deep-links'

describe('VIEW_SECTIONS', () => {
  it('führt die sieben Sektionen der Spezifikation', () => {
    expect(VIEW_SECTIONS).toHaveLength(7)
    expect(isViewSection('werkzeuge')).toBe(true)
    expect(isViewSection('irgendwas')).toBe(false)
  })
})

describe('buildDeepLink', () => {
  it('trägt Vergleich, Sektion und Befund', () => {
    const url = buildDeepLink({ comparisonId: 'abc-123', section: 'werkzeuge', differenceId: 'D-SBM-007' })
    expect(url).toBe('/qaf-differences?comparison=abc-123&section=werkzeuge#D-SBM-007')
  })

  it('kommt ohne Befund aus', () => {
    expect(buildDeepLink({ comparisonId: 'abc', section: 'uebersicht' })).toBe(
      '/qaf-differences?comparison=abc&section=uebersicht',
    )
  })

  it('setzt die Kennung in die Fragment-Kennung', () => {
    // Damit die geöffnete Ansicht ohne erneutes Laden an die Zeile springt.
    expect(buildDeepLink({ comparisonId: 'abc', section: 'datenqualitaet', findingId: 'DQ-B-003' })).toContain(
      '#DQ-B-003',
    )
  })

  it('kodiert Sonderzeichen in der Kennung', () => {
    expect(buildDeepLink({ comparisonId: 'a b&c', section: 'material' })).toContain('comparison=a+b%26c')
  })
})

describe('parseDeepLink', () => {
  it('liest den Verweis zurück', () => {
    const ziel = { comparisonId: 'abc-123', section: 'werkzeuge' as const, differenceId: 'D-SBM-007' }
    expect(parseDeepLink(buildDeepLink(ziel))).toEqual(ziel)
  })

  it('liest einen Befund-Verweis', () => {
    expect(parseDeepLink('/x?comparison=a&section=datenqualitaet#DQ-AB-011')).toEqual({
      comparisonId: 'a',
      section: 'datenqualitaet',
      findingId: 'DQ-AB-011',
    })
  })

  it('weist eine unbekannte Sektion zurück, statt auf die Übersicht zu leiten', () => {
    // Der Empfänger glaubt sonst, er sehe den zitierten Befund.
    expect(parseDeepLink('/x?comparison=a&section=erfunden')).toBeNull()
  })

  it('weist eine Kennung in fremdem Format zurück', () => {
    expect(parseDeepLink('/x?comparison=a&section=material#irgendwas')).toBeNull()
    expect(parseDeepLink('/x?comparison=a&section=material#D-XXX-001')).toBeNull()
  })

  it('weist einen Verweis ohne Vergleichskennung zurück', () => {
    expect(parseDeepLink('/x?section=material')).toBeNull()
    expect(parseDeepLink('/x?comparison=&section=material')).toBeNull()
  })

  it('weist eine Adresse ohne Abfrage zurück', () => {
    expect(parseDeepLink('/qaf-differences')).toBeNull()
  })
})

describe('sectionForDifference', () => {
  it('führt jede Bereichskennung in ihre Sektion', () => {
    expect(sectionForDifference('D-IDT-001')).toBe('praemissen')
    expect(sectionForDifference('D-SUM-002')).toBe('uebersicht')
    expect(sectionForDifference('D-MAT-003')).toBe('material')
    expect(sectionForDifference('D-MFG-004')).toBe('fertigung')
    expect(sectionForDifference('D-SBM-005')).toBe('werkzeuge')
  })

  it('gibt null für eine fremde Kennung zurück', () => {
    expect(sectionForDifference('DQ-B-001')).toBeNull()
    expect(sectionForDifference('D-XXX-001')).toBeNull()
  })

  it('erzeugt zusammen mit buildDeepLink einen lesbaren Verweis', () => {
    const section = sectionForDifference('D-SBM-007')!
    expect(parseDeepLink(buildDeepLink({ comparisonId: 'a', section, differenceId: 'D-SBM-007' }))).toMatchObject({
      section: 'werkzeuge',
      differenceId: 'D-SBM-007',
    })
  })
})
