/** @jest-environment node */ import { parseDesktopFeedback } from './feedback-contract' const feedbackId = '5acdddaa-6c85-4d20-9783-17de944b2e8a' const parse = (data: Record) => parseDesktopFeedback(JSON.stringify({ feedbackId, summary: '导出失败', ...data })) describe('desktop feedback contract', () => { it('normalizes the UUID, trims summary, and accepts 2000 Unicode characters', () => { const summary = '🖼'.repeat(2000) expect(parse({ feedbackId: feedbackId.toUpperCase(), summary: ` ${summary}\n` })).toMatchObject({ feedbackId, summary }) }) it.each(['', ' \n ', '\u0000', '字'.repeat(2001), '🖼'.repeat(2001), null])('rejects invalid summary %#', summary => { expect(() => parse({ summary })).toThrow('2000 字以内') }) it('rejects malformed body, invalid UUID and non-object context', () => { expect(() => parseDesktopFeedback('{')).toThrow('反馈资料格式不正确') expect(() => parse({ feedbackId: 'invalid' })).toThrow('反馈资料格式不正确') expect(() => parse({ context: [] })).toThrow('反馈资料格式不正确') }) it('drops unknown fields and removes credentials, URLs and images at every accepted depth', () => { const result = parse({ requestId: 'dcs_test_payload', context: { Authorization: 'unused-secret', client: { appVersion: '1.0.5', headers: { Cookie: 'sid=unused-secret' }, apiKey: 'unused-secret' }, project: { title: '详情制作', screenshot: { bytes: [1, 2, 3] } }, errors: [ 'Cookie: sid=unused-secret; another=unused-secret', 'b\u0000earer unused-secret', 'x-api-key="unused-secret"', '%41uthorization%3A Basic unused-secret', 'postgres://user:unused-secret@internal.invalid/db', '请求失败 https://service.invalid/?signature=unused-secret', 'signed request /assets/preview?X-Amz-Signature=unused-secret', 'sid=unused-secret; other=unused-secret', 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwC', '附件 data:image/png;base64,unused-image', ], events: [{ requestId: 'req-01', diagnostics: { message: '记录保留', screenshot: [1, 2, 3] } }], }, }) expect(result.requestId).toBe('[敏感信息已移除]') expect(result.snapshot).toEqual({ client: { appVersion: '1.0.5' }, project: { title: '详情制作', screenshot: '[图片已移除]' }, errors: [...Array(9).fill('[敏感信息已移除]'), '[图片已移除]'], events: [{ requestId: 'req-01', diagnostics: { message: '记录保留', screenshot: '[图片已移除]' } }], }) }) it('limits lengths, rows and depth after scanning for secrets', () => { const long = '长'.repeat(1500) const result = parse({ projectId: '项'.repeat(200), context: { errors: Array(16).fill(long), message: `${long} Bearer fake`, diagnostics: { client: { project: { task: { title: '过深' } } } } }, }) expect(result.projectId).toHaveLength(180) expect(result.snapshot).toEqual({ errors: Array(12).fill('长'.repeat(1000)), message: '[敏感信息已移除]', diagnostics: { client: { project: {} } }, }) }) })