Skip to content

Commit dce9a3e

Browse files
Add Vitest for testing and implement initial API tests
- Updated package.json to include Vitest as a dev dependency. - Created api.test.js to add tests for the API, including checks for OS name, rule validation, Java API, manifest handling, and miscellaneous functions. - Ensured tests cover both defined methods and expected behaviors.
1 parent 26be77c commit dce9a3e

File tree

5 files changed

+1502
-10
lines changed

5 files changed

+1502
-10
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ java-runtime
99
.DS_Store
1010
dist
1111
build
12-
__pycache__
12+
__pycache__
13+
test.txt

api.test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// windows only test
2+
import index from "./index.js"
3+
import * as java from "./java.js"
4+
import { describe, it, expect } from "vitest"
5+
import fs from "fs/promises"
6+
7+
describe("API exists", () => {
8+
it("Should be defined", () => {
9+
expect(index).toBeDefined()
10+
})
11+
})
12+
13+
describe("API methods", () => {
14+
it("Can get OS name", () => {
15+
expect(index.getOSName()).toBeDefined()
16+
expect(index.getOSName()).eq("windows")
17+
})
18+
it("Can check rules", () => {
19+
expect(index.checkRule({
20+
"action": "allow",
21+
"os": {
22+
"name": "osx"
23+
}
24+
})).eq(false)
25+
expect(index.checkRule({
26+
"action": "allow",
27+
"os": {
28+
"name": "linux"
29+
}
30+
})).eq(false)
31+
expect(index.checkRule({
32+
"action": "allow",
33+
"os": {
34+
"name": "windows"
35+
}
36+
})).eq(true)
37+
})
38+
})
39+
40+
describe("Java API", () => {
41+
it("Should be defined", () => {
42+
expect(java.downloadJava).toBeDefined()
43+
expect(java.downloadJava).toBeTypeOf("function")
44+
})
45+
})
46+
47+
describe("Can handle manifest", () => {
48+
it("Should be defined", async () => {
49+
expect(await index.loadManifest("neoforge-21.1.162.json")).toBeDefined()
50+
})
51+
it("Should merge Manifests", async () => {
52+
expect(await index.mergeManifests(await index.loadManifest("neoforge-21.1.162.json"), await index.loadManifest("1.21.1.json"))).toBeDefined()
53+
expect(await index.mergeManifests(await index.loadManifest("neoforge-21.1.162.json"), await index.loadManifest("1.21.1.json"))).toBeTypeOf("object")
54+
})
55+
})
56+
57+
describe("Misc functions", () => {
58+
it("Should be defined", async () => {
59+
expect(index.downloadFile).toBeDefined()
60+
await fs.rm("test.txt", { force: true })
61+
const download = await index.downloadFile("https://example.com", "test.txt")
62+
expect(download).toBeDefined()
63+
expect(download).toBeTypeOf("boolean")
64+
expect(download).eq(true)
65+
})
66+
})

index.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,24 @@ async function main() {
823823

824824
} // End of main function
825825

826-
main().catch(error => {
827-
console.error("\n--- An error occurred during setup or launch ---");
828-
console.error(error);
829-
process.exit(1);
830-
});
826+
const entryPointScript = process.argv[1].split(path.sep).pop();
827+
if (entryPointScript === __filename || entryPointScript === __dirname.split(path.sep).pop()) {
828+
main().catch(error => {
829+
console.error("\n--- An error occurred during setup or launch ---");
830+
console.error(error);
831+
process.exit(1);
832+
});
833+
}
834+
835+
export default {
836+
downloadFile,
837+
extractNatives,
838+
getOSName,
839+
getArchName,
840+
checkRule,
841+
checkItemRules,
842+
ensureLauncherProfiles,
843+
loadManifest,
844+
mergeManifests,
845+
main
846+
}

0 commit comments

Comments
 (0)