refactor: expand bootstrap helpers and tests

This commit is contained in:
Peter Steinberger
2026-01-18 05:50:23 +00:00
parent d5be8fa576
commit 88b37e80fc
8 changed files with 177 additions and 45 deletions

View File

@@ -0,0 +1,45 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import handler from "./handler.js";
import { createHookEvent } from "../../hooks.js";
import type { AgentBootstrapHookContext } from "../../hooks.js";
import type { ClawdbotConfig } from "../../../config/config.js";
describe("soul-evil hook", () => {
it("skips subagent sessions", async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-soul-"));
await fs.writeFile(path.join(tempDir, "SOUL_EVIL.md"), "chaotic", "utf-8");
const cfg: ClawdbotConfig = {
hooks: {
internal: {
entries: {
"soul-evil": { enabled: true, chance: 1 },
},
},
},
};
const context: AgentBootstrapHookContext = {
workspaceDir: tempDir,
bootstrapFiles: [
{
name: "SOUL.md",
path: path.join(tempDir, "SOUL.md"),
content: "friendly",
missing: false,
},
],
cfg,
sessionKey: "agent:main:subagent:abc",
};
const event = createHookEvent("agent", "bootstrap", "agent:main:subagent:abc", context);
await handler(event);
expect(context.bootstrapFiles[0]?.content).toBe("friendly");
});
});

View File

@@ -128,4 +128,29 @@ describe("applySoulEvilOverride", () => {
const soul = updated.find((file) => file.name === DEFAULT_SOUL_FILENAME);
expect(soul?.content).toBe("friendly");
});
it("leaves files untouched when SOUL.md is not in bootstrap files", async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-soul-"));
const evilPath = path.join(tempDir, DEFAULT_SOUL_EVIL_FILENAME);
await fs.writeFile(evilPath, "chaotic", "utf-8");
const files: WorkspaceBootstrapFile[] = [
{
name: "AGENTS.md",
path: path.join(tempDir, "AGENTS.md"),
content: "agents",
missing: false,
},
];
const updated = await applySoulEvilOverride({
files,
workspaceDir: tempDir,
config: { chance: 1 },
userTimezone: "UTC",
random: () => 0,
});
expect(updated).toEqual(files);
});
});