draft-stash.test.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { describe, expect, test } from "bun:test"
  2. import { saveDraft, takeDraft } from "../../src/component/prompt/draft-stash"
  3. import { emptyPrompt } from "../../src/prompt/history"
  4. // The Prompt component stashes an unsent draft in onCleanup and takes it back
  5. // in onMount across route remounts, keyed by sessionID or undefined for home.
  6. function draft(text: string, cursor = text.length) {
  7. return { prompt: { ...emptyPrompt(), text }, cursor }
  8. }
  9. describe("prompt draft stash", () => {
  10. test("tab-keyed drafts stay on the tab they were written in", () => {
  11. const two = draft("notes for session two")
  12. saveDraft("ses_two", two)
  13. // Switching to another tab or home finds nothing.
  14. expect(takeDraft("ses_one")).toBeUndefined()
  15. expect(takeDraft("home")).toBeUndefined()
  16. // Returning to the original tab restores exactly its draft, once.
  17. expect(takeDraft("ses_two")).toBe(two)
  18. expect(takeDraft("ses_two")).toBeUndefined()
  19. })
  20. test("each tab keeps its own draft, including home", () => {
  21. const one = draft("DRAFT-ONE")
  22. const home = draft("draft on home")
  23. saveDraft("ses_one", one)
  24. saveDraft(undefined, home)
  25. expect(takeDraft(undefined)).toBe(home)
  26. expect(takeDraft("ses_one")).toBe(one)
  27. })
  28. test("a newer draft for the same slot replaces the older one", () => {
  29. saveDraft("ses_a", draft("first"))
  30. const second = draft("second")
  31. saveDraft("ses_a", second)
  32. expect(takeDraft("ses_a")).toBe(second)
  33. })
  34. })