|
| 1 | +import { mkdtemp, mkdir, readdir, rm, stat, writeFile } from 'node:fs/promises' |
| 2 | +import os from 'node:os' |
| 3 | +import path from 'node:path' |
| 4 | +import { expect, test } from 'vitest' |
| 5 | +import { cleanup } from './cleanup.ts' |
| 6 | + |
| 7 | +test('cleanup removes caches and deletes data file when empty', async () => { |
| 8 | + const root = await mkdtemp(path.join(os.tmpdir(), 'epicshop-cleanup-')) |
| 9 | + const cacheDir = path.join(root, 'cache') |
| 10 | + const legacyCacheDir = path.join(root, 'legacy-cache') |
| 11 | + const dataPath = path.join(root, 'data.json') |
| 12 | + const reposDir = path.join(root, 'repos') |
| 13 | + |
| 14 | + try { |
| 15 | + await mkdir(reposDir, { recursive: true }) |
| 16 | + await mkdir(cacheDir, { recursive: true }) |
| 17 | + await mkdir(legacyCacheDir, { recursive: true }) |
| 18 | + await writeFile(path.join(cacheDir, 'cache.json'), '{}') |
| 19 | + await writeFile(path.join(legacyCacheDir, 'legacy.json'), '{}') |
| 20 | + await writeFile( |
| 21 | + dataPath, |
| 22 | + JSON.stringify( |
| 23 | + { |
| 24 | + preferences: { player: { muted: true } }, |
| 25 | + authInfos: { 'www.epicweb.dev': { id: 'user-1' } }, |
| 26 | + mutedNotifications: ['notice-1'], |
| 27 | + }, |
| 28 | + null, |
| 29 | + 2, |
| 30 | + ), |
| 31 | + ) |
| 32 | + |
| 33 | + const result = await cleanup({ |
| 34 | + silent: true, |
| 35 | + force: true, |
| 36 | + targets: ['caches', 'preferences', 'auth'], |
| 37 | + paths: { |
| 38 | + cacheDir, |
| 39 | + legacyCacheDir, |
| 40 | + dataPaths: [dataPath], |
| 41 | + reposDir, |
| 42 | + }, |
| 43 | + }) |
| 44 | + |
| 45 | + expect(result.success).toBe(true) |
| 46 | + await expect(stat(cacheDir)).rejects.toThrow() |
| 47 | + await expect(stat(legacyCacheDir)).rejects.toThrow() |
| 48 | + await expect(stat(dataPath)).rejects.toThrow() |
| 49 | + } finally { |
| 50 | + await rm(root, { recursive: true, force: true }) |
| 51 | + } |
| 52 | +}) |
| 53 | + |
| 54 | +test('cleanup removes workshops but keeps non-workshop entries', async () => { |
| 55 | + const root = await mkdtemp(path.join(os.tmpdir(), 'epicshop-cleanup-')) |
| 56 | + const reposDir = path.join(root, 'repos') |
| 57 | + const workshopDir = path.join(reposDir, 'sample-workshop') |
| 58 | + const keepDir = path.join(reposDir, 'notes') |
| 59 | + |
| 60 | + try { |
| 61 | + await mkdir(workshopDir, { recursive: true }) |
| 62 | + await writeFile( |
| 63 | + path.join(workshopDir, 'package.json'), |
| 64 | + JSON.stringify( |
| 65 | + { |
| 66 | + name: 'sample-workshop', |
| 67 | + epicshop: { title: 'Sample Workshop' }, |
| 68 | + }, |
| 69 | + null, |
| 70 | + 2, |
| 71 | + ), |
| 72 | + ) |
| 73 | + await mkdir(keepDir, { recursive: true }) |
| 74 | + await writeFile(path.join(keepDir, 'notes.txt'), 'keep') |
| 75 | + |
| 76 | + const result = await cleanup({ |
| 77 | + silent: true, |
| 78 | + force: true, |
| 79 | + targets: ['workshops'], |
| 80 | + workshops: ['sample-workshop'], |
| 81 | + workshopTargets: ['files'], |
| 82 | + paths: { reposDir }, |
| 83 | + }) |
| 84 | + |
| 85 | + expect(result.success).toBe(true) |
| 86 | + await expect(stat(workshopDir)).rejects.toThrow() |
| 87 | + |
| 88 | + const remaining = await readdir(reposDir) |
| 89 | + expect(remaining).toContain('notes') |
| 90 | + } finally { |
| 91 | + await rm(root, { recursive: true, force: true }) |
| 92 | + } |
| 93 | +}) |
| 94 | + |
| 95 | +test('cleanup removes offline videos directory', async () => { |
| 96 | + const root = await mkdtemp(path.join(os.tmpdir(), 'epicshop-cleanup-')) |
| 97 | + const offlineVideosDir = path.join(root, 'offline-videos') |
| 98 | + const indexPath = path.join(offlineVideosDir, 'index.json') |
| 99 | + const videoPath = path.join(offlineVideosDir, 'video.mp4') |
| 100 | + const reposDir = path.join(root, 'repos') |
| 101 | + |
| 102 | + try { |
| 103 | + await mkdir(reposDir, { recursive: true }) |
| 104 | + await mkdir(offlineVideosDir, { recursive: true }) |
| 105 | + await writeFile(videoPath, 'video-data') |
| 106 | + await writeFile( |
| 107 | + indexPath, |
| 108 | + JSON.stringify( |
| 109 | + { |
| 110 | + video123: { |
| 111 | + playbackId: 'video123', |
| 112 | + fileName: 'video.mp4', |
| 113 | + size: 10, |
| 114 | + workshops: [{ id: 'workshop-1', title: 'Workshop' }], |
| 115 | + }, |
| 116 | + }, |
| 117 | + null, |
| 118 | + 2, |
| 119 | + ), |
| 120 | + ) |
| 121 | + |
| 122 | + const result = await cleanup({ |
| 123 | + silent: true, |
| 124 | + force: true, |
| 125 | + targets: ['offline-videos'], |
| 126 | + paths: { offlineVideosDir, reposDir }, |
| 127 | + }) |
| 128 | + |
| 129 | + expect(result.success).toBe(true) |
| 130 | + await expect(stat(offlineVideosDir)).rejects.toThrow() |
| 131 | + } finally { |
| 132 | + await rm(root, { recursive: true, force: true }) |
| 133 | + } |
| 134 | +}) |
0 commit comments