forked from laurentenhoor/devclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace.test.ts
More file actions
122 lines (100 loc) · 4.95 KB
/
workspace.test.ts
File metadata and controls
122 lines (100 loc) · 4.95 KB
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
/**
* workspace.test.ts — Tests for write-once default file behavior.
*
* Verifies that ensureDefaultFiles() creates missing files but never
* overwrites user-owned config (workflow.yaml, prompts, IDENTITY.md).
*
* Run: npx tsx --test lib/setup/workspace.test.ts
*/
import { describe, it, afterEach } from "node:test";
import assert from "node:assert";
import fs from "node:fs/promises";
import path from "node:path";
import os from "node:os";
import { ensureDefaultFiles, fileExists } from "./workspace.js";
import { DATA_DIR } from "./migrate-layout.js";
let tmpDir: string;
async function makeTmpDir(): Promise<string> {
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "devclaw-ws-test-"));
// Create the log dir so audit logging doesn't fail
await fs.mkdir(path.join(tmpDir, DATA_DIR, "log"), { recursive: true });
return tmpDir;
}
afterEach(async () => {
if (tmpDir) await fs.rm(tmpDir, { recursive: true, force: true });
});
describe("ensureDefaultFiles — write-once behavior", () => {
it("should create workflow.yaml when missing", async () => {
const ws = await makeTmpDir();
await ensureDefaultFiles(ws);
const workflowPath = path.join(ws, DATA_DIR, "workflow.yaml");
assert.ok(await fileExists(workflowPath), "workflow.yaml should be created");
});
it("should NOT overwrite existing workflow.yaml", async () => {
const ws = await makeTmpDir();
const workflowPath = path.join(ws, DATA_DIR, "workflow.yaml");
await fs.mkdir(path.dirname(workflowPath), { recursive: true });
const customContent = "# My custom workflow\nroles:\n developer:\n models:\n junior: openai/gpt-4\n";
await fs.writeFile(workflowPath, customContent, "utf-8");
await ensureDefaultFiles(ws);
const afterContent = await fs.readFile(workflowPath, "utf-8");
assert.strictEqual(afterContent, customContent, "workflow.yaml should not be overwritten");
});
it("should create prompt files when missing", async () => {
const ws = await makeTmpDir();
await ensureDefaultFiles(ws);
const devPrompt = path.join(ws, DATA_DIR, "prompts", "developer.md");
assert.ok(await fileExists(devPrompt), "developer.md prompt should be created");
});
it("should NOT overwrite existing prompt files", async () => {
const ws = await makeTmpDir();
const devPrompt = path.join(ws, DATA_DIR, "prompts", "developer.md");
await fs.mkdir(path.dirname(devPrompt), { recursive: true });
const customPrompt = "# My custom developer instructions\nAlways use TypeScript.";
await fs.writeFile(devPrompt, customPrompt, "utf-8");
await ensureDefaultFiles(ws);
const afterContent = await fs.readFile(devPrompt, "utf-8");
assert.strictEqual(afterContent, customPrompt, "developer.md should not be overwritten");
});
it("should NOT delete project-specific prompts", async () => {
const ws = await makeTmpDir();
const projectPrompt = path.join(ws, DATA_DIR, "projects", "my-app", "prompts", "developer.md");
await fs.mkdir(path.dirname(projectPrompt), { recursive: true });
const customPrompt = "# My App Developer\nUse React.";
await fs.writeFile(projectPrompt, customPrompt, "utf-8");
await ensureDefaultFiles(ws);
assert.ok(await fileExists(projectPrompt), "project-specific prompt should still exist");
const afterContent = await fs.readFile(projectPrompt, "utf-8");
assert.strictEqual(afterContent, customPrompt, "project-specific prompt should be untouched");
});
it("should create IDENTITY.md when missing but not overwrite", async () => {
const ws = await makeTmpDir();
// First run: creates it
await ensureDefaultFiles(ws);
const identityPath = path.join(ws, "IDENTITY.md");
assert.ok(await fileExists(identityPath), "IDENTITY.md should be created");
// Customize it
const customIdentity = "# My Identity\nI am a lobster.";
await fs.writeFile(identityPath, customIdentity, "utf-8");
// Second run: should NOT overwrite
await ensureDefaultFiles(ws);
const afterContent = await fs.readFile(identityPath, "utf-8");
assert.strictEqual(afterContent, customIdentity, "IDENTITY.md should not be overwritten");
});
it("should always overwrite AGENTS.md (system instructions)", async () => {
const ws = await makeTmpDir();
const agentsPath = path.join(ws, "AGENTS.md");
await fs.writeFile(agentsPath, "# Old agents content", "utf-8");
await ensureDefaultFiles(ws);
const afterContent = await fs.readFile(agentsPath, "utf-8");
assert.notStrictEqual(afterContent, "# Old agents content", "AGENTS.md should be overwritten");
});
it("should write .version file", async () => {
const ws = await makeTmpDir();
await ensureDefaultFiles(ws);
const versionPath = path.join(ws, DATA_DIR, ".version");
assert.ok(await fileExists(versionPath), ".version file should be created");
const content = await fs.readFile(versionPath, "utf-8");
assert.ok(content.trim().length > 0, ".version should contain a version string");
});
});