|
| 1 | +/* eslint-disable @typescript-eslint/no-non-null-assertion */ |
| 2 | +import { describe, expect, it } from '@jest/globals'; |
| 3 | +import { AACTree, AACPage, AACButton } from '../src/core/treeStructure'; |
| 4 | +import { MetricsCalculator } from '../src/utilities/analytics/metrics/core'; |
| 5 | +import { EFFORT_CONSTANTS, visualScanEffort } from '../src/utilities/analytics/metrics/effort'; |
| 6 | + |
| 7 | +function buildTreeWithPredictions( |
| 8 | + predictions: string[], |
| 9 | + parametersPredictions?: string[], |
| 10 | + pos?: string |
| 11 | +): { tree: AACTree; btnId: string } { |
| 12 | + const tree = new AACTree(); |
| 13 | + const page = new AACPage({ |
| 14 | + id: 'root', |
| 15 | + name: 'Home', |
| 16 | + grid: { columns: 2, rows: 2 }, |
| 17 | + }); |
| 18 | + |
| 19 | + const btn = new AACButton({ |
| 20 | + id: 'some_btn', |
| 21 | + label: 'some', |
| 22 | + type: 'SPEAK', |
| 23 | + x: 0, |
| 24 | + y: 0, |
| 25 | + predictions, |
| 26 | + pos, |
| 27 | + parameters: parametersPredictions ? { predictions: parametersPredictions } : undefined, |
| 28 | + }); |
| 29 | + |
| 30 | + page.grid[0][0] = btn; |
| 31 | + page.addButton(btn); |
| 32 | + tree.addPage(page); |
| 33 | + tree.rootId = 'root'; |
| 34 | + |
| 35 | + return { tree, btnId: btn.id }; |
| 36 | +} |
| 37 | + |
| 38 | +describe('Suggest Words effort cost', () => { |
| 39 | + it('adds confirmation cost to Suggest Words word forms', () => { |
| 40 | + const suggestWords = ['something', 'someone', 'somewhere']; |
| 41 | + const { tree } = buildTreeWithPredictions(suggestWords, suggestWords); |
| 42 | + |
| 43 | + const calculator = new MetricsCalculator(); |
| 44 | + const result = calculator.analyze(tree, { useSmartGrammar: true }); |
| 45 | + |
| 46 | + const parentBtn = result.buttons.find((b) => b.label === 'some'); |
| 47 | + expect(parentBtn).toBeDefined(); |
| 48 | + |
| 49 | + const something = result.buttons.find((b) => b.label === 'something'); |
| 50 | + expect(something).toBeDefined(); |
| 51 | + expect(something!.is_word_form).toBe(true); |
| 52 | + expect(something!.is_suggest_words).toBe(true); |
| 53 | + |
| 54 | + const expectedEffort = |
| 55 | + parentBtn!.effort + visualScanEffort(0) + EFFORT_CONSTANTS.SUGGEST_WORDS_SELECTION_EFFORT; |
| 56 | + expect(something!.effort).toBeCloseTo(expectedEffort, 4); |
| 57 | + }); |
| 58 | + |
| 59 | + it('does not add confirmation cost to morphology word forms', () => { |
| 60 | + const predictions = ['goes', 'going', 'went']; |
| 61 | + const { tree } = buildTreeWithPredictions(predictions, undefined, 'Verb'); |
| 62 | + |
| 63 | + const calculator = new MetricsCalculator(); |
| 64 | + const result = calculator.analyze(tree, { useSmartGrammar: true }); |
| 65 | + |
| 66 | + const parentBtn = result.buttons.find((b) => b.label === 'some'); |
| 67 | + expect(parentBtn).toBeDefined(); |
| 68 | + |
| 69 | + const goes = result.buttons.find((b) => b.label === 'goes'); |
| 70 | + expect(goes).toBeDefined(); |
| 71 | + expect(goes!.is_word_form).toBe(true); |
| 72 | + expect(goes!.is_suggest_words).toBeUndefined(); |
| 73 | + |
| 74 | + expect(goes!.effort).toBeCloseTo(parentBtn!.effort + visualScanEffort(0), 4); |
| 75 | + }); |
| 76 | + |
| 77 | + it('only adds confirmation to Suggest Words forms when predictions are mixed', () => { |
| 78 | + const suggestWordsOriginals = ['something', 'someone']; |
| 79 | + const allPredictions = ['something', 'someone', 'somes']; |
| 80 | + const { tree } = buildTreeWithPredictions(allPredictions, suggestWordsOriginals, 'Noun'); |
| 81 | + |
| 82 | + const calculator = new MetricsCalculator(); |
| 83 | + const result = calculator.analyze(tree, { useSmartGrammar: true }); |
| 84 | + |
| 85 | + const parentBtn = result.buttons.find((b) => b.label === 'some'); |
| 86 | + |
| 87 | + const something = result.buttons.find((b) => b.label === 'something'); |
| 88 | + expect(something).toBeDefined(); |
| 89 | + expect(something!.is_suggest_words).toBe(true); |
| 90 | + expect(something!.effort).toBeCloseTo( |
| 91 | + parentBtn!.effort + visualScanEffort(0) + EFFORT_CONSTANTS.SUGGEST_WORDS_SELECTION_EFFORT, |
| 92 | + 4 |
| 93 | + ); |
| 94 | + |
| 95 | + // "somes" is at index 2 → predictionPriorItems = 2 |
| 96 | + const somes = result.buttons.find((b) => b.label === 'somes'); |
| 97 | + expect(somes).toBeDefined(); |
| 98 | + expect(somes!.is_suggest_words).toBeUndefined(); |
| 99 | + expect(somes!.effort).toBeCloseTo(parentBtn!.effort + visualScanEffort(2), 4); |
| 100 | + }); |
| 101 | + |
| 102 | + it('has no confirmation when parameters.predictions is absent', () => { |
| 103 | + const predictions = ['something', 'someone']; |
| 104 | + const { tree } = buildTreeWithPredictions(predictions, undefined, 'Noun'); |
| 105 | + |
| 106 | + const calculator = new MetricsCalculator(); |
| 107 | + const result = calculator.analyze(tree, { useSmartGrammar: true }); |
| 108 | + |
| 109 | + const parentBtn = result.buttons.find((b) => b.label === 'some'); |
| 110 | + |
| 111 | + const something = result.buttons.find((b) => b.label === 'something'); |
| 112 | + expect(something).toBeDefined(); |
| 113 | + expect(something!.is_suggest_words).toBeUndefined(); |
| 114 | + expect(something!.effort).toBeCloseTo(parentBtn!.effort + visualScanEffort(0), 4); |
| 115 | + }); |
| 116 | + |
| 117 | + it('SUGGEST_WORDS_SELECTION_EFFORT is between 0.5 and 1.0', () => { |
| 118 | + expect(EFFORT_CONSTANTS.SUGGEST_WORDS_SELECTION_EFFORT).toBeGreaterThanOrEqual(0.5); |
| 119 | + expect(EFFORT_CONSTANTS.SUGGEST_WORDS_SELECTION_EFFORT).toBeLessThanOrEqual(1.0); |
| 120 | + }); |
| 121 | +}); |
0 commit comments