-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
220 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ | ||
module.exports = { | ||
snapshotFormat: { | ||
escapeString: true, | ||
printBasicPrototype: true, | ||
}, | ||
transform: { | ||
'^.+\\.tsx?$': ['esbuild-jest', {tsconfig: './tsconfig.json'}], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`suggest should match code 1`] = ` | ||
Array [ | ||
" | ||
<a href=\\"./7\\"> | ||
<div></div> | ||
<div><span class=\\"mark\\">crm.stagehistory.list</span></div> | ||
</a> | ||
", | ||
] | ||
`; | ||
exports[`suggest should match html content 1`] = ` | ||
Array [ | ||
" | ||
<a href=\\"./1\\"> | ||
<div></div> | ||
<div><span class=\\"mark\\">Lorem ipsum</span> dolor sit amet, consectetur adipiscing elit. Integer sit amet enim velit.</div> | ||
</a> | ||
", | ||
] | ||
`; | ||
exports[`suggest should match title content 1`] = ` | ||
Array [ | ||
" | ||
<a href=\\"./3\\"> | ||
<div><span class=\\"mark\\">Lorem ipsum</span> 1</div> | ||
<div>Integer sit amet enim velit. Nam facilisis eget magna non blandit.</div> | ||
</a> | ||
", | ||
" | ||
<a href=\\"./4\\"> | ||
<div><span class=\\"mark\\">Lorem ipsum</span> 2</div> | ||
<div>Nam facilisis eget magna non blandit. Sed semper, dui ut suscipit semper, nibh justo tempor purus, quis placerat enim dolor vitae neque.</div> | ||
</a> | ||
", | ||
] | ||
`; | ||
exports[`suggest should score longest phrase 1`] = ` | ||
Array [ | ||
" | ||
<a href=\\"./6\\"> | ||
<div></div> | ||
<div>...urus, quis placerat <span class=\\"mark\\">enim dolor vitae</span> neque. Vivamus dignissim nunc et tortor vulputate maximus.</div> | ||
</a> | ||
", | ||
" | ||
<a href=\\"./5\\"> | ||
<div></div> | ||
<div>Lorem ipsum <span class=\\"mark\\">dolor</span> sit amet, consectetur adipiscing elit. Integer sit amet enim velit. Nam facilisis eget magna non blandit.</div> | ||
</a> | ||
", | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import type {Index} from 'lunr'; | ||
import type {Registry, WorkerConfig} from '../src/types'; | ||
import type {SearchSuggestPageItem} from '@diplodoc/components'; | ||
|
||
import {Indexer, ReleaseFormat} from '../src/indexer'; | ||
import {search} from '../src/worker/search'; | ||
import {format, short} from '../src/worker/format'; | ||
|
||
const Lorem = [ | ||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', | ||
'Integer sit amet enim velit.', | ||
'Nam facilisis eget magna non blandit.', | ||
'Sed semper, dui ut suscipit semper, nibh justo tempor purus, quis placerat enim dolor vitae neque.', | ||
'Vivamus dignissim nunc et tortor vulputate maximus.', | ||
'Fusce lobortis pretium lectus, non pretium mi rhoncus quis.', | ||
'Curabitur blandit imperdiet metus id luctus.', | ||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', | ||
'Aenean lobortis ligula a mauris posuere, luctus pretium mauris ultrices.', | ||
]; | ||
|
||
const Code = 'crm.stagehistory.list'; | ||
|
||
const item = ({link, title, description}: SearchSuggestPageItem) => ` | ||
<a href="${link}"> | ||
<div>${title}</div> | ||
<div>${description}</div> | ||
</a> | ||
`; | ||
|
||
describe('suggest', () => { | ||
const lang = 'ru'; | ||
let indexer: Indexer; | ||
let uid = 1; | ||
|
||
function suggest(query: string, config: Pick<WorkerConfig, 'confidence' | 'tolerance'>) { | ||
const {index, registry} = indexer.release(lang, ReleaseFormat.RAW); | ||
|
||
const results = search(config, index as Index, query, 10, false); | ||
|
||
return format({base: './', mark: 'mark'}, results, registry as Registry, short).map(item); | ||
} | ||
|
||
function add(html: string, title = '') { | ||
indexer.add(lang, String(uid++), { | ||
html, | ||
title, | ||
leading: false, | ||
meta: {}, | ||
toc: {items: [], href: ''}, | ||
}); | ||
} | ||
|
||
beforeEach(() => { | ||
indexer = new Indexer(); | ||
}); | ||
|
||
it('should match html content', () => { | ||
add(Lorem.slice(0, 2).join(' ')); | ||
add(Lorem.slice(1, 3).join(' ')); | ||
|
||
const config = {confidence: 'phrased', tolerance: 2} as const; | ||
|
||
expect(suggest('Lorem ipsum', config)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should match title content', () => { | ||
add(Lorem.slice(1, 3).join(' '), 'Lorem ipsum 1'); | ||
add(Lorem.slice(2, 4).join(' '), 'Lorem ipsum 2'); | ||
|
||
const config = {confidence: 'phrased', tolerance: 2} as const; | ||
|
||
expect(suggest('Lorem ipsum', config)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should score longest phrase', () => { | ||
add(Lorem.slice(0, 3).join(' ')); | ||
add(Lorem.slice(1, 5).join(' ')); | ||
|
||
const config = {confidence: 'phrased', tolerance: 2} as const; | ||
|
||
expect(suggest('enim dolor vitae', config)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should match code', () => { | ||
add(Code); | ||
|
||
const config = {confidence: 'phrased', tolerance: 2} as const; | ||
|
||
expect(suggest('stagehistory', config)).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,5 @@ | |
"baseUrl": ".", | ||
"outDir": "lib" | ||
}, | ||
"include": ["src"] | ||
"include": ["src", "test"] | ||
} |