import { describe, expect, it } from 'vitest'

import { AccessKeys, deriveAccessKeyCatalog, findAccessCatalogEntry, normalizeAccessKey } from '../access-keys'
import { accessTestOrganizationModel } from './access-test-fixtures'
import { scanAccessKeyUsages } from './access-key-scan'

describe('access key catalog drift', () => {
  it('keeps runtime literal access keys in the derived catalog', () => {
    const catalog = deriveAccessKeyCatalog(accessTestOrganizationModel)
    const usages = scanAccessKeyUsages()

    const unknownLiterals = usages.stringLiterals
      .map((usage) => {
        const key = normalizeAccessKey(usage.key)
        return findAccessCatalogEntry(catalog, key) === undefined
          ? `${usage.file}: ${JSON.stringify(usage.key)}`
          : null
      })
      .filter((usage): usage is string => usage !== null)

    expect(unknownLiterals).toEqual([])
  })

  it('keeps runtime AccessKeys references backed by exported symbols', () => {
    const usages = scanAccessKeyUsages()
    const exportedSymbols = new Set(Object.keys(AccessKeys))
    const unknownSymbols = usages.accessKeySymbols
      .filter((usage) => !exportedSymbols.has(usage.symbol))
      .map((usage) => `${usage.file}: AccessKeys.${usage.symbol}`)

    expect(unknownSymbols).toEqual([])
  })
})
