File size: 4,328 Bytes
34810d2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | // Channel config example tests validate channel configuration snippets in docs.
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import JSON5 from "json5";
import { describe, expect, it } from "vitest";
import { OpenClawSchema } from "../config/zod-schema.js";
import { expectNoReaddirSyncDuring } from "../test-utils/fs-scan-assertions.js";
import { listGitTrackedFiles } from "../test-utils/repo-files.js";
const CHANNEL_DOCS_DIR = path.join(process.cwd(), "docs", "channels");
function lineNumberAt(source: string, index: number): number {
return source.slice(0, index).split("\n").length;
}
function listChannelDocFiles(): string[] {
const externalFiles = listExternalChannelDocFiles();
if (externalFiles) {
return externalFiles;
}
return fs
.readdirSync(CHANNEL_DOCS_DIR)
.filter((entry) => entry.endsWith(".md"))
.map((fileName) => path.join(CHANNEL_DOCS_DIR, fileName))
.toSorted();
}
function listExternalChannelDocFiles(): string[] | null {
return listGitChannelDocFiles() ?? listFindChannelDocFiles();
}
function listGitChannelDocFiles(): string[] | null {
const files = listGitTrackedFiles({ pathspecs: "docs/channels/*.md" });
if (!files) {
return null;
}
return files.map((filePath) => path.join(process.cwd(), filePath)).toSorted();
}
function listFindChannelDocFiles(): string[] | null {
const result = spawnSync(
"find",
[CHANNEL_DOCS_DIR, "-maxdepth", "1", "-type", "f", "-name", "*.md"],
{
cwd: process.cwd(),
encoding: "utf8",
maxBuffer: 1024 * 1024,
stdio: ["ignore", "pipe", "ignore"],
},
);
if (result.status !== 0) {
return null;
}
return result.stdout
.split("\n")
.map((line) => line.trim())
.filter((line) => line.length > 0)
.toSorted();
}
describe("channel docs config examples", () => {
it("lists channel docs without scanning the docs directory in-process", () => {
expectNoReaddirSyncDuring(() => {
const files = listChannelDocFiles();
expect(files.length).toBeGreaterThan(0);
expect(files.every((filePath) => filePath.endsWith(".md"))).toBe(true);
});
});
it("keeps channel docs JSON fences parseable", () => {
const failures: string[] = [];
for (const docPath of listChannelDocFiles()) {
const fileName = path.basename(docPath);
const markdown = fs.readFileSync(docPath, "utf8");
const blocks = markdown.matchAll(/```(?:json5|json)\n([\s\S]*?)```/g);
for (const match of blocks) {
const code = match[1] ?? "";
const location = `${fileName}:${lineNumberAt(markdown, match.index ?? 0)}`;
const isStrictJson = match[0].startsWith("```json\n");
try {
if (isStrictJson) {
JSON.parse(code);
} else {
JSON5.parse(code);
}
} catch (error) {
failures.push(
`${location} ${isStrictJson ? "JSON" : "JSON5"} parse failed: ${String(error)}`,
);
}
}
}
expect(failures).toStrictEqual([]);
});
it("keeps OpenClaw channel config snippets parseable and schema-valid", () => {
const failures: string[] = [];
for (const docPath of listChannelDocFiles()) {
const fileName = path.basename(docPath);
const markdown = fs.readFileSync(docPath, "utf8");
const blocks = markdown.matchAll(/```(?:json5|json)\n([\s\S]*?)```/g);
for (const match of blocks) {
const code = match[1] ?? "";
if (!/(^|\n)\s*(?:"channels"|channels)\s*:/.test(code)) {
continue;
}
const location = `${fileName}:${lineNumberAt(markdown, match.index ?? 0)}`;
let parsed: unknown;
try {
parsed = JSON5.parse(code);
} catch (error) {
failures.push(`${location} JSON5 parse failed: ${String(error)}`);
continue;
}
const result = OpenClawSchema.safeParse(parsed);
if (!result.success) {
const issues = result.error.issues
.slice(0, 3)
.map((issue) => `${issue.path.join(".") || "<root>"}: ${issue.message}`)
.join("; ");
failures.push(`${location} schema failed: ${issues}`);
}
}
}
expect(failures).toStrictEqual([]);
});
});
|