2023-03-22 07:32:56 -04:00
|
|
|
import { zipFile } from "@/../tests/utils/zip-utils";
|
|
|
|
import { LoaderType } from "@/loaders/loader-type";
|
|
|
|
import mockFs from "mock-fs";
|
|
|
|
import {
|
|
|
|
LoaderMetadataReader,
|
|
|
|
combineLoaderMetadataReaders,
|
|
|
|
createLoaderMetadataReader,
|
|
|
|
createDefaultLoaderMetadataReader,
|
|
|
|
} from "@/loaders/loader-metadata-reader";
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
mockFs({
|
|
|
|
"fabric.jar": await zipFile([__dirname, "../../content/fabric/fabric.mod.json"]),
|
|
|
|
"quilt.jar": await zipFile([__dirname, "../../content/quilt/quilt.mod.json"]),
|
|
|
|
"forge.jar": await zipFile([__dirname, "../../content/forge/mods.toml"], "META-INF/mods.toml"),
|
2024-04-27 05:51:53 -04:00
|
|
|
"neoforge.jar": await zipFile([__dirname, "../../content/neoforge/neoforge.mods.toml"], "META-INF/neoforge.mods.toml"),
|
|
|
|
"neoforge.legacy.jar": await zipFile([__dirname, "../../content/neoforge/neoforge.mods.toml"], "META-INF/mods.toml"),
|
2023-03-22 07:32:56 -04:00
|
|
|
"text.txt": "",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mockFs.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("combineLoaderMetadataReaders", () => {
|
|
|
|
test("combined reader returns metadata from the first underlying reader that successfully reads the metadata", async () => {
|
|
|
|
const reader1 = { readMetadataFile: jest.fn().mockImplementation(x => x === "1" ? Promise.resolve({ id: "1" }) : Promise.reject(new Error("Unknown id"))) } as LoaderMetadataReader;
|
|
|
|
const reader2 = { readMetadataFile: jest.fn().mockImplementation(x => Promise.resolve(x === "2" ? { id: "2" } : undefined)) } as LoaderMetadataReader;
|
|
|
|
|
|
|
|
const combined = combineLoaderMetadataReaders([reader1, reader2]);
|
|
|
|
|
|
|
|
const metadata1 = await combined.readMetadataFile("1");
|
|
|
|
expect(metadata1).toEqual({ id: "1" });
|
|
|
|
expect(reader1.readMetadataFile).toHaveBeenCalledTimes(1);
|
|
|
|
expect(reader1.readMetadataFile).toHaveBeenCalledWith("1");
|
|
|
|
expect(reader2.readMetadataFile).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
const metadata2 = await combined.readMetadataFile("2");
|
|
|
|
expect(metadata2).toEqual({ id: "2" });
|
|
|
|
expect(reader1.readMetadataFile).toHaveBeenCalledTimes(2);
|
|
|
|
expect(reader1.readMetadataFile).toHaveBeenCalledWith("2");
|
|
|
|
expect(reader2.readMetadataFile).toHaveBeenCalledTimes(1);
|
|
|
|
expect(reader2.readMetadataFile).toHaveBeenCalledWith("2");
|
|
|
|
});
|
|
|
|
|
2024-01-15 07:49:16 -05:00
|
|
|
test("combined reader throws when no reader can read the metadata", async () => {
|
2023-03-22 07:32:56 -04:00
|
|
|
const combined = combineLoaderMetadataReaders([]);
|
|
|
|
|
2024-01-15 07:49:16 -05:00
|
|
|
await expect(combined.readMetadataFile("test")).rejects.toThrow();
|
2023-03-22 07:32:56 -04:00
|
|
|
});
|
|
|
|
|
2024-01-15 07:49:16 -05:00
|
|
|
test("combined reader throws if all underlying readers throw", async () => {
|
2023-03-22 07:32:56 -04:00
|
|
|
const reader1 = { readMetadataFile: jest.fn().mockRejectedValue(new Error("Cannot read the metadata file")) } as LoaderMetadataReader;
|
|
|
|
|
|
|
|
const combined = combineLoaderMetadataReaders([reader1]);
|
|
|
|
|
2024-01-15 07:49:16 -05:00
|
|
|
await expect(combined.readMetadataFile("test")).rejects.toThrow();
|
2023-03-22 07:32:56 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("createLoaderMetadataReader", () => {
|
|
|
|
test("creates a reader for every known loader", () => {
|
|
|
|
for (const loader of LoaderType.values()) {
|
|
|
|
expect(createLoaderMetadataReader(loader)).toBeDefined();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test("created reader is able to read metadata of its supported loader", async () => {
|
|
|
|
for (const loader of LoaderType.values()) {
|
|
|
|
const reader = createLoaderMetadataReader(loader);
|
|
|
|
const metadata = await reader.readMetadataFile(`${loader}.jar`);
|
|
|
|
|
|
|
|
expect(metadata).toBeDefined();
|
|
|
|
expect(metadata.version).toBe("0.1.0");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-01-15 07:49:16 -05:00
|
|
|
test("created reader throws for unsupported metadata files", async () => {
|
2023-03-22 07:32:56 -04:00
|
|
|
for (const loader of LoaderType.values()) {
|
|
|
|
const reader = createLoaderMetadataReader(loader);
|
|
|
|
|
2024-01-15 07:49:16 -05:00
|
|
|
await expect(reader.readMetadataFile("text.txt")).rejects.toThrow();
|
2023-03-22 07:32:56 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-01-15 07:49:16 -05:00
|
|
|
test("created reader throws for non-existing files", async () => {
|
2023-03-22 07:32:56 -04:00
|
|
|
for (const loader of LoaderType.values()) {
|
|
|
|
const reader = createLoaderMetadataReader(loader);
|
|
|
|
|
2024-01-15 07:49:16 -05:00
|
|
|
await expect(reader.readMetadataFile("text.json")).rejects.toThrow();
|
2023-03-22 07:32:56 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test("throws an error when unknown loader is provided", () => {
|
|
|
|
expect(() => createLoaderMetadataReader("unknown" as LoaderType)).toThrow("Unknown mod loader 'unknown'.");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("createDefaultLoaderMetadataReader", () => {
|
|
|
|
test("creates a reader that can read metadata from all known loaders", async () => {
|
|
|
|
const reader = createDefaultLoaderMetadataReader();
|
|
|
|
for (const loader of LoaderType.values()) {
|
|
|
|
const metadata = await reader.readMetadataFile(`${loader}.jar`);
|
|
|
|
|
|
|
|
expect(metadata).toBeDefined();
|
|
|
|
expect(metadata.version).toBe("0.1.0");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|