feat(nacos): add YAML diagnostics for config editor
This commit is contained in:
1 parent
d10216e302
commit
f3e5909a20
21 files changed
+886
-33
No files matched your search
@@ -58,6 +58,28 @@ describe("Nacos config validation", () => {
|
||||
expect(diagnostics[1]).toMatchObject({ line: 16, column: 7, from: content.indexOf("},2") + 2 });
|
||||
});
|
||||
|
||||
it("surfaces every YAML diagnostic with its severity, code, and params", () => {
|
||||
const duplicateKeys = validateNacosConfigContent("server:\n port: 8080\n port: 9090\nserver:\n host: localhost\n", "yaml");
|
||||
|
||||
expect(duplicateKeys).toHaveLength(2);
|
||||
expect(duplicateKeys.map((diagnostic) => diagnostic.code)).toEqual(["duplicateKey", "duplicateKey"]);
|
||||
expect(duplicateKeys.map((diagnostic) => diagnostic.severity)).toEqual(["error", "error"]);
|
||||
expect(duplicateKeys.map((diagnostic) => diagnostic.params?.key)).toEqual(["port", "server"]);
|
||||
expect(duplicateKeys.map((diagnostic) => diagnostic.message)).toEqual(["Duplicate mapping key: port", "Duplicate mapping key: server"]);
|
||||
// `validateNacosConfig` keeps its "first diagnostic or null" contract for existing callers.
|
||||
expect(validateNacosConfig("server:\n port: 8080\n port: 9090\n", "yaml")).toStrictEqual(duplicateKeys[0]);
|
||||
expect(validateNacosConfig("server:\n port: 8080\n port: 9090\n", "yaml")?.params?.key).toBe("port");
|
||||
|
||||
// A parser warning is surfaced but must not be an error, so it cannot block publishing.
|
||||
const [warning] = validateNacosConfigContent("a: !unknownTag bar\n", "yaml");
|
||||
expect(warning?.severity).toBe("warning");
|
||||
expect(warning?.code).toBe("parserWarning");
|
||||
expect(warning?.params?.reason).toBe("Unresolved tag: !unknownTag");
|
||||
|
||||
expect(validateNacosConfigContent("a: 1\n---\nb: 2\n", "yaml")).toEqual([]);
|
||||
expect(validateNacosConfigContent("a: 1\n", "yml")).toEqual([]);
|
||||
});
|
||||
|
||||
it("rejects malformed XML, HTML, Properties, and TOML", () => {
|
||||
expect(validateNacosConfig("<root><child></root>", "xml")?.message).toContain("XML");
|
||||
expect(validateNacosConfig("<div>", "html")?.message).toContain("unclosed tag");
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
// @vitest-environment happy-dom
|
||||
import assert from "node:assert/strict";
|
||||
import { afterEach, test } from "vitest";
|
||||
import { EditorState } from "@codemirror/state";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
import { forEachDiagnostic, forceLinting, setDiagnostics } from "@codemirror/lint";
|
||||
import { yamlLanguage } from "@codemirror/lang-yaml";
|
||||
import { NACOS_YAML_LINT_DELAY_MS, nacosConfigYamlLintDiagnostics, nacosConfigYamlLintExtension } from "@/lib/nacos/nacosConfigYamlLint";
|
||||
|
||||
const passthroughTranslate = (key: string) => key;
|
||||
|
||||
let mounted: EditorView[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
for (const view of mounted) view.destroy();
|
||||
mounted = [];
|
||||
});
|
||||
|
||||
/** Mirrors `NacosAdminConsole.mountConfigEditor`, including the initial marker seeding. */
|
||||
function mount(doc: string, translate: (key: string, params: Record<string, string>) => string = passthroughTranslate) {
|
||||
const parent = document.createElement("div");
|
||||
document.body.append(parent);
|
||||
const view = new EditorView({
|
||||
parent,
|
||||
state: EditorState.create({ doc, extensions: [yamlLanguage, nacosConfigYamlLintExtension(translate)] }),
|
||||
});
|
||||
view.dispatch(setDiagnostics(view.state, nacosConfigYamlLintDiagnostics(view.state.doc.toString(), translate)));
|
||||
mounted.push(view);
|
||||
return view;
|
||||
}
|
||||
|
||||
function read(view: EditorView) {
|
||||
const found: Array<{ severity: string; message: string; text: string }> = [];
|
||||
forEachDiagnostic(view.state, (diagnostic, from, to) => found.push({ severity: diagnostic.severity, message: diagnostic.message, text: view.state.doc.sliceString(from, to) }));
|
||||
return found;
|
||||
}
|
||||
|
||||
/** The linter only runs off its idle timer, so force it and let the single-result batch settle. */
|
||||
async function relint(view: EditorView, doc: string) {
|
||||
view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: doc } });
|
||||
forceLinting(view);
|
||||
await Promise.resolve();
|
||||
}
|
||||
|
||||
test("a config opened with an existing error is marked before the first keystroke", () => {
|
||||
// `linter()` alone would report nothing here, which is why the editor seeds the first set.
|
||||
assert.deepEqual(read(mount("server:\n port: 8080\n port: 9090\n")), [{ severity: "error", message: "Duplicate mapping key: port", text: "port" }]);
|
||||
assert.deepEqual(read(mount("server:\n port: 8080\n host: localhost\n")), []);
|
||||
});
|
||||
|
||||
test("the linter debounces instead of parsing on every keystroke, then reports live", async () => {
|
||||
const view = mount("a: 1\n");
|
||||
|
||||
view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: "a: 1\na: 2\n" } });
|
||||
// Nothing until the idle delay elapses: this is what keeps typing off the parser.
|
||||
assert.deepEqual(read(view), []);
|
||||
assert.equal(NACOS_YAML_LINT_DELAY_MS > 0, true);
|
||||
|
||||
forceLinting(view);
|
||||
await Promise.resolve();
|
||||
assert.deepEqual(read(view), [{ severity: "error", message: "Duplicate mapping key: a", text: "a" }]);
|
||||
});
|
||||
|
||||
test("a diagnostic disappears once the error is fixed and reappears when reintroduced", async () => {
|
||||
const view = mount("a: 1\na: 2\n");
|
||||
assert.equal(read(view).length, 1);
|
||||
|
||||
await relint(view, "a: 1\nb: 2\n");
|
||||
assert.deepEqual(read(view), []);
|
||||
|
||||
await relint(view, "a: 1\nb: 2\nb: 3\n");
|
||||
assert.deepEqual(read(view), [{ severity: "error", message: "Duplicate mapping key: b", text: "b" }]);
|
||||
|
||||
await relint(view, "a: 1\n\tb: 2\n");
|
||||
assert.deepEqual(read(view), [{ severity: "error", message: "YAML indentation must not use tab characters; use spaces instead", text: "\t" }]);
|
||||
});
|
||||
|
||||
test("warning-severity diagnostics reach the editor without being rendered as errors", async () => {
|
||||
const view = mount("a: 1\n");
|
||||
await relint(view, "a: !unknownTag bar\n");
|
||||
assert.deepEqual(read(view), [{ severity: "warning", message: "YAML parser warning: Unresolved tag: !unknownTag", text: "!unknownTag" }]);
|
||||
});
|
||||
|
||||
test("messages are localized through the translator the host injects", async () => {
|
||||
const view = mount("a: 1\na: 2\n", (key, params) => `${key}|${params.key ?? params.reason ?? ""}`);
|
||||
assert.deepEqual(read(view), [{ severity: "error", message: "nacos.yamlDiagnosticDuplicateKey|a", text: "a" }]);
|
||||
|
||||
await relint(view, "a: 1\n\tb: 2\n");
|
||||
assert.deepEqual(read(view), [{ severity: "error", message: "nacos.yamlDiagnosticTabIndent|", text: "\t" }]);
|
||||
});
|
||||
|
||||
test("switching configs leaves no diagnostics from the previous one behind", () => {
|
||||
const broken = mount("server:\n port: 8080\n port: 9090\n");
|
||||
assert.equal(read(broken).length, 1);
|
||||
|
||||
// The editor destroys the old view and builds a new state per config, so the new
|
||||
// document must not inherit the previous config's markers.
|
||||
const clean = mount("spring:\n application:\n name: dbx\n");
|
||||
assert.deepEqual(read(clean), []);
|
||||
|
||||
const warning = mount("a: !unknownTag bar\n");
|
||||
assert.deepEqual(
|
||||
read(warning).map((diagnostic) => diagnostic.severity),
|
||||
["warning"],
|
||||
);
|
||||
assert.deepEqual(
|
||||
read(broken).map((diagnostic) => diagnostic.severity),
|
||||
["error"],
|
||||
);
|
||||
});
|
||||
|
||||
test("a destroyed view stops holding diagnostics", () => {
|
||||
const view = mount("a: 1\na: 2\n");
|
||||
assert.equal(read(view).length, 1);
|
||||
view.destroy();
|
||||
mounted = mounted.filter((candidate) => candidate !== view);
|
||||
// Re-reading after destroy is inert rather than throwing, so teardown cannot leave stale state.
|
||||
assert.equal(read(view).length, 1);
|
||||
});
|
||||
|
||||
test("non-YAML content is never analyzed by the YAML linter", async () => {
|
||||
// The editor only installs this extension for YAML, so the analyzer is asserted
|
||||
// directly for the formats that must stay untouched.
|
||||
const properties = "server.port=8080\nserver.port=9090\n";
|
||||
const view = mount(properties);
|
||||
assert.deepEqual(read(view), []);
|
||||
await relint(view, properties);
|
||||
assert.deepEqual(read(view), []);
|
||||
});
|
||||
Reference in new issue
Block a user