layout-store.spec.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // @vitest-environment jsdom
  2. /**
  3. * createLayoutStore unit account: init shape, the action write set (clamp
  4. * inside actions), and the absence of browser persistence. Uses the
  5. * test-sanctioned path: factory self-call + .create() gives the
  6. * real engine instance (same create path as production).
  7. */
  8. import { beforeEach, describe, expect, it } from 'vitest'
  9. import { createLayoutStore } from '@deepseek-ai/dsh-client-ui-layout/src/client/stores.ts'
  10. import {
  11. DETAILS_DEFAULT, DETAILS_MAX, DETAILS_MIN,
  12. SIDEBAR_DEFAULT, SIDEBAR_MAX, SIDEBAR_MIN,
  13. } from '@deepseek-ai/dsh-client-ui-layout/src/client/columns.ts'
  14. const PERSIST_KEY = 'dsh.layout.panels'
  15. beforeEach(() => { localStorage.clear() })
  16. describe('createLayoutStore', () => {
  17. it('initializes the sidebar at its default width and details closed', () => {
  18. const { store } = createLayoutStore().create()
  19. expect(store.getSnapshot()).toEqual({ sidebar: SIDEBAR_DEFAULT, details: 0 })
  20. })
  21. it('each create() is an independent instance (factory is not a singleton)', () => {
  22. const a = createLayoutStore().create()
  23. const b = createLayoutStore().create()
  24. a.actions.setSidebar(400)
  25. expect(b.store.getSnapshot().sidebar).toBe(SIDEBAR_DEFAULT)
  26. })
  27. it('setSidebar/setDetails clamp into the contract ranges', () => {
  28. const { store, actions } = createLayoutStore().create()
  29. actions.setSidebar(1)
  30. expect(store.getSnapshot().sidebar).toBe(SIDEBAR_MIN)
  31. actions.setSidebar(9999)
  32. expect(store.getSnapshot().sidebar).toBe(SIDEBAR_MAX)
  33. actions.setDetails(1)
  34. expect(store.getSnapshot().details).toBe(DETAILS_MIN)
  35. actions.setDetails(9999)
  36. expect(store.getSnapshot().details).toBe(DETAILS_MAX)
  37. })
  38. it('toggleSidebar flips closed <-> contract default (drag width forgotten)', () => {
  39. const { store, actions } = createLayoutStore().create()
  40. actions.setSidebar(400)
  41. actions.toggleSidebar()
  42. expect(store.getSnapshot().sidebar).toBe(0)
  43. actions.toggleSidebar()
  44. expect(store.getSnapshot().sidebar).toBe(SIDEBAR_DEFAULT)
  45. })
  46. it('openDetails uses the contract default, preserves an open width, and closeDetails zeroes', () => {
  47. const { store, actions } = createLayoutStore().create()
  48. actions.openDetails()
  49. expect(store.getSnapshot().details).toBe(DETAILS_DEFAULT)
  50. actions.setDetails(500)
  51. actions.openDetails()
  52. expect(store.getSnapshot().details).toBe(500)
  53. actions.closeDetails()
  54. expect(store.getSnapshot().details).toBe(0)
  55. })
  56. it('does not persist panel geometry', () => {
  57. const first = createLayoutStore().create()
  58. first.actions.setSidebar(400)
  59. first.actions.openDetails()
  60. first.actions.setDetails(500)
  61. expect(localStorage.getItem(PERSIST_KEY)).toBeNull()
  62. const second = createLayoutStore().create()
  63. expect(second.store.getSnapshot()).toEqual({
  64. sidebar: SIDEBAR_DEFAULT,
  65. details: 0,
  66. })
  67. })
  68. })