|
| 1 | +import path from "path"; |
| 2 | +import { ASSETS_FOLDER } from "__tests__/consts"; |
| 3 | +import { createLocalMapsManagerServiceV2 } from "main/services/additional-content/maps/local-maps-manager-v2.service"; |
| 4 | +import { lastValueFrom } from "rxjs"; |
| 5 | +import { deleteFolder } from "main/helpers/fs.helpers"; |
| 6 | +import { ensureDir, existsSync, readdirSync } from "fs-extra"; |
| 7 | + |
| 8 | +jest.mock("electron-log", () => ({ |
| 9 | + info: jest.fn(), |
| 10 | + error: jest.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +const TEST_FOLDER = path.join(ASSETS_FOLDER, "zip"); |
| 14 | +const OUTPUT_FOLDER = path.join(ASSETS_FOLDER, "out"); |
| 15 | +const VERSION_FOLDER = path.join(OUTPUT_FOLDER, "version"); |
| 16 | +const SHARED_FOLDER = path.join(OUTPUT_FOLDER, "shared"); |
| 17 | +const PREFIX_FOLDER = path.join("SharedMaps", "CustomLevels"); |
| 18 | + |
| 19 | +const SAMPLE_MAP_ZIPS = [path.join(TEST_FOLDER, "sample_map_1.zip"), path.join(TEST_FOLDER, "sample_map_2.zip"), path.join(TEST_FOLDER, "sample_map_3.zip")]; |
| 20 | + |
| 21 | +const localMapsManagerV2 = createLocalMapsManagerServiceV2({ |
| 22 | + // Unused in testing |
| 23 | + localVersionService: { |
| 24 | + getVersionPath: async () => VERSION_FOLDER, |
| 25 | + }, |
| 26 | + installLocationService: { |
| 27 | + sharedContentPath: () => SHARED_FOLDER, |
| 28 | + }, |
| 29 | + songCacheService: { |
| 30 | + getMapInfoFromDirname: () => ({ |
| 31 | + hash: "some-hash-here", |
| 32 | + mapInfo: { |
| 33 | + version: "1.0", |
| 34 | + songName: "some song name", |
| 35 | + songAuthorName: "some author name", |
| 36 | + levelMappers: ["1st mapper"], |
| 37 | + levelLighters: [], |
| 38 | + beatsPerMinute: 777, |
| 39 | + previewStartTime: 0, |
| 40 | + previewDuration: 1000, |
| 41 | + songFilename: "song.wav", |
| 42 | + songPreviewFilename: "song-preview.wav", |
| 43 | + coverImageFilename: "cover.png", |
| 44 | + environmentNames: [], |
| 45 | + difficulties: [], |
| 46 | + }, |
| 47 | + }), |
| 48 | + }, |
| 49 | + songDetailsCacheService: { |
| 50 | + getSongDetails: () => undefined, |
| 51 | + }, |
| 52 | +}); |
| 53 | + |
| 54 | +describe("Test local-maps-manager-v2.service.ts", () => { |
| 55 | + beforeAll(async () => { |
| 56 | + if (existsSync(OUTPUT_FOLDER)) { |
| 57 | + await deleteFolder(OUTPUT_FOLDER).catch(() => {}); |
| 58 | + } |
| 59 | + await ensureDir(OUTPUT_FOLDER); |
| 60 | + }); |
| 61 | + |
| 62 | + afterEach(async () => { |
| 63 | + await deleteFolder(OUTPUT_FOLDER); |
| 64 | + }); |
| 65 | + |
| 66 | + it("Import a single map zip file to shared (info.dat)", async () => { |
| 67 | + await lastValueFrom(localMapsManagerV2.importMaps(SAMPLE_MAP_ZIPS.slice(0, 1))); |
| 68 | + const folder = path.resolve(SHARED_FOLDER, PREFIX_FOLDER, "sample_map_1"); |
| 69 | + |
| 70 | + expect(existsSync(folder)).toBe(true); |
| 71 | + expect(existsSync(path.join(folder, "info.dat"))).toBe(true); |
| 72 | + expect(readdirSync(folder).length).toBe(1); |
| 73 | + }); |
| 74 | + |
| 75 | + it("Import a single map zip file to shared (Info.dat)", async () => { |
| 76 | + await lastValueFrom(localMapsManagerV2.importMaps(SAMPLE_MAP_ZIPS.slice(1, 2))); |
| 77 | + const folder = path.resolve(SHARED_FOLDER, PREFIX_FOLDER, "sample_map_2"); |
| 78 | + |
| 79 | + expect(existsSync(folder)).toBe(true); |
| 80 | + expect(existsSync(path.join(folder, "Info.dat"))).toBe(true); |
| 81 | + expect(readdirSync(folder).length).toBe(1); |
| 82 | + }); |
| 83 | + |
| 84 | + it("Import a multi map zip file to shared", async () => { |
| 85 | + await lastValueFrom(localMapsManagerV2.importMaps(SAMPLE_MAP_ZIPS.slice(2, 3))); |
| 86 | + const folder = path.resolve(SHARED_FOLDER, PREFIX_FOLDER); |
| 87 | + |
| 88 | + expect(existsSync(folder)).toBe(true); |
| 89 | + expect(readdirSync(folder).length).toBe(4); |
| 90 | + |
| 91 | + for (let i = 1; i <= 4; ++i) { |
| 92 | + expect(existsSync(path.join(folder, `map_${i}`))).toBe(true); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + it("Import all zip files", async () => { |
| 97 | + await lastValueFrom(localMapsManagerV2.importMaps(SAMPLE_MAP_ZIPS)); |
| 98 | + const folder = path.resolve(SHARED_FOLDER, PREFIX_FOLDER); |
| 99 | + |
| 100 | + expect(existsSync(folder)).toBe(true); |
| 101 | + expect(readdirSync(folder).length).toBe(6); |
| 102 | + |
| 103 | + expect(existsSync(path.join(folder, "sample_map_1", "info.dat"))).toBe(true); |
| 104 | + expect(existsSync(path.join(folder, "sample_map_2", "Info.dat"))).toBe(true); |
| 105 | + for (let i = 1; i <= 4; ++i) { |
| 106 | + expect(existsSync(path.join(folder, `map_${i}`))).toBe(true); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | +}); |
0 commit comments