settings-store.client.spec.ts 1.2 KB

123456789101112131415161718192021222324252627282930
  1. /** Language row store: snapshot-mirror action and the revision guard. */
  2. import { describe, expect, it } from 'vitest'
  3. import { createLanguageRowStore } from '../src/client/settings-store.ts'
  4. const OPTIONS = [{ id: 'zh', label: '中文' }, { id: 'en', label: 'English' }]
  5. describe('createLanguageRowStore', () => {
  6. it('init shape: empty mirror with revision at -1', () => {
  7. const store = createLanguageRowStore().create()
  8. expect(store.getSnapshot()).toEqual({ active: '', options: [], revision: -1 })
  9. })
  10. it('sync mirrors the snapshot and advances the revision', () => {
  11. const store = createLanguageRowStore().create()
  12. store.actions.sync('zh', OPTIONS, 0)
  13. expect(store.getSnapshot()).toEqual({ active: 'zh', options: OPTIONS, revision: 0 })
  14. store.actions.sync('en', OPTIONS, 1)
  15. expect(store.getSnapshot().active).toBe('en')
  16. expect(store.getSnapshot().revision).toBe(1)
  17. })
  18. it('revision guard drops stale and duplicate writes', () => {
  19. const store = createLanguageRowStore().create()
  20. store.actions.sync('en', OPTIONS, 5)
  21. store.actions.sync('zh', OPTIONS, 4)
  22. store.actions.sync('zh', OPTIONS, 5)
  23. expect(store.getSnapshot().active).toBe('en')
  24. expect(store.getSnapshot().revision).toBe(5)
  25. })
  26. })