mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 11:21:23 +00:00
perf(test): speed up control-ui-assets suite
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import {
|
||||
resolveControlUiDistIndexHealth,
|
||||
resolveControlUiDistIndexPath,
|
||||
@@ -31,9 +31,27 @@ async function canonicalPath(p: string): Promise<string> {
|
||||
}
|
||||
|
||||
describe("control UI assets helpers", () => {
|
||||
let fixtureRoot = "";
|
||||
let caseId = 0;
|
||||
|
||||
async function withTempDir<T>(fn: (tmp: string) => Promise<T>): Promise<T> {
|
||||
const tmp = path.join(fixtureRoot, `case-${caseId++}`);
|
||||
await fs.mkdir(tmp, { recursive: true });
|
||||
return await fn(tmp);
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
if (fixtureRoot) {
|
||||
await fs.rm(fixtureRoot, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("resolves repo root from src argv1", async () => {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
try {
|
||||
await withTempDir(async (tmp) => {
|
||||
await fs.mkdir(path.join(tmp, "ui"), { recursive: true });
|
||||
await fs.writeFile(path.join(tmp, "ui", "vite.config.ts"), "export {};\n");
|
||||
await fs.writeFile(path.join(tmp, "package.json"), "{}\n");
|
||||
@@ -41,14 +59,11 @@ describe("control UI assets helpers", () => {
|
||||
await fs.writeFile(path.join(tmp, "src", "index.ts"), "export {};\n");
|
||||
|
||||
expect(resolveControlUiRepoRoot(path.join(tmp, "src", "index.ts"))).toBe(tmp);
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves repo root from dist argv1", async () => {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
try {
|
||||
await withTempDir(async (tmp) => {
|
||||
await fs.mkdir(path.join(tmp, "ui"), { recursive: true });
|
||||
await fs.writeFile(path.join(tmp, "ui", "vite.config.ts"), "export {};\n");
|
||||
await fs.writeFile(path.join(tmp, "package.json"), "{}\n");
|
||||
@@ -56,9 +71,7 @@ describe("control UI assets helpers", () => {
|
||||
await fs.writeFile(path.join(tmp, "dist", "index.js"), "export {};\n");
|
||||
|
||||
expect(resolveControlUiRepoRoot(path.join(tmp, "dist", "index.js"))).toBe(tmp);
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves dist control-ui index path for dist argv1", async () => {
|
||||
@@ -70,8 +83,7 @@ describe("control UI assets helpers", () => {
|
||||
});
|
||||
|
||||
it("resolves control-ui root for dist bundle argv1", async () => {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
try {
|
||||
await withTempDir(async (tmp) => {
|
||||
await fs.mkdir(path.join(tmp, "dist", "control-ui"), { recursive: true });
|
||||
await fs.writeFile(path.join(tmp, "dist", "bundle.js"), "export {};\n");
|
||||
await fs.writeFile(path.join(tmp, "dist", "control-ui", "index.html"), "<html></html>\n");
|
||||
@@ -79,14 +91,11 @@ describe("control UI assets helpers", () => {
|
||||
expect(resolveControlUiRootSync({ argv1: path.join(tmp, "dist", "bundle.js") })).toBe(
|
||||
path.join(tmp, "dist", "control-ui"),
|
||||
);
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves control-ui root for dist/gateway bundle argv1", async () => {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
try {
|
||||
await withTempDir(async (tmp) => {
|
||||
await fs.writeFile(path.join(tmp, "package.json"), JSON.stringify({ name: "openclaw" }));
|
||||
await fs.mkdir(path.join(tmp, "dist", "gateway"), { recursive: true });
|
||||
await fs.mkdir(path.join(tmp, "dist", "control-ui"), { recursive: true });
|
||||
@@ -96,14 +105,11 @@ describe("control UI assets helpers", () => {
|
||||
expect(
|
||||
resolveControlUiRootSync({ argv1: path.join(tmp, "dist", "gateway", "control-ui.js") }),
|
||||
).toBe(path.join(tmp, "dist", "control-ui"));
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves control-ui root from override directory or index.html", async () => {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
try {
|
||||
await withTempDir(async (tmp) => {
|
||||
const uiDir = path.join(tmp, "dist", "control-ui");
|
||||
await fs.mkdir(uiDir, { recursive: true });
|
||||
await fs.writeFile(path.join(uiDir, "index.html"), "<html></html>\n");
|
||||
@@ -111,14 +117,11 @@ describe("control UI assets helpers", () => {
|
||||
expect(resolveControlUiRootOverrideSync(uiDir)).toBe(uiDir);
|
||||
expect(resolveControlUiRootOverrideSync(path.join(uiDir, "index.html"))).toBe(uiDir);
|
||||
expect(resolveControlUiRootOverrideSync(path.join(uiDir, "missing.html"))).toBeNull();
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves dist control-ui index path from package root argv1", async () => {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
try {
|
||||
await withTempDir(async (tmp) => {
|
||||
await fs.writeFile(path.join(tmp, "package.json"), JSON.stringify({ name: "openclaw" }));
|
||||
await fs.writeFile(path.join(tmp, "openclaw.mjs"), "export {};\n");
|
||||
await fs.mkdir(path.join(tmp, "dist", "control-ui"), { recursive: true });
|
||||
@@ -127,14 +130,11 @@ describe("control UI assets helpers", () => {
|
||||
expect(await resolveControlUiDistIndexPath(path.join(tmp, "openclaw.mjs"))).toBe(
|
||||
path.join(tmp, "dist", "control-ui", "index.html"),
|
||||
);
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves control-ui root for package entrypoint argv1", async () => {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
try {
|
||||
await withTempDir(async (tmp) => {
|
||||
await fs.writeFile(path.join(tmp, "package.json"), JSON.stringify({ name: "openclaw" }));
|
||||
await fs.writeFile(path.join(tmp, "openclaw.mjs"), "export {};\n");
|
||||
await fs.mkdir(path.join(tmp, "dist", "control-ui"), { recursive: true });
|
||||
@@ -143,14 +143,11 @@ describe("control UI assets helpers", () => {
|
||||
expect(resolveControlUiRootSync({ argv1: path.join(tmp, "openclaw.mjs") })).toBe(
|
||||
path.join(tmp, "dist", "control-ui"),
|
||||
);
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves dist control-ui index path from .bin argv1", async () => {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
try {
|
||||
await withTempDir(async (tmp) => {
|
||||
const binDir = path.join(tmp, "node_modules", ".bin");
|
||||
const pkgRoot = path.join(tmp, "node_modules", "openclaw");
|
||||
await fs.mkdir(binDir, { recursive: true });
|
||||
@@ -162,14 +159,11 @@ describe("control UI assets helpers", () => {
|
||||
expect(await resolveControlUiDistIndexPath(path.join(binDir, "openclaw"))).toBe(
|
||||
path.join(pkgRoot, "dist", "control-ui", "index.html"),
|
||||
);
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves via fallback when package root resolution fails but package name matches", async () => {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
try {
|
||||
await withTempDir(async (tmp) => {
|
||||
// Package named "openclaw" but resolveOpenClawPackageRoot failed for other reasons
|
||||
await fs.writeFile(path.join(tmp, "package.json"), JSON.stringify({ name: "openclaw" }));
|
||||
await fs.writeFile(path.join(tmp, "openclaw.mjs"), "export {};\n");
|
||||
@@ -179,14 +173,11 @@ describe("control UI assets helpers", () => {
|
||||
expect(await resolveControlUiDistIndexPath(path.join(tmp, "openclaw.mjs"))).toBe(
|
||||
path.join(tmp, "dist", "control-ui", "index.html"),
|
||||
);
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("returns null when package name does not match openclaw", async () => {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
try {
|
||||
await withTempDir(async (tmp) => {
|
||||
// Package with different name should not be resolved
|
||||
await fs.writeFile(path.join(tmp, "package.json"), JSON.stringify({ name: "malicious-pkg" }));
|
||||
await fs.writeFile(path.join(tmp, "index.mjs"), "export {};\n");
|
||||
@@ -194,27 +185,21 @@ describe("control UI assets helpers", () => {
|
||||
await fs.writeFile(path.join(tmp, "dist", "control-ui", "index.html"), "<html></html>\n");
|
||||
|
||||
expect(await resolveControlUiDistIndexPath(path.join(tmp, "index.mjs"))).toBeNull();
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("returns null when no control-ui assets exist", async () => {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
try {
|
||||
await withTempDir(async (tmp) => {
|
||||
// Just a package.json, no dist/control-ui
|
||||
await fs.writeFile(path.join(tmp, "package.json"), JSON.stringify({ name: "some-pkg" }));
|
||||
await fs.writeFile(path.join(tmp, "index.mjs"), "export {};\n");
|
||||
|
||||
expect(await resolveControlUiDistIndexPath(path.join(tmp, "index.mjs"))).toBeNull();
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("reports health for existing control-ui assets at a known root", async () => {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
try {
|
||||
await withTempDir(async (tmp) => {
|
||||
const indexPath = resolveControlUiDistIndexPathForRoot(tmp);
|
||||
await fs.mkdir(path.dirname(indexPath), { recursive: true });
|
||||
await fs.writeFile(indexPath, "<html></html>\n");
|
||||
@@ -223,27 +208,21 @@ describe("control UI assets helpers", () => {
|
||||
indexPath,
|
||||
exists: true,
|
||||
});
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("reports health for missing control-ui assets at a known root", async () => {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
try {
|
||||
await withTempDir(async (tmp) => {
|
||||
const indexPath = resolveControlUiDistIndexPathForRoot(tmp);
|
||||
await expect(resolveControlUiDistIndexHealth({ root: tmp })).resolves.toEqual({
|
||||
indexPath,
|
||||
exists: false,
|
||||
});
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves control-ui root when argv1 is a symlink (nvm scenario)", async () => {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
try {
|
||||
await withTempDir(async (tmp) => {
|
||||
const realPkg = path.join(tmp, "real-pkg");
|
||||
const bin = path.join(tmp, "bin");
|
||||
await fs.mkdir(realPkg, { recursive: true });
|
||||
@@ -265,14 +244,11 @@ describe("control UI assets helpers", () => {
|
||||
expect(await canonicalPath(resolvedRoot ?? "")).toBe(
|
||||
await canonicalPath(path.join(realPkg, "dist", "control-ui")),
|
||||
);
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves package root via symlinked argv1", async () => {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
try {
|
||||
await withTempDir(async (tmp) => {
|
||||
const realPkg = path.join(tmp, "real-pkg");
|
||||
const bin = path.join(tmp, "bin");
|
||||
await fs.mkdir(realPkg, { recursive: true });
|
||||
@@ -292,14 +268,11 @@ describe("control UI assets helpers", () => {
|
||||
const packageRoot = await resolveOpenClawPackageRoot({ argv1: path.join(bin, "openclaw") });
|
||||
expect(packageRoot).not.toBeNull();
|
||||
expect(await canonicalPath(packageRoot ?? "")).toBe(await canonicalPath(realPkg));
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves dist index path via symlinked argv1 (async)", async () => {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
|
||||
try {
|
||||
await withTempDir(async (tmp) => {
|
||||
const realPkg = path.join(tmp, "real-pkg");
|
||||
const bin = path.join(tmp, "bin");
|
||||
await fs.mkdir(realPkg, { recursive: true });
|
||||
@@ -321,8 +294,6 @@ describe("control UI assets helpers", () => {
|
||||
expect(await canonicalPath(indexPath ?? "")).toBe(
|
||||
await canonicalPath(path.join(realPkg, "dist", "control-ui", "index.html")),
|
||||
);
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user