|
| 1 | +import { |
| 2 | + Command, |
| 3 | + TestCaseFixtureLegacy, |
| 4 | + getRecordedTestPaths, |
| 5 | + getRecordedTestsDirPath, |
| 6 | +} from "@cursorless/common"; |
| 7 | +import * as yaml from "js-yaml"; |
| 8 | +import * as fs from "node:fs"; |
| 9 | +import { promises as fsp } from "node:fs"; |
| 10 | +import * as path from "node:path"; |
| 11 | +import * as assert from "node:assert"; |
| 12 | +import { canonicalizeSpokenFormTestCommand } from "../core/commandVersionUpgrades/canonicalizeSpokenFormTestCommand"; |
| 13 | + |
| 14 | +suite.skip("Generate spoken forms", async function () { |
| 15 | + const relativeDir = path.dirname(getRecordedTestsDirPath()); |
| 16 | + |
| 17 | + getRecordedTestPaths().forEach((testPath) => |
| 18 | + test(path.relative(relativeDir, testPath.split(".")[0]), () => |
| 19 | + runTest(testPath), |
| 20 | + ), |
| 21 | + ); |
| 22 | +}); |
| 23 | + |
| 24 | +async function runTest(file: string) { |
| 25 | + const buffer = await fsp.readFile(file); |
| 26 | + const fixture = yaml.load(buffer.toString()) as TestCaseFixtureLegacy; |
| 27 | + const spokenFormCommand = canonicalizeSpokenFormTestCommand(fixture.command); |
| 28 | + |
| 29 | + if (spokenFormCommand == null) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + const suffix = getHatTokenMapSuffix(file, fixture.command); |
| 34 | + const spokenForm = spokenFormCommand.spokenForm + suffix; |
| 35 | + |
| 36 | + assert.equal(fixture.command.spokenForm, spokenForm); |
| 37 | +} |
| 38 | + |
| 39 | +function getHatTokenMapSuffix(file: string, command: Command): string { |
| 40 | + if (command.spokenForm == null || !isHatTokenMapTest(file)) { |
| 41 | + return ""; |
| 42 | + } |
| 43 | + |
| 44 | + const originalComponents = command.spokenForm.split(" "); |
| 45 | + if ( |
| 46 | + originalComponents.length > 2 && |
| 47 | + originalComponents[originalComponents.length - 2] === "take" |
| 48 | + ) { |
| 49 | + return ( |
| 50 | + " " + originalComponents.slice(originalComponents.length - 2).join(" ") |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + return ""; |
| 55 | +} |
| 56 | + |
| 57 | +async function isHatTokenMapTest(file: string): Promise<boolean> { |
| 58 | + const dir = path.dirname(file); |
| 59 | + const configFile = path.join(dir, "config.json"); |
| 60 | + if (fs.existsSync(configFile)) { |
| 61 | + const buffer = await fsp.readFile(file, { encoding: "utf-8" }); |
| 62 | + const config = JSON.parse(buffer); |
| 63 | + return Boolean(config["isHatTokenMapTest"]); |
| 64 | + } |
| 65 | + return false; |
| 66 | +} |
0 commit comments