count-words-in-file.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { readFile } from 'node:fs/promises'
  2. import path from 'node:path'
  3. import { countWordsInFile } from '@/features/word-count-modal/utils/count-words-in-file'
  4. import { WordCountData } from '@/features/word-count-modal/components/word-count-data'
  5. import { createSegmenters } from '@/features/word-count-modal/utils/segmenters'
  6. import { expect } from 'chai'
  7. import { ProjectSnapshot } from '@/infrastructure/project-snapshot'
  8. import { Snapshot } from 'overleaf-editor-core'
  9. describe('word-count', function () {
  10. beforeEach(async function () {
  11. this.data = {
  12. encode: '',
  13. textWords: 0,
  14. headWords: 0,
  15. outside: 0,
  16. headers: 0,
  17. elements: 0,
  18. mathInline: 0,
  19. mathDisplay: 0,
  20. errors: 0,
  21. messages: '',
  22. textCharacters: 0,
  23. headCharacters: 0,
  24. captionWords: 0,
  25. captionCharacters: 0,
  26. footnoteWords: 0,
  27. footnoteCharacters: 0,
  28. abstractWords: 0,
  29. abstractCharacters: 0,
  30. otherWords: 0,
  31. otherCharacters: 0,
  32. } satisfies WordCountData
  33. const files = {
  34. 'word-count.tex': {
  35. content: await readFile(
  36. path.join(__dirname, 'word-count.tex'),
  37. 'utf-8'
  38. ),
  39. },
  40. 'word-count-with-ignored-sections.tex': {
  41. content: await readFile(
  42. path.join(__dirname, 'word-count-with-ignored-sections.tex'),
  43. 'utf-8'
  44. ),
  45. },
  46. 'extra-words.tex': {
  47. content: await readFile(
  48. path.join(__dirname, 'extra-words.tex'),
  49. 'utf-8'
  50. ),
  51. },
  52. 'subfolder/extra-words.tex': {
  53. content: await readFile(
  54. path.join(__dirname, 'extra-words.tex'),
  55. 'utf-8'
  56. ),
  57. },
  58. }
  59. const projectSnapshot = new ProjectSnapshot('test')
  60. // @ts-expect-error ignoring that "snapshot" is private
  61. projectSnapshot.snapshot = Snapshot.fromRaw({ files })
  62. this.projectSnapshot = projectSnapshot
  63. this.segmenters = createSegmenters('en_US')
  64. })
  65. it('produces correct counts', function () {
  66. countWordsInFile(
  67. this.data,
  68. this.projectSnapshot,
  69. 'word-count.tex',
  70. '/',
  71. this.segmenters
  72. )
  73. expect(this.data).to.deep.include({
  74. abstractCharacters: 8,
  75. abstractWords: 2,
  76. captionCharacters: 16,
  77. captionWords: 4,
  78. footnoteCharacters: 8,
  79. footnoteWords: 2,
  80. headCharacters: 305,
  81. headWords: 53,
  82. otherCharacters: 10,
  83. otherWords: 2,
  84. textCharacters: 249,
  85. textWords: 56,
  86. })
  87. })
  88. it('skips ignored sections', function () {
  89. countWordsInFile(
  90. this.data,
  91. this.projectSnapshot,
  92. 'word-count-with-ignored-sections.tex',
  93. '/',
  94. this.segmenters
  95. )
  96. expect(this.data).to.deep.include({
  97. abstractCharacters: 0,
  98. abstractWords: 0,
  99. captionCharacters: 0,
  100. captionWords: 0,
  101. footnoteCharacters: 0,
  102. footnoteWords: 0,
  103. headCharacters: 0,
  104. headWords: 0,
  105. otherCharacters: 0,
  106. otherWords: 0,
  107. textCharacters: 10,
  108. textWords: 3,
  109. })
  110. })
  111. })