Lutra/ui/tests/editor/assist/settings.test.ts
danny 197d5b7d27 H4: string and path settings controls, live completion and tooltip settings
- Settings dialog: every string setting gets a text field with in-place
  hints (quotes or spaces around a path, control characters, length);
  keys ending in path/executable also get "Browse…" through the dialog
  plugin's native picker (new desktop IPC pick_file). git.executable
  from M4.1 gets the control without UI changes.
- settings.open {section?, query?} for agents and live checks.
- New keys editor.completion.autoPopup, editor.completion.delayMs and
  editor.hover.delayMs; the assist module reads them live through
  CodeMirror compartments.
- Tests: engine settings test, control/engine key lists, Vitest for the
  controls and the live wiring. Docs: architecture, ui-parity, parity
  Settings row, gaps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 13:10:39 +01:00

195 lines
6.6 KiB
TypeScript

/**
* Tests of the live assist settings (H4, `assist/settings.ts`): reading
* and clamping the values, following the settings store, reconfiguring
* open editors when a value changes, and the effect on completion (auto
* popup on/off, popup delay) through `assistExtensions`.
*/
import { EditorState, type Extension } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { createPinia, setActivePinia } from "pinia";
import { afterEach, describe, expect, it, vi } from "vitest";
import { nextTick } from "vue";
import { assistExtensions } from "../../../src/components/editor/assist";
import {
ASSIST_DEFAULTS,
liveAssistSettings,
storeAssistSettings,
toAssistSettings,
type AssistSettings,
type AssistSettingsSource,
} from "../../../src/components/editor/assist/settings";
import { useSettingsStore } from "../../../src/stores/settings";
import { fakeBuffer, fakeTransport } from "./fakes";
/** Views created by a test, destroyed after it. */
let views: EditorView[] = [];
afterEach(() => {
vi.useRealTimers();
for (const view of views) {
view.destroy();
}
views = [];
});
/** A mounted view with `doc` and `extensions`. */
function mount(doc: string, extensions: Extension): EditorView {
const parent = document.createElement("div");
document.body.append(parent);
const view = new EditorView({ state: EditorState.create({ doc, extensions }), parent });
views.push(view);
return view;
}
/** A settings source the test changes by hand. */
function fakeSource(initial: Partial<AssistSettings> = {}): AssistSettingsSource & {
change(next: Partial<AssistSettings>): void;
listeners: Set<() => void>;
} {
let value: AssistSettings = { ...ASSIST_DEFAULTS, ...initial };
const listeners = new Set<() => void>();
return {
listeners,
current: () => ({ ...value }),
subscribe(listener) {
listeners.add(listener);
return () => listeners.delete(listener);
},
change(next) {
value = { ...value, ...next };
for (const listener of listeners) {
listener();
}
},
};
}
/**
* Lets `ms` milliseconds pass on the fake clock, running the timers and
* promises due meanwhile.
*/
async function wait(ms: number): Promise<void> {
await vi.advanceTimersByTimeAsync(ms);
}
/** Types `text` at the end of the view's document as the user would. */
function type(view: EditorView, text: string): void {
const end = view.state.doc.length;
view.dispatch({
changes: { from: end, insert: text },
selection: { anchor: end + text.length },
userEvent: "input.type",
});
}
/** The completion queries a transport received. */
function completions(transport: ReturnType<typeof fakeTransport>): number {
return transport.calls.filter((c) => c.command === "lsp.completion").length;
}
describe("assist settings values", () => {
it("uses the defaults for missing or malformed values and clamps numbers", () => {
expect(toAssistSettings({})).toEqual({ autoPopup: true, completionDelay: 0, hoverDelay: 350 });
expect(
toAssistSettings({
"editor.completion.autoPopup": "no",
"editor.completion.delayMs": Number.NaN,
"editor.hover.delayMs": null,
}),
).toEqual(ASSIST_DEFAULTS);
expect(
toAssistSettings({
"editor.completion.autoPopup": false,
"editor.completion.delayMs": 9999,
"editor.hover.delayMs": 10.4,
}),
).toEqual({ autoPopup: false, completionDelay: 2000, hoverDelay: 100 });
});
it("reads and follows the settings store", async () => {
setActivePinia(createPinia());
const store = useSettingsStore();
expect(storeAssistSettings.current()).toEqual(ASSIST_DEFAULTS);
let changes = 0;
const stop = storeAssistSettings.subscribe(() => {
changes += 1;
});
store.values["editor.hover.delayMs"] = 800;
await nextTick();
expect(changes).toBe(1);
expect(storeAssistSettings.current().hoverDelay).toBe(800);
store.values["editor.fontSize"] = 20;
await nextTick();
expect(changes).toBe(1);
stop();
store.values["editor.completion.autoPopup"] = false;
await nextTick();
expect(changes).toBe(1);
});
});
describe("live reconfiguration", () => {
it("rebuilds the dependent extensions with new values and stops on destroy", async () => {
const source = fakeSource({ hoverDelay: 500 });
const built: number[] = [];
const view = mount("<?php\n", liveAssistSettings(source, [(s) => (built.push(s.hoverDelay), [])]));
await Promise.resolve();
expect(built).toEqual([500]);
source.change({ hoverDelay: 1200 });
expect(built).toEqual([500, 1200]);
source.change({ hoverDelay: 1200 });
expect(built).toEqual([500, 1200]);
view.destroy();
views = [];
expect(source.listeners.size).toBe(0);
});
it("catches up when a view gets a state built with older values", async () => {
const source = fakeSource();
const built: boolean[] = [];
const state = EditorState.create({
doc: "<?php\n",
extensions: liveAssistSettings(source, [(s) => (built.push(s.autoPopup), [])]),
});
source.change({ autoPopup: false });
const parent = document.createElement("div");
document.body.append(parent);
views.push(new EditorView({ state, parent }));
await Promise.resolve();
expect(built).toEqual([true, false]);
});
});
describe("completion follows the settings", () => {
it("opens while typing only with auto popup on", async () => {
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
const source = fakeSource({ autoPopup: false });
const transport = fakeTransport({ isIncomplete: false, items: [{ label: "Foo", kind: "class" }] });
const view = mount("<?php\n", assistExtensions("a.php", fakeBuffer(1), transport, source));
type(view, "Fo");
await wait(30);
expect(completions(transport)).toBe(0);
source.change({ autoPopup: true });
type(view, "o");
await wait(30);
expect(completions(transport)).toBe(1);
});
it("waits for the popup delay after typing", async () => {
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
const source = fakeSource({ completionDelay: 200 });
const transport = fakeTransport({ isIncomplete: true, items: [{ label: "Foo", kind: "class" }] });
const view = mount("<?php\n", assistExtensions("a.php", fakeBuffer(1), transport, source));
type(view, "F");
await wait(190);
expect(completions(transport)).toBe(0);
await wait(20);
expect(completions(transport)).toBe(1);
source.change({ completionDelay: 0 });
type(view, "o");
await wait(30);
expect(completions(transport)).toBe(2);
});
});