prompt.shared.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { describe, expect, test } from "bun:test"
  2. import {
  3. createPromptHistory,
  4. isExitCommand,
  5. isNewCommand,
  6. movePromptHistory,
  7. pushPromptHistory,
  8. } from "../../src/mini/prompt.shared"
  9. import type { RunPrompt } from "../../src/mini/types"
  10. function prompt(text: string, parts: RunPrompt["parts"] = []): RunPrompt {
  11. return { text, parts }
  12. }
  13. describe("run prompt shared", () => {
  14. test("filters blank prompts and dedupes consecutive history", () => {
  15. const out = createPromptHistory([prompt(" "), prompt("one"), prompt("one"), prompt("two"), prompt("one")])
  16. expect(out.items.map((item) => item.text)).toEqual(["one", "two", "one"])
  17. expect(out.index).toBeNull()
  18. expect(out.draft).toBe("")
  19. })
  20. test("push ignores blanks and dedupes only the latest item", () => {
  21. const base = createPromptHistory([prompt("one")])
  22. expect(pushPromptHistory(base, prompt(" ")).items.map((item) => item.text)).toEqual(["one"])
  23. expect(pushPromptHistory(base, prompt("one")).items.map((item) => item.text)).toEqual(["one"])
  24. expect(pushPromptHistory(base, prompt("two")).items.map((item) => item.text)).toEqual(["one", "two"])
  25. })
  26. test("moves through history only at input boundaries and restores draft", () => {
  27. const base = createPromptHistory([prompt("one"), prompt("two")])
  28. expect(movePromptHistory(base, -1, "draft", 1)).toEqual({
  29. state: base,
  30. apply: false,
  31. })
  32. const up = movePromptHistory(base, -1, "draft", 0)
  33. expect(up.apply).toBe(true)
  34. expect(up.text).toBe("two")
  35. expect(up.cursor).toBe(0)
  36. expect(up.state.index).toBe(1)
  37. expect(up.state.draft).toBe("draft")
  38. const older = movePromptHistory(up.state, -1, "two", 0)
  39. expect(older.apply).toBe(true)
  40. expect(older.text).toBe("one")
  41. expect(older.cursor).toBe(0)
  42. expect(older.state.index).toBe(0)
  43. const newer = movePromptHistory(older.state, 1, "one", 3)
  44. expect(newer.apply).toBe(true)
  45. expect(newer.text).toBe("two")
  46. expect(newer.cursor).toBe(3)
  47. expect(newer.state.index).toBe(1)
  48. const draft = movePromptHistory(newer.state, 1, "two", 3)
  49. expect(draft.apply).toBe(true)
  50. expect(draft.text).toBe("draft")
  51. expect(draft.cursor).toBe(5)
  52. expect(draft.state.index).toBeNull()
  53. })
  54. test("uses display-width cursors for history restoration", () => {
  55. const base = createPromptHistory([prompt("one"), prompt("中文")])
  56. const latest = movePromptHistory(base, -1, "草稿", 0)
  57. expect(latest.apply).toBe(true)
  58. expect(latest.text).toBe("中文")
  59. expect(latest.cursor).toBe(0)
  60. const older = movePromptHistory(latest.state, -1, "中文", 0)
  61. expect(older.apply).toBe(true)
  62. expect(older.text).toBe("one")
  63. expect(older.cursor).toBe(0)
  64. const newer = movePromptHistory(older.state, 1, "one", Bun.stringWidth("one"))
  65. expect(newer.apply).toBe(true)
  66. expect(newer.text).toBe("中文")
  67. expect(newer.cursor).toBe(Bun.stringWidth("中文"))
  68. const draft = movePromptHistory(newer.state, 1, "中文", Bun.stringWidth("中文"))
  69. expect(draft.apply).toBe(true)
  70. expect(draft.text).toBe("草稿")
  71. expect(draft.cursor).toBe(Bun.stringWidth("草稿"))
  72. })
  73. test("recognizes exit commands", () => {
  74. expect(isExitCommand("/exit")).toBe(true)
  75. expect(isExitCommand(" /Quit ")).toBe(true)
  76. expect(isExitCommand("/quit now")).toBe(false)
  77. })
  78. test("recognizes the new-session command", () => {
  79. expect(isNewCommand("/new")).toBe(true)
  80. expect(isNewCommand(" /NEW ")).toBe(true)
  81. expect(isNewCommand("/new now")).toBe(false)
  82. })
  83. })