import { describe, expect, it } from 'vitest'; import { getSchema } from '@tiptap/core'; import StarterKit from '@tiptap/starter-kit'; import { Node } from '@tiptap/pm/model'; import { dialectForLocale, isCheckedKind, kindLabel, lintSource, looksLikeWord, markClass, rangeForSpan, suggestionLabel, toIssues } from './proofread.ts'; /** * Harper is not imported here, so these run without the WebAssembly binary. The * two hard parts they pin are pure: document to text, and offset to position. */ const schema = getSchema([StarterKit.configure({ codeBlock: false, heading: { levels: [2, 3] } })]); function doc(content: unknown[]) { return Node.fromJSON(schema, { type: 'doc', content }); } function paragraph(text: string, mark?: string) { return { type: 'paragraph', content: [{ type: 'text', text, ...(mark ? { marks: [{ type: mark }] } : {}) }] }; } const SIMPLE = doc([ paragraph('The bell rang.'), { type: 'heading', attrs: { level: 2 }, content: [{ type: 'text', text: 'A Title' }] }, { type: 'paragraph', content: [ { type: 'text', text: 'Bold', marks: [{ type: 'bold' }] }, { type: 'text', text: ' plain ' }, { type: 'text', text: 'italic', marks: [{ type: 'italic' }] }, { type: 'hardBreak' }, { type: 'text', text: 'after break' } ] } ]); describe('lintSource', () => { it('reads blocks and hard breaks as lines, in document order', () => { expect(lintSource(SIMPLE).text).toBe('The bell rang.\nA Title\nBold plain italic\nafter break'); }); it('gives each character the position of the character itself', () => { const source = lintSource(SIMPLE); // 1 is inside the first paragraph, 17 inside the heading, 26 inside the // bold run, 37 inside the italic run, 44 after the hard break. expect(source.starts[0]).toBe(1); expect(source.text.slice(15, 16)).toBe('A'); expect(source.starts[15]).toBe(17); expect(source.starts[source.text.indexOf('Bold')]).toBe(26); expect(source.starts[source.text.indexOf('italic')]).toBe(37); expect(source.starts[source.text.indexOf('after break')]).toBe(44); }); it('marks the lines it inserted, so no range can cross one', () => { const source = lintSource(SIMPLE); const separator = source.text.indexOf('\n'); expect(source.starts[separator]).toBe(-1); expect(source.ends[separator]).toBe(-1); }); it('counts by code point, because Harper does', () => { const source = lintSource(doc([paragraph('tar𝔘t')])); // The astral character is two JavaScript units and one position step. expect(source.text.length).toBe(6); expect(source.starts.length).toBe(5); expect(source.starts[3]).toBe(4); expect(source.ends[3]).toBe(6); expect(source.starts[4]).toBe(6); }); }); describe('rangeForSpan', () => { it('maps a span inside one text node to the words it covers', () => { const source = lintSource(SIMPLE); const start = source.text.indexOf('plain'); const range = rangeForSpan(source, start, start + 'plain'.length); expect(range).not.toBeNull(); expect(SIMPLE.textBetween(range!.from, range!.to)).toBe('plain'); }); it('maps a span that crosses a mark boundary', () => { const source = lintSource(SIMPLE); const start = source.text.indexOf('Bold plain'); const range = rangeForSpan(source, start, start + 'Bold plain'.length); expect(range).not.toBeNull(); expect(SIMPLE.textBetween(range!.from, range!.to)).toBe('Bold plain'); }); it('refuses a span that crosses a line', () => { const source = lintSource(SIMPLE); const start = source.text.indexOf('rang'); const range = rangeForSpan(source, start, start + 'rang.\nA'.length); expect(range).toBeNull(); }); it('refuses a span outside the text or ending before it starts', () => { const source = lintSource(SIMPLE); expect(rangeForSpan(source, 0, 0)).toBeNull(); expect(rangeForSpan(source, 5, 2)).toBeNull(); expect(rangeForSpan(source, 0, source.starts.length + 1)).toBeNull(); expect(rangeForSpan(source, -1, 4)).toBeNull(); }); it('maps a span in a single paragraph when the paragraph is the whole document', () => { const source = lintSource(doc([paragraph('teh cat')])); const range = rangeForSpan(source, 0, 3); expect(range).toEqual({ from: 1, to: 4 }); }); }); describe('toIssues', () => { const source = lintSource(doc([paragraph('teh cat sat')])); function raw(kind: string, start: number, end: number) { return { id: 0, kind, message: 'A message.', problem: 'teh', start, end, suggestions: [{ kind: 'replace' as const, text: 'the' }] }; } it('keeps a spelling lint and carries its suggestion', () => { const [issue] = toIssues(source, [raw('Spelling', 0, 3)]); expect(issue).toMatchObject({ from: 1, to: 4, kind: 'Spelling', problem: 'teh' }); expect(issue.suggestions).toEqual([{ kind: 'replace', text: 'the' }]); }); it('drops a style opinion, because a novelist did not ask for one', () => { expect(toIssues(source, [raw('Enhancement', 0, 3)])).toEqual([]); expect(toIssues(source, [raw('Readability', 0, 3)])).toEqual([]); }); it('drops a lint it cannot place', () => { expect(toIssues(source, [raw('Spelling', 0, 100)])).toEqual([]); }); }); describe('preferences', () => { it('reads the spelling a browser language asks for', () => { expect(dialectForLocale('en-GB')).toBe('British'); expect(dialectForLocale('en-IE')).toBe('British'); expect(dialectForLocale('en-US')).toBe('American'); expect(dialectForLocale('en-AU')).toBe('Australian'); expect(dialectForLocale('en-CA')).toBe('Canadian'); expect(dialectForLocale('en-IN')).toBe('Indian'); expect(dialectForLocale('fr-FR')).toBe('American'); expect(dialectForLocale('en')).toBe('American'); }); it('only checks the categories the product keeps', () => { expect(isCheckedKind('Spelling')).toBe(true); expect(isCheckedKind('Agreement')).toBe(true); expect(isCheckedKind('Style')).toBe(false); expect(isCheckedKind('WordChoice')).toBe(false); }); it('reads a category and a suggestion in the product voice', () => { expect(kindLabel('Capitalization')).toBe('Capitalisation'); expect(kindLabel('Spelling')).toBe('Spelling'); expect(suggestionLabel({ kind: 'replace', text: 'the' })).toBe('Use “the”'); expect(suggestionLabel({ kind: 'remove', text: '' })).toBe('Remove'); }); it('tells a word from a phrase', () => { expect(looksLikeWord('Maren')).toBe(true); expect(looksLikeWord("O'Rourke")).toBe(true); expect(looksLikeWord('two words')).toBe(false); }); it('gives spelling and grammar different marks', () => { expect(markClass('Spelling')).toBe('proofread-spelling'); expect(markClass('Agreement')).toBe('proofread-grammar'); }); });