Files
adolf/extensions/migrate-hermes/files-and-skills.test.ts
alvis bedb527145
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Vendor OpenClaw source as Adolf fork baseline
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11),
free to diverge. Tree copied sans upstream .git; upstream remote added for
future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19.
Preserves docs/ARCHITECTURE.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
2026-07-05 09:36:54 +00:00

340 lines
13 KiB
TypeScript

// Migrate Hermes tests cover files and skills plugin behavior.
import fs from "node:fs/promises";
import path from "node:path";
import { DatabaseSync } from "node:sqlite";
import { loadAuthProfileStoreWithoutExternalProfiles } from "openclaw/plugin-sdk/agent-runtime";
import { MIGRATION_REASON_TARGET_EXISTS } from "openclaw/plugin-sdk/migration";
import { afterEach, describe, expect, it } from "vitest";
import { buildHermesMigrationProvider } from "./provider.js";
import { cleanupTempRoots, makeContext, makeTempRoot, writeFile } from "./test/provider-helpers.js";
describe("Hermes migration file and skill items", () => {
afterEach(async () => {
await cleanupTempRoots();
});
function configRuntime(config: Record<string, unknown>) {
return {
config: {
current: () => config,
mutateConfigFile: async ({
mutate,
}: {
mutate: (draft: Record<string, unknown>) => void | Promise<void>;
}) => {
const next = structuredClone(config);
await mutate(next);
Object.keys(config).forEach((key) => {
delete config[key];
});
Object.assign(config, next);
return { nextConfig: next };
},
},
} as never;
}
function itemById<T extends { id: string }>(items: T[], id: string): T | undefined {
return items.find((item) => item.id === id);
}
async function expectPathMissing(targetPath: string): Promise<void> {
try {
await fs.access(targetPath);
} catch (error) {
expect((error as NodeJS.ErrnoException).code).toBe("ENOENT");
return;
}
throw new Error(`Expected path to be missing: ${targetPath}`);
}
it("reports normalized skill-name collisions instead of overwriting during apply", async () => {
const root = await makeTempRoot();
const source = path.join(root, "hermes");
const workspaceDir = path.join(root, "workspace");
const stateDir = path.join(root, "state");
await writeFile(path.join(source, "skills", "Ship It", "SKILL.md"), "# Ship It\n");
await writeFile(path.join(source, "skills", "ship-it", "SKILL.md"), "# ship-it\n");
const provider = buildHermesMigrationProvider();
const plan = await provider.plan(makeContext({ source, stateDir, workspaceDir }));
const skillItems = plan.items.filter((item) => item.kind === "skill");
expect(skillItems).toHaveLength(2);
const shipIt = itemById(skillItems, "skill:ship-it");
expect(shipIt?.status).toBe("conflict");
expect(shipIt?.reason).toBe('multiple Hermes skill directories normalize to "ship-it"');
expect(shipIt?.target).toBe(path.join(workspaceDir, "skills", "ship-it"));
const result = await provider.apply(
makeContext({
source,
stateDir,
workspaceDir,
overwrite: true,
reportDir: path.join(root, "report"),
}),
);
expect(result.summary.conflicts).toBe(2);
await expectPathMissing(path.join(workspaceDir, "skills", "ship-it"));
});
it("reports late-created copy targets as conflicts without overwriting", async () => {
const root = await makeTempRoot();
const source = path.join(root, "hermes");
const workspaceDir = path.join(root, "workspace");
const stateDir = path.join(root, "state");
const reportDir = path.join(root, "report");
await writeFile(path.join(source, "AGENTS.md"), "# Hermes agents\n");
const provider = buildHermesMigrationProvider();
const ctx = makeContext({ source, stateDir, workspaceDir, reportDir });
const plan = await provider.plan(ctx);
await writeFile(path.join(workspaceDir, "AGENTS.md"), "# Late agents\n");
const result = await provider.apply(ctx, plan);
const agents = itemById(result.items, "workspace:AGENTS.md");
expect(agents?.status).toBe("conflict");
expect(agents?.reason).toBe(MIGRATION_REASON_TARGET_EXISTS);
expect(result.summary.conflicts).toBe(1);
expect(await fs.readFile(path.join(workspaceDir, "AGENTS.md"), "utf8")).toBe("# Late agents\n");
});
it("applies files, appended memories, item backups, reports, and opt-in API keys", async () => {
const root = await makeTempRoot();
const source = path.join(root, "hermes");
const workspaceDir = path.join(root, "workspace");
const stateDir = path.join(root, "state");
const reportDir = path.join(root, "report");
await writeFile(path.join(source, ".env"), "OPENAI_API_KEY=sk-hermes\n");
await writeFile(path.join(source, "AGENTS.md"), "# Hermes agents\n");
await writeFile(path.join(source, "memories", "MEMORY.md"), "memory line\n");
await writeFile(path.join(source, "skills", "Ship It", "SKILL.md"), "# Ship It\n");
await writeFile(path.join(workspaceDir, "AGENTS.md"), "# Existing agents\n");
const provider = buildHermesMigrationProvider();
const config: Record<string, unknown> = {};
const result = await provider.apply(
makeContext({
source,
stateDir,
workspaceDir,
includeSecrets: true,
overwrite: true,
reportDir,
runtime: configRuntime(config),
}),
);
expect(result.summary.errors).toBe(0);
expect(result.summary.conflicts).toBe(0);
expect(await fs.readFile(path.join(workspaceDir, "AGENTS.md"), "utf8")).toBe(
"# Hermes agents\n",
);
expect(
await fs.readFile(path.join(workspaceDir, "skills", "ship-it", "SKILL.md"), "utf8"),
).toBe("# Ship It\n");
await expect(fs.access(path.join(reportDir, "summary.md"))).resolves.toBeUndefined();
expect(await fs.readFile(path.join(workspaceDir, "MEMORY.md"), "utf8")).toContain(
"Imported from Hermes",
);
const copiedAgentsItem = result.items.find((item) => item.id === "workspace:AGENTS.md");
expect(String(copiedAgentsItem?.details?.backupPath)).toContain("AGENTS.md");
const agentDir = path.join(stateDir, "agents", "main", "agent");
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
const previousAgentDir = process.env.OPENCLAW_AGENT_DIR;
process.env.OPENCLAW_STATE_DIR = stateDir;
process.env.OPENCLAW_AGENT_DIR = agentDir;
try {
const authStore = loadAuthProfileStoreWithoutExternalProfiles(agentDir);
expect(authStore.profiles?.["openai:hermes-import"]).toEqual(
expect.objectContaining({
type: "api_key",
provider: "openai",
key: "sk-hermes",
}),
);
} finally {
if (previousStateDir === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = previousStateDir;
}
if (previousAgentDir === undefined) {
delete process.env.OPENCLAW_AGENT_DIR;
} else {
process.env.OPENCLAW_AGENT_DIR = previousAgentDir;
}
}
});
it("archives unsupported Hermes state without copying raw auth credentials", async () => {
const root = await makeTempRoot();
const source = path.join(root, "hermes");
const workspaceDir = path.join(root, "workspace");
const stateDir = path.join(root, "state");
const reportDir = path.join(root, "report");
await writeFile(path.join(source, "logs", "session.log"), "log line\n");
await writeFile(path.join(source, "auth.json"), '{"token":"opaque"}\n');
const provider = buildHermesMigrationProvider();
const plan = await provider.plan(makeContext({ source, stateDir, workspaceDir, reportDir }));
const plannedLogs = itemById(plan.items, "archive:logs");
expect(plannedLogs?.kind).toBe("archive");
expect(plannedLogs?.action).toBe("archive");
expect(plannedLogs?.status).toBe("planned");
expect(plan.items.find((item) => item.id === "archive:auth.json")).toBeUndefined();
expect(plan.warnings).toEqual([
"Some Hermes files are archive-only. They will be copied into the migration report for manual review, not loaded into OpenClaw.",
]);
const result = await provider.apply(makeContext({ source, stateDir, workspaceDir, reportDir }));
expect(result.summary.errors).toBe(0);
const migratedLogs = itemById(result.items, "archive:logs");
expect(migratedLogs?.status).toBe("migrated");
expect(migratedLogs?.target).toBe(path.join(reportDir, "archive", "logs"));
expect(await fs.readFile(path.join(reportDir, "archive", "logs", "session.log"), "utf8")).toBe(
"log line\n",
);
await expectPathMissing(path.join(reportDir, "archive", "auth.json"));
await expectPathMissing(path.join(workspaceDir, "logs", "session.log"));
});
it("archives committed Hermes SQLite WAL state", async () => {
const root = await makeTempRoot();
const source = path.join(root, "hermes");
const workspaceDir = path.join(root, "workspace");
const stateDir = path.join(root, "state");
const reportDir = path.join(root, "report");
const stateDbPath = path.join(source, "state.db");
await fs.mkdir(source, { recursive: true });
const sourceDb = new DatabaseSync(stateDbPath);
try {
sourceDb.exec(`
PRAGMA journal_mode = WAL;
CREATE TABLE marker(value TEXT NOT NULL);
PRAGMA wal_checkpoint(TRUNCATE);
`);
sourceDb.prepare("INSERT INTO marker(value) VALUES (?)").run("committed-only-in-wal");
expect((await fs.stat(`${stateDbPath}-wal`)).size).toBeGreaterThan(0);
const provider = buildHermesMigrationProvider();
const result = await provider.apply(
makeContext({ source, stateDir, workspaceDir, reportDir }),
);
const archivedState = itemById(result.items, "archive:state.db");
const archivedStatePath = path.join(reportDir, "archive", "state.db");
expect(archivedState?.status).toBe("migrated");
expect(archivedState?.source).toBe(stateDbPath);
expect(archivedState?.target).toBe(archivedStatePath);
const archivedDb = new DatabaseSync(archivedStatePath, { readOnly: true });
try {
expect(archivedDb.prepare("SELECT value FROM marker").all()).toEqual([
{ value: "committed-only-in-wal" },
]);
expect(archivedDb.prepare("PRAGMA integrity_check").get()).toEqual({
integrity_check: "ok",
});
} finally {
archivedDb.close();
}
} finally {
sourceDb.close();
}
});
it("preserves raw Hermes state when SQLite snapshotting fails", async () => {
const root = await makeTempRoot();
const source = path.join(root, "hermes");
const workspaceDir = path.join(root, "workspace");
const stateDir = path.join(root, "state");
const reportDir = path.join(root, "report");
const stateDbPath = path.join(source, "state.db");
const archivedStatePath = path.join(reportDir, "archive", "state.db");
await writeFile(stateDbPath, "legacy non-SQLite Hermes state\n");
const provider = buildHermesMigrationProvider();
const result = await provider.apply(makeContext({ source, stateDir, workspaceDir, reportDir }));
const archivedState = itemById(result.items, "archive:state.db");
expect(archivedState?.status).toBe("error");
expect(archivedState?.target).toBe(archivedStatePath);
expect(archivedState?.reason).toContain(
"SQLite snapshot failed; raw state.db preserved for manual review",
);
expect(await fs.readFile(archivedStatePath, "utf8")).toBe("legacy non-SQLite Hermes state\n");
expect(result.summary.errors).toBe(1);
});
it("reports legacy Hermes OpenAI auth.json OAuth state as manual reauth work", async () => {
const root = await makeTempRoot();
const source = path.join(root, "hermes");
const workspaceDir = path.join(root, "workspace");
const stateDir = path.join(root, "state");
await writeFile(
path.join(source, "auth.json"),
JSON.stringify({
providers: {
openai: {
tokens: {
access_token: "old-access",
refresh_token: "old-refresh",
},
},
},
credential_pool: {
openai: [
{
access_token: "pool-access",
refresh_token: "pool-refresh",
},
],
},
}),
);
const provider = buildHermesMigrationProvider();
const plan = await provider.plan(
makeContext({ source, stateDir, workspaceDir, includeSecrets: true }),
);
const manualAuth = itemById(plan.items, "manual:legacy-hermes-auth-json");
expect(manualAuth?.kind).toBe("manual");
expect(manualAuth?.status).toBe("skipped");
expect(manualAuth?.message).toContain("no longer imports");
expect(plan.items.some((item) => item.kind === "auth")).toBe(false);
expect(plan.warnings).toContain(
"Some Hermes settings require manual review before they can be activated safely.",
);
});
it("ignores empty Hermes auth.json credential containers", async () => {
const root = await makeTempRoot();
const source = path.join(root, "hermes");
const workspaceDir = path.join(root, "workspace");
const stateDir = path.join(root, "state");
await writeFile(
path.join(source, "auth.json"),
JSON.stringify({
providers: {},
credential_pool: {},
tokens: { anthropic: { access: "other-access", refresh: "other-refresh" } },
}),
);
const provider = buildHermesMigrationProvider();
const plan = await provider.plan(
makeContext({ source, stateDir, workspaceDir, includeSecrets: true }),
);
expect(plan.items.find((item) => item.id === "manual:legacy-hermes-auth-json")).toBeUndefined();
});
});