editor.test.ts 1002 B

1234567891011121314151617181920212223242526272829303132
  1. import { afterEach, expect, test } from "bun:test"
  2. import { normalizePromptContent, openEditor } from "../src/editor"
  3. const editor = process.env.EDITOR
  4. const visual = process.env.VISUAL
  5. afterEach(() => {
  6. process.env.EDITOR = editor
  7. process.env.VISUAL = visual
  8. })
  9. test("rejects when the external editor cannot start", async () => {
  10. delete process.env.VISUAL
  11. process.env.EDITOR = "opencode-editor-that-does-not-exist"
  12. const renderer = {
  13. suspend() {},
  14. resume() {},
  15. requestRender() {},
  16. currentRenderBuffer: { clear() {} },
  17. }
  18. await expect(openEditor({ value: "original", renderer: renderer as never })).rejects.toThrow()
  19. })
  20. test("normalizes a single trailing editor newline for one-line prompts", () => {
  21. expect(normalizePromptContent("hello\n")).toBe("hello")
  22. expect(normalizePromptContent("hello\r\n")).toBe("hello")
  23. })
  24. test("preserves multiline prompts that end with a newline", () => {
  25. expect(normalizePromptContent("hello\nworld\n")).toBe("hello\nworld\n")
  26. })