Skip to content

Commit 2046766

Browse files
committed
test: expand block search coverage
cover trigram edge cases, underscore normalization, dropdown alt text, and disambiguation of similar blocks
1 parent f568b95 commit 2046766

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

plugins/toolbox-search/test/tests.mocha.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ suite('Toolbox search', () => {
1515
});
1616

1717
suite('BlockSearcher', () => {
18+
test('generateTrigrams handles empty and short input', () => {
19+
const searcher = new BlockSearcher();
20+
const generateTrigrams = searcher.generateTrigrams.bind(searcher);
21+
22+
assert.deepEqual(generateTrigrams(''), []);
23+
assert.deepEqual(generateTrigrams('a'), ['a']);
24+
assert.deepEqual(generateTrigrams('abc'), ['abc']);
25+
});
26+
1827
test('indexes the default value of dropdown fields', () => {
1928
const searcher = new BlockSearcher();
2029
const blocks = [
@@ -74,6 +83,102 @@ suite('BlockSearcher', () => {
7483
);
7584
});
7685

86+
test('normalizes underscores in block types to spaces', () => {
87+
if (!Blockly.Blocks['searcher_underscore_block']) {
88+
Blockly.defineBlocksWithJsonArray([
89+
{
90+
type: 'searcher_underscore_block',
91+
message0: 'custom block with underscore',
92+
},
93+
]);
94+
}
95+
96+
const searcher = new BlockSearcher();
97+
const blockInfo = {
98+
kind: 'block',
99+
type: 'searcher_underscore_block',
100+
};
101+
searcher.indexBlocks([blockInfo]);
102+
103+
assert.sameMembers(
104+
searcher.blockTypesMatching('custom block with underscore'),
105+
[blockInfo],
106+
);
107+
assert.isEmpty(searcher.blockTypesMatching('custom_block_with_underscore'));
108+
});
109+
110+
test('longer queries disambiguate similar blocks', () => {
111+
if (!Blockly.Blocks['searcher_charlie']) {
112+
Blockly.defineBlocksWithJsonArray([
113+
{
114+
type: 'searcher_charlie',
115+
message0: 'alpha bravo charlie',
116+
},
117+
{
118+
type: 'searcher_delta',
119+
message0: 'alpha bravo delta',
120+
},
121+
]);
122+
}
123+
124+
const searcher = new BlockSearcher();
125+
const blockA = {kind: 'block', type: 'searcher_charlie'};
126+
const blockB = {kind: 'block', type: 'searcher_delta'};
127+
128+
searcher.indexBlocks([blockA, blockB]);
129+
130+
const broadQueryMatches = searcher.blockTypesMatching('alpha bravo');
131+
assert.sameMembers(broadQueryMatches, [blockA, blockB]);
132+
133+
const specificQueryMatches =
134+
searcher.blockTypesMatching('alpha bravo charlie');
135+
assert.sameMembers(specificQueryMatches, [blockA]);
136+
});
137+
138+
test('indexes dropdown alt text options', () => {
139+
if (!Blockly.Blocks['searcher_dropdown_alt']) {
140+
Blockly.defineBlocksWithJsonArray([
141+
{
142+
type: 'searcher_dropdown_alt',
143+
message0: 'weather %1',
144+
args0: [
145+
{
146+
type: 'field_dropdown',
147+
name: 'WEATHER',
148+
options: [
149+
[
150+
{
151+
src: 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEA',
152+
width: 1,
153+
height: 1,
154+
alt: 'Sunny',
155+
},
156+
'SUN',
157+
],
158+
[
159+
{
160+
src: 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEA',
161+
width: 1,
162+
height: 1,
163+
alt: 'Cloudy',
164+
},
165+
'CLOUD',
166+
],
167+
],
168+
},
169+
],
170+
},
171+
]);
172+
}
173+
174+
const searcher = new BlockSearcher();
175+
const blockInfo = {kind: 'block', type: 'searcher_dropdown_alt'};
176+
searcher.indexBlocks([blockInfo]);
177+
178+
assert.sameMembers(searcher.blockTypesMatching('sunny'), [blockInfo]);
179+
assert.sameMembers(searcher.blockTypesMatching('cloudy'), [blockInfo]);
180+
});
181+
77182
test('returns an empty list when no matches are found', () => {
78183
const searcher = new BlockSearcher();
79184
assert.isEmpty(searcher.blockTypesMatching('abc123'));

0 commit comments

Comments
 (0)