spellchecker.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import {
  2. getWordsFromLine,
  3. buildSpellCheckResult,
  4. Word,
  5. } from '@/features/source-editor/extensions/spelling/spellchecker'
  6. import { expect } from 'chai'
  7. import { EditorView } from '@codemirror/view'
  8. import { LaTeXLanguage } from '@/features/source-editor/languages/latex/latex-language'
  9. import { LanguageSupport } from '@codemirror/language'
  10. const extensions = [new LanguageSupport(LaTeXLanguage)]
  11. describe('SpellChecker', function () {
  12. describe('getWordsFromLine', function () {
  13. let lang: string
  14. beforeEach(function () {
  15. /* Note: ignore the word 'test' */
  16. lang = 'en'
  17. })
  18. it('should get words from a line', function () {
  19. const view = new EditorView({
  20. doc: 'Hello test one two',
  21. extensions,
  22. })
  23. const line = view.state.doc.line(1)
  24. const words = Array.from(getWordsFromLine(view, line, lang))
  25. expect(words).to.deep.equal([
  26. { text: 'Hello', from: 0, to: 5, lineNumber: 1, lang: 'en' },
  27. { text: 'test', from: 6, to: 10, lineNumber: 1, lang: 'en' },
  28. { text: 'one', from: 11, to: 14, lineNumber: 1, lang: 'en' },
  29. { text: 'two', from: 15, to: 18, lineNumber: 1, lang: 'en' },
  30. ])
  31. })
  32. it('should get no words from an empty line', function () {
  33. const view = new EditorView({
  34. doc: ' ',
  35. extensions,
  36. })
  37. const line = view.state.doc.line(1)
  38. const words = Array.from(getWordsFromLine(view, line, lang))
  39. expect(words).to.deep.equal([])
  40. })
  41. it('should ignore content of some commands in the text', function () {
  42. const view = new EditorView({
  43. doc: '\\usepackage[foo]{ bar } seven eight',
  44. extensions,
  45. })
  46. const line = view.state.doc.line(1)
  47. const words = Array.from(getWordsFromLine(view, line, lang))
  48. expect(words).to.deep.equal([
  49. { text: 'seven', from: 24, to: 29, lineNumber: 1, lang: 'en' },
  50. { text: 'eight', from: 30, to: 35, lineNumber: 1, lang: 'en' },
  51. ])
  52. })
  53. it('should ignore command names in the text', function () {
  54. const view = new EditorView({
  55. doc: '\\foo nine \\bar ten \\baz[]{}',
  56. extensions,
  57. })
  58. const line = view.state.doc.line(1)
  59. const words = Array.from(getWordsFromLine(view, line, lang))
  60. expect(words).to.deep.equal([
  61. { text: 'nine', from: 5, to: 9, lineNumber: 1, lang: 'en' },
  62. { text: 'ten', from: 15, to: 18, lineNumber: 1, lang: 'en' },
  63. ])
  64. })
  65. })
  66. describe('buildSpellCheckResult', function () {
  67. it('should build an empty result', function () {
  68. const knownMisspelledWords: Word[] = []
  69. const unknownWords: Word[] = []
  70. const misspellings: { index: number }[] = []
  71. const result = buildSpellCheckResult(
  72. knownMisspelledWords,
  73. unknownWords,
  74. misspellings
  75. )
  76. expect(result).to.deep.equal({
  77. cacheAdditions: [],
  78. misspelledWords: [],
  79. })
  80. })
  81. it('should build a realistic result', function () {
  82. const _makeWord = (text: string) => {
  83. return new Word({
  84. text,
  85. from: 0,
  86. to: 0,
  87. lineNumber: 0,
  88. lang: 'xx',
  89. })
  90. }
  91. // We know this word is misspelled
  92. const knownMisspelledWords = [_makeWord('fff')]
  93. // These words we didn't know
  94. const unknownWords = [
  95. _makeWord('aaa'),
  96. _makeWord('bbb'),
  97. _makeWord('ccc'),
  98. _makeWord('ddd'),
  99. ]
  100. // These are the suggestions we got back from the backend
  101. const misspellings = [{ index: 1 }, { index: 3 }]
  102. // Build the result structure
  103. const result = buildSpellCheckResult(
  104. knownMisspelledWords,
  105. unknownWords,
  106. misspellings
  107. )
  108. expect(result).to.have.keys('cacheAdditions', 'misspelledWords')
  109. // Check cache additions
  110. expect(result.cacheAdditions.map(([k, v]) => [k.text, v])).to.deep.equal([
  111. // Put these in cache as known misspellings
  112. ['bbb', false],
  113. ['ddd', false],
  114. // Put these in cache as known-correct
  115. ['aaa', true],
  116. ['ccc', true],
  117. ])
  118. // Check misspellings
  119. expect(result.misspelledWords.map(w => w.text)).to.deep.equal([
  120. // Words in the payload that we now know were misspelled
  121. 'bbb',
  122. 'ddd',
  123. // Word we already knew was misspelled, preserved here
  124. 'fff',
  125. ])
  126. })
  127. })
  128. })