-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-generator.test.ts
More file actions
80 lines (72 loc) · 2.8 KB
/
Copy pathtemplate-generator.test.ts
File metadata and controls
80 lines (72 loc) · 2.8 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
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { EventStore } from "../src/history/event-store.js";
import { TemplateGenerator } from "../src/history/template-generator.js";
describe("TemplateGenerator", () => {
let testDir: string;
let store: EventStore;
beforeEach(() => {
testDir = fs.mkdtempSync(path.join(os.tmpdir(), "template-generator-"));
process.env.PROMPT_REFINER_GLOBAL_DIR = testDir;
(EventStore as any).instance = null;
store = EventStore.getInstance();
});
afterEach(() => {
store.close();
delete process.env.PROMPT_REFINER_GLOBAL_DIR;
fs.rmSync(testDir, { recursive: true, force: true });
});
function addStory(id: string, repoId = "repo") {
const db = (store as any).db;
store.recordPrompt({ id: `prompt-${id}`, repo_id: repoId, client: "test", raw_prompt: `Prompt ${id}` });
store.recordExecution({
id: `execution-${id}`,
prompt_id: `prompt-${id}`,
workflow_name: "test",
executor_name: "test",
status: "completed",
result_summary: `Result ${id}`,
});
store.recordCommit({
id: `commit-${id}`,
repo_id: repoId,
sha: `sha-${id}`,
message: `feat: story ${id}`,
committed_at: new Date().toISOString(),
changed_files_json: JSON.stringify([`src/${id}.ts`]),
});
db.prepare("INSERT INTO execution_commits (execution_id, commit_id) VALUES (?, ?)").run(`execution-${id}`, `commit-${id}`);
}
it("does not invoke synthesis without enough successful history", async () => {
addStory("one");
const request = vi.fn();
await new TemplateGenerator(request).generateNewTemplates("repo");
expect(request).not.toHaveBeenCalled();
});
it("records synthesized templates and ignores unavailable or malformed responses", async () => {
addStory("one");
addStory("two");
const valid = JSON.stringify({
templates: [{
name: "Verified feature",
category: "feature",
template_text: "Implement [INTENT] and verify it.",
usage_notes: "Use for features.",
success_score: 95,
}],
});
const request = vi.fn()
.mockResolvedValueOnce(null)
.mockResolvedValueOnce("not json")
.mockResolvedValueOnce(valid);
const generator = new TemplateGenerator(request);
await generator.generateNewTemplates("repo");
await generator.generateNewTemplates("repo");
await generator.generateNewTemplates("repo");
const templates = (store as any).db.prepare("SELECT * FROM prompt_templates WHERE repo_id = ?").all("repo");
expect(templates).toMatchObject([{ title: "Verified feature", success_score: 95 }]);
expect(request.mock.calls[2][1]).toContain("feat: story one");
});
});