Gateway: optional canvas host

This commit is contained in:
Peter Steinberger
2025-12-18 11:35:21 +01:00
parent cfb36525ab
commit cd729e83b6
6 changed files with 383 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { WebSocket } from "ws";
import { defaultRuntime } from "../runtime.js";
import { injectCanvasLiveReload, startCanvasHost } from "./server.js";
describe("canvas host", () => {
it("injects live reload script", () => {
const out = injectCanvasLiveReload("<html><body>Hello</body></html>");
expect(out).toContain("/__clawdis/ws");
expect(out).toContain("location.reload");
});
it("serves HTML with injection and broadcasts reload on file changes", async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdis-canvas-"));
const index = path.join(dir, "index.html");
await fs.writeFile(index, "<html><body>v1</body></html>", "utf8");
const server = await startCanvasHost({
runtime: defaultRuntime,
rootDir: dir,
port: 0,
bind: "loopback",
allowInTests: true,
});
try {
const res = await fetch(`http://127.0.0.1:${server.port}/`);
const html = await res.text();
expect(res.status).toBe(200);
expect(html).toContain("v1");
expect(html).toContain("/__clawdis/ws");
const ws = new WebSocket(`ws://127.0.0.1:${server.port}/__clawdis/ws`);
await new Promise<void>((resolve, reject) => {
const timer = setTimeout(
() => reject(new Error("ws open timeout")),
2000,
);
ws.on("open", () => {
clearTimeout(timer);
resolve();
});
ws.on("error", (err) => {
clearTimeout(timer);
reject(err);
});
});
const msg = new Promise<string>((resolve, reject) => {
const timer = setTimeout(
() => reject(new Error("reload timeout")),
4000,
);
ws.on("message", (data) => {
clearTimeout(timer);
resolve(String(data));
});
});
await fs.writeFile(index, "<html><body>v2</body></html>", "utf8");
expect(await msg).toBe("reload");
ws.close();
} finally {
await server.close();
await fs.rm(dir, { recursive: true, force: true });
}
});
});