Skip to content

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

fix(speech): create the owner lifeline without blocking Electron

Sebastian Bcf8e464af32e
diff --git a/electron/speech-owner.node-test.mjs b/electron/speech-owner.node-test.mjs
index a7035635ae..e83a2237c1 100644
--- a/electron/speech-owner.node-test.mjs
+++ b/electron/speech-owner.node-test.mjs
@@ -14,10 +14,31 @@ const ownerSource = `
if (specifier === 'electron') return { shortCircuit: true, url: moduleURL(
'export const app = { isPackaged: true, getPath: () => process.argv[1] };') };
if (specifier === 'node:child_process') return { shortCircuit: true, url: moduleURL(\`
- export { execFileSync } from 'node:child_process';
+ import { execFile as run } from 'node:child_process';
+ export function execFile(file, args, options, callback) {
+ if (process.argv[2] === 'create-throw') throw new Error('mkfifo failed');
+ if (!process.argv[2].startsWith('pending')) return run(file, args, options, callback);
+ globalThis.pendingCreates ??= [];
+ if (process.argv[2].includes('created')) {
+ return run(file, args, options, (...result) => {
+ globalThis.pendingCreates.push(done => { callback(...result); done(); });
+ process.send({ type: 'pending', file: args.at(-1) });
+ });
+ }
+ globalThis.pendingCreates.push(done => {
+ if (process.argv[2].startsWith('pending-error')) {
+ callback(new Error('mkfifo failed'));
+ done();
+ } else {
+ run(file, args, options, (...result) => { callback(...result); done(); });
+ }
+ });
+ process.send({ type: 'pending', file: args.at(-1) });
+ }
import { EventEmitter } from 'node:events';
export function spawn(file, args, options) {
if (process.argv[2] === 'throw') throw new Error('launch failed');
+ globalThis.launches += 1;
globalThis.speechProc = new EventEmitter();
process.send({ type: 'launch', file, args, options });
return globalThis.speechProc;
@@ -29,15 +50,38 @@ const ownerSource = `
Object.defineProperty(process, 'platform', { value: 'darwin' });
process.resourcesPath = '/synthetic-speech-resources';
const speech = await import(${JSON.stringify(new URL('./speech.mjs', import.meta.url).href)});
- const sink = { transcript() {}, end(info) { process.send({ type: 'end', info }); }, replaced() {} };
+ const { EventEmitter } = await import('node:events');
+ const ends = [];
+ let replaced = 0;
+ let destroyed = false;
+ globalThis.launches = 0;
+ const sink = { transcript() {}, end(info) { ends.push(info); process.send({ type: 'end', info }); }, replaced() { replaced += 1; } };
+ const contents = Object.assign(new EventEmitter(), { send(channel, info) { if (channel === 'speech:end') sink.end(info); } });
+ const win = Object.assign(new EventEmitter(), { isDestroyed: () => destroyed });
+ Object.defineProperty(win, 'webContents', { get() { if (destroyed) throw new Error('Object has been destroyed'); return contents; } });
+ const target = process.argv[2].includes('window') ? win : sink;
+ const status = command => process.send({ type: 'done', command, busy: speech.speechBusy(), launches: globalThis.launches, ends, replaced,
+ listeners: [win.listenerCount('closed'), contents.listenerCount('did-navigate'), contents.listenerCount('render-process-gone')] });
process.on('message', command => {
+ if (command === 'resume') {
+ globalThis.pendingCreates.shift()(() => status(command));
+ return;
+ }
if (command === 'close') globalThis.speechProc.emit('close', 0);
if (command === 'error') globalThis.speechProc.emit('error', new Error('launch failed'));
if (command === 'stop') speech.stopSpeech();
if (command === 'finish') speech.finishSpeech();
- process.send({ type: 'done', command });
+ if (command === 'owner-stop') speech.stopSpeech(target);
+ if (command === 'owner-finish') speech.finishSpeech(target);
+ if (command === 'other-stop') speech.stopSpeech({});
+ if (command === 'other-finish') speech.finishSpeech({});
+ if (command === 'replace') speech.startSpeech(sink);
+ if (command === 'window-close') { destroyed = true; win.emit('closed'); }
+ if (command === 'navigate') contents.emit('did-navigate');
+ if (command === 'renderer-gone') contents.emit('render-process-gone');
+ status(command);
});
- speech.startSpeech(sink);
+ speech.startSpeech(target);
`;
async function owner(t, mode = "ok") {
@@ -46,13 +90,27 @@ async function owner(t, mode = "ok") {
execArgv: ["--input-type=module"], silent: true,
});
const exit = once(proc, "exit");
+ const messages = [];
+ let waiting;
+ proc.on("message", message => {
+ if (waiting) { const resolve = waiting; waiting = null; resolve(message); }
+ else messages.push(message);
+ });
+ const next = () => messages.length ? Promise.resolve(messages.shift()) : new Promise(resolve => { waiting = resolve; });
+ const command = async name => {
+ proc.send(name);
+ let response;
+ do { response = await next(); } while (response.type !== "done");
+ assert.equal(response.command, name);
+ return response;
+ };
t.after(async () => {
if (proc.exitCode === null && proc.signalCode === null) proc.kill("SIGKILL");
await exit;
rmSync(dir, { recursive: true, force: true });
});
- const [message] = await once(proc, "message");
- return { proc, dir, message, exit };
+ const message = await next();
+ return { proc, dir, message, exit, next, command };
}
function readLifeline(t, message) {
@@ -80,32 +138,130 @@ test("a speech helper's lifeline reaches EOF when its Electron owner is SIGKILLe
for (const command of ["close", "error"]) {
test(`speech ${command} closes the lifeline and removes the session`, options, async t => {
- const { proc, dir, message } = await owner(t);
+ const { dir, message, command: send } = await owner(t);
const read = readLifeline(t, message);
- proc.send(command);
- await once(proc, "message");
+ const ended = await send(command);
+ assert.deepEqual(ended.ends, [{ code: 1, reason: command === "error" ? "helper-start-failed" : "helper-exited" }]);
+ assert.deepEqual((await send("close")).ends, ended.ends);
assert.equal(read(), 0);
assert.deepEqual(readdirSync(dir), []);
});
}
test("stop and finish retain their markers while the owner lifeline stays open", options, async t => {
- const { proc, message } = await owner(t);
+ const { message, command } = await owner(t);
const read = readLifeline(t, message);
const flag = name => message.args[message.args.indexOf(name) + 1];
- proc.send("finish");
- await once(proc, "message");
+ await command("finish");
assert.equal(readFileSync(flag("--finish-file"), "utf8"), "finish");
assert.equal(existsSync(flag("--stop-file")), false);
assert.throws(read, { code: "EAGAIN" });
- proc.send("stop");
- await once(proc, "message");
+ await command("stop");
assert.equal(readFileSync(flag("--stop-file"), "utf8"), "stop");
assert.throws(read, { code: "EAGAIN" });
});
-test("a synchronous launch failure removes the session and its lifeline", options, async t => {
- const { dir, message } = await owner(t, "throw");
- assert.deepEqual(message, { type: "end", info: { code: 1, reason: "helper-start-failed" } });
- assert.deepEqual(readdirSync(dir), []);
+for (const failure of ["throw", "create-throw"]) {
+ test(`a synchronous ${failure} failure removes the session and its lifeline`, options, async t => {
+ const { dir, message, command } = await owner(t, failure);
+ assert.deepEqual(message, { type: "end", info: { code: 1, reason: "helper-start-failed" } });
+ assert.deepEqual(readdirSync(dir), []);
+ assert.equal((await command("status")).busy, false);
+ });
+}
+
+for (const stop of ["stop", "owner-stop", "window-close", "navigate", "renderer-gone"]) {
+ test(`${stop} cancels a pending FIFO without launching a helper or ending the sink`, options, async t => {
+ const session = await owner(t, "pending-window");
+ assert.equal(session.message.type, "pending");
+ const pending = await session.command("status");
+ assert.equal(pending.busy, true);
+ assert.deepEqual(pending.listeners, [1, 1, 1]);
+ const stopped = await session.command(stop);
+ assert.equal(stopped.busy, false);
+ assert.deepEqual(stopped.listeners, [0, 0, 0]);
+ assert.deepEqual(readdirSync(session.dir), []);
+ const resumed = await session.command("resume");
+ assert.equal(resumed.launches, 0);
+ assert.deepEqual(resumed.ends, []);
+ assert.deepEqual(readdirSync(session.dir), []);
+ });
+}
+
+test("a pending session ignores other owners and retains its owner's finish marker for launch", options, async t => {
+ const session = await owner(t, "pending");
+ assert.equal(session.message.type, "pending");
+ const dir = path.dirname(session.message.file);
+ for (const command of ["other-stop", "other-finish"]) {
+ assert.equal((await session.command(command)).busy, true);
+ }
+ assert.equal(existsSync(path.join(dir, "stop")), false);
+ assert.equal(existsSync(path.join(dir, "finish")), false);
+ assert.equal((await session.command("owner-finish")).busy, true);
+ assert.equal(readFileSync(path.join(dir, "finish"), "utf8"), "finish");
+ session.proc.send("resume");
+ const launched = await session.next();
+ const read = readLifeline(t, launched);
+ assert.throws(read, { code: "EAGAIN" });
+ const ready = await session.next();
+ assert.equal(ready.type, "done");
+ assert.equal(ready.busy, true);
+ assert.equal(readFileSync(path.join(dir, "finish"), "utf8"), "finish");
+ const ended = await session.command("close");
+ assert.deepEqual(ended.ends, [{ code: 1, reason: "helper-exited" }]);
+ assert.equal(read(), 0);
+ assert.deepEqual(readdirSync(session.dir), []);
+});
+
+test("replacing a pending session never launches or ends it when its FIFO completes late", options, async t => {
+ const session = await owner(t, "pending-created");
+ assert.equal(session.message.type, "pending");
+ assert.equal(statSync(session.message.file).isFIFO(), true);
+ const previous = path.dirname(session.message.file);
+ const replaced = await session.command("replace");
+ assert.equal(replaced.replaced, 1);
+ assert.equal(replaced.busy, true);
+ assert.equal(existsSync(previous), false);
+ const replacement = await session.next();
+ assert.equal(replacement.type, "pending");
+ assert.notEqual(replacement.file, session.message.file);
+ const stale = await session.command("resume");
+ assert.equal(stale.launches, 0);
+ assert.equal(stale.busy, true);
+ assert.deepEqual(stale.ends, []);
+ session.proc.send("resume");
+ const launched = await session.next();
+ const read = readLifeline(t, launched);
+ const ready = await session.next();
+ assert.equal(ready.launches, 1);
+ assert.equal(ready.busy, true);
+ await session.command("close");
+ assert.equal(read(), 0);
+ assert.deepEqual(readdirSync(session.dir), []);
+});
+
+test("stopping after FIFO creation ignores its late success callback", options, async t => {
+ const session = await owner(t, "pending-created-window");
+ assert.equal(session.message.type, "pending");
+ assert.equal(statSync(session.message.file).isFIFO(), true);
+ await session.command("owner-stop");
+ assert.deepEqual(readdirSync(session.dir), []);
+ const resumed = await session.command("resume");
+ assert.equal(resumed.busy, false);
+ assert.equal(resumed.launches, 0);
+ assert.deepEqual(resumed.ends, []);
+ assert.deepEqual(resumed.listeners, [0, 0, 0]);
+ assert.deepEqual(readdirSync(session.dir), []);
+});
+
+test("an asynchronous FIFO failure ends the sink once and removes its pending session", options, async t => {
+ const session = await owner(t, "pending-error-window");
+ assert.equal(session.message.type, "pending");
+ const failed = await session.command("resume");
+ assert.equal(failed.busy, false);
+ assert.equal(failed.launches, 0);
+ assert.deepEqual(failed.listeners, [0, 0, 0]);
+ assert.deepEqual(failed.ends, [{ code: 1, reason: "helper-start-failed" }]);
+ assert.deepEqual(readdirSync(session.dir), []);
+ assert.deepEqual((await session.command("stop")).ends, failed.ends);
});
diff --git a/electron/speech.mjs b/electron/speech.mjs
index 3778f75d91..6b7c0a83c2 100644
--- a/electron/speech.mjs
+++ b/electron/speech.mjs
@@ -1,5 +1,5 @@
// macOS privacy enforcement requires the code calling Speech/AVFoundation to launch with its own Info.plist identity.
-import { execFileSync, spawn } from "node:child_process";
+import { execFile, spawn } from "node:child_process";
import {
closeSync,
constants,
@@ -116,48 +116,9 @@ export function startSpeech(target, options = {}) {
writeFileSync(outputPath, "");
writeFileSync(errorPath, "");
- let proc;
let lifeline;
- try {
- execFileSync("/usr/bin/mkfifo", ["-m", "600", lifelinePath], { timeout: 5_000 });
- const reader = openSync(lifelinePath, constants.O_RDONLY | constants.O_NONBLOCK);
- try {
- // Only Electron holds a writer; LaunchServices cannot inherit a lifeline through `open`.
- lifeline = openSync(lifelinePath, constants.O_WRONLY | constants.O_NONBLOCK);
- } finally {
- closeSync(reader);
- }
- proc = spawn(
- "/usr/bin/open",
- [
- "-n",
- "-g",
- "-W",
- "-o",
- outputPath,
- "--stderr",
- errorPath,
- BUNDLE,
- "--args",
- ...args,
- "--stop-file",
- stopPath,
- "--finish-file",
- finishPath,
- "--lifeline-file",
- lifelinePath,
- ],
- { stdio: "ignore" },
- );
- } catch {
- if (lifeline !== undefined) closeSync(lifeline);
- rmSync(sessionDir, { recursive: true, force: true });
- sink.end({ code: 1, reason: "helper-start-failed" });
- return;
- }
-
const speechSession = {
- proc, outputPath, errorPath, stopPath, finishPath, sessionDir,
+ proc: null, outputPath, errorPath, stopPath, finishPath, sessionDir,
owner: sink.owner, target: sink,
};
child = speechSession;
@@ -193,37 +154,83 @@ export function startSpeech(target, options = {}) {
}
}
};
- watchFile(outputPath, { interval: 50, persistent: false }, drain);
-
let cleaned = false;
const cleanup = () => {
if (cleaned) return;
cleaned = true;
- closeSync(lifeline);
+ if (lifeline !== undefined) closeSync(lifeline);
forgetWindow();
unwatchFile(outputPath, drain);
rmSync(sessionDir, { recursive: true, force: true });
};
- proc.on("close", (code) => {
- drain();
- cleanup();
- // stopSpeech() clears child before the stop marker, so intentional TTS muting never looks like the end of a spoken turn.
- if (child !== speechSession) return;
- child = null;
- if (reportedError) {
- sink.end({ code: 1, reason: reportedError });
- } else if (completed && code === 0) {
- sink.end({ code: 0, reason: "completed" });
- } else {
- sink.end({ code: 1, reason: "helper-exited" });
- }
- });
- proc.on("error", () => {
+ speechSession.cleanup = cleanup;
+ const failed = () => {
cleanup();
if (child !== speechSession) return;
child = null;
sink.end({ code: 1, reason: "helper-start-failed" });
- });
+ };
+ try {
+ execFile("/usr/bin/mkfifo", ["-m", "600", lifelinePath], { timeout: 5_000 }, (error) => {
+ if (cleaned) return;
+ if (error) {
+ failed();
+ return;
+ }
+ try {
+ const reader = openSync(lifelinePath, constants.O_RDONLY | constants.O_NONBLOCK);
+ try {
+ // Only Electron holds a writer; LaunchServices cannot inherit a lifeline through `open`.
+ lifeline = openSync(lifelinePath, constants.O_WRONLY | constants.O_NONBLOCK);
+ } finally {
+ closeSync(reader);
+ }
+ const proc = spawn(
+ "/usr/bin/open",
+ [
+ "-n",
+ "-g",
+ "-W",
+ "-o",
+ outputPath,
+ "--stderr",
+ errorPath,
+ BUNDLE,
+ "--args",
+ ...args,
+ "--stop-file",
+ stopPath,
+ "--finish-file",
+ finishPath,
+ "--lifeline-file",
+ lifelinePath,
+ ],
+ { stdio: "ignore" },
+ );
+ speechSession.proc = proc;
+ watchFile(outputPath, { interval: 50, persistent: false }, drain);
+ proc.on("close", (code) => {
+ drain();
+ cleanup();
+ // stopSpeech() clears child before the stop marker, so intentional TTS muting never looks like the end of a spoken turn.
+ if (child !== speechSession) return;
+ child = null;
+ if (reportedError) {
+ sink.end({ code: 1, reason: reportedError });
+ } else if (completed && code === 0) {
+ sink.end({ code: 0, reason: "completed" });
+ } else {
+ sink.end({ code: 1, reason: "helper-exited" });
+ }
+ });
+ proc.on("error", failed);
+ } catch {
+ failed();
+ }
+ });
+ } catch {
+ failed();
+ }
}
const ownedBy = (owner) => child !== null && (owner === undefined || child.owner === owner);
@@ -232,6 +239,10 @@ export function stopSpeech(owner) {
if (!ownedBy(owner)) return;
const speechSession = child;
child = null;
+ if (!speechSession.proc) {
+ speechSession.cleanup();
+ return;
+ }
try {
writeFileSync(speechSession.stopPath, "stop");
} catch {}
diff --git a/electron/speech.test.mjs b/electron/speech.test.mjs
index a74b181aa1..973ee6d9b5 100644
--- a/electron/speech.test.mjs
+++ b/electron/speech.test.mjs
@@ -1,9 +1,9 @@
import { EventEmitter } from "node:events";
import { appendFileSync, existsSync } from "node:fs";
import { tmpdir } from "node:os";
-import { afterEach, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-const fixture = vi.hoisted(() => ({ launches: [], windows: [] }));
+const fixture = vi.hoisted(() => ({ launches: [], onLaunch: null }));
vi.mock("electron", () => ({ app: { isPackaged: true, getPath: () => tmpdir() } }));
vi.mock("node:child_process", async (importOriginal) => ({
...await importOriginal(),
@@ -11,6 +11,7 @@ vi.mock("node:child_process", async (importOriginal) => ({
const proc = new EventEmitter();
const arg = flag => args[args.indexOf(flag) + 1];
fixture.launches.push({ proc, args, output: arg("-o"), stop: arg("--stop-file"), finish: arg("--finish-file") });
+ fixture.onLaunch();
return proc;
},
}));
@@ -18,8 +19,13 @@ vi.mock("node:child_process", async (importOriginal) => ({
Object.defineProperty(process, "resourcesPath", { configurable: true, value: "/synthetic-speech-resources" });
const { startSpeech, stopSpeech, finishSpeech, speechBusy } = await import("./speech.mjs");
+const platform = process.platform;
const PAGE_GONE = ["did-navigate", "render-process-gone"];
-const launch = () => {
+const start = (target, options) => new Promise(resolve => {
+ fixture.onLaunch = resolve;
+ startSpeech(target, options);
+});
+const launch = async () => {
const events = [];
let destroyed = false;
const contents = Object.assign(new EventEmitter(), { send: (channel, body) => events.push({ channel, body }) });
@@ -27,21 +33,25 @@ const launch = () => {
// Electron: a destroyed BrowserWindow throws "Object has been destroyed" when its webContents is read.
Object.defineProperty(win, "webContents", { get: () => { if (destroyed) throw new TypeError("Object has been destroyed"); return contents; } });
const close = () => { destroyed = true; win.emit("closed"); };
- startSpeech(win);
+ await start(win);
return { ...fixture.launches.at(-1), events, win, contents, close };
};
const pageListeners = session => PAGE_GONE.map(event => session.contents.listenerCount(event));
const emit = (session, line) => appendFileSync(session.output, `${JSON.stringify(line)}\n`);
+beforeEach(() => {
+ Object.defineProperty(process, "platform", { value: "darwin" });
+});
afterEach(() => {
stopSpeech();
for (const session of fixture.launches) session.proc.emit("close", 0);
fixture.launches.length = 0;
+ Object.defineProperty(process, "platform", { value: platform });
});
-describe.skipIf(process.platform !== "darwin")("native speech file transport", () => {
+describe.skipIf(platform === "win32")("native speech file transport", () => {
it("assembles utterances before delivering them and keeps partials partial for calls", async () => {
- const session = launch();
+ const session = await launch();
emit(session, { text: "最初の文章。", partial: true, utteranceFinal: true, speechStart: 0 });
emit(session, { text: "次の文章。", partial: true });
emit(session, { text: "", partial: false });
@@ -55,8 +65,8 @@ describe.skipIf(process.platform !== "darwin")("native speech file transport", (
expect(existsSync(session.output)).toBe(false);
});
- it("rejects results and close events from a stopped session", () => {
- const session = launch();
+ it("rejects results and close events from a stopped session", async () => {
+ const session = await launch();
stopSpeech();
expect(existsSync(session.stop)).toBe(true);
emit(session, { text: "停止後の認識結果", partial: false });
@@ -64,9 +74,9 @@ describe.skipIf(process.platform !== "darwin")("native speech file transport", (
expect(session.events).toEqual([]);
});
- it("isolates a replacement session even if the older helper flushes late", () => {
- const first = launch();
- const second = launch();
+ it("isolates a replacement session even if the older helper flushes late", async () => {
+ const first = await launch();
+ const second = await launch();
emit(first, { text: "古い認識結果", partial: false });
first.proc.emit("close", 0);
emit(second, { text: "新しい認識結果", partial: false });
@@ -75,8 +85,8 @@ describe.skipIf(process.platform !== "darwin")("native speech file transport", (
expect(second.events[0].body.text).toBe("新しい認識結果");
});
- it("keeps push-to-talk finish separate from cancellation", () => {
- const session = launch();
+ it("keeps push-to-talk finish separate from cancellation", async () => {
+ const session = await launch();
finishSpeech();
expect(existsSync(session.finish)).toBe(true);
expect(existsSync(session.stop)).toBe(false);
@@ -91,9 +101,9 @@ describe.skipIf(process.platform !== "darwin")("native speech file transport", (
};
const flag = (session, name) => session.args.includes(name) ? session.args[session.args.indexOf(name) + 1] : undefined;
- it("delivers a sink's session to the sink and tells the helper which keys end it", () => {
+ it("delivers a sink's session to the sink and tells the helper which keys end it", async () => {
const { heard, sink } = sinkOf();
- startSpeech(sink, { finishOnRelease: ["control", "option"] });
+ await start(sink, { finishOnRelease: ["control", "option"] });
const session = fixture.launches.at(-1);
expect(flag(session, "--finish-on-release")).toBe("control,option");
expect(speechBusy()).toBe(true);
@@ -103,18 +113,18 @@ describe.skipIf(process.platform !== "darwin")("native speech file transport", (
expect(heard.ends).toEqual([{ code: 0, reason: "completed" }]);
expect(speechBusy()).toBe(false);
});
- it("tells a replaced sink, and never ends it", () => {
+ it("tells a replaced sink, and never ends it", async () => {
const { heard, sink } = sinkOf();
- startSpeech(sink);
+ await start(sink);
const first = fixture.launches.at(-1);
- launch();
+ await launch();
expect(heard.replaced).toBe(1);
first.proc.emit("close", 0);
expect(heard.ends).toEqual([]);
});
- it("a page's stop or finish never touches a session it did not start", () => {
+ it("a page's stop or finish never touches a session it did not start", async () => {
const { sink } = sinkOf();
- startSpeech(sink);
+ await start(sink);
const session = fixture.launches.at(-1);
const page = { isDestroyed: () => false, webContents: { send: () => {} } };
stopSpeech(page); finishSpeech(page); stopSpeech(null);
@@ -124,24 +134,24 @@ describe.skipIf(process.platform !== "darwin")("native speech file transport", (
stopSpeech(sink); expect(existsSync(session.stop)).toBe(true);
expect(speechBusy()).toBe(false);
});
- it("a window's stop and finish reach the session that window started", () => {
- const session = launch();
+ it("a window's stop and finish reach the session that window started", async () => {
+ const session = await launch();
finishSpeech(session.win);
expect(existsSync(session.finish)).toBe(true);
stopSpeech(session.win);
expect(existsSync(session.stop)).toBe(true);
expect(speechBusy()).toBe(false);
});
- it("another window's stop and finish leave a window's session alone", () => {
- const session = launch();
+ it("another window's stop and finish leave a window's session alone", async () => {
+ const session = await launch();
const other = { isDestroyed: () => false, webContents: { send: () => {} } };
stopSpeech(other); finishSpeech(other);
expect(existsSync(session.stop)).toBe(false);
expect(existsSync(session.finish)).toBe(false);
expect(speechBusy()).toBe(true);
});
- it("a window that closes stops the session it started, and an ended session lets go of its window", () => {
- const session = launch();
+ it("a window that closes stops the session it started, and an ended session lets go of its window", async () => {
+ const session = await launch();
expect(session.win.listenerCount("closed")).toBe(1);
expect(pageListeners(session)).toEqual([1, 1]);
session.close();
@@ -152,8 +162,8 @@ describe.skipIf(process.platform !== "darwin")("native speech file transport", (
expect(session.win.listenerCount("closed")).toBe(0);
expect(pageListeners(session)).toEqual([0, 0]);
});
- it("a window's page that reloads or navigates away stops the session it started; a route change inside the page does not", () => {
- const session = launch();
+ it("a window's page that reloads or navigates away stops the session it started; a route change inside the page does not", async () => {
+ const session = await launch();
session.contents.emit("did-navigate-in-page", {}, "http://127.0.0.1:8799/#/settings");
expect(existsSync(session.stop)).toBe(false);
expect(speechBusy()).toBe(true);
@@ -165,8 +175,8 @@ describe.skipIf(process.platform !== "darwin")("native speech file transport", (
expect(session.win.listenerCount("closed")).toBe(0);
expect(pageListeners(session)).toEqual([0, 0]);
});
- it("a window's page whose renderer goes away stops the session it started", () => {
- const session = launch();
+ it("a window's page whose renderer goes away stops the session it started", async () => {
+ const session = await launch();
session.contents.emit("render-process-gone", {}, { reason: "crashed" });
expect(existsSync(session.stop)).toBe(true);
expect(speechBusy()).toBe(false);
@@ -174,27 +184,27 @@ describe.skipIf(process.platform !== "darwin")("native speech file transport", (
expect(session.win.listenerCount("closed")).toBe(0);
expect(pageListeners(session)).toEqual([0, 0]);
});
- it("a window's session that ends on its own lets go of the window and its page", () => {
- const session = launch();
+ it("a window's session that ends on its own lets go of the window and its page", async () => {
+ const session = await launch();
emit(session, { text: "done", partial: false });
session.proc.emit("close", 0);
expect(session.events.at(-1)).toEqual({ channel: "speech:end", body: { code: 0, reason: "completed" } });
expect(session.win.listenerCount("closed")).toBe(0);
expect(pageListeners(session)).toEqual([0, 0]);
});
- it("a window or page that goes away after its session was replaced leaves the new session alone", () => {
- const first = launch();
- const second = launch();
+ it("a window or page that goes away after its session was replaced leaves the new session alone", async () => {
+ const first = await launch();
+ const second = await launch();
first.proc.emit("close", 0);
for (const event of PAGE_GONE) first.contents.emit(event, {});
first.close();
expect(existsSync(second.stop)).toBe(false);
expect(speechBusy()).toBe(true);
});
- it("passes only known release modifiers", () => {
- startSpeech(sinkOf().sink, { finishOnRelease: ["control", "hyper", "control"] });
+ it("passes only known release modifiers", async () => {
+ await start(sinkOf().sink, { finishOnRelease: ["control", "hyper", "control"] });
expect(flag(fixture.launches.at(-1), "--finish-on-release")).toBe("control");
- startSpeech(sinkOf().sink, { finishOnRelease: ["hyper"] });
+ await start(sinkOf().sink, { finishOnRelease: ["hyper"] });
expect(fixture.launches.at(-1).args).not.toContain("--finish-on-release");
});
});