select-controller.test.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { expect, test } from "bun:test"
  2. import {
  3. moveSelection,
  4. moveSelectionOffset,
  5. reconcileSelection,
  6. revealSelectionOffset,
  7. } from "../../src/ui/select-controller"
  8. test("reconciles and moves selections with explicit boundary policy", () => {
  9. expect([reconcileSelection(3, 0), reconcileSelection(4, 3), reconcileSelection(2, 6)]).toEqual([0, 2, 2])
  10. expect([
  11. moveSelection(0, { count: 3, delta: -1, policy: "clamp" }),
  12. moveSelection(2, { count: 3, delta: 1, policy: "clamp" }),
  13. moveSelection(0, { count: 3, delta: -1, policy: "wrap" }),
  14. moveSelection(2, { count: 3, delta: 1, policy: "wrap" }),
  15. ]).toEqual([0, 2, 2, 0])
  16. })
  17. test("reveals selections within bounded windows", () => {
  18. expect([
  19. revealSelectionOffset(5, { count: 20, limit: 8, selected: 3 }),
  20. revealSelectionOffset(3, { count: 20, limit: 8, selected: 11 }),
  21. revealSelectionOffset(3, { count: 20, limit: 8, selected: 10 }),
  22. revealSelectionOffset(20, { count: 20, limit: 8, selected: 19 }),
  23. ]).toEqual([3, 4, 3, 12])
  24. })
  25. test("keeps movement offsets and preview margins in bounds", () => {
  26. expect([
  27. moveSelectionOffset(0, { count: 20, limit: 8, selected: 6, direction: 1 }),
  28. moveSelectionOffset(8, { count: 20, limit: 8, selected: 9, direction: -1 }),
  29. moveSelectionOffset(12, { count: 20, limit: 8, selected: 19, direction: 1 }),
  30. moveSelectionOffset(4, { count: 4, limit: 8, selected: 3, direction: 1 }),
  31. ]).toEqual([1, 7, 12, 0])
  32. })