input-transform-streaming-example.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { describe, expect, it, vi } from "vitest";
  2. import inputTransformStreaming from "../examples/extensions/input-transform-streaming.ts";
  3. import type {
  4. ExecResult,
  5. ExtensionAPI,
  6. ExtensionContext,
  7. InputEvent,
  8. InputEventResult,
  9. } from "../src/core/extensions/index.ts";
  10. type InputHandler = (event: InputEvent, ctx: ExtensionContext) => Promise<InputEventResult | undefined>;
  11. function setup(execResult: ExecResult) {
  12. let handler: InputHandler | undefined;
  13. const exec = vi.fn<ExtensionAPI["exec"]>().mockResolvedValue(execResult);
  14. const api = {
  15. on: (event: string, h: InputHandler) => {
  16. if (event === "input") handler = h;
  17. },
  18. exec,
  19. } as unknown as ExtensionAPI;
  20. inputTransformStreaming(api);
  21. const ctx = {} as ExtensionContext;
  22. function emit(text: string, streamingBehavior?: "steer" | "followUp") {
  23. return handler!({ type: "input", text, source: "interactive", streamingBehavior }, ctx);
  24. }
  25. return { emit, exec };
  26. }
  27. describe("input-transform-streaming example", () => {
  28. const diffOutput = " src/index.ts | 5 ++---\n 1 file changed, 2 insertions(+), 3 deletions(-)";
  29. const gitSuccess: ExecResult = { stdout: diffOutput, stderr: "", code: 0, killed: false };
  30. const gitEmpty: ExecResult = { stdout: "", stderr: "", code: 0, killed: false };
  31. const gitFail: ExecResult = { stdout: "", stderr: "not a git repo", code: 128, killed: false };
  32. it("skips exec during steering", async () => {
  33. const { emit, exec } = setup(gitSuccess);
  34. const result = await emit("what changes did I make?", "steer");
  35. expect(result).toEqual({ action: "continue" });
  36. expect(exec).not.toHaveBeenCalled();
  37. });
  38. it("transforms when idle and text matches trigger", async () => {
  39. const { emit, exec } = setup(gitSuccess);
  40. const result = await emit("review my changes");
  41. expect(exec).toHaveBeenCalledWith("git", ["diff", "--stat"]);
  42. expect(result).toMatchObject({ action: "transform" });
  43. const text = (result as { text: string }).text;
  44. expect(text).toContain("review my changes");
  45. expect(text).toContain("src/index.ts");
  46. });
  47. it("transforms when queued as follow-up", async () => {
  48. const { emit, exec } = setup(gitSuccess);
  49. const result = await emit("show me the diff", "followUp");
  50. expect(exec).toHaveBeenCalled();
  51. expect(result).toMatchObject({ action: "transform" });
  52. });
  53. it("continues when text does not match trigger", async () => {
  54. const { emit, exec } = setup(gitSuccess);
  55. const result = await emit("explain this function");
  56. expect(result).toEqual({ action: "continue" });
  57. expect(exec).not.toHaveBeenCalled();
  58. });
  59. it("continues when git diff is empty", async () => {
  60. const { emit } = setup(gitEmpty);
  61. const result = await emit("any changes?");
  62. expect(result).toEqual({ action: "continue" });
  63. });
  64. it("continues when git fails", async () => {
  65. const { emit } = setup(gitFail);
  66. const result = await emit("show modified files");
  67. expect(result).toEqual({ action: "continue" });
  68. });
  69. });