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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test";
import {
interpretVoiceTranscript,
voiceSlug,
normaliseInterpretation,
buildInterpretPrompt,
__voiceTest,
} from "../lib/voice-to-pr";
describe("voiceSlug", () => {
it("kebab-cases the input", () => {
expect(voiceSlug("Add dark mode toggle")).toBe("add-dark-mode-toggle");
});
it("trims leading/trailing hyphens", () => {
expect(voiceSlug("--hello--")).toBe("hello");
});
it("caps at 40 chars", () => {
const long = "a".repeat(80);
expect(voiceSlug(long).length).toBeLessThanOrEqual(40);
});
it("falls back to 'voice-note' when empty", () => {
expect(voiceSlug("")).toBe("voice-note");
expect(voiceSlug(" !!! ")).toBe("voice-note");
});
});
describe("normaliseInterpretation", () => {
it("returns the heuristic when raw is null", () => {
const out = normaliseInterpretation(null, "Add dark mode");
expect(out.kind).toBe("spec");
expect(out.title.length).toBeGreaterThan(0);
expect(out.body_markdown).toBe("Add dark mode");
});
it("rejects unknown kinds and falls back to 'unclear'", () => {
const out = normaliseInterpretation(
{ kind: "thingy", title: "t", body_markdown: "b" },
"noop"
);
expect(out.kind).toBe("unclear");
});
it("trims and preserves valid fields", () => {
const out = normaliseInterpretation(
{
kind: "issue",
title: " Bug: header flickers ",
body_markdown: " On Safari only. ",
target_repo_id_hint: "repo-123",
},
"fallback"
);
expect(out.kind).toBe("issue");
expect(out.title).toBe("Bug: header flickers");
expect(out.body_markdown).toBe("On Safari only.");
expect(out.target_repo_id_hint).toBe("repo-123");
});
});
describe("buildInterpretPrompt", () => {
it("embeds the transcript verbatim", () => {
const p = buildInterpretPrompt("Add dark mode", []);
expect(p.includes("Add dark mode")).toBe(true);
expect(p.includes('"kind"')).toBe(true);
});
it("includes a repo list block when repos are supplied", () => {
const p = buildInterpretPrompt("x", [
{ id: "id-1", fullName: "alice/dashboard" },
{ id: "id-2", fullName: "alice/landing" },
]);
expect(p.includes("alice/dashboard")).toBe(true);
expect(p.includes("id-1")).toBe(true);
});
});
describe("__voiceTest.classifyHeuristically", () => {
const { classifyHeuristically } = __voiceTest;
it("classifies feature requests as spec", () => {
expect(classifyHeuristically("Add a dark mode toggle").kind).toBe("spec");
expect(classifyHeuristically("Implement export to CSV").kind).toBe("spec");
});
it("classifies bug reports as issue", () => {
expect(classifyHeuristically("Login is broken on Safari").kind).toBe("issue");
expect(classifyHeuristically("The dashboard crashes when I click").kind).toBe("issue");
});
it("falls back to 'unclear' for ambiguous text", () => {
expect(classifyHeuristically("hmm something").kind).toBe("unclear");
});
});
describe("interpretVoiceTranscript", () => {
it("returns ok:false on empty transcript", async () => {
const r = await interpretVoiceTranscript({ transcript: "" });
expect(r.ok).toBe(false);
});
it("uses the injected client.call and parses spec JSON", async () => {
const r = await interpretVoiceTranscript({
transcript: "Add a dark mode toggle to settings",
client: {
call: async () =>
JSON.stringify({
kind: "spec",
title: "Add dark mode toggle",
body_markdown: "Users want a moon icon in settings.",
}),
},
});
expect(r.ok).toBe(true);
if (!r.ok) throw new Error();
expect(r.suggestion.kind).toBe("spec");
expect(r.suggestion.title).toBe("Add dark mode toggle");
expect(r.suggestion.body_markdown).toContain("moon");
});
it("parses issue JSON wrapped in a ```json fence", async () => {
const r = await interpretVoiceTranscript({
transcript: "The deploy pill keeps flashing",
client: {
call: async () =>
'```json\n{"kind":"issue","title":"Deploy pill flashes","body_markdown":"Race in SSE reconnect."}\n```',
},
});
expect(r.ok).toBe(true);
if (!r.ok) throw new Error();
expect(r.suggestion.kind).toBe("issue");
});
it("falls back to 'unclear' on malformed model output", async () => {
const r = await interpretVoiceTranscript({
transcript: "Some random utterance",
client: { call: async () => "not json at all" },
});
expect(r.ok).toBe(true);
if (!r.ok) throw new Error();
expect(r.suggestion.title.length).toBeGreaterThan(0);
});
it("gracefully degrades to the heuristic when ANTHROPIC_API_KEY is missing", async () => {
const before = process.env.ANTHROPIC_API_KEY;
delete process.env.ANTHROPIC_API_KEY;
try {
const r = await interpretVoiceTranscript({
transcript: "Add a settings page",
});
expect(r.ok).toBe(true);
if (!r.ok) throw new Error();
expect(r.suggestion.kind).toBe("spec");
} finally {
if (before) process.env.ANTHROPIC_API_KEY = before;
}
});
it("returns ok:false (not throw) when the injected client throws", async () => {
const r = await interpretVoiceTranscript({
transcript: "stub fails",
client: {
call: async () => {
throw new Error("boom");
},
},
});
expect(r.ok).toBe(false);
if (r.ok) throw new Error();
expect(r.error).toContain("boom");
});
});
const gitWrites: Array<any> = [];
const issueInserts: Array<any> = [];
mock.module("../git/repository", () => ({
createOrUpdateFileOnBranch: async (input: any) => {
gitWrites.push(input);
return { commitSha: "deadbeef", blobSha: "cafebabe", parentSha: null };
},
}));
mock.module("../db", () => {
function selectStub(shape: Record<string, unknown>): any {
const keys = Object.keys(shape || {});
const row: Record<string, unknown> = {};
const aliasDefaults: Record<string, unknown> = {
id: "repo-1",
name: "demo",
repoName: "demo",
defaultBranch: "main",
ownerName: "alice",
issueCount: 0,
username: "alice",
email: "alice@example.com",
};
for (const k of keys) {
row[k] = aliasDefaults[k] ?? null;
}
return {
from: (_table: any) => ({
leftJoin: (_t: any, _on: any) => ({
where: (_cond: any) => ({
limit: async (_n: number) => [row],
}),
}),
innerJoin: (_t: any, _on: any) => ({
where: () => ({ limit: async () => [row] }),
}),
where: (_cond: any) => ({
limit: async (_n: number) => [row],
}),
}),
};
}
function insertStub(): any {
return {
values: (row: any) => ({
returning: async () => {
issueInserts.push(row);
return [{ id: "issue-id", number: 42 }];
},
}),
};
}
function updateStub(): any {
return {
set: (_row: any) => ({
where: async (_cond: any) => undefined,
}),
};
}
return {
db: {
select: (shape?: any) => selectStub(shape || {}),
insert: () => insertStub(),
update: () => updateStub(),
},
};
});
let shipAsSpec: typeof import("../lib/voice-to-pr").shipAsSpec;
let createIssueFromVoice: typeof import("../lib/voice-to-pr").createIssueFromVoice;
beforeEach(async () => {
gitWrites.length = 0;
issueInserts.length = 0;
const mod = await import("../lib/voice-to-pr");
shipAsSpec = mod.shipAsSpec;
createIssueFromVoice = mod.createIssueFromVoice;
});
afterEach(() => {
});
describe("shipAsSpec", () => {
it("rejects an empty transcript", async () => {
const r = await shipAsSpec({
repositoryId: "repo-1",
transcript: " ",
userId: "user-1",
});
expect(r.ok).toBe(false);
});
it("writes to `.gluecron/specs/voice-<slug>-<ts>.md` on the default branch", async () => {
const r = await shipAsSpec({
repositoryId: "repo-1",
transcript: "Add dark mode toggle to settings",
userId: "user-1",
interpretation: {
kind: "spec",
title: "Add dark mode toggle",
body_markdown: "Users want a moon icon.",
},
});
expect(r.ok).toBe(true);
if (!r.ok) throw new Error();
expect(r.specPath.startsWith(".gluecron/specs/voice-add-dark-mode-toggle-")).toBe(true);
expect(r.specPath.endsWith(".md")).toBe(true);
expect(r.branch).toBe("main");
expect(gitWrites.length).toBe(1);
const w = gitWrites[0];
expect(w.owner).toBe("alice");
expect(w.name).toBe("demo");
expect(w.branch).toBe("main");
const body = new TextDecoder().decode(w.bytes);
expect(body.includes("status: ready")).toBe(true);
expect(body.includes("source: voice-to-pr")).toBe(true);
expect(body.includes("Users want a moon icon.")).toBe(true);
});
it("uses a heuristic title when interpretation is omitted", async () => {
const r = await shipAsSpec({
repositoryId: "repo-1",
transcript: "Implement CSV export",
userId: "user-1",
});
expect(r.ok).toBe(true);
if (!r.ok) throw new Error();
expect(r.specPath.includes("voice-implement-csv-export-")).toBe(true);
});
});
describe("createIssueFromVoice", () => {
it("rejects an empty transcript", async () => {
const r = await createIssueFromVoice({
repositoryId: "repo-1",
transcript: "",
userId: "user-1",
});
expect(r.ok).toBe(false);
});
it("inserts an issue row and returns the issue number", async () => {
const r = await createIssueFromVoice({
repositoryId: "repo-1",
transcript: "Header flickers on Safari",
userId: "user-1",
interpretation: {
kind: "issue",
title: "Header flickers on Safari",
body_markdown: "Repro: open dashboard on Safari 17.",
},
});
expect(r.ok).toBe(true);
if (!r.ok) throw new Error();
expect(r.issueNumber).toBe(42);
expect(r.ownerName).toBe("alice");
expect(r.repoName).toBe("demo");
expect(issueInserts.length).toBe(1);
expect(issueInserts[0].repositoryId).toBe("repo-1");
expect(issueInserts[0].authorId).toBe("user-1");
expect(issueInserts[0].title).toContain("Header flickers");
expect(issueInserts[0].body).toContain("Repro");
expect(issueInserts[0].body.toLowerCase()).toContain("voice-to-pr");
});
});
|