refactor(tests): dedupe canvas host server setup

This commit is contained in:
Peter Steinberger
2026-03-03 02:21:19 +00:00
parent 4a59d0ad98
commit 4fb6da2b32

View File

@@ -65,6 +65,25 @@ describe("canvas host", () => {
return dir;
};
const startFixtureCanvasHost = async (
rootDir: string,
overrides: Partial<Parameters<typeof startCanvasHost>[0]> = {},
) =>
await startCanvasHost({
runtime: quietRuntime,
rootDir,
port: 0,
listenHost: "127.0.0.1",
allowInTests: true,
...overrides,
});
const fetchCanvasHtml = async (port: number) => {
const res = await fetch(`http://127.0.0.1:${port}${CANVAS_HOST_PATH}/`);
const html = await res.text();
return { res, html };
};
beforeAll(async () => {
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-canvas-fixtures-"));
});
@@ -84,17 +103,10 @@ describe("canvas host", () => {
it("creates a default index.html when missing", async () => {
const dir = await createCaseDir();
const server = await startCanvasHost({
runtime: quietRuntime,
rootDir: dir,
port: 0,
listenHost: "127.0.0.1",
allowInTests: true,
});
const server = await startFixtureCanvasHost(dir);
try {
const res = await fetch(`http://127.0.0.1:${server.port}${CANVAS_HOST_PATH}/`);
const html = await res.text();
const { res, html } = await fetchCanvasHtml(server.port);
expect(res.status).toBe(200);
expect(html).toContain("Interactive test page");
expect(html).toContain("openclawSendUserAction");
@@ -108,18 +120,10 @@ describe("canvas host", () => {
const dir = await createCaseDir();
await fs.writeFile(path.join(dir, "index.html"), "<html><body>no-reload</body></html>", "utf8");
const server = await startCanvasHost({
runtime: quietRuntime,
rootDir: dir,
port: 0,
listenHost: "127.0.0.1",
allowInTests: true,
liveReload: false,
});
const server = await startFixtureCanvasHost(dir, { liveReload: false });
try {
const res = await fetch(`http://127.0.0.1:${server.port}${CANVAS_HOST_PATH}/`);
const html = await res.text();
const { res, html } = await fetchCanvasHtml(server.port);
expect(res.status).toBe(200);
expect(html).toContain("no-reload");
expect(html).not.toContain(CANVAS_WS_PATH);
@@ -206,20 +210,13 @@ describe("canvas host", () => {
await fs.writeFile(index, "<html><body>v1</body></html>", "utf8");
const watcherStart = chokidarMockState.watchers.length;
const server = await startCanvasHost({
runtime: quietRuntime,
rootDir: dir,
port: 0,
listenHost: "127.0.0.1",
allowInTests: true,
});
const server = await startFixtureCanvasHost(dir);
try {
const watcher = chokidarMockState.watchers[watcherStart];
expect(watcher).toBeTruthy();
const res = await fetch(`http://127.0.0.1:${server.port}${CANVAS_HOST_PATH}/`);
const html = await res.text();
const { res, html } = await fetchCanvasHtml(server.port);
expect(res.status).toBe(200);
expect(html).toContain("v1");
expect(html).toContain(CANVAS_WS_PATH);
@@ -281,13 +278,7 @@ describe("canvas host", () => {
await fs.symlink(path.join(process.cwd(), "package.json"), linkPath);
createdLink = true;
const server = await startCanvasHost({
runtime: quietRuntime,
rootDir: dir,
port: 0,
listenHost: "127.0.0.1",
allowInTests: true,
});
const server = await startFixtureCanvasHost(dir);
try {
const res = await fetch(`http://127.0.0.1:${server.port}/__openclaw__/a2ui/`);