-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorrelation.test.ts
More file actions
181 lines (152 loc) · 6.51 KB
/
Copy pathcorrelation.test.ts
File metadata and controls
181 lines (152 loc) · 6.51 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { CorrelationEngine } from "../src/history/correlation-engine.js";
import { EventStore } from "../src/history/event-store.js";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
describe("CorrelationEngine", () => {
const testDir = path.join(os.tmpdir(), "refiner-correlation-test-" + Date.now());
beforeEach(() => {
process.env.PROMPT_REFINER_GLOBAL_DIR = testDir;
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true });
}
(EventStore as any).instance = null;
});
afterEach(() => {
const store = EventStore.getInstance();
store.close();
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true });
}
});
it("should correlate a commit with a relevant prompt", async () => {
const store = EventStore.getInstance();
// 1. Record a prompt
store.recordPrompt({
id: "p1",
repo_id: "test-repo",
client: "test",
raw_prompt: "Implement a new authentication system using JWT",
timestamp: "2026-04-12T10:00:00Z"
});
// 2. Record a commit that happens later and mentions keywords
const db = (store as any).db;
db.prepare(`
INSERT INTO commits (id, repo_id, sha, author, message, committed_at)
VALUES (?, ?, ?, ?, ?, ?)
`).run("c1", "test-repo", "sha1", "author", "feat: add JWT authentication", "2026-04-12T10:15:00Z");
const engine = new CorrelationEngine();
await engine.correlateAll();
// 3. Verify the link
const link = db.prepare("SELECT * FROM execution_commits WHERE commit_id = ?").get("c1");
expect(link).toBeDefined();
const execution = db.prepare("SELECT * FROM executions WHERE id = ?").get(link.execution_id);
expect(execution.prompt_id).toBe("p1");
const event = db.prepare("SELECT * FROM events WHERE event_type = ?").get("commit_correlated");
expect(event).toBeDefined();
expect(event.prompt_id).toBe("p1");
});
it("should not correlate unrelated commits", async () => {
const store = EventStore.getInstance();
store.recordPrompt({
id: "p_unrelated",
repo_id: "test-repo",
client: "test",
raw_prompt: "Fix bug in logging",
timestamp: "2026-04-12T10:00:00Z"
});
const db = (store as any).db;
db.prepare(`
INSERT INTO commits (id, repo_id, sha, author, message, committed_at)
VALUES (?, ?, ?, ?, ?, ?)
`).run("c_unrelated", "test-repo", "sha2", "author", "docs: update readme", "2026-04-12T10:15:00Z");
const engine = new CorrelationEngine();
await engine.correlateAll();
const link = db.prepare("SELECT * FROM execution_commits WHERE commit_id = ?").get("c_unrelated");
expect(link).toBeUndefined();
});
it("leaves a commit unlinked when there are no prompt candidates", async () => {
const store = EventStore.getInstance();
const db = (store as any).db;
db.prepare("INSERT INTO commits (id, repo_id, sha, author, message, committed_at) VALUES (?, ?, ?, ?, ?, ?)")
.run("orphan", "repo", "sha-orphan", "author", "feat: orphan", "2026-04-12T10:00:00Z");
await new CorrelationEngine().correlateAll();
expect(db.prepare("SELECT * FROM execution_commits WHERE commit_id = ?").get("orphan")).toBeUndefined();
});
it("uses file awareness and an existing execution when correlating", async () => {
const store = EventStore.getInstance();
const db = (store as any).db;
store.recordPrompt({
id: "file-prompt",
repo_id: "repo",
client: "test",
raw_prompt: "Update target.ts and docs",
normalized_prompt: "Modify target.ts",
timestamp: "2026-04-12T10:00:00Z",
});
store.recordExecution({
id: "existing-execution",
prompt_id: "file-prompt",
workflow_name: "manual",
executor_name: "test",
status: "completed",
});
db.prepare("INSERT INTO commits (id, repo_id, sha, author, message, committed_at, changed_files_json) VALUES (?, ?, ?, ?, ?, ?, ?)")
.run("file-commit", "repo", "sha-file", "author", "chore: unrelated wording", "2026-04-12T10:01:00Z", JSON.stringify(["src/target.ts", "/", "a"]));
await new CorrelationEngine().correlateAll();
expect(db.prepare("SELECT execution_id FROM execution_commits WHERE commit_id = ?").get("file-commit"))
.toEqual({ execution_id: "existing-execution" });
});
it("tolerates malformed changed files while correlating by content", async () => {
const store = EventStore.getInstance();
const db = (store as any).db;
store.recordPrompt({
id: "short-prompt",
repo_id: "repo",
client: "test",
raw_prompt: "matching change",
timestamp: "2026-04-12T10:00:00Z",
});
db.prepare("INSERT INTO commits (id, repo_id, sha, author, message, committed_at, changed_files_json) VALUES (?, ?, ?, ?, ?, ?, ?)")
.run("malformed-commit", "repo", "sha-malformed", "author", "matching change", "2026-04-12T10:01:00Z", "{");
await new CorrelationEngine().correlateAll();
expect(db.prepare("SELECT * FROM execution_commits WHERE commit_id = ?").get("malformed-commit")).toBeDefined();
});
it("evaluates but does not correlate a prompt with no usable keywords", async () => {
const store = EventStore.getInstance();
const db = (store as any).db;
store.recordPrompt({
id: "short-prompt",
repo_id: "repo",
client: "test",
raw_prompt: "a to an",
timestamp: "2026-04-12T10:00:00Z",
});
db.prepare("INSERT INTO commits (id, repo_id, sha, author, message, committed_at) VALUES (?, ?, ?, ?, ?, ?)")
.run("short-commit", "repo", "sha-short", "author", "anything", "2026-04-12T10:01:00Z");
await new CorrelationEngine().correlateAll();
expect(db.prepare("SELECT * FROM execution_commits WHERE commit_id = ?").get("short-commit")).toBeUndefined();
});
it("uses an empty changed-file list when defensive input omits it", () => {
const engine = new CorrelationEngine();
const candidate = {
id: "defensive-prompt",
raw_prompt: "matching change",
normalized_prompt: null,
intent: null,
timestamp: "2026-04-12T10:00:00Z",
};
(engine as any).eventStore = {
db: {
prepare: vi.fn().mockReturnValue({ all: vi.fn().mockReturnValue([candidate]) }),
},
};
expect((engine as any).findBestPromptMatch({
repo_id: "repo",
committed_at: "2026-04-12T10:01:00Z",
message: "matching change",
changed_files_json: null,
})).toBe(candidate);
});
});