Files
larksuite-cli/scripts/dev-svglide-ppe-env.test.js
2026-07-09 17:54:55 +08:00

117 lines
3.8 KiB
JavaScript

// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
const { describe, it } = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const { spawnSync } = require("node:child_process");
const repoRoot = path.resolve(__dirname, "..");
function createMockWhistle() {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "svglide-ppe-test-"));
const binDir = path.join(tmp, "bin");
const logPath = path.join(tmp, "w2.log");
fs.mkdirSync(binDir);
fs.writeFileSync(path.join(binDir, "w2"), `#!/usr/bin/env sh
case "$1" in
status)
printf '%s\\n' "Whistle is running"
;;
add)
node -e 'require(process.argv[1])((rule) => console.log(JSON.stringify({ name: rule.name, rules: rule.rules })))' "$2" >>"$SVGLIDE_TEST_W2_LOG"
;;
start)
printf '%s\\n' "START" >>"$SVGLIDE_TEST_W2_LOG"
;;
*)
printf 'unexpected w2 args: %s\\n' "$*" >&2
exit 2
;;
esac
`);
fs.chmodSync(path.join(binDir, "w2"), 0o755);
return { binDir, logPath };
}
function readLoggedRules(logPath) {
return fs
.readFileSync(logPath, "utf8")
.trim()
.split("\n")
.filter(Boolean)
.map((line) => JSON.parse(line));
}
describe("dev-svglide-ppe-env", () => {
it("keeps one canonical rule name in the script source", () => {
const source = fs.readFileSync(path.join(repoRoot, "scripts/dev-svglide-ppe-env.sh"), "utf8");
assert.match(source, /CANONICAL_RULE_NAME="lark-cli-openapi-ppe-svg-slides"/);
assert.doesNotMatch(
source,
/lark-cli-openapi-env-switch|lark-cli-openapi-ppe-svglide|lark-cli-openapi-ppe-pure-svg|lark-cli-ppe-svglide|lark-cli-prod-ppe/
);
});
it("prints only the canonical SVG Slides PPE lane", () => {
const result = spawnSync("sh", ["scripts/dev-svglide-ppe-env.sh", "rules"], {
cwd: repoRoot,
encoding: "utf8",
});
assert.equal(result.status, 0, result.stderr);
assert.match(result.stdout, /open\.feishu-pre\.cn/);
assert.match(result.stdout, /x-tt-env=ppe_svg_slides/);
assert.match(result.stdout, /x-use-ppe=1/);
assert.doesNotMatch(result.stdout, /ppe_pure_svg|ppe_svglide/);
});
it("keeps pre mode to one written Whistle rule", () => {
const { binDir, logPath } = createMockWhistle();
const result = spawnSync("sh", ["-c", ". ./scripts/dev-svglide-ppe-env.sh pre"], {
cwd: repoRoot,
env: {
...process.env,
PATH: `${binDir}${path.delimiter}${process.env.PATH}`,
SVGLIDE_TEST_W2_LOG: logPath,
},
encoding: "utf8",
});
assert.equal(result.status, 0, result.stderr);
const rules = readLoggedRules(logPath);
assert.equal(rules.length, 1);
assert.equal(rules[0].name, "lark-cli-openapi-ppe-svg-slides");
assert.match(rules[0].rules, /x-tt-env=ppe_svg_slides/);
assert.doesNotMatch(rules[0].rules, /ppe_pure_svg|ppe_svglide/);
});
it("keeps off mode to one disabled canonical Whistle rule", () => {
const { binDir, logPath } = createMockWhistle();
const result = spawnSync("sh", ["-c", ". ./scripts/dev-svglide-ppe-env.sh off"], {
cwd: repoRoot,
env: {
...process.env,
PATH: `${binDir}${path.delimiter}${process.env.PATH}`,
SVGLIDE_TEST_W2_LOG: logPath,
},
encoding: "utf8",
});
assert.equal(result.status, 0, result.stderr);
const rules = readLoggedRules(logPath);
assert.equal(rules.length, 1);
assert.equal(rules[0].name, "lark-cli-openapi-ppe-svg-slides");
assert.match(rules[0].rules, /disabled by scripts\/dev-svglide-ppe-env\.sh/);
assert.match(rules[0].rules, /canonical SVGlide PPE rule: lark-cli-openapi-ppe-svg-slides/);
assert.doesNotMatch(rules[0].rules, /legacy|ppe_pure_svg|ppe_svglide/);
});
});