prompt-submission-state.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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("does not restore over a prompt edited after submission", () => {
  36. const target = createPromptState()
  37. target.set([{ type: "text", content: "submitted", start: 0, end: 9 }])
  38. const submission = createPromptSubmissionState({
  39. target,
  40. prompt: target.current(),
  41. context: [],
  42. })
  43. submission.clear()
  44. target.set([{ type: "text", content: "new draft", start: 0, end: 9 }])
  45. expect(submission.restore()).toBeUndefined()
  46. expect(target.current()[0]).toMatchObject({ type: "text", content: "new draft" })
  47. })
  48. })