opencode/packages/tui/test/util/revert-prompt.test.ts
2026-07-05 23:38:43 -05:00

65 lines
1.8 KiB
TypeScript

import { describe, expect, test } from "bun:test"
import type { SessionMessageUser } from "@opencode-ai/sdk/v2"
import { revertedPrompt } from "../../src/util/revert-prompt"
const message: SessionMessageUser = {
id: "message-1",
type: "user",
text: "Fix the tests",
time: { created: 1 },
files: [
{
uri: "file:///repo/test.ts",
mime: "text/typescript",
name: "test.ts",
source: { start: 0, end: 8, text: "@test.ts" },
},
],
agents: [{ name: "review", source: { start: 9, end: 16, text: "@review" } }],
}
describe("reverted prompt", () => {
test("restores the reverted user message into an empty prompt", () => {
expect(revertedPrompt({ input: "", parts: [] }, message)).toEqual({
input: "Fix the tests",
parts: [
{
type: "file",
mime: "text/typescript",
filename: "test.ts",
url: "file:///repo/test.ts",
source: {
type: "file",
path: "test.ts",
text: { start: 0, end: 8, value: "@test.ts" },
},
},
{
type: "agent",
name: "review",
source: { start: 9, end: 16, value: "@review" },
},
],
})
})
test.each(["/", "/u", "/un", "/und", "/undo", " /undo "])("replaces the undo autocomplete query %p", (input) => {
expect(revertedPrompt({ input, parts: [] }, message)?.input).toBe("Fix the tests")
})
test("preserves an existing text draft", () => {
expect(revertedPrompt({ input: "Keep this", parts: [] }, message)).toBeUndefined()
})
test("preserves an existing attachment draft", () => {
expect(
revertedPrompt(
{
input: "",
parts: [{ type: "agent", name: "build" }],
},
message,
),
).toBeUndefined()
})
})