import assert from 'node:assert/strict' import test from 'node:test' import { readFileSync } from 'node:fs' import { canManageFeedback, feedbackDraftError, feedbackImageError, feedbackLabel, feedbackPageContext, FEEDBACK_STATUSES, FEEDBACK_TYPES } from '../../src/utils/user-feedback.js' const valid = () => ({ type: 'BUG', title: '采购编辑异常', description: '添加物品,填写型号后输入品牌,型号不应丢失。' }) test('only the system administrator sees all feedback and handling controls', () => { assert.equal(canManageFeedback(1), true) assert.equal(canManageFeedback('1'), true) for (const userId of [2, 201, '201', '', undefined, null, true, [1], { id: 1 }]) assert.equal(canManageFeedback(userId), false) const source = readFileSync(new URL('../../src/components/UserFeedback/index.vue', import.meta.url), 'utf8') assert.match(source, / { assert.deepEqual(feedbackPageContext({ path: '/purchase/request?token=secret#details', meta: { title: '采购明细' } }), { pagePath: '/purchase/request', pageTitle: '采购明细' }) for (const path of ['//external.example', 'https://external.example', 'javascript:alert(1)', '']) assert.equal(feedbackPageContext({ path }).pagePath, '') assert.equal(feedbackPageContext({ path: '/' + 'a'.repeat(600), meta: { title: '字'.repeat(120) } }).pagePath.length, 500) assert.equal(feedbackPageContext({ meta: { title: '字'.repeat(120) } }).pageTitle.length, 100) }) test('feedback validates supported images and exact size boundary', () => { for (const type of ['image/png', 'image/jpeg', 'image/webp']) assert.equal(feedbackImageError({ type, size: 5 * 1024 * 1024 }), '') for (const file of [undefined, { type: 'image/svg+xml', size: 100 }, { type: 'image/png', size: 0 }, { type: 'image/png', size: 5 * 1024 * 1024 + 1 }]) assert.ok(feedbackImageError(file)) }) test('feedback requires type, title, description and respects text boundaries', () => { assert.equal(feedbackDraftError(valid(), []), '') assert.equal(feedbackDraftError({ ...valid(), type: 'FEATURE', title: '字'.repeat(100), description: '字'.repeat(5000) }, []), '') for (const fields of [{ type: 'OTHER' }, { title: ' ' }, { description: '\n ' }, { title: '字'.repeat(101) }, { description: '字'.repeat(5001) }]) assert.ok(feedbackDraftError({ ...valid(), ...fields }, [])) }) test('feedback cannot submit pending, failed or unidentified image uploads', () => { const done = { id: 'a'.repeat(32), state: 'done' } assert.equal(feedbackDraftError(valid(), Array.from({ length: 5 }, () => done)), '') assert.ok(feedbackDraftError(valid(), Array.from({ length: 6 }, () => done))) for (const image of [{ id: '', state: 'done' }, { id: done.id, state: 'uploading' }, { id: done.id, state: 'error' }]) assert.ok(feedbackDraftError(valid(), [image])) }) test('feedback uses consistent type/status names and safe unknown labels', () => { assert.equal(feedbackLabel(FEEDBACK_TYPES, 'FEATURE').label, '功能需求') assert.equal(feedbackLabel(FEEDBACK_STATUSES, 'RESOLVED').label, '已解决') assert.equal(feedbackLabel(FEEDBACK_STATUSES, 'FUTURE').label, 'FUTURE') assert.equal(feedbackLabel(FEEDBACK_STATUSES, '').label, '—') }) test('feedback screenshots follow the modal stack, also on non-secure intranet origins', () => { const source = readFileSync(new URL('../../src/components/UserFeedback/index.vue', import.meta.url), 'utf8') const viewer = source.match(/]*>/)?.[0] assert.ok(viewer) assert.doesNotMatch(viewer, /:z-index=/, 'A fixed preview z-index can place images behind an already opened drawer') assert.match(source, /globalThis\.crypto\?\.randomUUID\?\.\(\)\s*\|\|/, 'HTTP intranet screenshot selection needs a local-key fallback') assert.match(source, /detail \|\| detailLoading \|\| detailError/, 'A failed detail load must retain its visible error and back button') })