library-transfer.service.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  2. import * as fs from 'fs'
  3. import * as os from 'os'
  4. import * as path from 'path'
  5. import type { WallpaperMeta } from '@shared/types'
  6. let roots: string[] = []
  7. let onScreen: string[] = []
  8. const wallpapers = new Map<string, WallpaperMeta>()
  9. const unsubscribe = vi.fn<(id: bigint) => Promise<void>>(async () => {})
  10. vi.mock('electron', () => ({ app: { getPath: () => os.tmpdir() } }))
  11. vi.mock('./config.service', () => ({ getConfiguredBackupPath: () => null }))
  12. vi.mock('../utils/paths', () => ({ getExtraLibraryRoots: () => roots }))
  13. vi.mock('./display.service', () => ({
  14. getScreenAssignments: () => onScreen.map((wallpaperId) => ({ screen: 'DP-1', wallpaperId }))
  15. }))
  16. vi.mock('./steam.service', () => ({ unsubscribeFromItem: (id: bigint) => unsubscribe(id) }))
  17. vi.mock('./library.service', () => ({
  18. getWallpaper: (id: string) => wallpapers.get(id) ?? null,
  19. updateWallpaper: (id: string, patch: Partial<WallpaperMeta>) =>
  20. wallpapers.set(id, { ...wallpapers.get(id)!, ...patch }),
  21. normalizeType: (t?: string) => t ?? 'scene',
  22. normalizeRating: () => 'everyone'
  23. }))
  24. const { transferWallpapers } = await import('./library-transfer.service')
  25. const win = { isDestroyed: () => false, webContents: { send: vi.fn<() => void>() } } as never
  26. let tmp = ''
  27. let target = ''
  28. function addWallpaper(id: string, dir: string, source: WallpaperMeta['source']): void {
  29. fs.mkdirSync(dir, { recursive: true })
  30. fs.writeFileSync(path.join(dir, 'project.json'), JSON.stringify({ preview: 'preview.jpg' }))
  31. fs.writeFileSync(path.join(dir, 'preview.jpg'), 'jpg')
  32. wallpapers.set(id, {
  33. id,
  34. title: id,
  35. type: 'scene',
  36. localPath: dir,
  37. previewLocal: path.join(dir, 'preview.jpg'),
  38. createdAt: 0,
  39. updatedAt: 0,
  40. subscribed: source === 'workshop',
  41. appliedCount: 0,
  42. source,
  43. tags: [],
  44. categories: []
  45. })
  46. }
  47. beforeEach(() => {
  48. tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'wem-transfer-'))
  49. target = path.join(tmp, 'hdd')
  50. fs.mkdirSync(target)
  51. roots = [path.join(tmp, 'ssd'), target]
  52. onScreen = []
  53. wallpapers.clear()
  54. unsubscribe.mockClear()
  55. })
  56. afterEach(() => fs.rmSync(tmp, { recursive: true, force: true }))
  57. describe('transferWallpapers', () => {
  58. it('copies without touching the entry or the original', async () => {
  59. const src = path.join(tmp, 'ssd', '111')
  60. addWallpaper('111', src, 'local')
  61. const result = await transferWallpapers(['111'], target, 'copy', win)
  62. expect(result).toEqual({ done: 1, alreadyThere: 0, failed: [] })
  63. expect(fs.existsSync(path.join(target, '111', 'project.json'))).toBe(true)
  64. expect(fs.existsSync(src)).toBe(true)
  65. expect(wallpapers.get('111')!.localPath).toBe(src)
  66. })
  67. it('moves a local wallpaper and points the entry at the new folder', async () => {
  68. const src = path.join(tmp, 'ssd', 'mine')
  69. addWallpaper('mine', src, 'local')
  70. await transferWallpapers(['mine'], target, 'move', win)
  71. const dest = path.join(target, 'mine')
  72. expect(fs.existsSync(src)).toBe(false)
  73. expect(wallpapers.get('mine')).toMatchObject({ localPath: dest, previewLocal: path.join(dest, 'preview.jpg') })
  74. expect(unsubscribe).not.toHaveBeenCalled()
  75. })
  76. it('unsubscribes a moved workshop wallpaper and makes it local', async () => {
  77. addWallpaper('222', path.join(tmp, 'workshop', '222'), 'workshop')
  78. await transferWallpapers(['222'], target, 'move', win)
  79. expect(unsubscribe).toHaveBeenCalledWith(222n)
  80. expect(wallpapers.get('222')).toMatchObject({ source: 'local', subscribed: false })
  81. })
  82. it('keeps the original when unsubscribing fails', async () => {
  83. const src = path.join(tmp, 'workshop', '333')
  84. addWallpaper('333', src, 'workshop')
  85. unsubscribe.mockRejectedValueOnce(new Error('Steam is not running'))
  86. const result = await transferWallpapers(['333'], target, 'move', win)
  87. expect(result.failed[0].reason).toMatch(/unsubscribing failed/)
  88. expect(fs.existsSync(src)).toBe(true)
  89. expect(wallpapers.get('333')!.source).toBe('workshop')
  90. })
  91. it('refuses to overwrite a different folder and to move what is on a screen', async () => {
  92. addWallpaper('444', path.join(tmp, 'ssd', '444'), 'local')
  93. fs.mkdirSync(path.join(target, '444'))
  94. fs.writeFileSync(path.join(target, '444', 'other.txt'), 'x')
  95. addWallpaper('555', path.join(tmp, 'ssd', '555'), 'local')
  96. onScreen = ['555']
  97. const result = await transferWallpapers(['444', '555'], target, 'move', win)
  98. expect(result.failed.map((f) => f.id)).toEqual(['444', '555'])
  99. expect(fs.existsSync(path.join(tmp, 'ssd', '555'))).toBe(true)
  100. expect(fs.existsSync(path.join(target, '444', 'other.txt'))).toBe(true)
  101. })
  102. it('reuses an identical folder left by an earlier copy after checking it on disk', async () => {
  103. const src = path.join(tmp, 'ssd', '777')
  104. addWallpaper('777', src, 'local')
  105. fs.cpSync(src, path.join(target, '777'), { recursive: true })
  106. expect(await transferWallpapers(['777'], target, 'move', win)).toMatchObject({ done: 1, failed: [] })
  107. expect(fs.existsSync(src)).toBe(false)
  108. expect(wallpapers.get('777')!.localPath).toBe(path.join(target, '777'))
  109. })
  110. it('counts wallpapers already in that library and rejects unknown folders', async () => {
  111. addWallpaper('666', path.join(target, '666'), 'local')
  112. expect(await transferWallpapers(['666'], target, 'copy', win)).toMatchObject({ done: 0, alreadyThere: 1 })
  113. await expect(transferWallpapers(['666'], path.join(tmp, 'elsewhere'), 'copy', win)).rejects.toThrow(
  114. /not a library folder/
  115. )
  116. })
  117. })