|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { |
| 3 | + DEFAULT_SERVER_SETTINGS, |
| 4 | + ProjectId, |
| 5 | + ProviderInstanceId, |
| 6 | + ProviderDriverKind, |
| 7 | + TextGenerationError, |
| 8 | + VOICE_TEXT_STARTER_PROMPTS_FORK, |
| 9 | + type VoiceTextConfigFork, |
| 10 | + type ModelSelection, |
| 11 | +} from "@t3tools/contracts"; |
| 12 | +import * as Effect from "effect/Effect"; |
| 13 | +import * as FileSystem from "effect/FileSystem"; |
| 14 | +import * as Layer from "effect/Layer"; |
| 15 | +import * as Option from "effect/Option"; |
| 16 | +import * as Path from "effect/Path"; |
| 17 | +import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; |
| 18 | +import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts"; |
| 19 | +import type { ProviderInstance } from "@t3tools/provider-core/server/driver"; |
| 20 | +import * as ProjectStore from "../orchestration-v2/ProjectStore.ts"; |
| 21 | +import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts"; |
| 22 | +import * as ServerSettings from "../serverSettings.ts"; |
| 23 | +import * as TextGeneration from "../textGeneration/TextGeneration.ts"; |
| 24 | +import * as TextGenerationOperations from "@t3tools/provider-core/server/textGenerationOperations"; |
| 25 | +import { withVoiceTextGenerationFork } from "@t3tools/provider-core/server/voiceTextGeneration.fork"; |
| 26 | +import * as VoiceText from "./VoiceText.fork.ts"; |
| 27 | +import { expandVoiceTextTemplateFork, voiceRecentMessagesFork } from "./voiceTextPrompts.fork.ts"; |
| 28 | + |
| 29 | +const projectId = ProjectId.make("dictation-project"); |
| 30 | +const modelSelection = { |
| 31 | + instanceId: ProviderInstanceId.make("dictation-model"), |
| 32 | + model: "test-model", |
| 33 | +}; |
| 34 | +const config: VoiceTextConfigFork = { |
| 35 | + modelSelection, |
| 36 | + cleanupMode: "manual", |
| 37 | + formattingMode: "manual", |
| 38 | + formattingMinWords: 50, |
| 39 | + recentMessageCount: 0, |
| 40 | + projectContext: "React, Zustand, Hyprland", |
| 41 | + ...VOICE_TEXT_STARTER_PROMPTS_FORK, |
| 42 | +}; |
| 43 | + |
| 44 | +function harness() { |
| 45 | + const stored = new Map<string, Uint8Array>(); |
| 46 | + const calls: Array<{ prompt: string; cwd: string; modelSelection: ModelSelection }> = []; |
| 47 | + const reads: string[] = []; |
| 48 | + let serverModel = DEFAULT_SERVER_SETTINGS.textGenerationModelSelection; |
| 49 | + let result = "Corrected transcription"; |
| 50 | + let fails = false; |
| 51 | + const instance: ProviderInstance = { |
| 52 | + instanceId: modelSelection.instanceId, |
| 53 | + driverKind: ProviderDriverKind.make("codex"), |
| 54 | + continuationIdentity: { driverKind: ProviderDriverKind.make("codex"), continuationKey: "test" }, |
| 55 | + displayName: undefined, |
| 56 | + enabled: true, |
| 57 | + get snapshot(): never { |
| 58 | + throw new Error("not used"); |
| 59 | + }, |
| 60 | + get orchestrationAdapter(): never { |
| 61 | + throw new Error("not used"); |
| 62 | + }, |
| 63 | + textGeneration: withVoiceTextGenerationFork( |
| 64 | + TextGeneration.TextGeneration.of({ |
| 65 | + generateCommitMessage: () => Effect.die("not used"), |
| 66 | + generatePrContent: () => Effect.die("not used"), |
| 67 | + generateBranchName: () => Effect.die("not used"), |
| 68 | + generateThreadTitle: () => Effect.die("not used"), |
| 69 | + generateReviewGuide: () => Effect.die("not used"), |
| 70 | + }), |
| 71 | + (input) => { |
| 72 | + calls.push(input); |
| 73 | + return fails |
| 74 | + ? Effect.fail( |
| 75 | + new TextGenerationError({ operation: "generateTextFork", detail: "unavailable" }), |
| 76 | + ) |
| 77 | + : TextGenerationOperations.decodeJsonReply( |
| 78 | + input, |
| 79 | + "test", |
| 80 | + JSON.stringify({ text: result }), |
| 81 | + ); |
| 82 | + }, |
| 83 | + ), |
| 84 | + }; |
| 85 | + const layer = VoiceText.layer.pipe( |
| 86 | + Layer.provide( |
| 87 | + Layer.mergeAll( |
| 88 | + Layer.mock(ServerSecretStore.ServerSecretStore)({ |
| 89 | + get: (name) => Effect.sync(() => Option.fromUndefinedOr(stored.get(name))), |
| 90 | + set: (name, value) => |
| 91 | + Effect.sync(() => { |
| 92 | + stored.set(name, value); |
| 93 | + }), |
| 94 | + }), |
| 95 | + Layer.mock(ServerSettings.ServerSettingsService)({ |
| 96 | + getSettings: Effect.sync(() => ({ |
| 97 | + ...DEFAULT_SERVER_SETTINGS, |
| 98 | + textGenerationModelSelection: serverModel, |
| 99 | + })), |
| 100 | + }), |
| 101 | + Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({ |
| 102 | + getInstance: (id) => |
| 103 | + Effect.succeed(id === modelSelection.instanceId ? instance : undefined), |
| 104 | + }), |
| 105 | + Layer.mock(ProjectStore.ProjectStoreV2)({ |
| 106 | + get: (id) => |
| 107 | + Effect.succeed( |
| 108 | + id === projectId |
| 109 | + ? Option.some({ |
| 110 | + projectId, |
| 111 | + title: "Dictation project", |
| 112 | + workspaceRoot: "/project", |
| 113 | + defaultModelSelection: null, |
| 114 | + defaultThreadEnvMode: null, |
| 115 | + autoPull: false, |
| 116 | + faviconPath: null, |
| 117 | + projectIcon: null, |
| 118 | + scripts: [], |
| 119 | + createdAt: "2026-10-09T00:00:00Z", |
| 120 | + updatedAt: "2026-10-09T00:00:00Z", |
| 121 | + deletedAt: null, |
| 122 | + }) |
| 123 | + : Option.none(), |
| 124 | + ), |
| 125 | + }), |
| 126 | + Layer.mock(ProjectionStore.ProjectionStoreV2)({ |
| 127 | + getThread: () => Effect.die("History must not be loaded without a thread"), |
| 128 | + getThreadRecords: () => Effect.die("History is disabled"), |
| 129 | + }), |
| 130 | + Path.layer, |
| 131 | + FileSystem.layerNoop({ |
| 132 | + realPath: (file) => |
| 133 | + Effect.succeed(file.endsWith("AGENTS.md") ? "/outside/AGENTS.md" : file), |
| 134 | + stat: () => Effect.succeed({ type: "File", size: 100n } as FileSystem.File.Info), |
| 135 | + readFileString: (file) => |
| 136 | + Effect.sync(() => { |
| 137 | + reads.push(file); |
| 138 | + return "Zustand and Hyprland"; |
| 139 | + }), |
| 140 | + }), |
| 141 | + ), |
| 142 | + ), |
| 143 | + ); |
| 144 | + const run = <A, E>(use: (service: VoiceText.VoiceText["Service"]) => Effect.Effect<A, E>) => |
| 145 | + Effect.flatMap(VoiceText.VoiceText, use).pipe(Effect.provide(layer)); |
| 146 | + return { |
| 147 | + run, |
| 148 | + calls, |
| 149 | + reads, |
| 150 | + setServerModel: (model: ModelSelection) => { |
| 151 | + serverModel = model; |
| 152 | + }, |
| 153 | + setResult: (value: string) => { |
| 154 | + result = value; |
| 155 | + }, |
| 156 | + fail: () => { |
| 157 | + fails = true; |
| 158 | + }, |
| 159 | + }; |
| 160 | +} |
| 161 | + |
| 162 | +describe("dictation text generation", () => { |
| 163 | + it.effect("initializes independently and retains its model when the title model changes", () => |
| 164 | + Effect.gen(function* () { |
| 165 | + const h = harness(); |
| 166 | + const initial = yield* h.run((s) => s.settings); |
| 167 | + h.setServerModel(modelSelection); |
| 168 | + const later = yield* h.run((s) => s.settings); |
| 169 | + expect(later.defaults.modelSelection).toEqual(initial.defaults.modelSelection); |
| 170 | + expect(later.defaults.recentMessageCount).toBe(0); |
| 171 | + }), |
| 172 | + ); |
| 173 | + it.effect("uses the project's edited prompt, context, and independently selected model", () => |
| 174 | + Effect.gen(function* () { |
| 175 | + const h = harness(); |
| 176 | + yield* h.run((s) => |
| 177 | + s.configure({ |
| 178 | + projectId, |
| 179 | + config: { |
| 180 | + ...config, |
| 181 | + cleanupPrompt: "Fix {{text}} using {{project_context}} / {{recent_messages}}", |
| 182 | + }, |
| 183 | + }), |
| 184 | + ); |
| 185 | + expect( |
| 186 | + yield* h.run((s) => |
| 187 | + s.transform({ projectId, operation: "cleanup", text: "react use affect" }), |
| 188 | + ), |
| 189 | + ).toEqual({ text: "Corrected transcription" }); |
| 190 | + expect(h.calls[0]).toMatchObject({ |
| 191 | + cwd: "/project", |
| 192 | + modelSelection, |
| 193 | + prompt: expect.stringContaining("Fix react use affect using React, Zustand, Hyprland / "), |
| 194 | + }); |
| 195 | + }), |
| 196 | + ); |
| 197 | + it.effect("restores environment settings when a project override is removed", () => |
| 198 | + Effect.gen(function* () { |
| 199 | + const h = harness(); |
| 200 | + yield* h.run((s) => s.configure({ config })); |
| 201 | + yield* h.run((s) => s.configure({ projectId, config: { ...config, cleanupMode: "off" } })); |
| 202 | + const disabled = yield* h |
| 203 | + .run((s) => s.transform({ projectId, operation: "cleanup", text: "draft" })) |
| 204 | + .pipe(Effect.flip); |
| 205 | + expect(disabled.reason).toBe("disabled"); |
| 206 | + expect(h.calls).toHaveLength(0); |
| 207 | + yield* h.run((s) => s.configure({ projectId, config: null })); |
| 208 | + yield* h.run((s) => s.transform({ projectId, operation: "cleanup", text: "draft" })); |
| 209 | + expect(h.calls).toHaveLength(1); |
| 210 | + }), |
| 211 | + ); |
| 212 | + it.effect("returns typed errors for empty output and model failure", () => |
| 213 | + Effect.gen(function* () { |
| 214 | + const h = harness(); |
| 215 | + yield* h.run((s) => s.configure({ config })); |
| 216 | + h.setResult(" "); |
| 217 | + expect( |
| 218 | + (yield* h |
| 219 | + .run((s) => s.transform({ projectId, operation: "format", text: "draft" })) |
| 220 | + .pipe(Effect.flip)).reason, |
| 221 | + ).toBe("response"); |
| 222 | + h.fail(); |
| 223 | + expect( |
| 224 | + (yield* h |
| 225 | + .run((s) => s.transform({ projectId, operation: "format", text: "draft" })) |
| 226 | + .pipe(Effect.flip)).reason, |
| 227 | + ).toBe("model"); |
| 228 | + }), |
| 229 | + ); |
| 230 | + it.effect( |
| 231 | + "collects bounded project sources without following a symlink outside the project", |
| 232 | + () => |
| 233 | + Effect.gen(function* () { |
| 234 | + const h = harness(); |
| 235 | + yield* h.run((s) => s.generateContext({ projectId, config })); |
| 236 | + expect(h.reads).toEqual(["/project/README.md", "/project/package.json"]); |
| 237 | + expect(h.calls[0]?.prompt).toContain("Project: Dictation project"); |
| 238 | + expect(h.calls[0]?.prompt).not.toContain("AGENTS.md"); |
| 239 | + expect((yield* h.run((s) => s.settings)).projects[projectId]).toBeUndefined(); |
| 240 | + }), |
| 241 | + ); |
| 242 | +}); |
| 243 | + |
| 244 | +describe("dictation prompt context", () => { |
| 245 | + it("does not expand placeholders injected by source text", () => { |
| 246 | + expect( |
| 247 | + expandVoiceTextTemplateFork("{{text}} / {{project_context}}", { |
| 248 | + text: "literally {{project_context}}", |
| 249 | + project_context: "React", |
| 250 | + }), |
| 251 | + ).toBe("literally {{project_context}} / React"); |
| 252 | + }); |
| 253 | + it("takes the most recent user and assistant contributions and excludes working traces", () => { |
| 254 | + const messages = [ |
| 255 | + { role: "user", text: "old request" }, |
| 256 | + { role: "assistant", text: "old answer" }, |
| 257 | + { role: "user", text: "fix useEffect" }, |
| 258 | + { role: "assistant", text: "check dependencies" }, |
| 259 | + { role: "assistant", text: "also check cleanup" }, |
| 260 | + { role: "reasoning", text: "private" }, |
| 261 | + { role: "assistant", text: "unfinished", streaming: true }, |
| 262 | + ]; |
| 263 | + expect(voiceRecentMessagesFork(messages, 0)).toBe(""); |
| 264 | + expect(voiceRecentMessagesFork(messages, 2)).toBe( |
| 265 | + "USER:\nfix useEffect\n\nASSISTANT:\nalso check cleanup", |
| 266 | + ); |
| 267 | + }); |
| 268 | + it("keeps role labels and complete boundaries within the history budget", () => { |
| 269 | + const history = voiceRecentMessagesFork( |
| 270 | + [ |
| 271 | + { role: "user", text: "a".repeat(4_000) }, |
| 272 | + { role: "assistant", text: "b".repeat(4_000) }, |
| 273 | + { role: "user", text: "c".repeat(4_000) }, |
| 274 | + { role: "assistant", text: "d".repeat(4_000) }, |
| 275 | + ], |
| 276 | + 4, |
| 277 | + ); |
| 278 | + expect(history).toHaveLength(12_000); |
| 279 | + expect(history).toMatch(/^ASSISTANT:\nb+/); |
| 280 | + expect(history.split("\n\n").map((entry) => entry.split("\n")[0])).toEqual([ |
| 281 | + "ASSISTANT:", |
| 282 | + "USER:", |
| 283 | + "ASSISTANT:", |
| 284 | + ]); |
| 285 | + expect(history).toContain(`USER:\n${"c".repeat(4_000)}`); |
| 286 | + expect(history.endsWith(`ASSISTANT:\n${"d".repeat(4_000)}`)).toBe(true); |
| 287 | + }); |
| 288 | + it.effect("shares the provider runner while enforcing the text result schema", () => |
| 289 | + Effect.gen(function* () { |
| 290 | + const runner: TextGenerationOperations.Runner = (request) => |
| 291 | + TextGenerationOperations.decodeJsonReply(request, "test", '{"text":"Use useEffect"}'); |
| 292 | + const service = withVoiceTextGenerationFork( |
| 293 | + TextGenerationOperations.fromRunner("test", runner), |
| 294 | + runner, |
| 295 | + ); |
| 296 | + expect( |
| 297 | + yield* service.generateTextFork!({ cwd: "/project", prompt: "Fix", modelSelection }), |
| 298 | + ).toEqual({ text: "Use useEffect" }); |
| 299 | + }), |
| 300 | + ); |
| 301 | +}); |
0 commit comments