Skip to content

Commit

Permalink
[test] added unit tests for map importing
Browse files Browse the repository at this point in the history
  • Loading branch information
silentrald committed Feb 17, 2025
1 parent d81972c commit 8dd4fd1
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 2 deletions.
Binary file added src/__tests__/assets/zip/sample_map_1.zip
Binary file not shown.
Binary file added src/__tests__/assets/zip/sample_map_2.zip
Binary file not shown.
Binary file added src/__tests__/assets/zip/sample_map_3.zip
Binary file not shown.
3 changes: 3 additions & 0 deletions src/__tests__/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import path from "path";

export const ASSETS_FOLDER = path.join(__dirname, "assets");
3 changes: 2 additions & 1 deletion src/__tests__/unit/fs.helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ASSETS_FOLDER } from "__tests__/consts";
import { mkdir, pathExistsSync, rm, writeFile } from "fs-extra";
import { getSize } from "main/helpers/fs.helpers";
import path from "path";
Expand All @@ -11,7 +12,7 @@ jest.mock("electron-log", () => ({
error: jest.fn(),
}));

const TEST_FOLDER = path.resolve(__dirname, "..", "assets", "fs");
const TEST_FOLDER = path.resolve(ASSETS_FOLDER, "fs");

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import path from "path";
import { ASSETS_FOLDER } from "__tests__/consts";
import { createLocalMapsManagerServiceV2 } from "main/services/additional-content/maps/local-maps-manager-v2.service";
import { lastValueFrom } from "rxjs";
import { deleteFolder } from "main/helpers/fs.helpers";
import { ensureDir, existsSync, readdirSync } from "fs-extra";

jest.mock("electron-log", () => ({
info: jest.fn(),
error: jest.fn(),
}));

const TEST_FOLDER = path.join(ASSETS_FOLDER, "zip");
const OUTPUT_FOLDER = path.join(ASSETS_FOLDER, "out");
const VERSION_FOLDER = path.join(OUTPUT_FOLDER, "version");
const SHARED_FOLDER = path.join(OUTPUT_FOLDER, "shared");
const PREFIX_FOLDER = path.join("SharedMaps", "CustomLevels");

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")];

const localMapsManagerV2 = createLocalMapsManagerServiceV2({
// Unused in testing
localVersionService: {
getVersionPath: async () => VERSION_FOLDER,
},
installLocationService: {
sharedContentPath: () => SHARED_FOLDER,
},
songCacheService: {
getMapInfoFromDirname: () => ({
hash: "some-hash-here",
mapInfo: {
version: "1.0",
songName: "some song name",
songAuthorName: "some author name",
levelMappers: ["1st mapper"],
levelLighters: [],
beatsPerMinute: 777,
previewStartTime: 0,
previewDuration: 1000,
songFilename: "song.wav",
songPreviewFilename: "song-preview.wav",
coverImageFilename: "cover.png",
environmentNames: [],
difficulties: [],
},
}),
},
songDetailsCacheService: {
getSongDetails: () => undefined,
},
});

describe("Test local-maps-manager-v2.service.ts", () => {
beforeAll(async () => {
if (existsSync(OUTPUT_FOLDER)) {
await deleteFolder(OUTPUT_FOLDER).catch(() => {});
}
await ensureDir(OUTPUT_FOLDER);
});

afterEach(async () => {
await deleteFolder(OUTPUT_FOLDER);
});

it("Import a single map zip file to shared (info.dat)", async () => {
await lastValueFrom(localMapsManagerV2.importMaps(SAMPLE_MAP_ZIPS.slice(0, 1)));
const folder = path.resolve(SHARED_FOLDER, PREFIX_FOLDER, "sample_map_1");

expect(existsSync(folder)).toBe(true);
expect(existsSync(path.join(folder, "info.dat"))).toBe(true);
expect(readdirSync(folder).length).toBe(1);
});

it("Import a single map zip file to shared (Info.dat)", async () => {
await lastValueFrom(localMapsManagerV2.importMaps(SAMPLE_MAP_ZIPS.slice(1, 2)));
const folder = path.resolve(SHARED_FOLDER, PREFIX_FOLDER, "sample_map_2");

expect(existsSync(folder)).toBe(true);
expect(existsSync(path.join(folder, "Info.dat"))).toBe(true);
expect(readdirSync(folder).length).toBe(1);
});

it("Import a multi map zip file to shared", async () => {
await lastValueFrom(localMapsManagerV2.importMaps(SAMPLE_MAP_ZIPS.slice(2, 3)));
const folder = path.resolve(SHARED_FOLDER, PREFIX_FOLDER);

expect(existsSync(folder)).toBe(true);
expect(readdirSync(folder).length).toBe(4);

for (let i = 1; i <= 4; ++i) {
expect(existsSync(path.join(folder, `map_${i}`))).toBe(true);
}
});

it("Import all zip files", async () => {
await lastValueFrom(localMapsManagerV2.importMaps(SAMPLE_MAP_ZIPS));
const folder = path.resolve(SHARED_FOLDER, PREFIX_FOLDER);

expect(existsSync(folder)).toBe(true);
expect(readdirSync(folder).length).toBe(6);

expect(existsSync(path.join(folder, "sample_map_1", "info.dat"))).toBe(true);
expect(existsSync(path.join(folder, "sample_map_2", "Info.dat"))).toBe(true);
for (let i = 1; i <= 4; ++i) {
expect(existsSync(path.join(folder, `map_${i}`))).toBe(true);
}
});

});
3 changes: 2 additions & 1 deletion src/__tests__/unit/zip.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from "path";
import { mkdir, pathExistsSync, readFile, rm } from "fs-extra";
import { BsmZipExtractor } from "main/models/bsm-zip-extractor.class";
import { ASSETS_FOLDER } from "__tests__/consts";

const TEST_FOLDER = path.resolve(__dirname, "..", "assets", "zip");
const TEST_FOLDER = path.resolve(ASSETS_FOLDER, "zip");
const STANDARD_ZIP = path.join(TEST_FOLDER, "standard.zip");
const WINDOWS_LEGACY_MAP_ZIP = path.join(TEST_FOLDER, "windows_legacy.zip");
const SUBFOLDERS_ZIP = path.join(TEST_FOLDER, "subfolders.zip");
Expand Down

0 comments on commit 8dd4fd1

Please sign in to comment.