Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/deviations.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,10 @@ Requests: (1) a way to stop playback, (2) choosing a Drama-3 voice by text promp
- Dependabot updates only the SHA, not the `# vX.Y.Z` comment, when another comment follows it on the line (`# zizmor: ignore[...]`); fix the comment by hand in such PRs (done in #30).
- **User impact**: None expected. The app starts slightly slower to parse (+79 kB of JavaScript).

### D-57 Esc closes 書き出し, and the モデル screen shows each profile's own env 【修正】
- **What**: Two bugs found while trying React 19 (D-56); both were there on React 18.3 too.
- **書き出し did not close on Esc.** The app's Esc handler clears the line selection first; the export dialog reads the selection, so it re-rendered during that keydown, and `Dialog` (`src/app/Dialog.tsx`) removed and re-added its window listener because `onClose` is a new function on every render. A listener added during a dispatch is not called for it. `Dialog` now adds one listener for its life and calls the latest `onClose` through a ref. 設定 was not affected because it does not read the selection.
- **The env and extra-args fields of the モデル screen kept the first profile's text** when another profile was picked (when the first one had any): they are uncontrolled (`defaultValue`, saved on blur), and the form was not remounted. Leaving the field then saved that text into the other profile, e.g. the Drama-3 Vulkan variables into Irodori. The form is now keyed by the profile id.
- **Checked**: `src/app/Dialog.test.tsx` and `src/features/models/ModelManager.test.tsx` fail without the fixes and pass with them. In the app (over the WebView2 DevTools protocol, real key and mouse input): Esc closes 書き出し and 設定, and switching between the Drama-3 and Irodori profiles shows each one's own env.
- **User impact**: Esc closes the export dialog. Picking a profile on the モデル screen no longer shows, or on leaving the field saves, another profile's environment variables.

45 changes: 45 additions & 0 deletions src/app/Dialog.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { fireEvent, render, screen } from "@testing-library/react";
import { flushSync } from "react-dom";
import { create } from "zustand";
import { Dialog } from "./Dialog";

// Stands for the line selection, which the app's own Esc handler clears before the dialog sees the key.
const useTick = create<{ n: number }>(() => ({ n: 0 }));

function Host({ onClose }: { onClose: () => void }) {
const n = useTick((s) => s.n);
// A new onClose on every render, as the dialogs pass an inline arrow.
return (
<Dialog title="書き出し" onClose={() => onClose()}>
<p>{n}</p>
</Dialog>
);
}

describe("Dialog", () => {
let earlier: ((e: KeyboardEvent) => void) | null = null;
afterEach(() => {
if (earlier) window.removeEventListener("keydown", earlier);
earlier = null;
});

it("closes on Esc", () => {
const onClose = vi.fn();
render(<Host onClose={onClose} />);
fireEvent.keyDown(window, { key: "Escape" });
expect(onClose).toHaveBeenCalledTimes(1);
});

it("closes on Esc when an earlier keydown listener re-renders it", () => {
// In the WebView the earlier listener's store update renders between listeners (a microtask);
// flushSync does the same here.
earlier = (e) => e.key === "Escape" && flushSync(() => useTick.setState((s) => ({ n: s.n + 1 })));
window.addEventListener("keydown", earlier);
const onClose = vi.fn();
render(<Host onClose={onClose} />);
fireEvent.keyDown(window, { key: "Escape" });
expect(screen.getByText("1")).toBeTruthy();
expect(onClose).toHaveBeenCalledTimes(1);
});
});
13 changes: 10 additions & 3 deletions src/app/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, type ReactNode } from "react";
import { useEffect, useLayoutEffect, useRef, type ReactNode } from "react";
import { Icon } from "./icons";

export function Dialog({
Expand All @@ -12,11 +12,18 @@ export function Dialog({
children: ReactNode;
wide?: boolean;
}) {
// One listener for the dialog's life, calling the latest onClose. Re-adding it whenever onClose changed (each
// render) lost Esc: the app's own Esc handler clears the line selection first, the dialog re-renders during
// that keydown, and a listener added mid-dispatch is not called for it.
const close = useRef(onClose);
useLayoutEffect(() => {
close.current = onClose;
});
useEffect(() => {
const k = (e: KeyboardEvent) => e.key === "Escape" && onClose();
const k = (e: KeyboardEvent) => e.key === "Escape" && close.current();
window.addEventListener("keydown", k);
return () => window.removeEventListener("keydown", k);
}, [onClose]);
}, []);
return (
<div className="backdrop" onMouseDown={(e) => e.target === e.currentTarget && onClose()}>
<div className={`dialog${wide ? " wide" : ""}`} role="dialog" aria-label={title}>
Expand Down
85 changes: 85 additions & 0 deletions src/features/models/ModelManager.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { fireEvent, render, screen } from "@testing-library/react";
import { ModelManager } from "./ModelManager";
import { useApp } from "../../store/app";
import type { AdapterDescriptor, AppSettings, ModelProfile } from "../../lib/types";

vi.mock("../../lib/ipc", () => ({
ipc: {
listDevices: vi.fn(async () => ({ devices: [] })),
modelCatalog: vi.fn(async () => ({ models: [] })),
speechVersion: vi.fn(async () => "speech 0.11.2"),
},
}));

const profile = (id: string, name: string, env: Record<string, string>, extraArgs: string[]): ModelProfile => ({
id,
name,
adapter: "irodori",
model: "/m.gguf",
speechExe: "speech",
device: "auto",
threads: null,
env,
extraArgs,
});
const settings: AppSettings = {
version: 1,
profiles: [profile("p1", "Drama-3", { GGML_VK_DISABLE_F16: "1" }, ["--foo"]), profile("p2", "Irodori Small", {}, [])],
activeProfileId: "p1",
speechExe: "speech",
asrModel: "",
asrDevice: "auto",
showNsfwTags: false,
theme: "dark",
recentTags: [],
recentProjects: [],
playbackVolume: 1,
playbackMuted: false,
};
const adapter: AdapterDescriptor = {
id: "irodori",
displayName: "Irodori-TTS",
capabilities: {
tagSyntax: "emoji",
tagPalette: "irodori",
designPrompt: "instructions",
instructions: true,
speed: true,
multiSpeaker: false,
multiSpeakerReliable: false,
voiceNeedsTranscript: false,
voiceAcceptsWav: true,
sampleRate: 48000,
maxSecondsPerRequest: 30,
licenseNote: "MIT",
},
options: [],
};

const saveSettings = vi.fn(async (_s: AppSettings) => {});

describe("ModelManager", () => {
beforeEach(() => {
vi.clearAllMocks();
useApp.setState({ settings, adapters: [adapter], engine: { state: "stopped" }, view: "models", saveSettings });
});

it("shows each profile's own env and extra args when another profile is picked", () => {
render(<ModelManager />);
const env = () => (screen.getByLabelText(/環境変数/) as HTMLTextAreaElement).value;
const args = () => (screen.getByLabelText(/追加の引数/) as HTMLInputElement).value;
expect(env()).toBe("GGML_VK_DISABLE_F16=1");
expect(args()).toBe("--foo");

fireEvent.click(screen.getByRole("button", { name: "Irodori Small" }));
expect(env()).toBe("");
expect(args()).toBe("");

// Leaving the field must not copy the first profile's env into this one.
fireEvent.blur(screen.getByLabelText(/環境変数/));
const calls = saveSettings.mock.calls;
const saved = calls[calls.length - 1]?.[0];
expect(saved?.profiles.find((p) => p.id === "p2")?.env ?? {}).toEqual({});
});
});
4 changes: 3 additions & 1 deletion src/features/models/ModelManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ export function ModelManager() {
))}
</div>
{prof && adapter && (
<div className="profile-form">
// Keyed by profile: the env and extra-args fields are uncontrolled (saved on blur), so without a
// remount they would keep the previous profile's text, and leaving them would save it here.
<div className="profile-form" key={prof.id}>
<label className="block">
名前
<input value={prof.name} onChange={(e) => save({ ...prof, name: e.target.value })} />
Expand Down