-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-end.test.js
More file actions
115 lines (102 loc) · 3.59 KB
/
Copy pathsession-end.test.js
File metadata and controls
115 lines (102 loc) · 3.59 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
const { describe, it } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { spawnSync } = require('node:child_process');
const hookPath = path.resolve(__dirname, '..', 'hooks', 'session-end.js');
function tmpDir() {
return fs.mkdtempSync(path.join(os.tmpdir(), 'evomap-opencode-session-end-'));
}
function cleanup(dir) {
try {
fs.rmSync(dir, { recursive: true, force: true });
} catch (_err) {
// best effort
}
}
function git(args, cwd) {
const result = spawnSync('git', args, {
cwd,
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
});
assert.equal(result.status, 0, result.stderr || result.stdout);
return result.stdout;
}
function initRepo(dir) {
fs.mkdirSync(dir, { recursive: true });
git(['init'], dir);
git(['config', 'user.email', 'test@example.com'], dir);
git(['config', 'user.name', 'Test User'], dir);
}
function runHook(projectDir, stateDir, graphPath, payload = {}) {
const result = spawnSync('node', [hookPath], {
cwd: projectDir,
input: JSON.stringify(payload),
encoding: 'utf8',
env: {
...process.env,
OPENCODE_PROJECT_DIR: projectDir,
MEMORY_GRAPH_PATH: graphPath,
EVOLVER_SESSION_STATE_DIR: stateDir,
EVOLVER_SESSION_END_STDIN_WATCHDOG_MS: '100',
EVOLVER_HOOK_LOG_DIR: path.join(stateDir, 'logs'),
},
});
assert.equal(result.status, 0, result.stderr || result.stdout);
return result.stdout ? JSON.parse(result.stdout) : {};
}
function readGraph(graphPath) {
try {
return fs.readFileSync(graphPath, 'utf8')
.split('\n')
.filter(Boolean)
.map((line) => JSON.parse(line));
} catch (_err) {
return [];
}
}
describe('session-end hook recording', () => {
it('records a changed working tree once per session id', () => {
const dir = tmpDir();
try {
const projectDir = path.join(dir, 'repo');
const stateDir = path.join(dir, 'state');
const graphPath = path.join(dir, 'memory.jsonl');
initRepo(projectDir);
fs.writeFileSync(path.join(projectDir, 'file.txt'), 'before\n', 'utf8');
git(['add', 'file.txt'], projectDir);
git(['commit', '-m', 'initial'], projectDir);
fs.writeFileSync(path.join(projectDir, 'file.txt'), 'before\nafter\n', 'utf8');
runHook(projectDir, stateDir, graphPath, { session_id: 'same-session' });
runHook(projectDir, stateDir, graphPath, { session_id: 'same-session' });
const rows = readGraph(graphPath);
assert.equal(rows.length, 1);
assert.equal(rows[0].session_id, 'same-session');
assert.equal(rows[0].diff_scope, 'working_tree');
assert.match(rows[0].diff_hash, /^[a-f0-9]{64}$/);
} finally {
cleanup(dir);
}
});
it('does not record prior committed changes when the working tree is clean', () => {
const dir = tmpDir();
try {
const projectDir = path.join(dir, 'repo');
const stateDir = path.join(dir, 'state');
const graphPath = path.join(dir, 'memory.jsonl');
initRepo(projectDir);
fs.writeFileSync(path.join(projectDir, 'file.txt'), 'one\n', 'utf8');
git(['add', 'file.txt'], projectDir);
git(['commit', '-m', 'initial'], projectDir);
fs.writeFileSync(path.join(projectDir, 'file.txt'), 'one\ntwo\n', 'utf8');
git(['add', 'file.txt'], projectDir);
git(['commit', '-m', 'second'], projectDir);
runHook(projectDir, stateDir, graphPath, { session_id: 'clean-session' });
assert.deepEqual(readGraph(graphPath), []);
} finally {
cleanup(dir);
}
});
});