feat: 细节优化 #22

Merged
hera merged 4 commits from feat/polish into main 2026-04-10 22:16:05 +00:00
4 changed files with 194 additions and 15 deletions

View File

@@ -0,0 +1,126 @@
import { describe, it, expect } from 'vitest'
import { EDITOR_ONLY_TAGS } from '../stores/recipes'
// ---------------------------------------------------------------------------
// Tag sorting
// ---------------------------------------------------------------------------
describe('Tag sorting', () => {
it('sorts tags alphabetically with localeCompare zh', () => {
const tags = ['香水', '呼吸', '消化', '美容']
const sorted = [...tags].sort((a, b) => a.localeCompare(b, 'zh'))
expect(sorted[0]).toBe('呼吸')
// All sorted
for (let i = 1; i < sorted.length; i++) {
expect(sorted[i - 1].localeCompare(sorted[i], 'zh')).toBeLessThanOrEqual(0)
}
})
it('EDITOR_ONLY_TAGS filters correctly', () => {
const allTags = ['呼吸', '已审核', '消化', '香水']
const visible = allTags.filter(t => !EDITOR_ONLY_TAGS.includes(t))
expect(visible).not.toContain('已审核')
expect(visible).toContain('呼吸')
expect(visible).toHaveLength(3)
})
})
// ---------------------------------------------------------------------------
// Recipe save data format
// ---------------------------------------------------------------------------
describe('Recipe data format', () => {
it('oil_name format overwrites oil format when spread', () => {
// This test documents the bug that was fixed
const localRecipe = {
ingredients: [{ oil: '薰衣草', drops: 3 }],
}
const payload = {
ingredients: [{ oil_name: '薰衣草', drops: 3 }],
}
const merged = { ...localRecipe, ...payload }
// After merge, ingredients have oil_name not oil — this was the bug
expect(merged.ingredients[0]).toHaveProperty('oil_name')
expect(merged.ingredients[0]).not.toHaveProperty('oil')
})
it('loadRecipes mapping converts oil_name to oil', () => {
// Simulate what loadRecipes does
const apiData = [
{ id: 1, name: 'test', ingredients: [{ oil_name: '薰衣草', drops: 3 }], tags: [] }
]
const mapped = apiData.map(r => ({
...r,
ingredients: r.ingredients.map(ing => ({
oil: ing.oil_name ?? ing.oil,
drops: ing.drops,
}))
}))
expect(mapped[0].ingredients[0].oil).toBe('薰衣草')
expect(mapped[0].ingredients[0]).not.toHaveProperty('oil_name')
})
})
// ---------------------------------------------------------------------------
// Volume detection from ingredients
// ---------------------------------------------------------------------------
describe('Volume detection', () => {
const DROPS_PER_ML = 18.6
function guessVolume(eoDrops, cocoDrops) {
const totalDrops = eoDrops + cocoDrops
const ml = totalDrops / DROPS_PER_ML
if (ml <= 2) return 'single'
if (Math.abs(ml - 5) < 1.5) return '5'
if (Math.abs(ml - 10) < 2.5) return '10'
if (Math.abs(ml - 15) < 2.5) return '15'
if (Math.abs(ml - 20) < 3) return '20'
if (Math.abs(ml - 30) < 6) return '30'
return 'custom'
}
it('detects single use (small amounts)', () => {
expect(guessVolume(5, 10)).toBe('single')
})
it('detects 5ml', () => {
expect(guessVolume(15, Math.round(5 * DROPS_PER_ML) - 15)).toBe('5')
})
it('detects 10ml', () => {
expect(guessVolume(20, Math.round(10 * DROPS_PER_ML) - 20)).toBe('10')
})
it('detects 30ml', () => {
expect(guessVolume(50, Math.round(30 * DROPS_PER_ML) - 50)).toBe('30')
})
it('no coconut returns no volume', () => {
// When cocoDrops is 0, function still returns based on total
// But in real code, no coconut → formVolume = ''
expect(guessVolume(10, 0)).toBe('single')
})
it('detects custom for large volumes', () => {
expect(guessVolume(100, 1000)).toBe('custom')
})
})
// ---------------------------------------------------------------------------
// Dilution ratio calculation
// ---------------------------------------------------------------------------
describe('Dilution ratio', () => {
it('calculates ratio correctly', () => {
expect(Math.round(60 / 10)).toBe(6) // 1:6
expect(Math.round(30 / 10)).toBe(3) // 1:3
expect(Math.round(100 / 10)).toBe(10) // 1:10
})
it('snaps to nearest option', () => {
const options = [3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20]
const snap = (ratio) => options.reduce((a, b) => Math.abs(b - ratio) < Math.abs(a - ratio) ? b : a)
expect(snap(6)).toBe(6)
expect([10, 12]).toContain(snap(11)) // equidistant
expect(snap(13)).toBe(12)
expect([12, 15]).toContain(snap(14))
expect(snap(18)).toBe(20)
})
})

View File

@@ -36,8 +36,8 @@ const auth = useAuthStore()
const visibleTags = computed(() => {
if (!props.recipe.tags) return []
if (auth.canEdit) return props.recipe.tags
return props.recipe.tags.filter(t => !EDITOR_ONLY_TAGS.includes(t))
const tags = auth.canEdit ? [...props.recipe.tags] : props.recipe.tags.filter(t => !EDITOR_ONLY_TAGS.includes(t))
return tags.sort((a, b) => a.localeCompare(b, 'zh'))
})
const oilNames = computed(() =>

View File

@@ -48,10 +48,8 @@ export const useRecipesStore = defineStore('recipes', () => {
async function saveRecipe(recipe) {
if (recipe._id) {
const data = await api.put(`/api/recipes/${recipe._id}`, recipe)
const idx = recipes.value.findIndex((r) => r._id === recipe._id)
if (idx !== -1) {
recipes.value[idx] = { ...recipes.value[idx], ...recipe, _version: data._version ?? data.version ?? recipe._version }
}
// Reload from server to get properly formatted data (oil_name → oil mapping)
await loadRecipes()
return data
} else {
const data = await api.post('/api/recipes', recipe)

View File

@@ -42,7 +42,7 @@
<button class="action-chip" :class="{ active: isAllSelected }" @click="toggleSelectAll">
全选<span v-if="totalSelected > 0" class="chip-count">{{ totalSelected }}</span>
</button>
<button class="action-chip" :class="{ active: showTagFilter }" @click="showTagFilter = !showTagFilter">标签</button>
<button class="action-chip" :class="{ active: showTagFilter }" @click="toggleTagFilter">标签</button>
<button v-if="totalSelected > 0" class="action-chip" :class="{ active: showBatchMenu }" @click="showBatchMenu = !showBatchMenu">批量</button>
<button v-if="totalSelected > 0" class="action-chip cancel" @click="clearSelection">取消</button>
<button v-if="auth.isAdmin" class="export-btn" @click="exportExcel" title="导出Excel">📥</button>
@@ -317,8 +317,8 @@
<button class="add-row-btn" @click="addOilRow">+ 添加精油</button>
</div>
<!-- Real-time summary -->
<div class="recipe-summary">
<!-- Real-time summary (only when volume selected) -->
<div v-if="formVolume" class="recipe-summary">
{{ recipeSummaryText }}
</div>
@@ -443,7 +443,13 @@ const formCustomVolume = ref(null)
const formCustomUnit = ref('drops')
const formDilution = ref(6)
const formCocoRow = ref({ oil: '椰子油', drops: 10, _search: '椰子油', _open: false })
const formCocoRow = ref(null)
watch(() => formVolume.value, (vol) => {
if (vol && !formCocoRow.value) {
formCocoRow.value = { oil: '椰子油', drops: vol === 'single' ? 10 : 0, _search: '椰子油', _open: false }
}
})
// EO ingredients (everything except coconut)
const formEoIngredients = computed(() =>
@@ -562,6 +568,15 @@ async function deleteGlobalTag(tag) {
}
}
function toggleTagFilter() {
if (showTagFilter.value) {
showTagFilter.value = false
selectedTags.value = []
} else {
showTagFilter.value = true
}
}
function toggleTag(tag) {
const idx = selectedTags.value.indexOf(tag)
if (idx >= 0) selectedTags.value.splice(idx, 1)
@@ -749,10 +764,31 @@ function editRecipe(recipe) {
const ings = recipe.ingredients || []
formIngredients.value = ings.filter(i => i.oil !== '椰子油').map(i => ({ ...i, _search: i.oil, _open: false }))
const coco = ings.find(i => i.oil === '椰子油')
formCocoRow.value = coco ? { ...coco, _search: '椰子油', _open: false } : { oil: '椰子油', drops: 10, _search: '椰子油', _open: false }
if (coco) {
formCocoRow.value = { ...coco, _search: '椰子油', _open: false }
// Guess volume from total drops
const eoDrops = ings.filter(i => i.oil && i.oil !== '椰子油').reduce((s, i) => s + (i.drops || 0), 0)
const totalDrops = eoDrops + coco.drops
const ml = totalDrops / DROPS_PER_ML
if (ml <= 2) formVolume.value = 'single'
else if (Math.abs(ml - 5) < 1.5) formVolume.value = '5'
else if (Math.abs(ml - 10) < 2.5) formVolume.value = '10'
else if (Math.abs(ml - 15) < 2.5) formVolume.value = '15'
else if (Math.abs(ml - 20) < 3) formVolume.value = '20'
else if (Math.abs(ml - 30) < 6) formVolume.value = '30'
else { formVolume.value = 'custom'; formCustomVolume.value = Math.round(ml) }
// Guess dilution
if (eoDrops > 0 && coco.drops > 0) {
const ratio = Math.round(coco.drops / eoDrops)
const options = [3,4,5,6,7,8,9,10,12,15,20]
formDilution.value = options.reduce((a, b) => Math.abs(b - ratio) < Math.abs(a - ratio) ? b : a)
}
} else {
formCocoRow.value = null
formVolume.value = ''
}
formNote.value = recipe.note || ''
formTags.value = [...(recipe.tags || [])]
calcDilutionFromIngs(recipe.ingredients)
showAddOverlay.value = true
}
@@ -765,7 +801,7 @@ function closeOverlay() {
function resetForm() {
formName.value = ''
formIngredients.value = [{ oil: '', drops: 1, _search: '', _open: false }]
formCocoRow.value = { oil: '椰子油', drops: 10, _search: '椰子油', _open: false }
formCocoRow.value = null
formNote.value = ''
formTags.value = []
smartPasteText.value = ''
@@ -1218,10 +1254,29 @@ function editDiaryRecipe(diary) {
const ings = diary.ingredients || []
formIngredients.value = ings.filter(i => i.oil !== '椰子油').map(i => ({ ...i, _search: i.oil, _open: false }))
const coco = ings.find(i => i.oil === '椰子油')
formCocoRow.value = coco ? { ...coco, _search: '椰子油', _open: false } : { oil: '椰子油', drops: 10, _search: '椰子油', _open: false }
if (coco) {
formCocoRow.value = { ...coco, _search: '椰子油', _open: false }
const eoDrops = ings.filter(i => i.oil && i.oil !== '椰子油').reduce((s, i) => s + (i.drops || 0), 0)
const totalDrops = eoDrops + coco.drops
const ml = totalDrops / DROPS_PER_ML
if (ml <= 2) formVolume.value = 'single'
else if (Math.abs(ml - 5) < 1.5) formVolume.value = '5'
else if (Math.abs(ml - 10) < 2.5) formVolume.value = '10'
else if (Math.abs(ml - 15) < 2.5) formVolume.value = '15'
else if (Math.abs(ml - 20) < 3) formVolume.value = '20'
else if (Math.abs(ml - 30) < 6) formVolume.value = '30'
else { formVolume.value = 'custom'; formCustomVolume.value = Math.round(ml) }
if (eoDrops > 0 && coco.drops > 0) {
const ratio = Math.round(coco.drops / eoDrops)
const options = [3,4,5,6,7,8,9,10,12,15,20]
formDilution.value = options.reduce((a, b) => Math.abs(b - ratio) < Math.abs(a - ratio) ? b : a)
}
} else {
formCocoRow.value = null
formVolume.value = ''
}
formNote.value = diary.note || ''
formTags.value = [...(diary.tags || [])]
calcDilutionFromIngs(diary.ingredients)
showAddOverlay.value = true
}