prompt-submission-state.test.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { describe, expect, test } from "bun:test"
  2. import { createPromptState } from "@/context/prompt"
  3. import { createPromptSubmissionState } from "@/components/prompt-input/submission-state"
  4. describe("prompt submission state", () => {
  5. test("keeps failed submission restoration with the prompt where it started", () => {
  6. const target = createPromptState()
  7. const submission = createPromptSubmissionState({
  8. target,
  9. prompt: [{ type: "text", content: "prompt-A", start: 0, end: 8 }],
  10. context: [{ key: "file:src/index.ts:undefined:undefined", type: "file", path: "src/index.ts" }],
  11. })
  12. expect(submission.restore()).toEqual({
  13. target,
  14. prompt: [{ type: "text", content: "prompt-A", start: 0, end: 8 }],
  15. context: [{ key: "file:src/index.ts:undefined:undefined", type: "file", path: "src/index.ts" }],
  16. })
  17. })
  18. test("moves first-submit restoration and context to the promoted session", () => {
  19. const draft = createPromptState()
  20. const session = createPromptState()
  21. const submission = createPromptSubmissionState({
  22. target: draft,
  23. prompt: [{ type: "text", content: "first prompt", start: 0, end: 12 }],
  24. context: [{ key: "file:src/index.ts:undefined:undefined", type: "file", path: "src/index.ts" }],
  25. })
  26. submission.retarget(session)
  27. expect(submission.restore()).toEqual({
  28. target: session,
  29. prompt: [{ type: "text", content: "first prompt", start: 0, end: 12 }],
  30. context: [{ key: "file:src/index.ts:undefined:undefined", type: "file", path: "src/index.ts" }],
  31. })
  32. expect(session.context.items()).toHaveLength(1)
  33. expect(session.context.items()[0]).toMatchObject({ type: "file", path: "src/index.ts" })
  34. })
  35. test("clears the original first-submit prompt after retargeting", () => {
  36. const workspace = createPromptState()
  37. const session = createPromptState()
  38. workspace.set([{ type: "text", content: "first prompt", start: 0, end: 12 }])
  39. const submission = createPromptSubmissionState({
  40. target: workspace,
  41. prompt: workspace.current(),
  42. context: [],
  43. })
  44. submission.retarget(session)
  45. submission.clear()
  46. expect(workspace.current()[0]).toMatchObject({ type: "text", content: "" })
  47. expect(session.current()[0]).toMatchObject({ type: "text", content: "" })
  48. })
  49. test("does not restore over a prompt edited after submission", () => {
  50. const target = createPromptState()
  51. target.set([{ type: "text", content: "submitted", start: 0, end: 9 }])
  52. const submission = createPromptSubmissionState({
  53. target,
  54. prompt: target.current(),
  55. context: [],
  56. })
  57. submission.clear()
  58. target.set([{ type: "text", content: "new draft", start: 0, end: 9 }])
  59. expect(submission.restore()).toBeUndefined()
  60. expect(target.current()[0]).toMatchObject({ type: "text", content: "new draft" })
  61. })
  62. })