chat-store.spec.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // @vitest-environment jsdom
  2. /** Chat-store actions, scoped persistence, and instance isolation. */
  3. import { beforeEach, describe, expect, it } from 'vitest'
  4. import { createChatStore } from '../src/client/stores.ts'
  5. const KEY = 'dsh.conversation.chat'
  6. beforeEach(() => {
  7. localStorage.clear()
  8. })
  9. describe('createChatStore', () => {
  10. it('init shape: empty selection/draft/view', () => {
  11. const store = createChatStore().create()
  12. expect(store.store.getSnapshot()).toEqual({ selection: null, draft: '', view: null, inspect: null })
  13. })
  14. it('actions cover the declared write set', () => {
  15. const store = createChatStore().create()
  16. store.actions.select({ turnSeq: 3, callId: 'c1', toolName: 'bash' })
  17. expect(store.store.getSnapshot().selection).toEqual({ turnSeq: 3, callId: 'c1', toolName: 'bash' })
  18. store.actions.select(null)
  19. expect(store.store.getSnapshot().selection).toBeNull()
  20. store.actions.setDraft('hello')
  21. expect(store.store.getSnapshot().draft).toBe('hello')
  22. store.actions.clearDraft()
  23. expect(store.store.getSnapshot().draft).toBe('')
  24. store.actions.setView('chat')
  25. expect(store.store.getSnapshot().view).toBe('chat')
  26. store.actions.setInspect({ callId: 'c1' })
  27. expect(store.store.getSnapshot().inspect).toEqual({ callId: 'c1' })
  28. store.actions.setInspect(null)
  29. expect(store.store.getSnapshot().inspect).toBeNull()
  30. })
  31. it('restoreDraft only fills an empty draft (optimistic-send rollback contract)', () => {
  32. const store = createChatStore().create()
  33. // Rollback path: draft was cleared by send, nothing typed since.
  34. store.actions.restoreDraft('failed text')
  35. expect(store.store.getSnapshot().draft).toBe('failed text')
  36. // The user typed something new before the failure landed: keep theirs.
  37. store.actions.setDraft('newer input')
  38. store.actions.restoreDraft('stale text')
  39. expect(store.store.getSnapshot().draft).toBe('newer input')
  40. })
  41. it('persists per scope key and rehydrates a fresh instance', () => {
  42. const handle = createChatStore()
  43. const s1 = handle.create('sess-1')
  44. s1.actions.setDraft('draft for one')
  45. s1.actions.select({ turnSeq: 1 })
  46. // Scope-suffixed key: each session persists separately.
  47. expect(localStorage.getItem(`${KEY}.sess-1`)).not.toBeNull()
  48. expect(localStorage.getItem(`${KEY}.sess-2`)).toBeNull()
  49. // A rebuilt instance under the same scope key rehydrates the state.
  50. const again = createChatStore().create('sess-1')
  51. expect(again.store.getSnapshot().draft).toBe('draft for one')
  52. expect(again.store.getSnapshot().selection).toEqual({ turnSeq: 1 })
  53. // A sibling scope starts clean.
  54. const other = createChatStore().create('sess-2')
  55. expect(other.store.getSnapshot().draft).toBe('')
  56. })
  57. it('clearPersisted removes the scope entry (session-death cleanup hook)', () => {
  58. const store = createChatStore().create('sess-9')
  59. store.actions.setDraft('doomed')
  60. expect(localStorage.getItem(`${KEY}.sess-9`)).not.toBeNull()
  61. store.clearPersisted()
  62. expect(localStorage.getItem(`${KEY}.sess-9`)).toBeNull()
  63. })
  64. it('every create() is an independent instance; the factory holds no singleton', () => {
  65. const handle = createChatStore()
  66. const a = handle.create()
  67. const b = handle.create()
  68. a.actions.setDraft('only in a')
  69. expect(b.store.getSnapshot().draft).toBe('')
  70. // Two factory calls likewise share no LIVE state (identity is per handle
  71. // VALUE, not per module — the sharing contract lives in the framework's
  72. // handle x scope-key resolution, not in module state). Persistence is the
  73. // one sanctioned cross-instance channel: clear it so this assertion sees
  74. // memory identity, not rehydration (covered by the persist case above).
  75. localStorage.clear()
  76. const c = createChatStore().create()
  77. expect(c.store.getSnapshot().draft).toBe('')
  78. })
  79. })