-
Notifications
You must be signed in to change notification settings - Fork 663
fix: toolbox search final trigram #2648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
goughjo02
wants to merge
2
commits into
RaspberryPiFoundation:main
Choose a base branch
from
goughjo02:fix/toolbox-search-final-trigram
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -15,6 +15,15 @@ suite('Toolbox search', () => { | |
| }); | ||
|
|
||
| suite('BlockSearcher', () => { | ||
| test('generateTrigrams handles empty and short input', () => { | ||
| const searcher = new BlockSearcher(); | ||
| const generateTrigrams = searcher.generateTrigrams.bind(searcher); | ||
|
|
||
| assert.deepEqual(generateTrigrams(''), []); | ||
| assert.deepEqual(generateTrigrams('a'), ['a']); | ||
| assert.deepEqual(generateTrigrams('abc'), ['abc']); | ||
| }); | ||
|
|
||
| test('indexes the default value of dropdown fields', () => { | ||
| const searcher = new BlockSearcher(); | ||
| const blocks = [ | ||
|
|
@@ -57,6 +66,119 @@ suite('BlockSearcher', () => { | |
| assert.sameMembers(ransomNoteMatches, [listCreateWithBlock]); | ||
| }); | ||
|
|
||
| test('requires the final trigram when matching longer queries', () => { | ||
| const searcher = new BlockSearcher(); | ||
| const mathConstrainBlock = { | ||
| kind: 'block', | ||
| type: 'math_constrain', | ||
| }; | ||
| searcher.indexBlocks([mathConstrainBlock]); | ||
|
|
||
| const matches = searcher.blockTypesMatching('conso'); | ||
|
|
||
| assert.notInclude( | ||
| matches, | ||
| mathConstrainBlock, | ||
| 'query missing trailing trigram should not match', | ||
| ); | ||
| }); | ||
|
|
||
| test('normalizes underscores in block types to spaces', () => { | ||
| if (!Blockly.Blocks['searcher_underscore_block']) { | ||
| Blockly.defineBlocksWithJsonArray([ | ||
| { | ||
| type: 'searcher_underscore_block', | ||
| message0: 'custom block with underscore', | ||
| }, | ||
| ]); | ||
| } | ||
|
|
||
| const searcher = new BlockSearcher(); | ||
| const blockInfo = { | ||
| kind: 'block', | ||
| type: 'searcher_underscore_block', | ||
| }; | ||
| searcher.indexBlocks([blockInfo]); | ||
|
|
||
| assert.sameMembers( | ||
| searcher.blockTypesMatching('custom block with underscore'), | ||
| [blockInfo], | ||
| ); | ||
| assert.isEmpty(searcher.blockTypesMatching('custom_block_with_underscore')); | ||
| }); | ||
|
|
||
| test('longer queries disambiguate similar blocks', () => { | ||
| if (!Blockly.Blocks['searcher_charlie']) { | ||
| Blockly.defineBlocksWithJsonArray([ | ||
| { | ||
| type: 'searcher_charlie', | ||
| message0: 'alpha bravo charlie', | ||
| }, | ||
| { | ||
| type: 'searcher_delta', | ||
| message0: 'alpha bravo delta', | ||
| }, | ||
| ]); | ||
| } | ||
|
|
||
| const searcher = new BlockSearcher(); | ||
| const blockA = {kind: 'block', type: 'searcher_charlie'}; | ||
| const blockB = {kind: 'block', type: 'searcher_delta'}; | ||
|
|
||
| searcher.indexBlocks([blockA, blockB]); | ||
|
|
||
| const broadQueryMatches = searcher.blockTypesMatching('alpha bravo'); | ||
| assert.sameMembers(broadQueryMatches, [blockA, blockB]); | ||
|
|
||
| const specificQueryMatches = | ||
| searcher.blockTypesMatching('alpha bravo charlie'); | ||
| assert.sameMembers(specificQueryMatches, [blockA]); | ||
| }); | ||
|
|
||
| test('indexes dropdown alt text options', () => { | ||
| if (!Blockly.Blocks['searcher_dropdown_alt']) { | ||
| Blockly.defineBlocksWithJsonArray([ | ||
| { | ||
| type: 'searcher_dropdown_alt', | ||
| message0: 'weather %1', | ||
| args0: [ | ||
| { | ||
| type: 'field_dropdown', | ||
| name: 'WEATHER', | ||
| options: [ | ||
| [ | ||
| { | ||
| src: 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEA', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't appear to actually be a valid image; if it doesn't otherwise cause problems, can you set |
||
| width: 1, | ||
| height: 1, | ||
| alt: 'Sunny', | ||
| }, | ||
| 'SUN', | ||
| ], | ||
| [ | ||
| { | ||
| src: 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEA', | ||
| width: 1, | ||
| height: 1, | ||
| alt: 'Cloudy', | ||
| }, | ||
| 'CLOUD', | ||
| ], | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
| } | ||
|
|
||
| const searcher = new BlockSearcher(); | ||
| const blockInfo = {kind: 'block', type: 'searcher_dropdown_alt'}; | ||
| searcher.indexBlocks([blockInfo]); | ||
|
|
||
| assert.sameMembers(searcher.blockTypesMatching('sunny'), [blockInfo]); | ||
| assert.sameMembers(searcher.blockTypesMatching('cloudy'), [blockInfo]); | ||
| }); | ||
|
|
||
| test('returns an empty list when no matches are found', () => { | ||
| const searcher = new BlockSearcher(); | ||
| assert.isEmpty(searcher.blockTypesMatching('abc123')); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the test is verifying that underscores in the block type get treated as spaces, should this be 'searcher underscore block'? As written this would match the unmodified message, but not verify the normalization of the block type.