prompt-special-chars.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import path from "path"
  2. import { describe, expect, test } from "bun:test"
  3. import { fileURLToPath } from "url"
  4. import { Instance } from "../../src/project/instance"
  5. import { Log } from "../../src/util/log"
  6. import { Session } from "../../src/session"
  7. import { SessionPrompt } from "../../src/session/prompt"
  8. import { MessageV2 } from "../../src/session/message-v2"
  9. import { tmpdir } from "../fixture/fixture"
  10. Log.init({ print: false })
  11. describe("session.prompt special characters", () => {
  12. test("handles filenames with # character", async () => {
  13. await using tmp = await tmpdir({
  14. git: true,
  15. init: async (dir) => {
  16. await Bun.write(path.join(dir, "file#name.txt"), "special content\n")
  17. },
  18. })
  19. await Instance.provide({
  20. directory: tmp.path,
  21. fn: async () => {
  22. const session = await Session.create({})
  23. const template = "Read @file#name.txt"
  24. const parts = await SessionPrompt.resolvePromptParts(template)
  25. const fileParts = parts.filter((part) => part.type === "file")
  26. expect(fileParts.length).toBe(1)
  27. expect(fileParts[0].filename).toBe("file#name.txt")
  28. // Verify the URL is properly encoded (# should be %23)
  29. expect(fileParts[0].url).toContain("%23")
  30. // Verify the URL can be correctly converted back to a file path
  31. const decodedPath = fileURLToPath(fileParts[0].url)
  32. expect(decodedPath).toBe(path.join(tmp.path, "file#name.txt"))
  33. const message = await SessionPrompt.prompt({
  34. sessionID: session.id,
  35. parts,
  36. noReply: true,
  37. })
  38. const stored = await MessageV2.get({ sessionID: session.id, messageID: message.info.id })
  39. // Verify the file content was read correctly
  40. const textParts = stored.parts.filter((part) => part.type === "text")
  41. const hasContent = textParts.some((part) => part.text.includes("special content"))
  42. expect(hasContent).toBe(true)
  43. await Session.remove(session.id)
  44. },
  45. })
  46. })
  47. })