Files
adolf/test/scripts/format-generated-module.test.ts
alvis bedb527145
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Vendor OpenClaw source as Adolf fork baseline
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11),
free to diverge. Tree copied sans upstream .git; upstream remote added for
future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19.
Preserves docs/ARCHITECTURE.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
2026-07-05 09:36:54 +00:00

171 lines
5.3 KiB
TypeScript

// Format Generated Module tests cover format generated module script behavior.
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import {
formatGeneratedModule,
GENERATED_MODULE_FORMAT_MAX_BUFFER_BYTES,
GENERATED_MODULE_FORMAT_TIMEOUT_MS,
resolveGeneratedModuleFormatter,
} from "../../scripts/lib/format-generated-module.mjs";
const tempDirs: string[] = [];
function makeRepoRoot() {
const repoRoot = mkdtempSync(path.join(os.tmpdir(), "openclaw-format-generated-module-"));
tempDirs.push(repoRoot);
const formatterDir = path.join(repoRoot, "node_modules", ".bin");
mkdirSync(formatterDir, { recursive: true });
writeFileSync(path.join(formatterDir, "oxfmt"), "#!/bin/sh\n", "utf8");
return repoRoot;
}
afterEach(() => {
for (const dir of tempDirs.splice(0)) {
rmSync(dir, { force: true, recursive: true });
}
});
describe("resolveGeneratedModuleFormatter", () => {
it("uses the direct formatter binary on non-Windows when available", () => {
const formatterPath = path.join("/repo", "node_modules", ".bin", "oxfmt");
expect(
resolveGeneratedModuleFormatter({
existsSync: (value) => value === formatterPath,
outputPath: "/tmp/generated.ts",
platform: "linux",
repoRoot: "/repo",
}),
).toEqual({
command: formatterPath,
args: ["--write", "/tmp/generated.ts"],
shell: false,
});
});
it("wraps pnpm.cmd explicitly on Windows instead of using shell mode", () => {
expect(
resolveGeneratedModuleFormatter({
comSpec: "C:\\Windows\\System32\\cmd.exe",
env: { PATH: "" },
existsSync: () => false,
npmExecPath: "",
outputPath: "C:\\Users\\test\\AppData\\Local\\Temp\\generated output.ts",
platform: "win32",
repoRoot: "C:\\repo",
}),
).toEqual({
command: "C:\\Windows\\System32\\cmd.exe",
args: [
"/d",
"/s",
"/c",
'pnpm.cmd exec oxfmt --write "C:\\Users\\test\\AppData\\Local\\Temp\\generated output.ts"',
],
shell: false,
windowsVerbatimArguments: true,
});
});
it("runs generated module formatting with bounded child execution", () => {
const repoRoot = makeRepoRoot();
const calls: unknown[] = [];
const formatted = formatGeneratedModule(
"export const value=1;",
{
errorLabel: "test module",
outputPath: "generated.ts",
repoRoot,
},
{
resolveFormatter: ({ outputPath }: { outputPath: string }) => ({
args: ["--write", outputPath],
command: "oxfmt",
shell: false,
}),
spawnSync: (_command: string, args: string[], options: unknown) => {
calls.push(options);
writeFileSync(args[1] ?? "", "export const value = 1;\n", "utf8");
return { status: 0, stderr: "", stdout: "" };
},
},
);
expect(formatted).toBe("export const value = 1;\n");
expect(calls).toHaveLength(1);
expect(calls[0]).toMatchObject({
cwd: repoRoot,
encoding: "utf8",
maxBuffer: GENERATED_MODULE_FORMAT_MAX_BUFFER_BYTES,
shell: false,
timeout: GENERATED_MODULE_FORMAT_TIMEOUT_MS,
});
});
it("reports formatter timeouts with bounded output tails", () => {
const repoRoot = makeRepoRoot();
const timeoutError = Object.assign(new Error("spawnSync oxfmt ETIMEDOUT"), {
code: "ETIMEDOUT",
});
expect(() =>
formatGeneratedModule(
"export const value=1;",
{
errorLabel: "test module",
outputPath: "generated.ts",
repoRoot,
},
{
resolveFormatter: ({ outputPath }: { outputPath: string }) => ({
args: ["--write", outputPath],
command: "oxfmt",
shell: false,
}),
spawnSync: () => ({
error: timeoutError,
signal: "SIGTERM",
status: null,
stderr: `DO_NOT_DUMP_OLD_STDERR${"x".repeat(20 * 1024)}\nrecent stderr tail`,
stdout: `DO_NOT_DUMP_OLD_STDOUT${"x".repeat(20 * 1024)}\nrecent stdout tail`,
}),
},
),
).toThrow(
/formatter timed out after 30000ms[\s\S]*recent stderr tail[\s\S]*recent stdout tail/u,
);
try {
formatGeneratedModule(
"export const value=1;",
{
errorLabel: "test module",
outputPath: "generated.ts",
repoRoot,
},
{
resolveFormatter: ({ outputPath }: { outputPath: string }) => ({
args: ["--write", outputPath],
command: "oxfmt",
shell: false,
}),
spawnSync: () => ({
error: timeoutError,
signal: "SIGTERM",
status: null,
stderr: `DO_NOT_DUMP_OLD_STDERR${"x".repeat(20 * 1024)}\nrecent stderr tail`,
stdout: `DO_NOT_DUMP_OLD_STDOUT${"x".repeat(20 * 1024)}\nrecent stdout tail`,
}),
},
);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
expect(message).not.toContain("DO_NOT_DUMP_OLD_STDERR");
expect(message).not.toContain("DO_NOT_DUMP_OLD_STDOUT");
}
});
});