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