test: PR#27新增单元测试和e2e测试
All checks were successful
PR Preview / teardown-preview (pull_request) Has been skipped
Test / unit-test (push) Successful in 5s
Test / build-check (push) Successful in 3s
PR Preview / test (pull_request) Successful in 5s
PR Preview / deploy-preview (pull_request) Successful in 17s
Test / e2e-test (push) Successful in 53s

后端: auto_translate/title_case 14个测试
前端: EDITOR_ONLY_TAGS、drop单复数、已下架过滤 11个测试
E2E: en_name title case、删除用户转移配方、管理配方登录引导

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-12 10:26:44 +00:00
parent 0f7ae6ecc7
commit 6f1b9f3f68
3 changed files with 384 additions and 0 deletions

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)
})
})