|
| 1 | +import { getCursorlessApi } from "@cursorless/vscode-common"; |
| 2 | +import { LATEST_VERSION, ScopeTypeInfo, sleep } from "@cursorless/common"; |
| 3 | +import * as sinon from "sinon"; |
| 4 | +import { |
| 5 | + assertCalled, |
| 6 | + assertCalledWithScopeInfo, |
| 7 | +} from "./assertCalledWithScopeInfo"; |
| 8 | +import { stat, unlink, writeFile } from "fs/promises"; |
| 9 | +import { sleepWithBackoff } from "../../endToEndTestSetup"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Tests that the scope provider correctly reports custom spoken forms |
| 13 | + */ |
| 14 | +export async function runCustomSpokenFormScopeInfoTest() { |
| 15 | + const { scopeProvider, spokenFormsJsonPath } = (await getCursorlessApi()) |
| 16 | + .testHelpers!; |
| 17 | + const fake = sinon.fake<[scopeInfos: ScopeTypeInfo[]], void>(); |
| 18 | + |
| 19 | + const disposable = scopeProvider.onDidChangeScopeInfo(fake); |
| 20 | + |
| 21 | + try { |
| 22 | + await assertCalled( |
| 23 | + fake, |
| 24 | + [ |
| 25 | + roundStandard, |
| 26 | + namedFunctionStandard, |
| 27 | + lambdaStandard, |
| 28 | + statementStandard, |
| 29 | + squareStandard, |
| 30 | + subjectStandard, |
| 31 | + ], |
| 32 | + [], |
| 33 | + ); |
| 34 | + |
| 35 | + await writeFile( |
| 36 | + spokenFormsJsonPath, |
| 37 | + JSON.stringify(spokenFormJsonContents), |
| 38 | + ); |
| 39 | + await sleepWithBackoff(50); |
| 40 | + await assertCalledWithScopeInfo( |
| 41 | + fake, |
| 42 | + subjectCustom, |
| 43 | + roundCustom, |
| 44 | + namedFunctionCustom, |
| 45 | + lambdaCustom, |
| 46 | + statementMissing, |
| 47 | + squareMissing, |
| 48 | + ); |
| 49 | + |
| 50 | + await unlink(spokenFormsJsonPath); |
| 51 | + await sleepWithBackoff(50); |
| 52 | + await assertCalled( |
| 53 | + fake, |
| 54 | + [ |
| 55 | + roundStandard, |
| 56 | + namedFunctionStandard, |
| 57 | + lambdaStandard, |
| 58 | + statementStandard, |
| 59 | + squareStandard, |
| 60 | + subjectStandard, |
| 61 | + ], |
| 62 | + [], |
| 63 | + ); |
| 64 | + } finally { |
| 65 | + disposable.dispose(); |
| 66 | + |
| 67 | + // Delete spokenFormsJsonPath if it exists |
| 68 | + try { |
| 69 | + await stat(spokenFormsJsonPath); |
| 70 | + await unlink(spokenFormsJsonPath); |
| 71 | + // Sleep to ensure that the scope support provider has time to update |
| 72 | + // before the next test starts |
| 73 | + await sleep(250); |
| 74 | + } catch (e) { |
| 75 | + // Do nothing |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +const spokenFormJsonContents = { |
| 81 | + version: LATEST_VERSION, |
| 82 | + entries: [ |
| 83 | + { |
| 84 | + type: "pairedDelimiter", |
| 85 | + id: "parentheses", |
| 86 | + spokenForms: ["custom round", "alternate custom round"], |
| 87 | + }, |
| 88 | + { |
| 89 | + type: "simpleScopeTypeType", |
| 90 | + id: "switchStatementSubject", |
| 91 | + spokenForms: ["custom subject"], |
| 92 | + }, |
| 93 | + { |
| 94 | + type: "simpleScopeTypeType", |
| 95 | + id: "namedFunction", |
| 96 | + spokenForms: ["custom funk"], |
| 97 | + }, |
| 98 | + { |
| 99 | + type: "simpleScopeTypeType", |
| 100 | + id: "anonymousFunction", |
| 101 | + spokenForms: [], |
| 102 | + }, |
| 103 | + ], |
| 104 | +}; |
| 105 | + |
| 106 | +const subjectStandard: ScopeTypeInfo = { |
| 107 | + humanReadableName: "switch statement subject", |
| 108 | + isLanguageSpecific: true, |
| 109 | + scopeType: { type: "switchStatementSubject" }, |
| 110 | + spokenForm: { |
| 111 | + isSecret: true, |
| 112 | + reason: |
| 113 | + "simple scope type type with id switchStatementSubject; please see https://www.cursorless.org/docs/user/customization/ for more information", |
| 114 | + requiresTalonUpdate: false, |
| 115 | + type: "error", |
| 116 | + }, |
| 117 | +}; |
| 118 | + |
| 119 | +const subjectCustom: ScopeTypeInfo = { |
| 120 | + humanReadableName: "switch statement subject", |
| 121 | + isLanguageSpecific: true, |
| 122 | + scopeType: { type: "switchStatementSubject" }, |
| 123 | + spokenForm: { |
| 124 | + alternatives: [], |
| 125 | + preferred: "custom subject", |
| 126 | + type: "success", |
| 127 | + }, |
| 128 | +}; |
| 129 | + |
| 130 | +const roundStandard: ScopeTypeInfo = { |
| 131 | + humanReadableName: "Matching pair of parentheses", |
| 132 | + isLanguageSpecific: false, |
| 133 | + scopeType: { type: "surroundingPair", delimiter: "parentheses" }, |
| 134 | + spokenForm: { |
| 135 | + alternatives: [], |
| 136 | + preferred: "round", |
| 137 | + type: "success", |
| 138 | + }, |
| 139 | +}; |
| 140 | + |
| 141 | +const roundCustom: ScopeTypeInfo = { |
| 142 | + humanReadableName: "Matching pair of parentheses", |
| 143 | + isLanguageSpecific: false, |
| 144 | + scopeType: { type: "surroundingPair", delimiter: "parentheses" }, |
| 145 | + spokenForm: { |
| 146 | + alternatives: ["alternate custom round"], |
| 147 | + preferred: "custom round", |
| 148 | + type: "success", |
| 149 | + }, |
| 150 | +}; |
| 151 | + |
| 152 | +const squareStandard: ScopeTypeInfo = { |
| 153 | + humanReadableName: "Matching pair of square brackets", |
| 154 | + isLanguageSpecific: false, |
| 155 | + scopeType: { type: "surroundingPair", delimiter: "squareBrackets" }, |
| 156 | + spokenForm: { |
| 157 | + alternatives: [], |
| 158 | + preferred: "box", |
| 159 | + type: "success", |
| 160 | + }, |
| 161 | +}; |
| 162 | + |
| 163 | +const squareMissing: ScopeTypeInfo = { |
| 164 | + humanReadableName: "Matching pair of square brackets", |
| 165 | + isLanguageSpecific: false, |
| 166 | + scopeType: { type: "surroundingPair", delimiter: "squareBrackets" }, |
| 167 | + spokenForm: { |
| 168 | + isSecret: false, |
| 169 | + reason: |
| 170 | + "paired delimiter with id squareBrackets; please see https://www.cursorless.org/docs/user/customization/ for more information", |
| 171 | + requiresTalonUpdate: true, |
| 172 | + type: "error", |
| 173 | + }, |
| 174 | +}; |
| 175 | + |
| 176 | +const namedFunctionStandard: ScopeTypeInfo = { |
| 177 | + humanReadableName: "named function", |
| 178 | + isLanguageSpecific: true, |
| 179 | + scopeType: { type: "namedFunction" }, |
| 180 | + spokenForm: { |
| 181 | + alternatives: [], |
| 182 | + preferred: "funk", |
| 183 | + type: "success", |
| 184 | + }, |
| 185 | +}; |
| 186 | + |
| 187 | +const namedFunctionCustom: ScopeTypeInfo = { |
| 188 | + humanReadableName: "named function", |
| 189 | + isLanguageSpecific: true, |
| 190 | + scopeType: { type: "namedFunction" }, |
| 191 | + spokenForm: { |
| 192 | + alternatives: [], |
| 193 | + preferred: "custom funk", |
| 194 | + type: "success", |
| 195 | + }, |
| 196 | +}; |
| 197 | + |
| 198 | +const lambdaStandard: ScopeTypeInfo = { |
| 199 | + humanReadableName: "anonymous function", |
| 200 | + isLanguageSpecific: true, |
| 201 | + scopeType: { type: "anonymousFunction" }, |
| 202 | + spokenForm: { |
| 203 | + alternatives: [], |
| 204 | + preferred: "lambda", |
| 205 | + type: "success", |
| 206 | + }, |
| 207 | +}; |
| 208 | + |
| 209 | +const lambdaCustom: ScopeTypeInfo = { |
| 210 | + humanReadableName: "anonymous function", |
| 211 | + isLanguageSpecific: true, |
| 212 | + scopeType: { type: "anonymousFunction" }, |
| 213 | + spokenForm: { |
| 214 | + isSecret: false, |
| 215 | + reason: |
| 216 | + "simple scope type type with id anonymousFunction; please see https://www.cursorless.org/docs/user/customization/ for more information", |
| 217 | + requiresTalonUpdate: false, |
| 218 | + type: "error", |
| 219 | + }, |
| 220 | +}; |
| 221 | + |
| 222 | +const statementStandard: ScopeTypeInfo = { |
| 223 | + humanReadableName: "statement", |
| 224 | + isLanguageSpecific: true, |
| 225 | + scopeType: { type: "statement" }, |
| 226 | + spokenForm: { |
| 227 | + alternatives: [], |
| 228 | + preferred: "state", |
| 229 | + type: "success", |
| 230 | + }, |
| 231 | +}; |
| 232 | + |
| 233 | +const statementMissing: ScopeTypeInfo = { |
| 234 | + humanReadableName: "statement", |
| 235 | + isLanguageSpecific: true, |
| 236 | + scopeType: { type: "statement" }, |
| 237 | + spokenForm: { |
| 238 | + isSecret: false, |
| 239 | + reason: |
| 240 | + "simple scope type type with id statement; please see https://www.cursorless.org/docs/user/customization/ for more information", |
| 241 | + requiresTalonUpdate: true, |
| 242 | + type: "error", |
| 243 | + }, |
| 244 | +}; |
0 commit comments