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