diff --git a/frontend/src/__tests__/pr27Features.test.js b/frontend/src/__tests__/pr27Features.test.js index aa41519..4aa015e 100644 --- a/frontend/src/__tests__/pr27Features.test.js +++ b/frontend/src/__tests__/pr27Features.test.js @@ -1,5 +1,6 @@ import { describe, it, expect } from 'vitest' import { recipeNameEn, oilEn } from '../composables/useOilTranslation' +import { matchesPinyinInitials, getPinyinInitials } from '../composables/usePinyinMatch' // --------------------------------------------------------------------------- // EDITOR_ONLY_TAGS includes '已下架' @@ -283,3 +284,95 @@ describe('one-time username change guard', () => { expect(!!user.username_changed).toBe(false) }) }) + +// --------------------------------------------------------------------------- +// Pinyin matching — PR29 extended coverage +// --------------------------------------------------------------------------- +describe('pinyin matching — extended oil names', () => { + it('matches mlk → 麦卢卡', () => { + expect(matchesPinyinInitials('麦卢卡', 'mlk')).toBe(true) + }) + + it('matches tx → 檀香', () => { + expect(matchesPinyinInitials('檀香', 'tx')).toBe(true) + }) + + it('matches xm → 香茅', () => { + expect(matchesPinyinInitials('香茅', 'xm')).toBe(true) + }) + + it('matches gbxz → 古巴香脂', () => { + expect(matchesPinyinInitials('古巴香脂', 'gbxz')).toBe(true) + }) + + it('matches my → 没药', () => { + expect(matchesPinyinInitials('没药', 'my')).toBe(true) + }) + + it('matches xhx → 小茴香', () => { + expect(matchesPinyinInitials('小茴香', 'xhx')).toBe(true) + }) + + it('matches jybh → 椒样薄荷', () => { + expect(matchesPinyinInitials('椒样薄荷', 'jybh')).toBe(true) + }) + + it('matches xbynz → 西班牙牛至', () => { + expect(matchesPinyinInitials('西班牙牛至', 'xbynz')).toBe(true) + }) + + it('matches sc → 顺畅呼吸 prefix', () => { + expect(matchesPinyinInitials('顺畅呼吸', 'sc')).toBe(true) + }) + + it('does not match wrong initials', () => { + expect(matchesPinyinInitials('麦卢卡', 'abc')).toBe(false) + }) + + it('getPinyinInitials returns correct string', () => { + expect(getPinyinInitials('麦卢卡')).toBe('mlk') + expect(getPinyinInitials('檀香')).toBe('tx') + expect(getPinyinInitials('没药')).toBe('my') + }) +}) + +// --------------------------------------------------------------------------- +// Viewer tag visibility — PR29 +// --------------------------------------------------------------------------- +describe('viewer tag visibility logic', () => { + const EDITOR_ONLY_TAGS_VAL = ['已审核', '已下架'] + + it('editor sees all tags', () => { + const allTags = ['美容', '儿童', '已审核', '已下架'] + const canEdit = true + const visible = canEdit ? allTags : [] + expect(visible).toEqual(allTags) + }) + + it('viewer sees no public tags', () => { + const canEdit = false + const myDiary = [ + { tags: ['我的标签'] }, + { tags: ['我的标签', '另一个'] }, + ] + // Viewer: collect tags from own diary only + const myTags = new Set() + for (const d of myDiary) { + for (const t of (d.tags || [])) myTags.add(t) + } + const visible = canEdit ? ['美容', '已审核'] : [...myTags] + expect(visible).toContain('我的标签') + expect(visible).toContain('另一个') + expect(visible).not.toContain('美容') + expect(visible).not.toContain('已审核') + }) + + it('viewer with no diary tags sees empty', () => { + const myDiary = [] + const myTags = new Set() + for (const d of myDiary) { + for (const t of (d.tags || [])) myTags.add(t) + } + expect([...myTags]).toHaveLength(0) + }) +})