test(web): update workspace init tests for new profile-based flow
This commit is contained in:
parent
3568779912
commit
efbeacff54
@ -1,3 +1,6 @@
|
|||||||
|
import { EventEmitter } from "node:events";
|
||||||
|
import { join } from "node:path";
|
||||||
|
import type { Dirent } from "node:fs";
|
||||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||||
|
|
||||||
vi.mock("node:fs", () => ({
|
vi.mock("node:fs", () => ({
|
||||||
@ -7,6 +10,7 @@ vi.mock("node:fs", () => ({
|
|||||||
writeFileSync: vi.fn(),
|
writeFileSync: vi.fn(),
|
||||||
mkdirSync: vi.fn(),
|
mkdirSync: vi.fn(),
|
||||||
copyFileSync: vi.fn(),
|
copyFileSync: vi.fn(),
|
||||||
|
cpSync: vi.fn(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("node:child_process", () => ({
|
vi.mock("node:child_process", () => ({
|
||||||
@ -20,18 +24,55 @@ vi.mock("node:child_process", () => ({
|
|||||||
cb(null, { stdout: "" });
|
cb(null, { stdout: "" });
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
spawn: vi.fn(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("node:os", () => ({
|
vi.mock("node:os", () => ({
|
||||||
homedir: vi.fn(() => "/home/testuser"),
|
homedir: vi.fn(() => "/home/testuser"),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
||||||
import { join } from "node:path";
|
|
||||||
|
|
||||||
describe("POST /api/workspace/init", () => {
|
describe("POST /api/workspace/init", () => {
|
||||||
const originalEnv = { ...process.env };
|
const originalEnv = { ...process.env };
|
||||||
const STATE_DIR = join("/home/testuser", ".openclaw");
|
const HOME = "/home/testuser";
|
||||||
|
const IRONCLAW_STATE = join(HOME, ".openclaw-ironclaw");
|
||||||
|
const WORK_STATE = join(HOME, ".openclaw-work");
|
||||||
|
const IRONCLAW_CONFIG = join(IRONCLAW_STATE, "openclaw.json");
|
||||||
|
const IRONCLAW_AUTH = join(IRONCLAW_STATE, "agents", "main", "agent", "auth-profiles.json");
|
||||||
|
|
||||||
|
function makeDirent(name: string, isDir: boolean): Dirent {
|
||||||
|
return {
|
||||||
|
name,
|
||||||
|
isDirectory: () => isDir,
|
||||||
|
isFile: () => !isDir,
|
||||||
|
isBlockDevice: () => false,
|
||||||
|
isCharacterDevice: () => false,
|
||||||
|
isFIFO: () => false,
|
||||||
|
isSocket: () => false,
|
||||||
|
isSymbolicLink: () => false,
|
||||||
|
path: "",
|
||||||
|
parentPath: "",
|
||||||
|
} as Dirent;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mockSpawnExit(code: number, stderr = "") {
|
||||||
|
return vi.fn(() => {
|
||||||
|
const child = new EventEmitter() as EventEmitter & {
|
||||||
|
stdout: EventEmitter;
|
||||||
|
stderr: EventEmitter;
|
||||||
|
kill: ReturnType<typeof vi.fn>;
|
||||||
|
};
|
||||||
|
child.stdout = new EventEmitter();
|
||||||
|
child.stderr = new EventEmitter();
|
||||||
|
child.kill = vi.fn();
|
||||||
|
queueMicrotask(() => {
|
||||||
|
if (stderr) {
|
||||||
|
child.stderr.emit("data", Buffer.from(stderr));
|
||||||
|
}
|
||||||
|
child.emit("close", code);
|
||||||
|
});
|
||||||
|
return child as unknown as ReturnType<typeof import("node:child_process").spawn>;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.resetModules();
|
vi.resetModules();
|
||||||
@ -49,6 +90,7 @@ describe("POST /api/workspace/init", () => {
|
|||||||
writeFileSync: vi.fn(),
|
writeFileSync: vi.fn(),
|
||||||
mkdirSync: vi.fn(),
|
mkdirSync: vi.fn(),
|
||||||
copyFileSync: vi.fn(),
|
copyFileSync: vi.fn(),
|
||||||
|
cpSync: vi.fn(),
|
||||||
}));
|
}));
|
||||||
vi.mock("node:child_process", () => ({
|
vi.mock("node:child_process", () => ({
|
||||||
execSync: vi.fn(() => ""),
|
execSync: vi.fn(() => ""),
|
||||||
@ -61,6 +103,7 @@ describe("POST /api/workspace/init", () => {
|
|||||||
cb(null, { stdout: "" });
|
cb(null, { stdout: "" });
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
spawn: vi.fn(),
|
||||||
}));
|
}));
|
||||||
vi.mock("node:os", () => ({
|
vi.mock("node:os", () => ({
|
||||||
homedir: vi.fn(() => "/home/testuser"),
|
homedir: vi.fn(() => "/home/testuser"),
|
||||||
@ -81,139 +124,128 @@ describe("POST /api/workspace/init", () => {
|
|||||||
return POST(req);
|
return POST(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
it("creates default workspace directory", async () => {
|
it("rejects missing or invalid profile names", async () => {
|
||||||
const mockMkdir = vi.mocked(mkdirSync);
|
const missing = await callInit({});
|
||||||
const response = await callInit({});
|
expect(missing.status).toBe(400);
|
||||||
expect(response.status).toBe(200);
|
|
||||||
expect(mockMkdir).toHaveBeenCalledWith(
|
const invalid = await callInit({ profile: "../bad" });
|
||||||
join(STATE_DIR, "workspace"),
|
expect(invalid.status).toBe(400);
|
||||||
{ recursive: true },
|
|
||||||
);
|
|
||||||
const json = await response.json();
|
|
||||||
expect(json.profile).toBe("default");
|
|
||||||
expect(json.workspaceDir).toBe(join(STATE_DIR, "workspace"));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("creates profile-specific workspace directory", async () => {
|
it("returns 409 when the profile already exists", async () => {
|
||||||
const mockMkdir = vi.mocked(mkdirSync);
|
const { readdirSync, readFileSync } = await import("node:fs");
|
||||||
|
const mockReaddir = vi.mocked(readdirSync);
|
||||||
|
const mockReadFile = vi.mocked(readFileSync);
|
||||||
|
mockReaddir.mockReturnValue([makeDirent(".openclaw-work", true)] as unknown as Dirent[]);
|
||||||
|
mockReadFile.mockImplementation(() => {
|
||||||
|
throw new Error("ENOENT");
|
||||||
|
});
|
||||||
|
|
||||||
const response = await callInit({ profile: "work" });
|
const response = await callInit({ profile: "work" });
|
||||||
|
expect(response.status).toBe(409);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("creates a profile, copies config/auth, allocates gateway port, and runs onboard", async () => {
|
||||||
|
const { existsSync, readFileSync, readdirSync, copyFileSync } = await import("node:fs");
|
||||||
|
const { spawn } = await import("node:child_process");
|
||||||
|
const mockExists = vi.mocked(existsSync);
|
||||||
|
const mockReadFile = vi.mocked(readFileSync);
|
||||||
|
const mockReaddir = vi.mocked(readdirSync);
|
||||||
|
const mockCopyFile = vi.mocked(copyFileSync);
|
||||||
|
const mockSpawn = vi.mocked(spawn);
|
||||||
|
|
||||||
|
mockSpawn.mockImplementation(mockSpawnExit(0));
|
||||||
|
mockReaddir.mockReturnValue([
|
||||||
|
makeDirent(".openclaw-ironclaw", true),
|
||||||
|
makeDirent("Documents", true),
|
||||||
|
] as unknown as Dirent[]);
|
||||||
|
mockExists.mockImplementation((p) => {
|
||||||
|
const s = String(p);
|
||||||
|
return (
|
||||||
|
s === IRONCLAW_CONFIG ||
|
||||||
|
s === IRONCLAW_AUTH ||
|
||||||
|
s.endsWith("docs/reference/templates/AGENTS.md") ||
|
||||||
|
s.endsWith("assets/seed/workspace.duckdb")
|
||||||
|
);
|
||||||
|
});
|
||||||
|
mockReadFile.mockImplementation((p) => {
|
||||||
|
const s = String(p);
|
||||||
|
if (s === IRONCLAW_CONFIG) {
|
||||||
|
return JSON.stringify({ gateway: { mode: "local", port: 18789 } }) as never;
|
||||||
|
}
|
||||||
|
if (s.endsWith("/openclaw.json")) {
|
||||||
|
return JSON.stringify({}) as never;
|
||||||
|
}
|
||||||
|
if (s.endsWith("/AGENTS.md")) {
|
||||||
|
return "# AGENTS\n" as never;
|
||||||
|
}
|
||||||
|
return "" as never;
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await callInit({
|
||||||
|
profile: "work",
|
||||||
|
seedBootstrap: true,
|
||||||
|
copyConfigAuth: true,
|
||||||
|
});
|
||||||
expect(response.status).toBe(200);
|
expect(response.status).toBe(200);
|
||||||
expect(mockMkdir).toHaveBeenCalledWith(
|
|
||||||
join(STATE_DIR, "workspace-work"),
|
|
||||||
{ recursive: true },
|
|
||||||
);
|
|
||||||
const json = await response.json();
|
const json = await response.json();
|
||||||
expect(json.profile).toBe("work");
|
expect(json.profile).toBe("work");
|
||||||
});
|
expect(json.stateDir).toBe(WORK_STATE);
|
||||||
|
expect(json.gatewayPort).toBe(18809);
|
||||||
it("rejects invalid profile names", async () => {
|
expect(json.copiedFiles).toEqual(
|
||||||
const response = await callInit({ profile: "invalid profile!" });
|
expect.arrayContaining(["openclaw.json", "agents/main/agent/auth-profiles.json"]),
|
||||||
expect(response.status).toBe(400);
|
|
||||||
const json = await response.json();
|
|
||||||
expect(json.error).toContain("Invalid profile name");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("allows alphanumeric, hyphens, and underscores in profile names", async () => {
|
|
||||||
const response = await callInit({ profile: "my-work_1" });
|
|
||||||
expect(response.status).toBe(200);
|
|
||||||
const json = await response.json();
|
|
||||||
expect(json.profile).toBe("my-work_1");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("accepts 'default' as profile name", async () => {
|
|
||||||
const response = await callInit({ profile: "default" });
|
|
||||||
expect(response.status).toBe(200);
|
|
||||||
const json = await response.json();
|
|
||||||
expect(json.workspaceDir).toBe(join(STATE_DIR, "workspace"));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("seeds bootstrap files when seedBootstrap is not false", async () => {
|
|
||||||
const mockWrite = vi.mocked(writeFileSync);
|
|
||||||
await callInit({});
|
|
||||||
const writtenPaths = mockWrite.mock.calls.map((c) => c[0] as string);
|
|
||||||
const bootstrapFiles = writtenPaths.filter(
|
|
||||||
(p) =>
|
|
||||||
p.endsWith("AGENTS.md") ||
|
|
||||||
p.endsWith("SOUL.md") ||
|
|
||||||
p.endsWith("TOOLS.md") ||
|
|
||||||
p.endsWith("IDENTITY.md") ||
|
|
||||||
p.endsWith("USER.md") ||
|
|
||||||
p.endsWith("HEARTBEAT.md") ||
|
|
||||||
p.endsWith("BOOTSTRAP.md"),
|
|
||||||
);
|
);
|
||||||
expect(bootstrapFiles.length).toBeGreaterThan(0);
|
expect(json.activeProfile).toBe("work");
|
||||||
});
|
|
||||||
|
|
||||||
it("returns seeded files list", async () => {
|
const onboardCall = mockSpawn.mock.calls.find(
|
||||||
const response = await callInit({});
|
(call) =>
|
||||||
const json = await response.json();
|
String(call[0]) === "openclaw" &&
|
||||||
expect(Array.isArray(json.seededFiles)).toBe(true);
|
Array.isArray(call[1]) &&
|
||||||
});
|
(call[1] as string[]).includes("onboard"),
|
||||||
|
);
|
||||||
it("skips bootstrap seeding when seedBootstrap is false", async () => {
|
expect(onboardCall).toBeTruthy();
|
||||||
const mockWrite = vi.mocked(writeFileSync);
|
const args = onboardCall?.[1] as string[];
|
||||||
const callsBefore = mockWrite.mock.calls.length;
|
expect(args).toEqual(
|
||||||
await callInit({ seedBootstrap: false });
|
expect.arrayContaining([
|
||||||
const bootstrapWrites = mockWrite.mock.calls
|
"--profile",
|
||||||
.slice(callsBefore)
|
"work",
|
||||||
.filter((c) => {
|
"onboard",
|
||||||
const p = c[0] as string;
|
"--install-daemon",
|
||||||
return p.endsWith(".md") && !p.endsWith("workspace-state.json");
|
"--gateway-port",
|
||||||
});
|
"18809",
|
||||||
expect(bootstrapWrites).toHaveLength(0);
|
"--non-interactive",
|
||||||
});
|
"--accept-risk",
|
||||||
|
"--skip-ui",
|
||||||
it("does not overwrite existing bootstrap files (idempotent)", async () => {
|
]),
|
||||||
const mockExist = vi.mocked(existsSync);
|
);
|
||||||
const wsDir = join(STATE_DIR, "workspace");
|
expect(mockCopyFile).toHaveBeenCalledWith(IRONCLAW_CONFIG, join(WORK_STATE, "openclaw.json"));
|
||||||
mockExist.mockImplementation((p) => {
|
expect(mockCopyFile).toHaveBeenCalledWith(
|
||||||
const s = String(p);
|
IRONCLAW_AUTH,
|
||||||
return s === join(wsDir, "AGENTS.md") || s === join(wsDir, "SOUL.md");
|
join(WORK_STATE, "agents", "main", "agent", "auth-profiles.json"),
|
||||||
});
|
|
||||||
|
|
||||||
const response = await callInit({});
|
|
||||||
const json = await response.json();
|
|
||||||
expect(json.seededFiles).not.toContain("AGENTS.md");
|
|
||||||
expect(json.seededFiles).not.toContain("SOUL.md");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("handles custom workspace path", async () => {
|
|
||||||
const mockMkdir = vi.mocked(mkdirSync);
|
|
||||||
const response = await callInit({
|
|
||||||
profile: "custom",
|
|
||||||
path: "/my/custom/workspace",
|
|
||||||
});
|
|
||||||
expect(response.status).toBe(200);
|
|
||||||
expect(mockMkdir).toHaveBeenCalledWith("/my/custom/workspace", {
|
|
||||||
recursive: true,
|
|
||||||
});
|
|
||||||
const json = await response.json();
|
|
||||||
expect(json.workspaceDir).toBe("/my/custom/workspace");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("resolves tilde in custom path", async () => {
|
|
||||||
const mockMkdir = vi.mocked(mkdirSync);
|
|
||||||
await callInit({ profile: "tilde", path: "~/my-workspace" });
|
|
||||||
expect(mockMkdir).toHaveBeenCalledWith(
|
|
||||||
join("/home/testuser", "my-workspace"),
|
|
||||||
{ recursive: true },
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("auto-switches to new profile after creation", async () => {
|
it("returns 500 when onboard fails", async () => {
|
||||||
const response = await callInit({ profile: "newprofile" });
|
const { readdirSync, readFileSync, existsSync } = await import("node:fs");
|
||||||
const json = await response.json();
|
const { spawn } = await import("node:child_process");
|
||||||
expect(json.activeProfile).toBe("newprofile");
|
const mockReaddir = vi.mocked(readdirSync);
|
||||||
});
|
const mockReadFile = vi.mocked(readFileSync);
|
||||||
|
const mockExists = vi.mocked(existsSync);
|
||||||
|
const mockSpawn = vi.mocked(spawn);
|
||||||
|
|
||||||
it("handles mkdir failure with 500", async () => {
|
mockSpawn.mockImplementation(mockSpawnExit(1, "onboard error"));
|
||||||
const mockMkdir = vi.mocked(mkdirSync);
|
mockReaddir.mockReturnValue([makeDirent(".openclaw-ironclaw", true)] as unknown as Dirent[]);
|
||||||
mockMkdir.mockImplementation(() => {
|
mockExists.mockImplementation((p) => String(p) === IRONCLAW_CONFIG || String(p) === IRONCLAW_AUTH);
|
||||||
throw new Error("EACCES: permission denied");
|
mockReadFile.mockImplementation((p) => {
|
||||||
|
if (String(p) === IRONCLAW_CONFIG) {
|
||||||
|
return JSON.stringify({ gateway: { port: 18789 } }) as never;
|
||||||
|
}
|
||||||
|
return "" as never;
|
||||||
});
|
});
|
||||||
const response = await callInit({ profile: "fail" });
|
|
||||||
|
const response = await callInit({ profile: "work" });
|
||||||
expect(response.status).toBe(500);
|
expect(response.status).toBe(500);
|
||||||
const json = await response.json();
|
const json = await response.json();
|
||||||
expect(json.error).toContain("Failed to create workspace directory");
|
expect(String(json.error)).toContain("onboarding failed");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user