@astryxdesign/cli 0.7.3-canary.b74cf21 → 0.7.3-canary.ca48bb7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (213) hide show
  1. package/README.md +68 -68
  2. package/api/build/blocks/blocks.d.mts +6 -4
  3. package/api/build/blocks/blocks.mjs +103 -20
  4. package/api/build/blocks/blocks.test.mjs +126 -13
  5. package/api/build/blocks/blocks.weights.json +1 -1
  6. package/api/build/kit/answer.d.mts +5 -4
  7. package/api/build/kit/answer.mjs +114 -12
  8. package/api/build/kit/answer.test.mjs +73 -3
  9. package/api/build/kit/signals.d.mts +8 -0
  10. package/api/build/kit/signals.mjs +10 -0
  11. package/api/build/tree.test.mjs +19 -11
  12. package/api/component/component-replacement.test.mjs +1 -0
  13. package/api/component/component.mjs +17 -3
  14. package/api/component/component.test.mjs +15 -0
  15. package/api/component/doc-links.test.mjs +2 -2
  16. package/api/docs/_adapter.d.mts +2 -2
  17. package/api/docs/_adapter.mjs +61 -18
  18. package/api/docs/docs-homes.test.mjs +127 -0
  19. package/api/docs/docs.doc.mjs +3 -3
  20. package/api/docs/docs.test.mjs +28 -18
  21. package/api/docs/integration-tree.test.mjs +20 -20
  22. package/api/docs/integrationDocs.test.mjs +1 -1
  23. package/api/docs/namespace-section.test.mjs +6 -6
  24. package/api/docs/reference-blocks.test.mjs +2 -2
  25. package/api/doctor/doctor.mjs +16 -12
  26. package/api/doctor/doctor.test.mjs +37 -12
  27. package/api/hook/hook.mjs +1 -1
  28. package/api/integration/_adapter.d.mts +8 -0
  29. package/api/integration/_adapter.mjs +21 -1
  30. package/api/integration/authoring-checks.test.mjs +220 -0
  31. package/api/integration/pack-check.mjs +110 -4
  32. package/api/integration/pack-check.test.mjs +69 -0
  33. package/api/integration/validate-integration-fixes.test.mjs +6 -5
  34. package/api/integration/validate-integration-gaps.test.mjs +304 -1
  35. package/api/integration/validate-integration.d.mts +6 -1
  36. package/api/integration/validate-integration.mjs +82 -13
  37. package/api/integration/validate-integration.type.d.mts +37 -4
  38. package/api/integration/validate-integration.type.mjs +28 -5
  39. package/api/integration/validateIntegration.doc.mjs +1 -1
  40. package/api/search/_adapter.d.mts +16 -0
  41. package/api/search/_adapter.mjs +39 -0
  42. package/api/search/_adapter.test.mjs +173 -0
  43. package/api/search/_candidate-score.mjs +1 -1
  44. package/api/search/_entries.d.mts +11 -0
  45. package/api/search/_entries.mjs +77 -3
  46. package/api/search/_entries.test.mjs +48 -0
  47. package/api/search/_gather-docs.mjs +3 -3
  48. package/api/search/search.doc.mjs +1 -1
  49. package/api/search/search.mjs +6 -2
  50. package/api/search/search.test.mjs +14 -14
  51. package/api/upgrade/_adapter.d.mts +57 -3
  52. package/api/upgrade/_adapter.mjs +149 -27
  53. package/api/upgrade/registry/registry.d.mts +10 -4
  54. package/api/upgrade/registry/registry.mjs +33 -16
  55. package/api/upgrade/registry/registry.test.mjs +85 -0
  56. package/api/upgrade/run/run.d.mts +4 -2
  57. package/api/upgrade/run/run.mjs +13 -2
  58. package/api/upgrade/run/run.test.mjs +417 -3
  59. package/api/upgrade/upgrade.doc.mjs +10 -5
  60. package/api/upgrade/upgrade.mjs +13 -2
  61. package/api/upgrade/upgrade.type.d.mts +5 -3
  62. package/api/upgrade/upgrade.type.mjs +8 -6
  63. package/assets/docs/author-a-theme.doc.mjs +2 -2
  64. package/assets/docs/is-my-code-good.doc.mjs +287 -0
  65. package/assets/docs/tree/add-a-component.doc.mjs +1 -1
  66. package/assets/docs/tree/add-a-theme.doc.mjs +1 -1
  67. package/assets/docs/tree/add-a-topic.doc.mjs +2 -2
  68. package/assets/docs/tree/agent-guidance.doc.mjs +1 -1
  69. package/assets/docs/tree/{integrations.doc.mjs → authoring.doc.mjs} +15 -6
  70. package/assets/docs/tree/block-template.doc.mjs +1 -1
  71. package/assets/docs/tree/build-the-template.doc.mjs +1 -1
  72. package/assets/docs/tree/building-blocks.doc.mjs +2 -2
  73. package/assets/docs/tree/check-the-props.doc.mjs +1 -1
  74. package/assets/docs/tree/check-your-docs.doc.mjs +2 -2
  75. package/assets/docs/tree/checks.doc.mjs +3 -3
  76. package/assets/docs/tree/cli-command.doc.mjs +1 -1
  77. package/assets/docs/tree/cli.doc.mjs +1 -1
  78. package/assets/docs/tree/codemods.doc.mjs +1 -1
  79. package/assets/docs/tree/component-family.doc.mjs +1 -1
  80. package/assets/docs/tree/component-imports.doc.mjs +1 -1
  81. package/assets/docs/tree/components.doc.mjs +1 -1
  82. package/assets/docs/tree/configuration.doc.mjs +1 -1
  83. package/assets/docs/tree/debug-and-gap-reports.doc.mjs +1 -1
  84. package/assets/docs/tree/define-the-theme.doc.mjs +1 -1
  85. package/assets/docs/tree/describe-the-component.doc.mjs +1 -1
  86. package/assets/docs/tree/docs.doc.mjs +1 -1
  87. package/assets/docs/tree/document-the-template.doc.mjs +1 -1
  88. package/assets/docs/tree/document-the-theme.doc.mjs +1 -1
  89. package/assets/docs/tree/export-template-assets.doc.mjs +1 -1
  90. package/assets/docs/tree/extend-or-replace.doc.mjs +1 -1
  91. package/assets/docs/{authoring.doc.mjs → tree/field-reference.doc.mjs} +4 -3
  92. package/assets/docs/tree/fonts-and-assets.doc.mjs +1 -1
  93. package/assets/docs/tree/generate-a-palette.doc.mjs +1 -1
  94. package/assets/docs/tree/global-integrations.doc.mjs +2 -2
  95. package/assets/docs/tree/grade-template-with-agent.doc.mjs +4 -4
  96. package/assets/docs/tree/help.doc.mjs +2 -2
  97. package/assets/docs/tree/links.doc.mjs +1 -1
  98. package/assets/docs/tree/package-and-test.doc.mjs +1 -1
  99. package/assets/docs/tree/page-template.doc.mjs +1 -1
  100. package/assets/docs/tree/publishing.doc.mjs +1 -1
  101. package/assets/docs/tree/quick-start.doc.mjs +1 -1
  102. package/assets/docs/tree/replace-a-core-component.doc.mjs +1 -1
  103. package/assets/docs/tree/replace-a-core-template.doc.mjs +1 -1
  104. package/assets/docs/tree/sections-and-placement.doc.mjs +1 -1
  105. package/assets/docs/tree/see-it-in-an-app.doc.mjs +1 -1
  106. package/assets/docs/tree/ship.doc.mjs +2 -2
  107. package/assets/docs/tree/short-and-findable.doc.mjs +1 -1
  108. package/assets/docs/tree/single-component.doc.mjs +1 -1
  109. package/assets/docs/tree/start-a-template.doc.mjs +1 -1
  110. package/assets/docs/tree/subcomponent.doc.mjs +1 -1
  111. package/assets/docs/tree/template-assets.doc.mjs +1 -1
  112. package/assets/docs/tree/template-doc-overview.doc.mjs +2 -2
  113. package/assets/docs/tree/template-fonts.doc.mjs +1 -1
  114. package/assets/docs/tree/template-grading-rubric.doc.mjs +17 -38
  115. package/assets/docs/tree/template-icons.doc.mjs +1 -1
  116. package/assets/docs/tree/template-images-media.doc.mjs +1 -1
  117. package/assets/docs/tree/template-styles.doc.mjs +1 -1
  118. package/assets/docs/tree/templates.doc.mjs +1 -1
  119. package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
  120. package/assets/docs/tree/test-template-in-app.doc.mjs +1 -1
  121. package/assets/docs/tree/themes.doc.mjs +1 -1
  122. package/assets/docs/tree/troubleshooting.doc.mjs +1 -1
  123. package/assets/docs/tree/upgrading.doc.mjs +1 -1
  124. package/assets/docs/tree/use-a-theme-in-an-app.doc.mjs +1 -1
  125. package/assets/docs/tree/verify-packed-template.doc.mjs +1 -1
  126. package/assets/docs/tree/versioning.doc.mjs +5 -1
  127. package/assets/docs/tree/write-good-templates.doc.mjs +1 -1
  128. package/assets/docs/tree/write-the-template-file.doc.mjs +1 -1
  129. package/assets/docs/use-integrations.doc.mjs +120 -0
  130. package/assets/templates/themes/butter/butterTheme.ts +4 -2
  131. package/assets/templates/themes/stone/stoneTheme.ts +4 -2
  132. package/assets/templates/themes/y2k/y2kTheme.ts +4 -2
  133. package/authoring/doctypes/schema/schema.doc.mjs +1 -1
  134. package/authoring/integration/integration.doc.mjs +1 -1
  135. package/authoring/integration/type.ts +1 -1
  136. package/clients/cli/command-result-coverage.test.mjs +6 -5
  137. package/clients/cli/commands/build-theme.color-scheme.test.mjs +1 -1
  138. package/clients/cli/commands/{component/index.mjs → component.mjs} +37 -37
  139. package/clients/cli/commands/component.test.mjs +6 -6
  140. package/clients/cli/commands/docs.doc.mjs +2 -2
  141. package/clients/cli/commands/docs.mjs +1 -1
  142. package/clients/cli/commands/docs.test.mjs +14 -14
  143. package/clients/cli/commands/doctor-integration.test.mjs +49 -1
  144. package/clients/cli/commands/doctor.mjs +10 -0
  145. package/clients/cli/commands/{hook/index.mjs → hook.mjs} +22 -22
  146. package/clients/cli/commands/import-hint-correctness.test.mjs +1 -1
  147. package/clients/cli/commands/integration-check.test.mjs +48 -0
  148. package/clients/cli/commands/integration.doc.mjs +1 -1
  149. package/clients/cli/commands/manifest.doc.mjs +1 -1
  150. package/clients/cli/commands/manifest.mjs +67 -0
  151. package/clients/cli/commands/postinstall.doc.mjs +23 -0
  152. package/clients/cli/commands/postinstall.mjs +31 -0
  153. package/clients/cli/commands/{build-theme.mjs → theme.mjs} +1 -1
  154. package/clients/cli/commands/upgrade.doc.mjs +4 -2
  155. package/clients/cli/commands/upgrade.mjs +9 -2
  156. package/clients/cli/commands/upgrade.subfolder-path.test.mjs +97 -0
  157. package/clients/cli/index.mjs +10 -85
  158. package/clients/cli/lib/component-format.mjs +1 -1
  159. package/clients/cli/lib/define-command.mjs +12 -7
  160. package/clients/cli/lib/exit-codes.test.mjs +5 -0
  161. package/clients/cli/lib/manifest.test.mjs +12 -0
  162. package/clients/cli/lib/welcome-box.mjs +50 -0
  163. package/foundation/agent-docs/agent-docs.mjs +2 -1
  164. package/foundation/agent-docs/agent-docs.test.mjs +5 -4
  165. package/foundation/config/project.mjs +5 -20
  166. package/foundation/config/project.test.mjs +56 -0
  167. package/foundation/discovery/authoring-self-docs.d.mts +7 -0
  168. package/foundation/discovery/authoring-self-docs.mjs +16 -4
  169. package/foundation/discovery/authoring-self-docs.test.mjs +15 -3
  170. package/foundation/discovery/authoring-surface.d.mts +1 -1
  171. package/foundation/discovery/authoring-surface.mjs +2 -2
  172. package/foundation/discovery/authoring-surface.test.mjs +1 -1
  173. package/foundation/discovery/cli-self-docs.d.mts +2 -1
  174. package/foundation/discovery/cli-self-docs.mjs +4 -3
  175. package/foundation/discovery/cli-self-docs.test.mjs +2 -2
  176. package/foundation/discovery/runner-allowlist.d.mts +2 -4
  177. package/foundation/discovery/runner-allowlist.mjs +2 -4
  178. package/foundation/discovery/runner-commands.d.mts +77 -0
  179. package/foundation/discovery/runner-commands.mjs +294 -0
  180. package/foundation/discovery/runner-commands.test.mjs +128 -0
  181. package/foundation/discovery/stray-pointers.test.mjs +1 -1
  182. package/foundation/discovery/template-adapter.mjs +18 -1
  183. package/foundation/discovery/template-needs.mjs +1 -44
  184. package/foundation/discovery/theme-discovery.mjs +147 -3
  185. package/foundation/doc-compiler/doc-loads.test.mjs +25 -9
  186. package/foundation/doc-compiler/read.mjs +59 -1
  187. package/foundation/doc-compiler/read.test.mjs +11 -2
  188. package/foundation/doc-compiler/split-topic-keywords.test.mjs +3 -2
  189. package/foundation/doc-compiler/tree.d.mts +32 -6
  190. package/foundation/doc-compiler/tree.mjs +77 -43
  191. package/foundation/doc-compiler/tree.test.mjs +7 -7
  192. package/foundation/fs/paths.d.mts +70 -8
  193. package/foundation/fs/paths.mjs +526 -35
  194. package/foundation/fs/paths.test.mjs +430 -54
  195. package/foundation/fs/read-record.d.mts +18 -0
  196. package/foundation/fs/read-record.mjs +131 -0
  197. package/foundation/fs/read-record.test.mjs +77 -0
  198. package/foundation/integrations/autolink.d.mts +2 -1
  199. package/foundation/integrations/autolink.mjs +4 -13
  200. package/foundation/integrations/cli-peer-version.d.mts +36 -0
  201. package/foundation/integrations/cli-peer-version.mjs +132 -0
  202. package/foundation/integrations/cli-peer-version.test.mjs +354 -0
  203. package/foundation/integrations/exports-subpaths.d.mts +57 -0
  204. package/foundation/integrations/exports-subpaths.mjs +113 -0
  205. package/foundation/integrations/integrations.d.mts +20 -1
  206. package/foundation/integrations/integrations.mjs +59 -2
  207. package/foundation/integrations/public-imports.d.mts +118 -0
  208. package/foundation/integrations/public-imports.mjs +1769 -0
  209. package/foundation/integrations/public-imports.test.mjs +1793 -0
  210. package/foundation/integrations/validate-contributions.d.mts +19 -0
  211. package/foundation/integrations/validate-contributions.mjs +172 -3
  212. package/foundation/response/response-types.doc.mjs +1 -1
  213. package/package.json +9 -9
@@ -4,18 +4,20 @@
4
4
  /**
5
5
  * The blocks build names for a request, re-ranked by the picker.
6
6
  * @param {string} request
7
- * @param {{kitBlocks: string[], searched: BlockScore[], foreign: string[]}} signals
7
+ * @param {{kitBlocks: string[], searched: BlockScore[], foreign: string[], blockText: Map<string, string>}} signals
8
8
  * build's signals: the kit's blocks, search's block results [id, score,
9
- * matched, total] in order, and the blocks whose package is not Core's
9
+ * matched, total] in order, the blocks whose package is not Core's, and
10
+ * each searched block's display name and description
10
11
  * @param {BlockPickerWeights} weights
11
- * @returns {string[]} the kit's blocks in a new order: a block the request
12
+ * @returns {string[]} as many blocks as build's own list: a block the request
12
13
  * names, one the weights do not know, or one an integration ships keeps its
13
- * slot; the others, best first, fill the remaining slots
14
+ * slot; the picker's best, from build's blocks and search's, fill the others
14
15
  */
15
16
  export function pickBlocks(request: string, signals: {
16
17
  kitBlocks: string[];
17
18
  searched: BlockScore[];
18
19
  foreign: string[];
20
+ blockText: Map<string, string>;
19
21
  }, weights: BlockPickerWeights): string[];
20
22
  export type BlockPickerWeights = {
21
23
  /**
@@ -16,19 +16,24 @@
16
16
  *
17
17
  * How it scores. The request's words and adjacent word pairs, each weighted
18
18
  * (1 + ln count) x its vocabulary weight and scaled to unit length, give every
19
- * block a linear score (its bias plus the weighted words' rows). Only the
20
- * blocks build already names are re-ranked: never another block, never more.
21
- * Each one's final score combines five numbers: search's score / 100, the
22
- * share of the request search matched, whether the kit names it, 1 / (1 + its
23
- * place in build's list), and the block's log-share of the linear scores.
24
- * Ties keep build's order. A block the request names (its name, or the
25
- * component its name starts with, written out, joined, or plural), a block the
26
- * weights do not know, and a block whose package is not Core's keep the slot
27
- * build gave them; the picker re-orders the rest within the other slots.
19
+ * block a linear score (its bias plus the weighted words' rows). The
20
+ * candidates are build's own blocks and search's first 100 block results
21
+ * (each a block search returned, with its real score and reason); a block
22
+ * build does not name joins only if it shares a content word (the same word
23
+ * up to plural and stem form, as search's sameWord reads it) with the
24
+ * request (its display name and description against the request's words).
25
+ * Each candidate's final score combines five numbers: search's score / 100,
26
+ * the share of the request search matched, whether build names it, 1 / (1 +
27
+ * its place in build's list, then search's), and the block's log-share of the
28
+ * linear scores. Ties keep that order. A block the request names (its name,
29
+ * or the component its name starts with, written out, joined, or plural), a
30
+ * block the weights do not know, and a block whose package is not Core's keep
31
+ * the slot build gave them; the picker's best fill the other slots, so the
32
+ * kit never lists more blocks than build's own.
28
33
  */
29
34
 
30
- import {pluralOf} from '../../search/_query.mjs';
31
- import {wordsOf} from '../../search/_words.mjs';
35
+ import {pluralOf, STOPWORDS} from '../../search/_query.mjs';
36
+ import {sameWord, wordsOf} from '../../search/_words.mjs';
32
37
 
33
38
  /**
34
39
  * @typedef {object} BlockPickerWeights
@@ -100,6 +105,66 @@ function linearScores(request, weights) {
100
105
  return {z, lse: max + Math.log(sum)};
101
106
  }
102
107
 
108
+ /** How many of search's block results are candidates. */
109
+ const SEARCH_POOL = 100;
110
+
111
+ /**
112
+ * Generic English function words (pronouns, relative pronouns, determiners,
113
+ * quantifiers, auxiliaries, conjunctions, prepositions): never a reason to tie
114
+ * a block to a request.
115
+ */
116
+ const FUNCTION_WORDS = (
117
+ 'about above across after again against all along already also although ' +
118
+ 'always am among amongst an and another any anyone anything anywhere are ' +
119
+ 'around as at be because been before behind being below beneath beside ' +
120
+ 'besides between beyond both but by can could despite did do does doing ' +
121
+ 'down during each either else enough even ever every everyone everything ' +
122
+ 'everywhere except few following for from further had has have having he ' +
123
+ 'her here hers herself him himself his how however if in inside instead ' +
124
+ 'into is it its itself just least less little lot lots many may maybe me ' +
125
+ 'might more most much must my myself near neither never no nobody none ' +
126
+ 'nor not nothing now nowhere of off often on once one ones only onto or ' +
127
+ 'other others ought our ours ourselves out outside over own past per ' +
128
+ 'perhaps quite rather same several shall she should since so some someone ' +
129
+ 'something sometimes somewhat somewhere still such than that the their ' +
130
+ 'theirs them themselves then there these they this those though through ' +
131
+ 'throughout thus till to together too toward towards under underneath ' +
132
+ 'unless unlike until up upon usually very via was we were what whatever ' +
133
+ 'when whenever where whereas whether which whichever while who whoever ' +
134
+ 'whom whomever whose why will with within without would yet you your ' +
135
+ 'yours yourself yourselves'
136
+ ).split(' ');
137
+
138
+ /**
139
+ * Words too common to tie a block to a request: search's own stopwords, the
140
+ * function words, and words every build request or block shares.
141
+ */
142
+ const STOP = new Set([
143
+ ...STOPWORDS,
144
+ ...FUNCTION_WORDS,
145
+ ...(
146
+ 'a an the and or of for to in on with by at from as is are be this that ' +
147
+ 'it its my our your their me we you i into via per each all any some page ' +
148
+ 'app screen ui view build make create need want like show shows showing ' +
149
+ 'using use one two new add list item items basic simple showcase example ' +
150
+ 'default variant'
151
+ ).split(' '),
152
+ ]);
153
+
154
+ /**
155
+ * The content words of a text: its lowercase words of three or more letters
156
+ * that are not in {@link STOP}.
157
+ * @param {string} text
158
+ * @returns {string[]}
159
+ */
160
+ function contentWords(text) {
161
+ const words =
162
+ String(text)
163
+ .toLowerCase()
164
+ .match(/[a-z0-9]{3,}/g) ?? [];
165
+ return [...new Set(words.filter(w => !STOP.has(w)))];
166
+ }
167
+
103
168
  /**
104
169
  * Whether a request names a block: a run of one to three adjacent words of
105
170
  * the request, joined, is the block's name, the start of its name that names
@@ -130,13 +195,14 @@ function names(words, block) {
130
195
  /**
131
196
  * The blocks build names for a request, re-ranked by the picker.
132
197
  * @param {string} request
133
- * @param {{kitBlocks: string[], searched: BlockScore[], foreign: string[]}} signals
198
+ * @param {{kitBlocks: string[], searched: BlockScore[], foreign: string[], blockText: Map<string, string>}} signals
134
199
  * build's signals: the kit's blocks, search's block results [id, score,
135
- * matched, total] in order, and the blocks whose package is not Core's
200
+ * matched, total] in order, the blocks whose package is not Core's, and
201
+ * each searched block's display name and description
136
202
  * @param {BlockPickerWeights} weights
137
- * @returns {string[]} the kit's blocks in a new order: a block the request
203
+ * @returns {string[]} as many blocks as build's own list: a block the request
138
204
  * names, one the weights do not know, or one an integration ships keeps its
139
- * slot; the others, best first, fill the remaining slots
205
+ * slot; the picker's best, from build's blocks and search's, fill the others
140
206
  */
141
207
  export function pickBlocks(request, signals, weights) {
142
208
  const own = signals.kitBlocks;
@@ -151,17 +217,34 @@ export function pickBlocks(request, signals, weights) {
151
217
  const keeps = own.map(
152
218
  b => !classIndex.has(b) || foreign.has(b) || names(words, b),
153
219
  );
220
+ /** @type {Map<string, number>} */
221
+ const place = new Map();
222
+ for (const b of [...own, ...signals.searched.map(s => s[0])])
223
+ if (!place.has(b)) place.set(b, place.size);
224
+ const requestWords = contentWords(request);
225
+ // The same word, up to plural and stem form, as search reads it.
226
+ const shares = (/** @type {string} */ b) =>
227
+ contentWords(
228
+ `${wordsOf(b).join(' ')} ${signals.blockText.get(b) ?? ''}`,
229
+ ).some(v => requestWords.some(w => sameWord(w, v)));
230
+ const pool = own.filter((_, k) => !keeps[k]);
231
+ for (const [b] of signals.searched.slice(0, SEARCH_POOL)) {
232
+ if (own.includes(b) || pool.includes(b)) continue;
233
+ if (!classIndex.has(b) || foreign.has(b) || !shares(b)) continue;
234
+ pool.push(b);
235
+ }
154
236
  const [t0, t1, t2, t3, t4] = weights.theta;
155
- const scored = own
156
- .map((b, pos) => ({b, pos}))
157
- .filter((_, k) => !keeps[k])
158
- .map(({b, pos}) => {
237
+ const scored = pool
238
+ .map((b, pos) => {
159
239
  const s = found.get(b);
160
240
  const c = classIndex.get(b);
241
+ const at = place.get(b);
161
242
  const f0 = s ? s[1] / 100 : 0;
162
243
  const f1 = s ? (s[2] ?? 0) / Math.max(s[3] ?? 0, 1) : 0;
244
+ const f2 = own.includes(b) ? 1 : 0;
245
+ const f3 = at === undefined ? 0 : 1 / (1 + at);
163
246
  const f4 = (c === undefined ? UNSCORED : z[c]) - lse;
164
- return {b, pos, s: t0 * f0 + t1 * f1 + t2 + t3 / (1 + pos) + t4 * f4};
247
+ return {b, pos, s: t0 * f0 + t1 * f1 + t2 * f2 + t3 * f3 + t4 * f4};
165
248
  })
166
249
  .sort((a, b) => b.s - a.s || a.pos - b.pos);
167
250
  let next = 0;
@@ -1,10 +1,11 @@
1
1
  // Copyright (c) Meta Platforms, Inc. and affiliates.
2
2
 
3
3
  /**
4
- * @file Unit tests for build's block picker on small hand-written weights: it
5
- * only re-orders the blocks build names (never another block, never more), a
6
- * block the request names keeps its slot, as does a block the weights do not
7
- * know or an integration ships, and ties keep build's order.
4
+ * @file Unit tests for build's block picker on small hand-written weights: its
5
+ * blocks come from build's own list or search's results (never another,
6
+ * never more), a block search returned joins only if it shares a content word
7
+ * with the request, a block the request names keeps its slot, as does a block
8
+ * the weights do not know or an integration ships, and ties keep the order.
8
9
  */
9
10
 
10
11
  import {describe, expect, it} from 'vitest';
@@ -31,6 +32,19 @@ const SEARCHED = [
31
32
  ['CardStat', 40, 1, 3],
32
33
  ];
33
34
 
35
+ /**
36
+ * Two blocks, no words, and a theta that prefers a block build did not name:
37
+ * a block search returned wins the slot whenever the word check lets it in.
38
+ * @type {import('./blocks.mjs').BlockPickerWeights}
39
+ */
40
+ const TIES_ALL = {
41
+ vocab: {},
42
+ classes: ['ListPlain', 'CardStat'],
43
+ bias: [0, 0],
44
+ words: {},
45
+ theta: [0, 0, -1, 0, 0],
46
+ };
47
+
34
48
  /** @type {import('./blocks.mjs').BlockPickerWeights} */
35
49
  const TIES = {
36
50
  vocab: {alpha: 1024},
@@ -44,30 +58,53 @@ const TIES = {
44
58
  * @param {string[]} kitBlocks
45
59
  * @param {string[]} [foreign]
46
60
  */
47
- const signals = (kitBlocks, foreign = []) => ({
61
+ const signals = (kitBlocks, foreign = [], searched = SEARCHED) => ({
48
62
  kitBlocks,
49
- searched: SEARCHED,
63
+ searched,
50
64
  foreign,
65
+ blockText: new Map([
66
+ ['ChartBar', 'Bar chart A bar chart of sales by month.'],
67
+ ['CardStat', 'Stat card A card with one number.'],
68
+ ['ListPlain', 'Plain list A list of rows.'],
69
+ ['TableBasic', 'Basic table A table of rows.'],
70
+ ]),
51
71
  });
52
72
 
53
73
  describe('pickBlocks', () => {
54
- it('re-orders only the blocks build names, never adding one', () => {
74
+ it('names only blocks from build’s list or search’s, as many as build’s', () => {
55
75
  const own = ['TableBasic', 'ListPlain', 'CardStat'];
56
76
  const picks = pickBlocks('sales chart with a row', signals(own), WORDS);
57
- expect([...picks].sort()).toEqual([...own].sort());
58
- expect(picks).toEqual(['CardStat', 'TableBasic', 'ListPlain']);
77
+ const allowed = new Set([...own, ...SEARCHED.map(s => s[0])]);
78
+ expect(picks).toHaveLength(own.length);
79
+ for (const b of picks) expect(allowed.has(b)).toBe(true);
80
+ expect(new Set(picks).size).toBe(picks.length);
59
81
  });
60
82
 
61
- it('never names a block the words score highly that build did not name', () => {
83
+ it('adds a block search returned that shares a word with the request', () => {
62
84
  const picks = pickBlocks(
63
85
  'sales chart chart chart',
64
86
  signals(['TableBasic', 'ListPlain']),
65
87
  WORDS,
66
88
  );
67
- expect(picks).not.toContain('ChartBar');
89
+ expect(picks).toContain('ChartBar');
68
90
  expect(picks).toHaveLength(2);
69
91
  });
70
92
 
93
+ it('never adds a block search did not return, or one sharing no word', () => {
94
+ const searched = /** @type {[string, number, number, number][]} */ ([
95
+ ['TableBasic', 96, 1, 3],
96
+ ['CardStat', 90, 1, 3],
97
+ ]);
98
+ const picks = pickBlocks(
99
+ 'sales chart chart chart',
100
+ signals(['TableBasic', 'ListPlain'], [], searched),
101
+ WORDS,
102
+ );
103
+ expect(picks).not.toContain('ChartBar');
104
+ expect(picks).not.toContain('CardStat');
105
+ expect([...picks].sort()).toEqual(['ListPlain', 'TableBasic']);
106
+ });
107
+
71
108
  it('keeps a block the request names in its slot', () => {
72
109
  const own = ['TableBasic', 'CalendarBasic', 'CardStat'];
73
110
  for (const request of [
@@ -88,13 +125,17 @@ describe('pickBlocks', () => {
88
125
  );
89
126
  expect(picks[1]).toBe('AcmeCallout');
90
127
  expect(picks[3]).toBe('ListPlain');
91
- expect([...picks].sort()).toEqual([...own].sort());
128
+ expect(picks).toHaveLength(own.length);
92
129
  });
93
130
 
94
131
  it('keeps build’s order on ties and when no word is known', () => {
95
132
  const own = ['X', 'Y', 'Z'];
96
133
  expect(
97
- pickBlocks('??', {kitBlocks: own, searched: [], foreign: []}, TIES),
134
+ pickBlocks(
135
+ '??',
136
+ {kitBlocks: own, searched: [], foreign: [], blockText: new Map()},
137
+ TIES,
138
+ ),
98
139
  ).toEqual(own);
99
140
  });
100
141
 
@@ -105,4 +146,76 @@ describe('pickBlocks', () => {
105
146
  pickBlocks('sales chart', signals(own), WORDS);
106
147
  expect(own).toEqual(copy);
107
148
  });
149
+
150
+ it('reads a request word and a block word as the same up to plural and stem', () => {
151
+ for (const [word, blockWord] of [
152
+ ['settings', 'setting'],
153
+ ['statuses', 'status'],
154
+ ['tables', 'table'],
155
+ ['classes', 'class'],
156
+ ['files', 'file'],
157
+ ['categories', 'category'],
158
+ ]) {
159
+ const sig = {
160
+ kitBlocks: ['ListPlain'],
161
+ searched: /** @type {[string, number, number, number][]} */ ([
162
+ ['ListPlain', 96, 1, 3],
163
+ ['CardStat', 90, 1, 3],
164
+ ]),
165
+ foreign: [],
166
+ blockText: new Map([['CardStat', `Stat card. A ${blockWord} panel.`]]),
167
+ };
168
+ expect(pickBlocks(`one ${word} view`, sig, TIES_ALL)).toEqual([
169
+ 'CardStat',
170
+ ]);
171
+ }
172
+ const none = {
173
+ kitBlocks: ['ListPlain'],
174
+ searched: /** @type {[string, number, number, number][]} */ ([
175
+ ['ListPlain', 96, 1, 3],
176
+ ['CardStat', 90, 1, 3],
177
+ ]),
178
+ foreign: [],
179
+ blockText: new Map([['CardStat', 'Stat card. A setting panel.']]),
180
+ };
181
+ expect(pickBlocks('one widget view', none, TIES_ALL)).toEqual([
182
+ 'ListPlain',
183
+ ]);
184
+ });
185
+
186
+ it('never adds a block that shares only a function word with the request', () => {
187
+ const searched = /** @type {[string, number, number, number][]} */ ([
188
+ ['TableBasic', 96, 1, 3],
189
+ ['CardStat', 90, 1, 3],
190
+ ]);
191
+ const sig = {
192
+ kitBlocks: ['TableBasic', 'ListPlain'],
193
+ searched,
194
+ foreign: [],
195
+ blockText: new Map([['CardStat', 'Stat card Each card under a header.']]),
196
+ };
197
+ const picks = pickBlocks('each row under which', sig, WORDS);
198
+ expect(picks).not.toContain('CardStat');
199
+ });
200
+ it('never adds a block for a function word alone', () => {
201
+ const sig = {
202
+ kitBlocks: ['ListPlain'],
203
+ searched: /** @type {[string, number, number, number][]} */ ([
204
+ ['ListPlain', 96, 1, 3],
205
+ ['CardStat', 90, 1, 3],
206
+ ]),
207
+ foreign: [],
208
+ blockText: new Map([
209
+ [
210
+ 'CardStat',
211
+ 'Shows which one is done. Each, every, any, some, all, both, either, neither, few, many, much, several, such, that, who, whom, whose.',
212
+ ],
213
+ ]),
214
+ };
215
+ for (const w of 'which that who whom whose each every any some all both either neither few many much several such'.split(
216
+ ' ',
217
+ ))
218
+ expect(pickBlocks(`a ${w} b`, sig, TIES_ALL)).toEqual(['ListPlain']);
219
+ expect(pickBlocks('each, which', sig, TIES_ALL)).toEqual(['ListPlain']);
220
+ });
108
221
  });