refactor: eliminate remaining duplicate blocks across draft streams and tests

This commit is contained in:
Peter Steinberger
2026-02-21 23:56:58 +00:00
parent abf3dfc375
commit ad1c07e7c0
16 changed files with 316 additions and 199 deletions

View File

@@ -1,25 +1,19 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { createTrackedTempDirs } from "../test-utils/tracked-temp-dirs.js";
import { writeSkill } from "./skills.e2e-test-helpers.js";
import { buildWorkspaceSkillSnapshot } from "./skills.js";
const tempDirs: string[] = [];
async function createTempDir(prefix: string) {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
tempDirs.push(dir);
return dir;
}
const tempDirs = createTrackedTempDirs();
afterEach(async () => {
await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })));
await tempDirs.cleanup();
});
describe("buildWorkspaceSkillSnapshot", () => {
it("returns an empty snapshot when skills dirs are missing", async () => {
const workspaceDir = await createTempDir("openclaw-");
const workspaceDir = await tempDirs.make("openclaw-");
const snapshot = buildWorkspaceSkillSnapshot(workspaceDir, {
managedSkillsDir: path.join(workspaceDir, ".managed"),
@@ -31,7 +25,7 @@ describe("buildWorkspaceSkillSnapshot", () => {
});
it("omits disable-model-invocation skills from the prompt", async () => {
const workspaceDir = await createTempDir("openclaw-");
const workspaceDir = await tempDirs.make("openclaw-");
await writeSkill({
dir: path.join(workspaceDir, "skills", "visible-skill"),
name: "visible-skill",
@@ -58,7 +52,7 @@ describe("buildWorkspaceSkillSnapshot", () => {
});
it("truncates the skills prompt when it exceeds the configured char budget", async () => {
const workspaceDir = await createTempDir("openclaw-");
const workspaceDir = await tempDirs.make("openclaw-");
// Make a bunch of skills with very long descriptions.
for (let i = 0; i < 25; i += 1) {
@@ -88,8 +82,8 @@ describe("buildWorkspaceSkillSnapshot", () => {
});
it("limits discovery for nested repo-style skills roots (dir/skills/*)", async () => {
const workspaceDir = await createTempDir("openclaw-");
const repoDir = await createTempDir("openclaw-skills-repo-");
const workspaceDir = await tempDirs.make("openclaw-");
const repoDir = await tempDirs.make("openclaw-skills-repo-");
for (let i = 0; i < 20; i += 1) {
const name = `repo-skill-${String(i).padStart(2, "0")}`;
@@ -123,7 +117,7 @@ describe("buildWorkspaceSkillSnapshot", () => {
});
it("skips skills whose SKILL.md exceeds maxSkillFileBytes", async () => {
const workspaceDir = await createTempDir("openclaw-");
const workspaceDir = await tempDirs.make("openclaw-");
await writeSkill({
dir: path.join(workspaceDir, "skills", "small-skill"),
@@ -157,8 +151,8 @@ describe("buildWorkspaceSkillSnapshot", () => {
});
it("detects nested skills roots beyond the first 25 entries", async () => {
const workspaceDir = await createTempDir("openclaw-");
const repoDir = await createTempDir("openclaw-skills-repo-");
const workspaceDir = await tempDirs.make("openclaw-");
const repoDir = await tempDirs.make("openclaw-skills-repo-");
// Create 30 nested dirs, but only the last one is an actual skill.
for (let i = 0; i < 30; i += 1) {
@@ -194,8 +188,8 @@ describe("buildWorkspaceSkillSnapshot", () => {
});
it("enforces maxSkillFileBytes for root-level SKILL.md", async () => {
const workspaceDir = await createTempDir("openclaw-");
const rootSkillDir = await createTempDir("openclaw-root-skill-");
const workspaceDir = await tempDirs.make("openclaw-");
const rootSkillDir = await tempDirs.make("openclaw-root-skill-");
await writeSkill({
dir: rootSkillDir,