-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
133 lines (113 loc) · 4.43 KB
/
test.js
File metadata and controls
133 lines (113 loc) · 4.43 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
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { spawnSync } from 'node:child_process';
const rootDir = path.resolve('.');
function createTempDir(prefix) {
return fs.mkdtempSync(path.join(os.tmpdir(), prefix));
}
function createStubOpenClaw(binDir) {
const runnerPath = path.join(binDir, 'openclaw-stub.mjs');
const cmdPath = path.join(binDir, 'openclaw.cmd');
const script = `#!/usr/bin/env node
const args = process.argv.slice(2);
const query = args[2] || '';
const payloads = {
"build api": {
results: [
{ path: "memory/api.md", startLine: 10, endLine: 20, score: 0.44, snippet: "Build API guidance and decisions.", source: "memory" },
{ path: "memory/auth.md", startLine: 5, endLine: 12, score: 0.39, snippet: "Auth middleware patterns for API services.", source: "memory" }
]
},
"build api architecture decisions": {
results: [
{ path: "memory/api.md", startLine: 10, endLine: 20, score: 0.45, snippet: "Build API guidance and decisions.", source: "memory" },
{ path: "vault/decisions.md", startLine: 30, endLine: 40, score: 0.41, snippet: "Architecture decisions for service boundaries.", source: "memory" }
]
},
"build api implementation patterns": {
results: [
{ path: "memory/patterns.md", startLine: 7, endLine: 16, score: 0.40, snippet: "Implementation patterns for handlers and validation.", source: "memory" }
]
},
"api architecture decisions": {
results: [
{ path: "session/notes.md", startLine: 1, endLine: 6, score: 0.36, snippet: "API notes from a previous session.", source: "memory" }
]
}
};
const payload = payloads[query] || {
results: [
{ path: "memory/general.md", startLine: 1, endLine: 3, score: 0.33, snippet: "General fallback result.", source: "memory" }
]
};
process.stdout.write(JSON.stringify(payload));
`;
fs.writeFileSync(runnerPath, script, 'utf8');
fs.writeFileSync(cmdPath, `@echo off\r\n"${process.execPath}" "${runnerPath}" %*\r\n`, 'utf8');
}
function runNodeScript(scriptName, args, extraEnv = {}) {
return spawnSync(process.execPath, [path.join(rootDir, scriptName), ...args], {
encoding: 'utf8',
cwd: rootDir,
env: {
...process.env,
...extraEnv,
},
});
}
function testMemoryQueryHumanOutput(stubDir) {
const result = runNodeScript('memory-query.js', ['build api'], {
PATH: `${stubDir};${process.env.PATH || ''}`,
});
assert.equal(result.status, 0, result.stderr);
assert.match(result.stdout, /Memory search results for "build api" \(agent: ops\)/);
assert.match(result.stdout, /memory\/api\.md:10-20 \[score: 0.440\]/);
assert.match(result.stdout, /Auth middleware patterns for API services\./);
}
function testMemoryQueryJsonOutput(stubDir) {
const result = runNodeScript('memory-query.js', ['build api', '--json'], {
PATH: `${stubDir};${process.env.PATH || ''}`,
});
assert.equal(result.status, 0, result.stderr);
const payload = JSON.parse(result.stdout);
assert.equal(payload.results.length, 2);
assert.equal(payload.results[0].path, 'memory/api.md');
}
function testPreflightContext(stubDir) {
const workdir = createTempDir('memory-bridge-workdir-');
const result = runNodeScript(
'preflight-context.js',
['--task', 'build api', '--workdir', workdir, '--context-limit', '4'],
{
PATH: `${stubDir};${process.env.PATH || ''}`,
},
);
assert.equal(result.status, 0, result.stderr);
const contextPath = path.join(workdir, 'CONTEXT.md');
assert.equal(fs.existsSync(contextPath), true);
const markdown = fs.readFileSync(contextPath, 'utf8');
assert.match(markdown, /This context was pulled from your memory system/);
assert.match(markdown, /## memory\/api\.md/);
assert.match(markdown, /Matched by: build api \| build api architecture decisions/);
assert.match(markdown, /Architecture decisions for service boundaries\./);
}
function testMissingOpenClaw() {
const result = runNodeScript('memory-query.js', ['build api'], {
PATH: '',
});
assert.notEqual(result.status, 0);
assert.match(result.stderr, /The `openclaw` CLI was not found in PATH/);
}
function main() {
const stubDir = createTempDir('memory-bridge-stub-');
createStubOpenClaw(stubDir);
testMemoryQueryHumanOutput(stubDir);
testMemoryQueryJsonOutput(stubDir);
testPreflightContext(stubDir);
testMissingOpenClaw();
process.stdout.write('All smoke tests passed.\n');
}
main();