diff --git a/docs/deviations.md b/docs/deviations.md index e134e59..942b76c 100644 --- a/docs/deviations.md +++ b/docs/deviations.md @@ -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. + diff --git a/src/app/Dialog.test.tsx b/src/app/Dialog.test.tsx new file mode 100644 index 0000000..f5b146d --- /dev/null +++ b/src/app/Dialog.test.tsx @@ -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 ( + onClose()}> +

{n}

+
+ ); +} + +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(); + 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(); + fireEvent.keyDown(window, { key: "Escape" }); + expect(screen.getByText("1")).toBeTruthy(); + expect(onClose).toHaveBeenCalledTimes(1); + }); +}); diff --git a/src/app/Dialog.tsx b/src/app/Dialog.tsx index 3c16525..f96e559 100644 --- a/src/app/Dialog.tsx +++ b/src/app/Dialog.tsx @@ -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({ @@ -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 (
e.target === e.currentTarget && onClose()}>
diff --git a/src/features/models/ModelManager.test.tsx b/src/features/models/ModelManager.test.tsx new file mode 100644 index 0000000..9a88093 --- /dev/null +++ b/src/features/models/ModelManager.test.tsx @@ -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, 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(); + 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({}); + }); +}); diff --git a/src/features/models/ModelManager.tsx b/src/features/models/ModelManager.tsx index 01c9ea4..070165e 100644 --- a/src/features/models/ModelManager.tsx +++ b/src/features/models/ModelManager.tsx @@ -102,7 +102,9 @@ export function ModelManager() { ))}
{prof && adapter && ( -
+ // 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. +