| 1 | import { test } from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { decodeUploadFilename } from '../dist/studio-server.js'; |
| 4 | |
| 5 | // Regression coverage for issue #9 (CJK attachment filename mojibake). |
| 6 | // The multipart body is read as a latin1 string, so a UTF-8 filename in the |
| 7 | // plain `filename="..."` form arrives with each byte as a latin1 char; we |
| 8 | // round-trip latin1->utf8 to restore it. |
| 9 | |
| 10 | // Simulate what the latin1-read header carries for a UTF-8 filename: take the |
| 11 | // real name, encode to UTF-8 bytes, reinterpret those bytes as latin1 chars. |
| 12 | function asLatin1(realUtf8Name: string): string { |
| 13 | return Buffer.from(realUtf8Name, 'utf8').toString('latin1'); |
| 14 | } |
| 15 | |
| 16 | test('plain filename: CJK name restored from latin1 bytes', () => { |
| 17 | const real = '扑克流程简写语言小白教程.md'; |
| 18 | assert.equal(decodeUploadFilename(undefined, asLatin1(real)), real); |
| 19 | }); |
| 20 | |
| 21 | test('plain filename: mixed CJK + digits + ext', () => { |
| 22 | const real = '局部截取_20260604_223241.png'; |
| 23 | assert.equal(decodeUploadFilename(undefined, asLatin1(real)), real); |
| 24 | }); |
| 25 | |
| 26 | test('plain ASCII filename is unchanged (round-trip is a no-op)', () => { |
| 27 | assert.equal(decodeUploadFilename(undefined, 'report.csv'), 'report.csv'); |
| 28 | }); |
| 29 | |
| 30 | test('RFC 5987 filename* wins and is percent-decoded', () => { |
| 31 | const star = "UTF-8''" + encodeURIComponent('中文文件.md'); |
| 32 | assert.equal(decodeUploadFilename(star, 'fallback.md'), '中文文件.md'); |
| 33 | }); |
| 34 | |
| 35 | test('RFC 5987 with language tag prefix', () => { |
| 36 | const star = "UTF-8'zh-CN'" + encodeURIComponent('数据.json'); |
| 37 | assert.equal(decodeUploadFilename(star, undefined), '数据.json'); |
| 38 | }); |
| 39 | |
| 40 | test('neither form present yields a safe default', () => { |
| 41 | assert.equal(decodeUploadFilename(undefined, undefined), 'upload'); |
| 42 | }); |
| 43 |