prompt.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import path from "path"
  2. import { describe, expect, test } from "bun:test"
  3. import { Session } from "../../src/session"
  4. import { SessionPrompt } from "../../src/session/prompt"
  5. import { MessageV2 } from "../../src/session/message-v2"
  6. import { Instance } from "../../src/project/instance"
  7. import { Log } from "../../src/util/log"
  8. import { tmpdir } from "../fixture/fixture"
  9. Log.init({ print: false })
  10. describe("SessionPrompt ordering", () => {
  11. test("keeps @file order with read output parts", async () => {
  12. await using tmp = await tmpdir({
  13. git: true,
  14. init: async (dir) => {
  15. await Bun.write(path.join(dir, "a.txt"), "28\n")
  16. await Bun.write(path.join(dir, "b.txt"), "42\n")
  17. },
  18. })
  19. await Instance.provide({
  20. directory: tmp.path,
  21. fn: async () => {
  22. const session = await Session.create({})
  23. const template = "What numbers are written in files @a.txt and @b.txt ?"
  24. const parts = await SessionPrompt.resolvePromptParts(template)
  25. const fileParts = parts.filter((part) => part.type === "file")
  26. expect(fileParts.map((part) => part.filename)).toStrictEqual(["a.txt", "b.txt"])
  27. const message = await SessionPrompt.prompt({
  28. sessionID: session.id,
  29. parts,
  30. noReply: true,
  31. })
  32. const stored = await MessageV2.get({ sessionID: session.id, messageID: message.info.id })
  33. const items = stored.parts
  34. const aPath = path.join(tmp.path, "a.txt")
  35. const bPath = path.join(tmp.path, "b.txt")
  36. const sequence = items.flatMap((part) => {
  37. if (part.type === "text") {
  38. if (part.text.includes(aPath)) return ["input:a"]
  39. if (part.text.includes(bPath)) return ["input:b"]
  40. if (part.text.includes("00001| 28")) return ["output:a"]
  41. if (part.text.includes("00001| 42")) return ["output:b"]
  42. return []
  43. }
  44. if (part.type === "file") {
  45. if (part.filename === "a.txt") return ["file:a"]
  46. if (part.filename === "b.txt") return ["file:b"]
  47. }
  48. return []
  49. })
  50. expect(sequence).toStrictEqual(["input:a", "output:a", "file:a", "input:b", "output:b", "file:b"])
  51. await Session.remove(session.id)
  52. },
  53. })
  54. })
  55. })