Files
openclaw/src/agents/docs-path.test.ts
2026-06-17 10:27:05 +08:00

86 lines
3.2 KiB
TypeScript

// Covers locating OpenClaw docs and source paths from package roots.
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { resolveOpenClawReferencePaths } from "./docs-path.js";
async function makePackageRoot(prefix: string): Promise<string> {
// Tests create minimal package roots so path resolution is checked without
// depending on this checkout's real docs or git state.
const root = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
await fs.writeFile(path.join(root, "package.json"), '{"name":"openclaw"}\n');
return root;
}
async function writeDocsJson(root: string): Promise<void> {
await fs.mkdir(path.join(root, "docs"), { recursive: true });
await fs.writeFile(path.join(root, "docs", "docs.json"), "{}\n");
}
describe("resolveOpenClawDocsPath", () => {
it("uses the workspace docs directory when it has canonical docs metadata", async () => {
const root = await makePackageRoot("openclaw-docs-workspace-");
await writeDocsJson(root);
await expect(resolveOpenClawReferencePaths({ workspaceDir: root })).resolves.toMatchObject({
docsPath: path.join(root, "docs"),
});
});
it("finds bundled package docs from a nested package path", async () => {
const root = await makePackageRoot("openclaw-docs-package-");
await writeDocsJson(root);
const nested = path.join(root, "dist", "agents");
await fs.mkdir(nested, { recursive: true });
await expect(resolveOpenClawReferencePaths({ cwd: nested })).resolves.toMatchObject({
docsPath: path.join(root, "docs"),
});
});
it("does not accept incomplete template-only docs directories", async () => {
// Template folders alone are not published docs; docs.json is the canonical
// marker that the path is usable for model reference context.
const root = await makePackageRoot("openclaw-docs-incomplete-");
await fs.mkdir(path.join(root, "docs", "reference", "templates"), { recursive: true });
await expect(resolveOpenClawReferencePaths({ cwd: root })).resolves.toMatchObject({
docsPath: null,
});
});
});
describe("resolveOpenClawSourcePath", () => {
it("returns the package root only for git checkouts", async () => {
const root = await makePackageRoot("openclaw-source-git-");
await fs.mkdir(path.join(root, ".git"));
await expect(resolveOpenClawReferencePaths({ cwd: root })).resolves.toMatchObject({
sourcePath: root,
});
});
it("omits source path for npm-style package installs", async () => {
// npm installs may contain package files but not source checkout metadata.
const root = await makePackageRoot("openclaw-source-npm-");
await expect(resolveOpenClawReferencePaths({ cwd: root })).resolves.toMatchObject({
sourcePath: null,
});
});
});
describe("resolveOpenClawReferencePaths", () => {
it("returns docs and local source together for git checkouts", async () => {
const root = await makePackageRoot("openclaw-reference-git-");
await writeDocsJson(root);
await fs.mkdir(path.join(root, ".git"));
await expect(resolveOpenClawReferencePaths({ cwd: root })).resolves.toEqual({
docsPath: path.join(root, "docs"),
sourcePath: root,
});
});
});