chinese-path.test.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { test } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { promises as fs } from 'node:fs'
  4. import path from 'node:path'
  5. import os from 'node:os'
  6. import { CacheManager } from '../../src/cache/index.js'
  7. import { ChapterReader } from '../../src/storage/adapters/ChapterReader.js'
  8. test('Windows 中文路径全链路', async (t) => {
  9. if (process.platform !== 'win32') {
  10. t.skip('Windows 专用测试')
  11. return
  12. }
  13. const tmpDir = path.join(os.tmpdir(), '测试书仓库', `test-${Date.now()}`)
  14. try {
  15. // 构建含中文目录/文件的 fixture
  16. await fs.mkdir(path.join(tmpDir, '定稿', '正文'), { recursive: true })
  17. await fs.mkdir(path.join(tmpDir, '.cache'), { recursive: true })
  18. const testChapter = `---
  19. 章号: 1
  20. 标题: 测试章节
  21. 卷: 1
  22. 字数: 100
  23. 章定位: 推进
  24. ---
  25. 这是测试正文。`
  26. await fs.writeFile(
  27. path.join(tmpDir, '定稿', '正文', '0001-测试章节.md'),
  28. testChapter,
  29. 'utf8'
  30. )
  31. await fs.writeFile(
  32. path.join(tmpDir, 'book.yaml'),
  33. 'spec_version: "7.0"\n书名: 测试\n类型: 玄幻\n每章目标字数: 3000\n卷规模: 40',
  34. 'utf8'
  35. )
  36. // 重建缓存
  37. const cache = new CacheManager(path.join(tmpDir, '.cache', 'index.db'))
  38. await cache.ensureReady(tmpDir)
  39. // 读取接口
  40. const reader = new ChapterReader(tmpDir, cache)
  41. const result = await reader.readFrontMatter(1)
  42. assert.equal(result.ok, true, `读取失败:${result.error}`)
  43. // 缓存返回英文字段名(title),源文件解析返回中文字段名(标题)
  44. assert.equal(result.data.title || result.data.标题, '测试章节')
  45. // 显式关闭数据库连接(避免 Windows 文件锁导致清理失败)
  46. await cache.close()
  47. } finally {
  48. // 清理
  49. try {
  50. await fs.rm(tmpDir, { recursive: true, force: true })
  51. } catch (err) {
  52. // Windows 文件锁可能导致删除失败,忽略
  53. }
  54. }
  55. })