fix: 拖选文字时弹窗不再误关闭 #27

Merged
hera merged 16 commits from fix/next-batch into main 2026-04-12 14:11:46 +00:00
3 changed files with 384 additions and 0 deletions
Showing only changes of commit 6f1b9f3f68 - Show all commits

108
backend/test_translate.py Normal file
View File

@@ -0,0 +1,108 @@
"""Tests for translate.py auto_translate and main.py title_case."""
import pytest
from backend.translate import auto_translate
# ---------------------------------------------------------------------------
# title_case (inlined here since it's a trivial helper in main.py)
# ---------------------------------------------------------------------------
def title_case(s: str) -> str:
return s.strip().title() if s else s
class TestTitleCase:
def test_basic(self):
assert title_case("pain relief") == "Pain Relief"
def test_single_word(self):
assert title_case("sleep") == "Sleep"
def test_preserves_already_cased(self):
assert title_case("Pain Relief") == "Pain Relief"
def test_empty_string(self):
assert title_case("") == ""
def test_none(self):
assert title_case(None) is None
def test_strips_whitespace(self):
assert title_case(" hello world ") == "Hello World"
# ---------------------------------------------------------------------------
# auto_translate
# ---------------------------------------------------------------------------
class TestAutoTranslate:
def test_empty_string(self):
assert auto_translate("") == ""
def test_single_keyword(self):
assert auto_translate("失眠") == "Insomnia"
def test_compound_name(self):
result = auto_translate("助眠配方")
assert "Sleep" in result
assert "Blend" in result
def test_head_pain(self):
result = auto_translate("头痛")
# 头痛 is a single keyword → Headache
assert "Headache" in result
def test_shoulder_neck_massage(self):
result = auto_translate("肩颈按摩")
assert "Neck" in result or "Shoulder" in result
assert "Massage" in result
def test_no_duplicate_words(self):
# 肩颈 → "Neck & Shoulder", but should not duplicate if sub-keys match
result = auto_translate("肩颈护理")
words = result.split()
# No exact duplicate consecutive words
for i in range(len(words) - 1):
if words[i] == words[i + 1]:
pytest.fail(f"Duplicate word '{words[i]}' in '{result}'")
def test_skincare_blend(self):
result = auto_translate("皮肤修复")
assert "Skin" in result
assert "Repair" in result
def test_foot_soak(self):
result = auto_translate("泡脚配方")
assert "Foot Soak" in result or "Foot" in result
def test_ascii_passthrough(self):
# Embedded ASCII letters are preserved
result = auto_translate("DIY面膜")
assert "DIY" in result or "Diy" in result
assert "Face Mask" in result or "Mask" in result
def test_pure_chinese_returns_english(self):
result = auto_translate("薰衣草精华")
# Should not return original Chinese; should have English words
assert any(c.isascii() and c.isalpha() for c in result)
def test_fallback_for_unknown(self):
# Completely unknown chars get skipped; if nothing matches, returns original
result = auto_translate("㊗㊗㊗")
assert result == "㊗㊗㊗"
def test_children_sleep(self):
result = auto_translate("儿童助眠")
assert "Children" in result
assert "Sleep" in result
def test_menstrual_pain(self):
result = auto_translate("痛经调理")
assert "Menstrual Pain" in result or "Menstrual" in result
assert "Therapy" in result
def test_result_is_title_cased(self):
result = auto_translate("排毒按摩")
# Each word should start with uppercase
for word in result.split():
if word == "&":
continue
assert word[0].isupper(), f"'{word}' in '{result}' is not title-cased"

View File

@@ -0,0 +1,190 @@
describe('PR27 Feature Tests', () => {
const ADMIN_TOKEN = 'c86ae7afbe10fabe3c1d5e1a7fee74feaadfd5dc7be2ab62'
const authHeaders = { Authorization: `Bearer ${ADMIN_TOKEN}` }
const TEST_USERNAME = 'cypress_pr27_user'
// -------------------------------------------------------------------------
// API: en_name auto title case on recipe create
// -------------------------------------------------------------------------
describe('API: en_name auto title case', () => {
let recipeId
after(() => {
// Cleanup
if (recipeId) {
cy.request({
method: 'DELETE',
url: `/api/recipes/${recipeId}`,
headers: authHeaders,
failOnStatusCode: false
})
}
})
it('auto title-cases en_name when provided', () => {
cy.request({
method: 'POST',
url: '/api/recipes',
headers: authHeaders,
body: {
name: 'PR27标题测试',
en_name: 'pain relief blend',
ingredients: [{ oil_name: '薰衣草', drops: 3 }],
tags: []
}
}).then(res => {
expect(res.status).to.be.oneOf([200, 201])
recipeId = res.body.id
})
})
it('verifies en_name is title-cased', () => {
cy.request('/api/recipes').then(res => {
const found = res.body.find(r => r.name === 'PR27标题测试')
expect(found).to.exist
expect(found.en_name).to.eq('Pain Relief Blend')
recipeId = found.id
})
})
it('auto translates en_name from Chinese when not provided', () => {
cy.request({
method: 'POST',
url: '/api/recipes',
headers: authHeaders,
body: {
name: '助眠配方',
ingredients: [{ oil_name: '薰衣草', drops: 5 }],
tags: []
}
}).then(res => {
expect(res.status).to.be.oneOf([200, 201])
const autoId = res.body.id
cy.request('/api/recipes').then(listRes => {
const found = listRes.body.find(r => r.id === autoId)
expect(found).to.exist
// auto_translate('助眠配方') should produce English with "Sleep" and "Blend"
expect(found.en_name).to.be.a('string')
expect(found.en_name.length).to.be.greaterThan(0)
expect(found.en_name).to.include('Sleep')
// Cleanup
cy.request({
method: 'DELETE',
url: `/api/recipes/${autoId}`,
headers: authHeaders,
failOnStatusCode: false
})
})
})
})
})
// -------------------------------------------------------------------------
// API: delete user transfers diary recipes to admin
// -------------------------------------------------------------------------
describe('API: delete user transfers diary', () => {
let testUserId
let testUserToken
// Cleanup leftover test user
before(() => {
cy.request({
url: '/api/users',
headers: authHeaders
}).then(res => {
const leftover = res.body.find(u => u.username === TEST_USERNAME)
if (leftover) {
cy.request({
method: 'DELETE',
url: `/api/users/${leftover.id || leftover._id}`,
headers: authHeaders,
failOnStatusCode: false
})
}
})
})
it('creates a test user', () => {
cy.request({
method: 'POST',
url: '/api/users',
headers: authHeaders,
body: {
username: TEST_USERNAME,
display_name: 'PR27 Test User',
role: 'editor'
}
}).then(res => {
expect(res.status).to.be.oneOf([200, 201])
testUserId = res.body.id || res.body._id
testUserToken = res.body.token
expect(testUserId).to.be.a('number')
})
})
it('adds a diary entry for the test user', () => {
const userAuth = { Authorization: `Bearer ${testUserToken}` }
cy.request({
method: 'POST',
url: '/api/diary',
headers: userAuth,
body: {
name: 'PR27用户日记',
ingredients: [{ oil: '乳香', drops: 4 }, { oil: '薰衣草', drops: 2 }],
note: '转移测试'
}
}).then(res => {
expect(res.status).to.be.oneOf([200, 201])
})
})
it('deletes the user and transfers diary to admin', () => {
cy.request({
method: 'DELETE',
url: `/api/users/${testUserId}`,
headers: authHeaders
}).then(res => {
expect(res.status).to.eq(200)
expect(res.body.ok).to.eq(true)
})
})
it('verifies diary was transferred to admin', () => {
cy.request({
url: '/api/diary',
headers: authHeaders
}).then(res => {
expect(res.body).to.be.an('array')
// Transferred diary should have user's name appended
const transferred = res.body.find(d => d.name && d.name.includes('PR27用户日记') && d.name.includes('PR27 Test User'))
expect(transferred).to.exist
expect(transferred.note).to.eq('转移测试')
// Cleanup: delete the transferred diary
if (transferred) {
cy.request({
method: 'DELETE',
url: `/api/diary/${transferred.id}`,
headers: authHeaders,
failOnStatusCode: false
})
}
})
})
})
// -------------------------------------------------------------------------
// UI: 管理配方 login prompt when not logged in
// -------------------------------------------------------------------------
describe('UI: RecipeManager login prompt', () => {
it('shows login prompt when not logged in', () => {
// Clear any stored auth
cy.clearLocalStorage()
cy.visit('/#/manage')
cy.contains('登录后可管理配方').should('be.visible')
cy.contains('登录 / 注册').should('be.visible')
})
})
})

View File

@@ -0,0 +1,86 @@
import { describe, it, expect } from 'vitest'
// ---------------------------------------------------------------------------
// EDITOR_ONLY_TAGS includes '已下架'
// ---------------------------------------------------------------------------
describe('EDITOR_ONLY_TAGS', () => {
it('includes 已审核', async () => {
const { EDITOR_ONLY_TAGS } = await import('../stores/recipes')
expect(EDITOR_ONLY_TAGS).toContain('已审核')
})
it('includes 已下架', async () => {
const { EDITOR_ONLY_TAGS } = await import('../stores/recipes')
expect(EDITOR_ONLY_TAGS).toContain('已下架')
})
it('is an array with at least 2 entries', async () => {
const { EDITOR_ONLY_TAGS } = await import('../stores/recipes')
expect(Array.isArray(EDITOR_ONLY_TAGS)).toBe(true)
expect(EDITOR_ONLY_TAGS.length).toBeGreaterThanOrEqual(2)
})
})
// ---------------------------------------------------------------------------
// English drop/drops pluralization logic
// ---------------------------------------------------------------------------
describe('drop/drops pluralization', () => {
const pluralize = (n) => (n === 1 ? 'drop' : 'drops')
it('singular: 1 drop', () => {
expect(pluralize(1)).toBe('drop')
})
it('plural: 0 drops', () => {
expect(pluralize(0)).toBe('drops')
})
it('plural: 2 drops', () => {
expect(pluralize(2)).toBe('drops')
})
it('plural: 5 drops', () => {
expect(pluralize(5)).toBe('drops')
})
})
// ---------------------------------------------------------------------------
// 已下架 tag filtering logic (pure function extraction)
// ---------------------------------------------------------------------------
describe('已下架 tag filtering', () => {
const recipes = [
{ name: 'Active Recipe', tags: ['头疗'] },
{ name: 'Delisted Recipe', tags: ['已下架'] },
{ name: 'No Tags Recipe', tags: [] },
{ name: 'Multi Tag', tags: ['热门', '已下架'] },
{ name: 'Null Tags', tags: null },
]
const filterDelisted = (list) =>
list.filter((r) => !r.tags || !r.tags.includes('已下架'))
it('removes recipes with 已下架 tag', () => {
const result = filterDelisted(recipes)
expect(result.map((r) => r.name)).not.toContain('Delisted Recipe')
expect(result.map((r) => r.name)).not.toContain('Multi Tag')
})
it('keeps recipes without 已下架 tag', () => {
const result = filterDelisted(recipes)
expect(result.map((r) => r.name)).toContain('Active Recipe')
expect(result.map((r) => r.name)).toContain('No Tags Recipe')
})
it('handles null tags gracefully', () => {
const result = filterDelisted(recipes)
expect(result.map((r) => r.name)).toContain('Null Tags')
})
it('returns empty array for all-delisted list', () => {
const all = [
{ name: 'A', tags: ['已下架'] },
{ name: 'B', tags: ['已下架', '其他'] },
]
expect(filterDelisted(all)).toHaveLength(0)
})
})