Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Windows speech helpers: exercise them in Windows PowerShell on the Windows shards

milind-sonie83459a420e2
diff --git a/electron/speech-helper-windows.test.mjs b/electron/speech-helper-windows.test.mjs
index bddb669030..a7deaaeaad 100644
--- a/electron/speech-helper-windows.test.mjs
+++ b/electron/speech-helper-windows.test.mjs
@@ -1,6 +1,8 @@
// The Windows speech helpers run in Windows PowerShell 5.1 (powershell.exe): on Windows these tests use it, elsewhere
// PowerShell 7 (pwsh) when it is installed.
import { execFileSync, spawnSync } from "node:child_process";
+import { copyFileSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
@@ -43,3 +45,55 @@ describe.skipIf(!found)("Windows speech helper scripts", () => {
expect(result.stdout).toBe("");
}, 30_000);
});
+
+// What a Windows runner can check without a microphone or a speech pack: the newer speech's types load in Windows
+// PowerShell 5.1, the desktop helper hands over in the same session, and the newer helper never hangs or writes when it
+// cannot listen.
+describe.skipIf(!found || process.platform !== "win32")("Windows speech helpers in Windows PowerShell", () => {
+ it("the newer speech finds both task helpers wherever its Windows types load", () => {
+ const answer = run([
+ `$errors = $null; $ast = ${parse(ONECORE)}`,
+ "try { Add-Type -AssemblyName System.Runtime.WindowsRuntime; $null = [Windows.Media.SpeechRecognition.SpeechRecognizer, Windows.Media.SpeechRecognition, ContentType = WindowsRuntime] } catch { 'no-winrt'; exit 0 }",
+ "foreach ($name in 'asTask', 'asTaskAction', 'asTaskOperation') { $stmt = $ast.Find({ param($node) $node -is [System.Management.Automation.Language.AssignmentStatementAst] -and $node.Left.VariablePath.UserPath -eq $name }, $true); . ([ScriptBlock]::Create($stmt.Extent.Text)) }",
+ "\"$($null -ne $asTaskAction) $($null -ne $asTaskOperation)\"",
+ ].join("; "));
+ expect(["True True", "no-winrt"]).toContain(answer);
+ }, 60_000);
+
+ it("the desktop helper hands a language it has no recognizer for to the newer speech in its own session", () => {
+ const has = run([
+ "Add-Type -AssemblyName System.Speech",
+ "try { $installed = @([System.Speech.Recognition.SpeechRecognitionEngine]::InstalledRecognizers()) } catch { $installed = @() }",
+ "$culture = [System.Globalization.CultureInfo]::CurrentUICulture",
+ "$match = @($installed | Where-Object { $_.Culture.TwoLetterISOLanguageName -eq $culture.TwoLetterISOLanguageName })",
+ "\"$($match.Count) $($installed.Count)\"",
+ ].join("; "));
+ const [matching, installed] = has.split(" ").map(Number);
+ if (matching > 0) return; // this runner's display language has a desktop recognizer: the hand-over never happens
+ const dir = mkdtempSync(path.join(os.tmpdir(), "omb-speech-"));
+ try {
+ copyFileSync(CLASSIC, path.join(dir, "speech-helper.ps1"));
+ const helper = (stub) => {
+ writeFileSync(path.join(dir, "speech-helper-onecore.ps1"), stub);
+ return spawnSync(shell, [...ARGS, "-ExecutionPolicy", "Bypass", "-File", path.join(dir, "speech-helper.ps1")], { encoding: "utf8", input: "", timeout: 60_000 });
+ };
+ const heard = helper("param([int]$EndpointMs = 0)\nSend-Line @{ partial = $false; text = (Join-Text 'from' 'onecore') }\nexit 0\n");
+ expect(heard.status).toBe(0);
+ expect(heard.stdout.trim()).toBe(JSON.stringify({ partial: false, text: "from onecore" }));
+ if (installed === 0) {
+ const missing = helper("param([int]$EndpointMs = 0)\nexit 3\n");
+ expect(missing.status).toBe(1);
+ expect(missing.stdout.trim()).toBe(JSON.stringify({ error: "speech-language-missing" }));
+ }
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ }, 120_000);
+
+ it("the newer speech ends by itself with nothing written when it cannot listen or its input closes", () => {
+ const result = spawnSync(shell, [...ARGS, "-ExecutionPolicy", "Bypass", "-File", ONECORE], { encoding: "utf8", input: "", timeout: 90_000 });
+ expect(result.error).toBeUndefined();
+ expect([0, 3]).toContain(result.status);
+ expect(result.stdout).toBe("");
+ }, 120_000);
+});