coverage.test.js 1.33 KB
import { describe, it, expect } from 'vitest'
import { computeCoverage, flattenKeys } from '../scripts/check-coverage.js'
import { SUPPORTED_LOCALES, DEFAULT_LOCALE } from '../src/index.js'

describe('@drama/i18n 覆盖率 / key 树同构', () => {
  it('只暴露产品支持的 6 种语言', () => {
    expect(SUPPORTED_LOCALES).toEqual(['en', 'zh', 'ja', 'ko', 'es', 'pt'])
  })

  it('en 基准非空且零缺失(硬门禁)', () => {
    const { baseKeyCount, perLocale } = computeCoverage()
    expect(baseKeyCount).toBeGreaterThan(0)
    expect(perLocale[DEFAULT_LOCALE].missing).toEqual([])
  })

  it('任何语言都不得含 en 没有的私有 key(防 key 树漂移,硬失败)', () => {
    const { perLocale } = computeCoverage()
    for (const code of SUPPORTED_LOCALES) {
      expect(perLocale[code].extra, `${code} 出现私有 key`).toEqual([])
    }
  })

  it('flattenKeys 正确拍平嵌套', () => {
    const keys = flattenKeys({ a: { b: 'x', c: 'y' }, d: 'z' })
    expect([...keys].sort()).toEqual(['a.b', 'a.c', 'd'])
  })

  it('当前全部 6 语言完整(缺失清单为空)—— 一期可放宽为告警,此处记录实际状态', () => {
    const { perLocale } = computeCoverage()
    for (const code of SUPPORTED_LOCALES) {
      expect(perLocale[code].missing, `${code} 缺失 key`).toEqual([])
    }
  })
})