prompt.editor.test.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { describe, expect, test } from "bun:test"
  2. import { realignEditorPromptParts, resolveEditorSlashValue } from "@/cli/cmd/run/prompt.editor"
  3. import type { RunPromptPart } from "@/cli/cmd/run/types"
  4. describe("run prompt editor helpers", () => {
  5. test("strips the local /editor command from the initial editor text", () => {
  6. expect(resolveEditorSlashValue("/editor")).toBe("")
  7. expect(resolveEditorSlashValue("/editor draft message")).toBe("draft message")
  8. expect(resolveEditorSlashValue("/editor first line\nsecond line")).toBe("first line\nsecond line")
  9. })
  10. test("realigns file and agent parts after external editing", () => {
  11. const filePart = {
  12. type: "file",
  13. mime: "text/plain",
  14. filename: "src/app.ts",
  15. url: "file:///src/app.ts",
  16. source: {
  17. type: "file",
  18. path: "src/app.ts",
  19. text: {
  20. start: 0,
  21. end: 11,
  22. value: "@src/app.ts",
  23. },
  24. },
  25. } satisfies RunPromptPart
  26. const agentPart = {
  27. type: "agent",
  28. name: "helper",
  29. source: {
  30. start: 12,
  31. end: 19,
  32. value: "@helper",
  33. },
  34. } satisfies RunPromptPart
  35. const parts = [filePart, agentPart]
  36. expect(realignEditorPromptParts("Please check @helper before @src/app.ts", parts)).toEqual([
  37. {
  38. ...filePart,
  39. source: {
  40. ...filePart.source,
  41. text: {
  42. ...filePart.source.text,
  43. start: 28,
  44. end: 39,
  45. value: "@src/app.ts",
  46. },
  47. },
  48. },
  49. {
  50. ...agentPart,
  51. source: {
  52. start: 13,
  53. end: 20,
  54. value: "@helper",
  55. },
  56. },
  57. ])
  58. })
  59. test("drops parts whose virtual text was deleted", () => {
  60. const filePart = {
  61. type: "file",
  62. mime: "text/plain",
  63. filename: "src/app.ts",
  64. url: "file:///src/app.ts",
  65. source: {
  66. type: "file",
  67. path: "src/app.ts",
  68. text: {
  69. start: 0,
  70. end: 11,
  71. value: "@src/app.ts",
  72. },
  73. },
  74. } satisfies RunPromptPart
  75. const agentPart = {
  76. type: "agent",
  77. name: "helper",
  78. source: {
  79. start: 12,
  80. end: 19,
  81. value: "@helper",
  82. },
  83. } satisfies RunPromptPart
  84. const parts = [filePart, agentPart]
  85. expect(realignEditorPromptParts("Only @helper remains", parts)).toEqual([
  86. {
  87. ...agentPart,
  88. source: {
  89. start: 5,
  90. end: 12,
  91. value: "@helper",
  92. },
  93. },
  94. ])
  95. })
  96. })