import { describe, expect, it } from 'vitest' import type { ArticleCapability, CoverageEntry } from '@/composables/useArticles' import { filterRows, versionRows } from '@/lib/editorVersions' function entry( id: number, lang: string, capabilities: ArticleCapability[] = ['read', 'edit'], language_enabled = true, ): CoverageEntry { return { id, lang, title: lang, is_online: id === 10, language_enabled, capabilities } } const translations = [entry(10, 'en'), entry(13, 'ar', ['read']), entry(12, 'fr')] function rows(capabilities: ArticleCapability[], enabled = ['en', 'fr', 'de', 'ar']) { return versionRows({ translations, currentId: 12, capabilities, enabledLanguages: enabled }) } describe('versionRows', () => { it('tolerates an entry without capabilities and tags it read only', () => { const broken = { ...entry(13, 'ar'), capabilities: null } as unknown as CoverageEntry const list = versionRows({ translations: [entry(12, 'fr'), broken], currentId: 12, capabilities: ['read'], enabledLanguages: ['fr', 'ar'], }) expect(list.find((row) => row.lang === 'ar')?.readOnly).toBe(true) }) it('follows translations[] order and marks the current row', () => { const list = rows(['read', 'edit']) expect(list.map((row) => [row.lang, row.current])).toEqual([ ['en', false], ['ar', false], ['fr', true], ]) }) it('tags a version without edit as read only and keeps editable ones untagged', () => { const list = rows(['read', 'edit']) expect(list.find((row) => row.lang === 'ar')?.readOnly).toBe(true) expect(list.find((row) => row.lang === 'en')?.readOnly).toBe(false) }) it('offers missing enabled languages only with create_translation', () => { expect(rows(['read', 'edit']).some((row) => row.addTranslation)).toBe(false) const withAdd = rows(['read', 'edit', 'create_translation']) expect(withAdd.filter((row) => row.addTranslation).map((row) => row.lang)).toEqual(['de']) }) it('keeps a disabled language listed and marked', () => { const list = versionRows({ translations: [entry(10, 'en'), entry(12, 'fr', ['read', 'edit'], false)], currentId: 10, capabilities: ['read'], enabledLanguages: ['en'], }) expect(list.find((row) => row.lang === 'fr')?.languageDisabled).toBe(true) }) }) describe('filterRows', () => { it('narrows by language name, ignoring case', () => { const list = rows(['read']) expect(filterRows(list, 'FRE').map((row) => row.lang)).toEqual(['fr']) expect(filterRows(list, '')).toHaveLength(3) }) })