Skip to content

Commit

Permalink
Add tests based on official spritepacks
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamscached committed Mar 29, 2024
1 parent 8d03897 commit 81e6988
Show file tree
Hide file tree
Showing 8 changed files with 2,433 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Run official spritepack tests

on:
pull_request:
push:

jobs:
test:
name: Test schema files on official spritepacks
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup NodeJS
uses: actions/setup-node@v4
with: { node-version: 21 }
- name: Install packages
run: cd test && yarn install
- name: Run tests
run: cd test && yarn test
2 changes: 2 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
mas_git
44 changes: 44 additions & 0 deletions test/git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { stat } from "fs/promises";
import { exec } from "child_process";
import { glob } from "glob";

const masGitUrl = "https://github.com/Monika-After-Story/MonikaModDev.git";
const masGitDir = "mas_git";

async function isMasGitPresent() {
try {
await stat(masGitDir);
return true;
} catch (e) {
if (e.code === "ENOENT") return false;
throw e;
}
}

async function cloneMasGit() {
return await new Promise((resolve, reject) => {
exec(`git clone --depth=1 ${masGitUrl} ${masGitDir}`, (err) => {
if (!err) resolve();
else reject(err);
});
});
}

async function updateMasGit() {
return await new Promise((resolve, reject) => {
exec(`git pull origin master`, { cwd: masGitDir }, (err) => {
if (!err) resolve();
else reject(err);
})
});
}

export async function syncMasGit() {
const exists = await isMasGitPresent();
if (!exists) await cloneMasGit();
else await updateMasGit();
}

export async function getJsonList(type = "released") {
return await glob(`${masGitDir}/spritepacks/${type}/**/*.json`);
}
3 changes: 3 additions & 0 deletions test/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"setupFilesAfterEnv": ["jest-expect-message"]
}
17 changes: 17 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"type": "module",
"scripts": {
"test": "node --experimental-vm-modules $(yarn bin jest)"
},
"dependencies": {
"ajv": "^8.12.0",
"glob": "^10.3.12"
},
"devDependencies": {
"jest": "^29.7.0",
"jest-expect-message": "^1.1.3"
},
"resolutions": {
"string-width": "4.2.3"
}
}
30 changes: 30 additions & 0 deletions test/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { readFile } from "fs/promises";
import Ajv from "ajv";

const ajv = new Ajv({
allowUnionTypes: true,
allErrors: true,
allowMatchingProperties: true
});

async function compileSchemaJson(name) {
const json = await readFile(`../${name}.schema.json`, { encoding: "utf-8" });
return ajv.compile(JSON.parse(json));
}

const schema = {};
schema["0"] = await compileSchemaJson("acs");
schema["0-split"] = await compileSchemaJson("acs-split");
schema["1"] = await compileSchemaJson("hair");
schema["2"] = await compileSchemaJson("clothes");

export function inferSchema(json) {
const { type, arm_split } = json;
if (type === 0) return schema[!!arm_split ? "0-split" : "0"];
return schema[type];
}

export async function readJson(path) {
const json = await readFile(path, { encoding: "utf-8" });
return JSON.parse(json);
}
19 changes: 19 additions & 0 deletions test/schema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getJsonList, syncMasGit } from "./git.js";
import { inferSchema, readJson } from "./schema.js";

function testJson(path, json) {
test(path, () => {
const validate = inferSchema(json);
const pass = validate(json);
const errors = validate.errors ?? [];
const message = JSON.stringify(errors, null, 2);
expect(pass, message, { showPrefix: false }).toBeTruthy();
});
}

await syncMasGit();
await Promise.all((await getJsonList())
.map(async (it) => {
const json = await readJson(it);
testJson(it, json);
}));
Loading

0 comments on commit 81e6988

Please sign in to comment.