61 lines
2.6 KiB
TypeScript
61 lines
2.6 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { existsSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join, resolve } from "node:path";
|
|
import { spawnSync } from "node:child_process";
|
|
import test from "node:test";
|
|
|
|
const CLI = resolve("src/cli/commands.ts");
|
|
|
|
test("CLI exposes YAML build with an explicit output path", () => {
|
|
const dir = mkdtempSync(join(tmpdir(), "drawio-cli-build-test-"));
|
|
const input = join(dir, "spec.yaml");
|
|
const output = join(dir, "diagram.drawio");
|
|
try {
|
|
writeFileSync(input, "version: 1\nnodes:\n - { id: one, label: One }\n", "utf8");
|
|
const result = spawnSync(process.execPath, ["--import", "tsx", CLI, "--action", "build", "--file", input, "--output", output], {
|
|
cwd: resolve("."),
|
|
encoding: "utf8",
|
|
});
|
|
assert.equal(result.status, 0, result.stdout + result.stderr);
|
|
assert.match(result.stdout, /action: build/);
|
|
assert.equal(existsSync(output), true);
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("CLI rejects partially numeric page indexes", () => {
|
|
const result = spawnSync(process.execPath, ["--import", "tsx", CLI, "--action", "summary", "--file", "unused.drawio", "--page", "1abc"], {
|
|
cwd: resolve("."),
|
|
encoding: "utf8",
|
|
});
|
|
assert.equal(result.status, 1);
|
|
assert.match(result.stderr, /non-negative integer/);
|
|
});
|
|
|
|
test("CLI help documents lifecycle options and file-free doctor", () => {
|
|
const result = spawnSync(process.execPath, ["--import", "tsx", CLI, "--help"], { cwd: resolve("."), encoding: "utf8" });
|
|
assert.equal(result.status, 0, result.stdout + result.stderr);
|
|
for (const option of ["--spec", "--base", "--strict", "--prune", "--dry-run", "--fail", "--views", "--property", "--from", "--to"]) {
|
|
assert.match(result.stdout, new RegExp(option));
|
|
}
|
|
assert.match(result.stdout, /doctor.*does not require --file/i);
|
|
});
|
|
|
|
test("CLI validate exits non-zero when the validation summary is invalid", () => {
|
|
const dir = mkdtempSync(join(tmpdir(), "drawio-cli-validate-test-"));
|
|
const input = join(dir, "empty.drawio");
|
|
try {
|
|
writeFileSync(input, `<mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/></root></mxGraphModel>`, "utf8");
|
|
const result = spawnSync(process.execPath, ["--import", "tsx", CLI, "--action", "validate", "--file", input], {
|
|
cwd: resolve("."),
|
|
encoding: "utf8",
|
|
});
|
|
assert.equal(result.status, 1, result.stdout + result.stderr);
|
|
assert.match(result.stdout, /valid: false/);
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|