codeCommentContext.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildPromptWithCodeComments,
  4. parsePromptCodeComments,
  5. type CodeCommentComposerAttachment,
  6. } from "@/lib/codeCommentContext.js";
  7. describe("buildPromptWithCodeComments", () => {
  8. const baseComment: CodeCommentComposerAttachment = {
  9. id: "comment-1",
  10. workspacePath: "/workspace",
  11. sourcePath: "/workspace/src/app.ts",
  12. sourceTitle: "app.ts",
  13. startLine: 17,
  14. endLine: 17,
  15. selectedText: "const value = 1;",
  16. comment: "Rename this value.",
  17. contextLabel: "Code context",
  18. commentLabel: "Comment",
  19. };
  20. it("keeps the typed prompt and appends code comment markdown at send time", () => {
  21. const prompt = buildPromptWithCodeComments("Please fix this", [baseComment]);
  22. expect(prompt).toContain("Please fix this");
  23. expect(prompt).toContain("# Code comments:");
  24. expect(prompt).toContain("File: /workspace/src/app.ts");
  25. expect(prompt).toContain("Lines: 17");
  26. expect(prompt).toContain("const value = 1;");
  27. expect(prompt).toContain("Comment:\nRename this value.");
  28. });
  29. it("supports multiple comments without requiring visible composer text", () => {
  30. const prompt = buildPromptWithCodeComments("", [
  31. baseComment,
  32. {
  33. ...baseComment,
  34. id: "comment-2",
  35. startLine: 20,
  36. endLine: 22,
  37. selectedText: "run();",
  38. comment: "Check the range.",
  39. },
  40. ]);
  41. expect(prompt).toContain("## Comment 1");
  42. expect(prompt).toContain("Lines: 17");
  43. expect(prompt).toContain("## Comment 2");
  44. expect(prompt).toContain("Lines: 20-22");
  45. expect(prompt).toContain("Check the range.");
  46. });
  47. it("parses code comment blocks back into visible content and attachments", () => {
  48. const prompt = buildPromptWithCodeComments("Please fix this", [
  49. baseComment,
  50. {
  51. ...baseComment,
  52. id: "comment-2",
  53. startLine: 20,
  54. endLine: 22,
  55. selectedText: "run();",
  56. comment: "Check the range.",
  57. },
  58. ]);
  59. const parsed = parsePromptCodeComments(prompt, {
  60. workspacePath: "/workspace",
  61. });
  62. expect(parsed.visibleContent).toBe("Please fix this");
  63. expect(parsed.codeCommentAttachments).toHaveLength(2);
  64. expect(parsed.codeCommentAttachments[0]).toMatchObject({
  65. workspacePath: "/workspace",
  66. sourcePath: "/workspace/src/app.ts",
  67. sourceTitle: "app.ts",
  68. startLine: 17,
  69. endLine: 17,
  70. selectedText: "const value = 1;",
  71. comment: "Rename this value.",
  72. });
  73. expect(parsed.codeCommentAttachments[1]).toMatchObject({
  74. startLine: 20,
  75. endLine: 22,
  76. selectedText: "run();",
  77. comment: "Check the range.",
  78. });
  79. });
  80. it("allows adding selected code to chat without writing a comment", () => {
  81. const prompt = buildPromptWithCodeComments("Please inspect this", [
  82. {
  83. ...baseComment,
  84. comment: "",
  85. },
  86. ]);
  87. expect(prompt).toContain("# Code comments:");
  88. expect(prompt).toContain("Selected text:");
  89. expect(prompt).toContain("Comment:");
  90. const parsed = parsePromptCodeComments(prompt, {
  91. workspacePath: "/workspace",
  92. });
  93. expect(parsed.visibleContent).toBe("Please inspect this");
  94. expect(parsed.codeCommentAttachments).toHaveLength(1);
  95. expect(parsed.codeCommentAttachments[0]).toMatchObject({
  96. sourcePath: "/workspace/src/app.ts",
  97. selectedText: "const value = 1;",
  98. comment: "",
  99. });
  100. });
  101. });