lines.client.spec.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * The body's page arithmetic: how a page's text and line count become lines,
  3. * how the store's page table becomes the pages in file order, and how far they
  4. * reach.
  5. */
  6. import { describe, expect, it } from 'vitest'
  7. import { lastLineLoaded, linesOf, loadedPages } from '../src/client/TextPreview.tsx'
  8. import type { TextPage } from '../src/client/store.ts'
  9. const held = (text: string, lines: number): TextPage => ({ text, lines })
  10. describe('linesOf', () => {
  11. it('splits on newlines, so a trailing one ends an empty last line as the Host counted it', () => {
  12. expect(linesOf(held('a\nb', 2))).toEqual(['a', 'b'])
  13. expect(linesOf(held('a\n', 2))).toEqual(['a', ''])
  14. expect(linesOf(held('a\nb\n', 3))).toEqual(['a', 'b', ''])
  15. expect(linesOf(held('a\n\nb', 3))).toEqual(['a', '', 'b'])
  16. })
  17. it('tells a page holding one empty line from a page past the file\'s last line by the count', () => {
  18. expect(linesOf(held('', 1))).toEqual([''])
  19. expect(linesOf(held('', 0))).toEqual([])
  20. })
  21. })
  22. describe('loadedPages', () => {
  23. it('orders the store\'s page table by the line each page starts at', () => {
  24. expect(loadedPages({})).toEqual([])
  25. expect(loadedPages({ 4: held('d\ne', 2), 1: held('a\nb\nc', 3) })).toEqual([
  26. { offset: 1, text: 'a\nb\nc', lines: 3 },
  27. { offset: 4, text: 'd\ne', lines: 2 },
  28. ])
  29. })
  30. })
  31. describe('lastLineLoaded', () => {
  32. it('is 0 before the first page and the last line of the last page after, by the Host\'s count', () => {
  33. expect(lastLineLoaded([])).toBe(0)
  34. expect(lastLineLoaded(loadedPages({ 4: held('d\ne', 2), 1: held('a\nb\nc', 3) }))).toBe(5)
  35. expect(lastLineLoaded(loadedPages({ 1: held('', 1) }))).toBe(1)
  36. expect(lastLineLoaded(loadedPages({ 1: held('a', 1), 2: held('', 0) }))).toBe(1)
  37. })
  38. })