chinese-path.test.js 1023 B

12345678910111213141516171819202122
  1. import { test } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { mkdtemp, mkdir, writeFile, readFile, rm } from 'node:fs/promises'
  4. import { tmpdir } from 'node:os'
  5. import { join } from 'node:path'
  6. // 守护不变量:中文目录名、中文文件名、中文内容在任何平台都必须 UTF-8 正确往返,
  7. // 不依赖系统 locale(Windows 中文环境是一等公民,story-repo-spec §2.2)。
  8. test('中文路径与中文内容 UTF-8 往返一致', async () => {
  9. const base = await mkdtemp(join(tmpdir(), 'wnw-'))
  10. try {
  11. const dir = join(base, '测试书-第05卷')
  12. await mkdir(dir, { recursive: true })
  13. const file = join(dir, '伏笔-031-灭门真凶.md')
  14. const content = '# 北境的雪\n林晚于北境得血书,玄阶令牌现世。\n'
  15. await writeFile(file, content, { encoding: 'utf8' })
  16. const readBack = await readFile(file, { encoding: 'utf8' })
  17. assert.equal(readBack, content)
  18. } finally {
  19. await rm(base, { recursive: true, force: true })
  20. }
  21. })