Skip to content

Commit 8dd4fd1

Browse files
committed
[test] added unit tests for map importing
1 parent d81972c commit 8dd4fd1

File tree

7 files changed

+117
-2
lines changed

7 files changed

+117
-2
lines changed
114 Bytes
Binary file not shown.
114 Bytes
Binary file not shown.
1.09 KB
Binary file not shown.

src/__tests__/consts.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import path from "path";
2+
3+
export const ASSETS_FOLDER = path.join(__dirname, "assets");

src/__tests__/unit/fs.helpers.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ASSETS_FOLDER } from "__tests__/consts";
12
import { mkdir, pathExistsSync, rm, writeFile } from "fs-extra";
23
import { getSize } from "main/helpers/fs.helpers";
34
import path from "path";
@@ -11,7 +12,7 @@ jest.mock("electron-log", () => ({
1112
error: jest.fn(),
1213
}));
1314

14-
const TEST_FOLDER = path.resolve(__dirname, "..", "assets", "fs");
15+
const TEST_FOLDER = path.resolve(ASSETS_FOLDER, "fs");
1516

1617
describe("Test fs.helpers getSize", () => {
1718

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
});

src/__tests__/unit/zip.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import path from "path";
22
import { mkdir, pathExistsSync, readFile, rm } from "fs-extra";
33
import { BsmZipExtractor } from "main/models/bsm-zip-extractor.class";
4+
import { ASSETS_FOLDER } from "__tests__/consts";
45

5-
const TEST_FOLDER = path.resolve(__dirname, "..", "assets", "zip");
6+
const TEST_FOLDER = path.resolve(ASSETS_FOLDER, "zip");
67
const STANDARD_ZIP = path.join(TEST_FOLDER, "standard.zip");
78
const WINDOWS_LEGACY_MAP_ZIP = path.join(TEST_FOLDER, "windows_legacy.zip");
89
const SUBFOLDERS_ZIP = path.join(TEST_FOLDER, "subfolders.zip");

0 commit comments

Comments
 (0)