mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-04-02 19:55:57 +08:00
- 引入基于YAML和JSON Schema的静态内容配置系统 - 实现运行时只读查询和Source Generator支持 - 提供VS Code扩展用于配置浏览、验证和轻量编辑 - 支持开发期热重载和跨表引用校验功能 - 包含完整的文档说明和工具链集成
112 lines
2.9 KiB
JavaScript
112 lines
2.9 KiB
JavaScript
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const {
|
|
applyScalarUpdates,
|
|
parseSchemaContent,
|
|
parseTopLevelYaml,
|
|
validateParsedConfig
|
|
} = require("../src/configValidation");
|
|
|
|
test("parseSchemaContent should capture scalar and array property metadata", () => {
|
|
const schema = parseSchemaContent(`
|
|
{
|
|
"type": "object",
|
|
"required": ["id", "name"],
|
|
"properties": {
|
|
"id": { "type": "integer" },
|
|
"name": { "type": "string" },
|
|
"dropRates": {
|
|
"type": "array",
|
|
"items": { "type": "integer" }
|
|
}
|
|
}
|
|
}
|
|
`);
|
|
|
|
assert.deepEqual(schema.required, ["id", "name"]);
|
|
assert.deepEqual(schema.properties, {
|
|
id: {type: "integer"},
|
|
name: {type: "string"},
|
|
dropRates: {type: "array", itemType: "integer"}
|
|
});
|
|
});
|
|
|
|
test("validateParsedConfig should report missing and unknown properties", () => {
|
|
const schema = parseSchemaContent(`
|
|
{
|
|
"type": "object",
|
|
"required": ["id", "name"],
|
|
"properties": {
|
|
"id": { "type": "integer" },
|
|
"name": { "type": "string" }
|
|
}
|
|
}
|
|
`);
|
|
const yaml = parseTopLevelYaml(`
|
|
id: 1
|
|
title: Slime
|
|
`);
|
|
|
|
const diagnostics = validateParsedConfig(schema, yaml);
|
|
|
|
assert.equal(diagnostics.length, 2);
|
|
assert.equal(diagnostics[0].severity, "error");
|
|
assert.match(diagnostics[0].message, /name/u);
|
|
assert.equal(diagnostics[1].severity, "error");
|
|
assert.match(diagnostics[1].message, /title/u);
|
|
});
|
|
|
|
test("validateParsedConfig should report array item type mismatches", () => {
|
|
const schema = parseSchemaContent(`
|
|
{
|
|
"type": "object",
|
|
"properties": {
|
|
"dropRates": {
|
|
"type": "array",
|
|
"items": { "type": "integer" }
|
|
}
|
|
}
|
|
}
|
|
`);
|
|
const yaml = parseTopLevelYaml(`
|
|
dropRates:
|
|
- 1
|
|
- potion
|
|
`);
|
|
|
|
const diagnostics = validateParsedConfig(schema, yaml);
|
|
|
|
assert.equal(diagnostics.length, 1);
|
|
assert.equal(diagnostics[0].severity, "error");
|
|
assert.match(diagnostics[0].message, /dropRates/u);
|
|
});
|
|
|
|
test("parseTopLevelYaml should classify nested mappings as object entries", () => {
|
|
const yaml = parseTopLevelYaml(`
|
|
reward:
|
|
gold: 10
|
|
name: Slime
|
|
`);
|
|
|
|
assert.equal(yaml.entries.get("reward").kind, "object");
|
|
assert.equal(yaml.entries.get("name").kind, "scalar");
|
|
});
|
|
|
|
test("applyScalarUpdates should update top-level scalars and append new keys", () => {
|
|
const updated = applyScalarUpdates(
|
|
[
|
|
"id: 1",
|
|
"name: Slime",
|
|
"dropRates:",
|
|
" - 1"
|
|
].join("\n"),
|
|
{
|
|
name: "Goblin",
|
|
hp: "25"
|
|
});
|
|
|
|
assert.match(updated, /^name: Goblin$/mu);
|
|
assert.match(updated, /^hp: 25$/mu);
|
|
assert.match(updated, /^ - 1$/mu);
|
|
});
|