Compare commits
3 Commits
feat/next-
...
feat/more-
| Author | SHA1 | Date | |
|---|---|---|---|
| 3c808be7e5 | |||
| 0dfef3ab16 | |||
| 49aa5a0f3c |
87
frontend/src/__tests__/newFeatures.test.js
Normal file
87
frontend/src/__tests__/newFeatures.test.js
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
import { describe, it, expect } from 'vitest'
|
||||||
|
import { parseMultiRecipes } from '../composables/useSmartPaste'
|
||||||
|
import { getPinyinInitials, matchesPinyinInitials } from '../composables/usePinyinMatch'
|
||||||
|
|
||||||
|
const oilNames = ['薰衣草','茶树','柠檬','芳香调理','永久花','椒样薄荷','乳香','檀香','天竺葵','佛手柑','生姜']
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// parseMultiRecipes
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
describe('parseMultiRecipes', () => {
|
||||||
|
it('parses single recipe with name', () => {
|
||||||
|
const results = parseMultiRecipes('舒缓放松,薰衣草3,茶树2', oilNames)
|
||||||
|
expect(results).toHaveLength(1)
|
||||||
|
expect(results[0].name).toBe('舒缓放松')
|
||||||
|
expect(results[0].ingredients).toHaveLength(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('parses recipe with space-separated parts', () => {
|
||||||
|
const results = parseMultiRecipes('长高 芳香调理8 永久花10', oilNames)
|
||||||
|
expect(results).toHaveLength(1)
|
||||||
|
expect(results[0].name).toBe('长高')
|
||||||
|
expect(results[0].ingredients.length).toBeGreaterThanOrEqual(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('parses recipe with concatenated name+oil', () => {
|
||||||
|
const results = parseMultiRecipes('长高芳香调理8永久花10', oilNames)
|
||||||
|
expect(results).toHaveLength(1)
|
||||||
|
expect(results[0].name).toBe('长高')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('parses multiple recipes', () => {
|
||||||
|
const results = parseMultiRecipes('舒缓放松,薰衣草3,茶树2,提神醒脑,柠檬5', oilNames)
|
||||||
|
expect(results).toHaveLength(2)
|
||||||
|
expect(results[0].name).toBe('舒缓放松')
|
||||||
|
expect(results[1].name).toBe('提神醒脑')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('handles recipe with no name', () => {
|
||||||
|
const results = parseMultiRecipes('薰衣草3,茶树2', oilNames)
|
||||||
|
expect(results).toHaveLength(1)
|
||||||
|
expect(results[0].ingredients).toHaveLength(2)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Pinyin matching
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
describe('getPinyinInitials', () => {
|
||||||
|
it('returns correct initials for common oils', () => {
|
||||||
|
expect(getPinyinInitials('薰衣草')).toBe('xyc')
|
||||||
|
expect(getPinyinInitials('茶树')).toBe('cs')
|
||||||
|
expect(getPinyinInitials('生姜')).toBe('sj')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('handles 忍冬花', () => {
|
||||||
|
expect(getPinyinInitials('忍冬花呵护')).toBe('rdhhh')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('matchesPinyinInitials', () => {
|
||||||
|
it('matches prefix only', () => {
|
||||||
|
expect(matchesPinyinInitials('生姜', 's')).toBe(true)
|
||||||
|
expect(matchesPinyinInitials('生姜', 'sj')).toBe(true)
|
||||||
|
expect(matchesPinyinInitials('茶树', 's')).toBe(false) // cs doesn't start with s
|
||||||
|
expect(matchesPinyinInitials('茶树', 'cs')).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not match substring', () => {
|
||||||
|
expect(matchesPinyinInitials('茶树', 's')).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('matches 忍冬花 with r', () => {
|
||||||
|
expect(matchesPinyinInitials('忍冬花呵护', 'r')).toBe(true)
|
||||||
|
expect(matchesPinyinInitials('忍冬花呵护', 'rdh')).toBe(true)
|
||||||
|
expect(matchesPinyinInitials('忍冬花呵护', 'l')).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// EDITOR_ONLY_TAGS
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
describe('EDITOR_ONLY_TAGS', () => {
|
||||||
|
it('exports EDITOR_ONLY_TAGS from recipes store', async () => {
|
||||||
|
const { EDITOR_ONLY_TAGS } = await import('../stores/recipes')
|
||||||
|
expect(EDITOR_ONLY_TAGS).toContain('已审核')
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -20,11 +20,9 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { computed } from 'vue'
|
import { computed } from 'vue'
|
||||||
import { useOilsStore } from '../stores/oils'
|
import { useOilsStore } from '../stores/oils'
|
||||||
import { useRecipesStore } from '../stores/recipes'
|
import { useRecipesStore, EDITOR_ONLY_TAGS } from '../stores/recipes'
|
||||||
import { useAuthStore } from '../stores/auth'
|
import { useAuthStore } from '../stores/auth'
|
||||||
|
|
||||||
const EDITOR_ONLY_TAGS = ['已审核']
|
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
recipe: { type: Object, required: true },
|
recipe: { type: Object, required: true },
|
||||||
index: { type: Number, required: true },
|
index: { type: Number, required: true },
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ import { defineStore } from 'pinia'
|
|||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { api } from '../composables/useApi'
|
import { api } from '../composables/useApi'
|
||||||
|
|
||||||
|
export const EDITOR_ONLY_TAGS = ['已审核']
|
||||||
|
|
||||||
export const useRecipesStore = defineStore('recipes', () => {
|
export const useRecipesStore = defineStore('recipes', () => {
|
||||||
const recipes = ref([])
|
const recipes = ref([])
|
||||||
const allTags = ref([])
|
const allTags = ref([])
|
||||||
|
|||||||
@@ -49,7 +49,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div v-if="showTagFilter" class="tag-list-bar">
|
<div v-if="showTagFilter" class="tag-list-bar">
|
||||||
<span
|
<span
|
||||||
v-for="tag in recipeStore.allTags"
|
v-for="tag in visibleAllTags"
|
||||||
:key="tag"
|
:key="tag"
|
||||||
class="tag-chip"
|
class="tag-chip"
|
||||||
:class="{ active: selectedTags.includes(tag) }"
|
:class="{ active: selectedTags.includes(tag) }"
|
||||||
@@ -126,7 +126,7 @@
|
|||||||
<div class="row-info" @click="editDiaryRecipe(d)">
|
<div class="row-info" @click="editDiaryRecipe(d)">
|
||||||
<span class="row-name">{{ d.name }}</span>
|
<span class="row-name">{{ d.name }}</span>
|
||||||
<span class="row-tags">
|
<span class="row-tags">
|
||||||
<span v-for="t in (d.tags || [])" :key="t" class="mini-tag">{{ t }}</span>
|
<span v-for="t in (d.tags || []).filter(t => auth.canEdit || !EDITOR_ONLY_TAGS.includes(t))" :key="t" class="mini-tag">{{ t }}</span>
|
||||||
</span>
|
</span>
|
||||||
<span class="row-cost">{{ oils.fmtPrice(oils.calcCost(d.ingredients || [])) }}</span>
|
<span class="row-cost">{{ oils.fmtPrice(oils.calcCost(d.ingredients || [])) }}</span>
|
||||||
<span v-if="getDiaryShareStatus(d) === 'shared'" class="share-tag shared">已共享</span>
|
<span v-if="getDiaryShareStatus(d) === 'shared'" class="share-tag shared">已共享</span>
|
||||||
@@ -165,7 +165,7 @@
|
|||||||
<div class="row-info" @click="editRecipe(r)">
|
<div class="row-info" @click="editRecipe(r)">
|
||||||
<span class="row-name">{{ r.name }}</span>
|
<span class="row-name">{{ r.name }}</span>
|
||||||
<span class="row-tags">
|
<span class="row-tags">
|
||||||
<span v-for="t in r.tags" :key="t" class="mini-tag">{{ t }}</span>
|
<span v-for="t in (r.tags || []).filter(t => auth.canEdit || !EDITOR_ONLY_TAGS.includes(t))" :key="t" class="mini-tag">{{ t }}</span>
|
||||||
</span>
|
</span>
|
||||||
<span class="row-cost">{{ oils.fmtPrice(oils.calcCost(r.ingredients)) }}</span>
|
<span class="row-cost">{{ oils.fmtPrice(oils.calcCost(r.ingredients)) }}</span>
|
||||||
</div>
|
</div>
|
||||||
@@ -397,7 +397,7 @@
|
|||||||
import { ref, computed, reactive, onMounted, watch } from 'vue'
|
import { ref, computed, reactive, onMounted, watch } from 'vue'
|
||||||
import { useAuthStore } from '../stores/auth'
|
import { useAuthStore } from '../stores/auth'
|
||||||
import { useOilsStore } from '../stores/oils'
|
import { useOilsStore } from '../stores/oils'
|
||||||
import { useRecipesStore } from '../stores/recipes'
|
import { useRecipesStore, EDITOR_ONLY_TAGS } from '../stores/recipes'
|
||||||
import { useDiaryStore } from '../stores/diary'
|
import { useDiaryStore } from '../stores/diary'
|
||||||
import { useUiStore } from '../stores/ui'
|
import { useUiStore } from '../stores/ui'
|
||||||
import { api } from '../composables/useApi'
|
import { api } from '../composables/useApi'
|
||||||
@@ -744,7 +744,7 @@ function calcDilutionFromIngs() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function editRecipe(recipe) {
|
function editRecipe(recipe) {
|
||||||
editingRecipe.value = recipe
|
editingRecipe.value = { _id: recipe._id, _version: recipe._version, name: recipe.name }
|
||||||
formName.value = recipe.name
|
formName.value = recipe.name
|
||||||
const ings = recipe.ingredients || []
|
const ings = recipe.ingredients || []
|
||||||
formIngredients.value = ings.filter(i => i.oil !== '椰子油').map(i => ({ ...i, _search: i.oil, _open: false }))
|
formIngredients.value = ings.filter(i => i.oil !== '椰子油').map(i => ({ ...i, _search: i.oil, _open: false }))
|
||||||
@@ -1016,12 +1016,17 @@ async function saveCurrentRecipe() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (editingRecipe.value && editingRecipe.value._id) {
|
if (editingRecipe.value && editingRecipe.value._id) {
|
||||||
// Editing an existing public recipe
|
// Editing an existing public recipe — safety check
|
||||||
|
const mappedIngs = cleanIngs.map(i => ({ oil_name: i.oil, drops: i.drops }))
|
||||||
|
if (mappedIngs.length === 0) {
|
||||||
|
const ok = await showConfirm('配方中没有精油成分,确定保存吗?这将清空所有成分。')
|
||||||
|
if (!ok) return
|
||||||
|
}
|
||||||
const payload = {
|
const payload = {
|
||||||
_id: editingRecipe.value._id,
|
_id: editingRecipe.value._id,
|
||||||
_version: editingRecipe.value._version,
|
_version: editingRecipe.value._version,
|
||||||
name: formName.value.trim(),
|
name: formName.value.trim(),
|
||||||
ingredients: cleanIngs.map(i => ({ oil_name: i.oil, drops: i.drops })),
|
ingredients: mappedIngs,
|
||||||
note: formNote.value,
|
note: formNote.value,
|
||||||
tags: formTags.value,
|
tags: formTags.value,
|
||||||
}
|
}
|
||||||
@@ -1123,6 +1128,11 @@ async function loadContribution() {
|
|||||||
const previewRecipeIndex = ref(null)
|
const previewRecipeIndex = ref(null)
|
||||||
const previewRecipeData = ref(null)
|
const previewRecipeData = ref(null)
|
||||||
const showBatchMenu = ref(false)
|
const showBatchMenu = ref(false)
|
||||||
|
const visibleAllTags = computed(() => {
|
||||||
|
const tags = recipeStore.allTags
|
||||||
|
if (auth.canEdit) return tags
|
||||||
|
return tags.filter(t => !EDITOR_ONLY_TAGS.includes(t))
|
||||||
|
})
|
||||||
const showBatchTagPicker = ref(false)
|
const showBatchTagPicker = ref(false)
|
||||||
const batchTagsSelected = ref([])
|
const batchTagsSelected = ref([])
|
||||||
const batchNewTag = ref('')
|
const batchNewTag = ref('')
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ import { ref, computed, onMounted, nextTick, watch } from 'vue'
|
|||||||
import { useRoute, useRouter } from 'vue-router'
|
import { useRoute, useRouter } from 'vue-router'
|
||||||
import { useAuthStore } from '../stores/auth'
|
import { useAuthStore } from '../stores/auth'
|
||||||
import { useOilsStore } from '../stores/oils'
|
import { useOilsStore } from '../stores/oils'
|
||||||
import { useRecipesStore } from '../stores/recipes'
|
import { useRecipesStore, EDITOR_ONLY_TAGS } from '../stores/recipes'
|
||||||
import { useDiaryStore } from '../stores/diary'
|
import { useDiaryStore } from '../stores/diary'
|
||||||
import { useUiStore } from '../stores/ui'
|
import { useUiStore } from '../stores/ui'
|
||||||
import { api } from '../composables/useApi'
|
import { api } from '../composables/useApi'
|
||||||
@@ -315,7 +315,8 @@ const exactResults = computed(() => {
|
|||||||
const q = searchQuery.value.trim().toLowerCase()
|
const q = searchQuery.value.trim().toLowerCase()
|
||||||
return recipeStore.recipes.filter(r => {
|
return recipeStore.recipes.filter(r => {
|
||||||
const nameMatch = r.name.toLowerCase().includes(q)
|
const nameMatch = r.name.toLowerCase().includes(q)
|
||||||
const tagMatch = r.tags && r.tags.some(t => t.toLowerCase().includes(q))
|
const visibleTags = auth.canEdit ? (r.tags || []) : (r.tags || []).filter(t => !EDITOR_ONLY_TAGS.includes(t))
|
||||||
|
const tagMatch = visibleTags.some(t => t.toLowerCase().includes(q))
|
||||||
return nameMatch || tagMatch
|
return nameMatch || tagMatch
|
||||||
}).sort((a, b) => a.name.localeCompare(b.name, 'zh'))
|
}).sort((a, b) => a.name.localeCompare(b.name, 'zh'))
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user