repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
scripts/test_extensions_static_check.mjs
Raw#!/usr/bin/env node
import assert from "node:assert/strict";
import {
mkdirSync,
mkdtempSync,
readdirSync,
readFileSync,
rmSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
HARNESS,
isSecretScanIgnored,
rootPkg,
secretScanIgnorePaths,
staticCheck,
} from "./extensions.mjs";
import { ROOT } from "./lib/common.mjs";
function writeExtension(root, name, overrides, index = "") {
const ext = join(root, name);
mkdirSync(join(ext, "__tests__"), { recursive: true });
const pkg = {
name: `@bugabinga/pi-ext-${name}`,
private: true,
type: "module",
main: "index.ts",
license: "MIT",
peerDependencies: { "@earendil-works/pi-coding-agent": "*" },
devDependencies: { [HARNESS]: "^0.6.1" },
...overrides,
};
for (const [key, value] of Object.entries(overrides))
if (value === null) delete pkg[key];
writeFileSync(join(ext, "package.json"), JSON.stringify(pkg), "utf8");
writeFileSync(join(ext, "index.ts"), index, "utf8");
writeFileSync(join(ext, "README.md"), `# ${name}\n`, "utf8");
writeFileSync(join(ext, "__tests__", "harness.test.ts"), "", "utf8");
return ext;
}
function withTemp(fn) {
const dir = mkdtempSync(join(tmpdir(), "pi-ext-static-"));
try {
fn(dir);
} finally {
rmSync(dir, { recursive: true, force: true });
}
}
withTemp((root) => {
const ext = writeExtension(
root,
"typed",
{
peerDependencies: {
"@earendil-works/pi-coding-agent": "*",
typebox: "^1.0.0",
},
devDependencies: { [HARNESS]: "^0.6.1", typebox: "^1.0.0" },
optionalDependencies: { typebox: "^1.0.0" },
},
'import { Type } from "typebox";\nvoid Type;\n',
);
const errors = staticCheck(ext, { root });
assert(
errors.includes(
"typed: Pi package typebox peerDependency range must be '*'",
),
);
assert(
errors.includes(
"typed: Pi package typebox belongs only in peerDependencies, not devDependencies",
),
);
assert(
errors.includes(
"typed: Pi package typebox belongs only in peerDependencies, not optionalDependencies",
),
);
});
withTemp((root) => {
writeExtension(
root,
"bad-config",
{ devDependencies: { [HARNESS]: "^0.6.1" } },
[
'import path from "node:path";',
'const a = path.join(process.cwd(), "settings.json");',
'const b = path.join(process.cwd(), ".pi", "tune", "config.json");',
'const c = "tune.json";',
"void [a, b, c];",
].join("\n"),
);
const errors = staticCheck(join(root, "bad-config"), { root });
assert(errors.some((e) => e.includes("workspace-root settings.json")));
assert(errors.some((e) => e.includes("custom tune config file")));
assert(errors.some((e) => e.includes("agent extension config file")));
});
withTemp((root) => {
mkdirSync(join(root, "scripts"));
writeFileSync(
join(root, "scripts", "extensions.json"),
JSON.stringify({
secretScanIgnore: ["extensions/leaks/__tests__/ux.test.ts"],
}),
"utf8",
);
assert.deepEqual([...secretScanIgnorePaths(root)].sort(), [
"extensions/leaks/__tests__/ux.test.ts",
]);
assert.equal(
isSecretScanIgnored(
join(root, "extensions/leaks/__tests__/ux.test.ts"),
new Set(["extensions/leaks/__tests__/ux.test.ts"]),
root,
),
true,
);
assert.equal(
isSecretScanIgnored(
join(root, "extensions/leaks/index.ts"),
new Set(["extensions/leaks/__tests__/ux.test.ts"]),
root,
),
false,
);
});
assert.deepEqual(rootPkg().pi, {
extensions: ["extensions/*/index.ts"],
skills: [
"extensions/the-system/skills",
"extensions/ultra/skills",
"extensions/lazy/skills",
],
prompts: ["extensions/the-system/prompts"],
});
assert.deepEqual(
JSON.parse(
readFileSync(join(ROOT, "extensions/the-system/package.json"), "utf8"),
).pi,
{
extensions: ["index.ts"],
skills: ["skills"],
prompts: ["prompts"],
},
);
const legacyReferences = [
["extensions", "super"].join("/"),
["pi-ext", "super"].join("-"),
];
const textExtensions = new Set([
".html",
".json",
".md",
".mjs",
".toml",
".ts",
".yaml",
".yml",
]);
function liveTextFiles(dir = ROOT) {
const files = [];
for (const entry of readdirSync(dir, { withFileTypes: true })) {
const path = join(dir, entry.name);
if (entry.isDirectory()) {
if (
entry.name === ".git" ||
entry.name === ".jj" ||
entry.name === "node_modules" ||
path === join(ROOT, "docs", "super")
)
continue;
files.push(...liveTextFiles(path));
} else if (
entry.isFile() &&
entry.name !== "CHANGELOG.md" &&
textExtensions.has(entry.name.slice(entry.name.lastIndexOf(".")))
) {
files.push(path);
}
}
return files;
}
const legacyReferenceViolations = liveTextFiles().flatMap((path) => {
const content = readFileSync(path, "utf8");
return legacyReferences
.filter((reference) => content.includes(reference))
.map((reference) => `${path}: forbidden live reference ${reference}`);
});
assert.deepEqual(legacyReferenceViolations, []);
console.log("OK static-check tests");