mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 08:07:40 +00:00
refactor(test): dedupe agents identity test setup
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { makeTempWorkspace } from "../test-helpers/workspace.js";
|
||||||
import { baseConfigSnapshot, createTestRuntime } from "./test-runtime-config-helpers.js";
|
import { baseConfigSnapshot, createTestRuntime } from "./test-runtime-config-helpers.js";
|
||||||
|
|
||||||
const configMocks = vi.hoisted(() => ({
|
const configMocks = vi.hoisted(() => ({
|
||||||
@@ -21,6 +21,27 @@ vi.mock("../config/config.js", async (importOriginal) => {
|
|||||||
import { agentsSetIdentityCommand } from "./agents.js";
|
import { agentsSetIdentityCommand } from "./agents.js";
|
||||||
|
|
||||||
const runtime = createTestRuntime();
|
const runtime = createTestRuntime();
|
||||||
|
type ConfigWritePayload = {
|
||||||
|
agents?: { list?: Array<{ id: string; identity?: Record<string, string> }> };
|
||||||
|
};
|
||||||
|
|
||||||
|
async function createIdentityWorkspace(subdir = "work") {
|
||||||
|
const root = await makeTempWorkspace("openclaw-identity-");
|
||||||
|
const workspace = path.join(root, subdir);
|
||||||
|
await fs.mkdir(workspace, { recursive: true });
|
||||||
|
return { root, workspace };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function writeIdentityFile(workspace: string, lines: string[]) {
|
||||||
|
const identityPath = path.join(workspace, "IDENTITY.md");
|
||||||
|
await fs.writeFile(identityPath, `${lines.join("\n")}\n`, "utf-8");
|
||||||
|
return identityPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getWrittenMainIdentity() {
|
||||||
|
const written = configMocks.writeConfigFile.mock.calls[0]?.[0] as ConfigWritePayload;
|
||||||
|
return written.agents?.list?.find((entry) => entry.id === "main")?.identity;
|
||||||
|
}
|
||||||
|
|
||||||
describe("agents set-identity command", () => {
|
describe("agents set-identity command", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -32,20 +53,14 @@ describe("agents set-identity command", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("sets identity from workspace IDENTITY.md", async () => {
|
it("sets identity from workspace IDENTITY.md", async () => {
|
||||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-identity-"));
|
const { root, workspace } = await createIdentityWorkspace();
|
||||||
const workspace = path.join(root, "work");
|
await writeIdentityFile(workspace, [
|
||||||
await fs.mkdir(workspace, { recursive: true });
|
"- Name: OpenClaw",
|
||||||
await fs.writeFile(
|
"- Creature: helpful sloth",
|
||||||
path.join(workspace, "IDENTITY.md"),
|
"- Emoji: :)",
|
||||||
[
|
"- Avatar: avatars/openclaw.png",
|
||||||
"- Name: OpenClaw",
|
"",
|
||||||
"- Creature: helpful sloth",
|
]);
|
||||||
"- Emoji: :)",
|
|
||||||
"- Avatar: avatars/openclaw.png",
|
|
||||||
"",
|
|
||||||
].join("\n"),
|
|
||||||
"utf-8",
|
|
||||||
);
|
|
||||||
|
|
||||||
configMocks.readConfigFileSnapshot.mockResolvedValue({
|
configMocks.readConfigFileSnapshot.mockResolvedValue({
|
||||||
...baseConfigSnapshot,
|
...baseConfigSnapshot,
|
||||||
@@ -62,11 +77,7 @@ describe("agents set-identity command", () => {
|
|||||||
await agentsSetIdentityCommand({ workspace }, runtime);
|
await agentsSetIdentityCommand({ workspace }, runtime);
|
||||||
|
|
||||||
expect(configMocks.writeConfigFile).toHaveBeenCalledTimes(1);
|
expect(configMocks.writeConfigFile).toHaveBeenCalledTimes(1);
|
||||||
const written = configMocks.writeConfigFile.mock.calls[0]?.[0] as {
|
expect(getWrittenMainIdentity()).toEqual({
|
||||||
agents?: { list?: Array<{ id: string; identity?: Record<string, string> }> };
|
|
||||||
};
|
|
||||||
const main = written.agents?.list?.find((entry) => entry.id === "main");
|
|
||||||
expect(main?.identity).toEqual({
|
|
||||||
name: "OpenClaw",
|
name: "OpenClaw",
|
||||||
theme: "helpful sloth",
|
theme: "helpful sloth",
|
||||||
emoji: ":)",
|
emoji: ":)",
|
||||||
@@ -75,10 +86,8 @@ describe("agents set-identity command", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("errors when multiple agents match the same workspace", async () => {
|
it("errors when multiple agents match the same workspace", async () => {
|
||||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-identity-"));
|
const { workspace } = await createIdentityWorkspace("shared");
|
||||||
const workspace = path.join(root, "shared");
|
await writeIdentityFile(workspace, ["- Name: Echo"]);
|
||||||
await fs.mkdir(workspace, { recursive: true });
|
|
||||||
await fs.writeFile(path.join(workspace, "IDENTITY.md"), "- Name: Echo\n", "utf-8");
|
|
||||||
|
|
||||||
configMocks.readConfigFileSnapshot.mockResolvedValue({
|
configMocks.readConfigFileSnapshot.mockResolvedValue({
|
||||||
...baseConfigSnapshot,
|
...baseConfigSnapshot,
|
||||||
@@ -100,20 +109,14 @@ describe("agents set-identity command", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("overrides identity file values with explicit flags", async () => {
|
it("overrides identity file values with explicit flags", async () => {
|
||||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-identity-"));
|
const { workspace } = await createIdentityWorkspace();
|
||||||
const workspace = path.join(root, "work");
|
await writeIdentityFile(workspace, [
|
||||||
await fs.mkdir(workspace, { recursive: true });
|
"- Name: OpenClaw",
|
||||||
await fs.writeFile(
|
"- Theme: space lobster",
|
||||||
path.join(workspace, "IDENTITY.md"),
|
"- Emoji: :)",
|
||||||
[
|
"- Avatar: avatars/openclaw.png",
|
||||||
"- Name: OpenClaw",
|
"",
|
||||||
"- Theme: space lobster",
|
]);
|
||||||
"- Emoji: :)",
|
|
||||||
"- Avatar: avatars/openclaw.png",
|
|
||||||
"",
|
|
||||||
].join("\n"),
|
|
||||||
"utf-8",
|
|
||||||
);
|
|
||||||
|
|
||||||
configMocks.readConfigFileSnapshot.mockResolvedValue({
|
configMocks.readConfigFileSnapshot.mockResolvedValue({
|
||||||
...baseConfigSnapshot,
|
...baseConfigSnapshot,
|
||||||
@@ -131,11 +134,7 @@ describe("agents set-identity command", () => {
|
|||||||
runtime,
|
runtime,
|
||||||
);
|
);
|
||||||
|
|
||||||
const written = configMocks.writeConfigFile.mock.calls[0]?.[0] as {
|
expect(getWrittenMainIdentity()).toEqual({
|
||||||
agents?: { list?: Array<{ id: string; identity?: Record<string, string> }> };
|
|
||||||
};
|
|
||||||
const main = written.agents?.list?.find((entry) => entry.id === "main");
|
|
||||||
expect(main?.identity).toEqual({
|
|
||||||
name: "Nova",
|
name: "Nova",
|
||||||
theme: "space lobster",
|
theme: "space lobster",
|
||||||
emoji: "🦞",
|
emoji: "🦞",
|
||||||
@@ -144,21 +143,14 @@ describe("agents set-identity command", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("reads identity from an explicit IDENTITY.md path", async () => {
|
it("reads identity from an explicit IDENTITY.md path", async () => {
|
||||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-identity-"));
|
const { workspace } = await createIdentityWorkspace();
|
||||||
const workspace = path.join(root, "work");
|
const identityPath = await writeIdentityFile(workspace, [
|
||||||
const identityPath = path.join(workspace, "IDENTITY.md");
|
"- **Name:** C-3PO",
|
||||||
await fs.mkdir(workspace, { recursive: true });
|
"- **Creature:** Flustered Protocol Droid",
|
||||||
await fs.writeFile(
|
"- **Emoji:** 🤖",
|
||||||
identityPath,
|
"- **Avatar:** avatars/c3po.png",
|
||||||
[
|
"",
|
||||||
"- **Name:** C-3PO",
|
]);
|
||||||
"- **Creature:** Flustered Protocol Droid",
|
|
||||||
"- **Emoji:** 🤖",
|
|
||||||
"- **Avatar:** avatars/c3po.png",
|
|
||||||
"",
|
|
||||||
].join("\n"),
|
|
||||||
"utf-8",
|
|
||||||
);
|
|
||||||
|
|
||||||
configMocks.readConfigFileSnapshot.mockResolvedValue({
|
configMocks.readConfigFileSnapshot.mockResolvedValue({
|
||||||
...baseConfigSnapshot,
|
...baseConfigSnapshot,
|
||||||
@@ -167,11 +159,7 @@ describe("agents set-identity command", () => {
|
|||||||
|
|
||||||
await agentsSetIdentityCommand({ agent: "main", identityFile: identityPath }, runtime);
|
await agentsSetIdentityCommand({ agent: "main", identityFile: identityPath }, runtime);
|
||||||
|
|
||||||
const written = configMocks.writeConfigFile.mock.calls[0]?.[0] as {
|
expect(getWrittenMainIdentity()).toEqual({
|
||||||
agents?: { list?: Array<{ id: string; identity?: Record<string, string> }> };
|
|
||||||
};
|
|
||||||
const main = written.agents?.list?.find((entry) => entry.id === "main");
|
|
||||||
expect(main?.identity).toEqual({
|
|
||||||
name: "C-3PO",
|
name: "C-3PO",
|
||||||
theme: "Flustered Protocol Droid",
|
theme: "Flustered Protocol Droid",
|
||||||
emoji: "🤖",
|
emoji: "🤖",
|
||||||
@@ -180,14 +168,8 @@ describe("agents set-identity command", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("accepts avatar-only identity from IDENTITY.md", async () => {
|
it("accepts avatar-only identity from IDENTITY.md", async () => {
|
||||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-identity-"));
|
const { workspace } = await createIdentityWorkspace();
|
||||||
const workspace = path.join(root, "work");
|
await writeIdentityFile(workspace, ["- Avatar: avatars/only.png"]);
|
||||||
await fs.mkdir(workspace, { recursive: true });
|
|
||||||
await fs.writeFile(
|
|
||||||
path.join(workspace, "IDENTITY.md"),
|
|
||||||
"- Avatar: avatars/only.png\n",
|
|
||||||
"utf-8",
|
|
||||||
);
|
|
||||||
|
|
||||||
configMocks.readConfigFileSnapshot.mockResolvedValue({
|
configMocks.readConfigFileSnapshot.mockResolvedValue({
|
||||||
...baseConfigSnapshot,
|
...baseConfigSnapshot,
|
||||||
@@ -196,11 +178,7 @@ describe("agents set-identity command", () => {
|
|||||||
|
|
||||||
await agentsSetIdentityCommand({ workspace, fromIdentity: true }, runtime);
|
await agentsSetIdentityCommand({ workspace, fromIdentity: true }, runtime);
|
||||||
|
|
||||||
const written = configMocks.writeConfigFile.mock.calls[0]?.[0] as {
|
expect(getWrittenMainIdentity()).toEqual({
|
||||||
agents?: { list?: Array<{ id: string; identity?: Record<string, string> }> };
|
|
||||||
};
|
|
||||||
const main = written.agents?.list?.find((entry) => entry.id === "main");
|
|
||||||
expect(main?.identity).toEqual({
|
|
||||||
avatar: "avatars/only.png",
|
avatar: "avatars/only.png",
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -216,19 +194,13 @@ describe("agents set-identity command", () => {
|
|||||||
runtime,
|
runtime,
|
||||||
);
|
);
|
||||||
|
|
||||||
const written = configMocks.writeConfigFile.mock.calls[0]?.[0] as {
|
expect(getWrittenMainIdentity()).toEqual({
|
||||||
agents?: { list?: Array<{ id: string; identity?: Record<string, string> }> };
|
|
||||||
};
|
|
||||||
const main = written.agents?.list?.find((entry) => entry.id === "main");
|
|
||||||
expect(main?.identity).toEqual({
|
|
||||||
avatar: "https://example.com/avatar.png",
|
avatar: "https://example.com/avatar.png",
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("errors when identity data is missing", async () => {
|
it("errors when identity data is missing", async () => {
|
||||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-identity-"));
|
const { workspace } = await createIdentityWorkspace();
|
||||||
const workspace = path.join(root, "work");
|
|
||||||
await fs.mkdir(workspace, { recursive: true });
|
|
||||||
|
|
||||||
configMocks.readConfigFileSnapshot.mockResolvedValue({
|
configMocks.readConfigFileSnapshot.mockResolvedValue({
|
||||||
...baseConfigSnapshot,
|
...baseConfigSnapshot,
|
||||||
|
|||||||
Reference in New Issue
Block a user