fix(mcp): persist the access policy only on explicit apply

This commit is contained in:
onenewcode authored and GitHub committed 2026-09-30 15:34:23 +08:00
1 parent dc02e3bccf
commit 20ffc62249
5 files changed
+198 -245

No files matched your search

@@ -1,11 +1,8 @@
// @vitest-environment happy-dom
// Real-DOM integration for the MCP query-timeout input (#8616). The settings
// dialog reads the fresh value from the native event target in capture phase,
// before Input's passive v-model proxy updates the parent ref. This test mounts the real
// Input component with exactly that wiring and drives it with real DOM events
// to prove the typing -> debounce -> saveMcpPolicy chain persists the fresh
// value (a two-way v-model would make the passive proxy emit asynchronously,
// so the handler would read a stale ref during the same native event).
// Real-DOM integration for the MCP query-timeout input. The settings dialog
// reads the fresh value from the native event target in capture phase, before
// Input's passive v-model proxy updates the parent ref. Typing stages that
// fresh value into the policy draft and does not persist it.
import { afterEach, describe, expect, it, vi } from "vitest";
import { createApp, defineComponent, h, nextTick, type App } from "vue";
import Input from "@/components/ui/input/Input.vue";
@@ -44,44 +41,37 @@ describe("EditorSettingsDialog MCP query timeout real-DOM integration", () => {
return el as HTMLInputElement;
}
function harnessFor(saveMcpPolicy: ReturnType<typeof vi.fn>): { harness: McpQueryTimeoutHarness; inputRef: { value: string } } {
function harnessFor(): { harness: McpQueryTimeoutHarness; inputRef: { value: string } } {
const inputRef = { value: "" };
const harness = createMcpQueryTimeoutHarness({
source: dialogSource,
input: inputRef,
policyQueryTimeoutSecs: null,
saveMcpPolicy,
});
return { harness, inputRef };
}
it("typing into the real Input persists the fresh value after debounce", async () => {
it("typing into the real Input stages the fresh value after debounce", async () => {
vi.useFakeTimers();
const saveMcpPolicy = vi.fn();
const { harness, inputRef } = harnessFor(saveMcpPolicy);
const { harness, inputRef } = harnessFor();
const el = await mountInput({
modelValue: inputRef,
onInput: harness.onMcpQueryTimeoutInput,
});
// Simulate real typing: set the DOM value and dispatch a native input event.
el.value = "300";
el.dispatchEvent(new Event("input", { bubbles: true }));
// The debounce has not elapsed yet: no save.
expect(saveMcpPolicy).not.toHaveBeenCalled();
expect(harness.draft.value.queryTimeoutSecs).toBeNull();
// Elapse the debounce window and assert the FRESH value was saved.
vi.advanceTimersByTime(300);
expect(saveMcpPolicy).toHaveBeenCalledTimes(1);
expect(saveMcpPolicy.mock.calls.at(-1)?.[0]).toEqual({ queryTimeoutSecs: 300 });
expect(harness.draft.value.queryTimeoutSecs).toBe(300);
});
it("typing then flushing on close persists the fresh value immediately", async () => {
it("staging before the debounce elapses keeps the fresh value in the draft only", async () => {
vi.useFakeTimers();
const saveMcpPolicy = vi.fn();
const { harness, inputRef } = harnessFor(saveMcpPolicy);
const { harness, inputRef } = harnessFor();
const el = await mountInput({
modelValue: inputRef,
@@ -91,16 +81,13 @@ describe("EditorSettingsDialog MCP query timeout real-DOM integration", () => {
el.value = "120";
el.dispatchEvent(new Event("input", { bubbles: true }));
// Close path: flush before the debounce elapses.
harness.flushMcpQueryTimeoutSave();
expect(saveMcpPolicy).toHaveBeenCalledTimes(1);
expect(saveMcpPolicy.mock.calls.at(-1)?.[0]).toEqual({ queryTimeoutSecs: 120 });
harness.stageMcpQueryTimeoutDraft();
expect(harness.draft.value.queryTimeoutSecs).toBe(120);
});
it("rapid typing in the real DOM coalesces into one save of the last value", async () => {
it("rapid typing in the real DOM coalesces into one draft update of the last value", async () => {
vi.useFakeTimers();
const saveMcpPolicy = vi.fn();
const { harness, inputRef } = harnessFor(saveMcpPolicy);
const { harness, inputRef } = harnessFor();
const el = await mountInput({
modelValue: inputRef,
@@ -117,7 +104,6 @@ describe("EditorSettingsDialog MCP query timeout real-DOM integration", () => {
el.dispatchEvent(new Event("input", { bubbles: true }));
vi.advanceTimersByTime(300);
expect(saveMcpPolicy).toHaveBeenCalledTimes(1);
expect(saveMcpPolicy.mock.calls.at(-1)?.[0]).toEqual({ queryTimeoutSecs: 123 });
expect(harness.draft.value.queryTimeoutSecs).toBe(123);
});
});
@@ -1,21 +1,18 @@
import { readFileSync } from "node:fs";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createMcpQueryTimeoutHarness } from "./mcpQueryTimeoutHarness";
import { createMcpQueryTimeoutHarness, extractMcpQueryTimeoutDebounceBlock } from "./mcpQueryTimeoutHarness";
const dialogSource = readFileSync(new URL("../EditorSettingsDialog.vue", import.meta.url), "utf8");
// Behavioral coverage for the debounce/flush runtime (review finding): the
// string assertions above cannot catch a broken runtime path, so this harness
// extracts the real debounce block from the .vue source, compiles it, and
// drives it with fake timers + mocked deps to assert actual save behaviour.
// The debounce block stages a query timeout into the policy draft. It must not
// persist: explicit save is the only write, and close/discard drops the draft.
function nativeInputEvent(value: string, badInput = false): Event {
return { currentTarget: { value, validity: { badInput } } } as unknown as Event;
}
function expectQueryTimeoutSave(saveMcpPolicy: ReturnType<typeof vi.fn>, queryTimeoutSecs: number | null) {
expect(saveMcpPolicy).toHaveBeenCalled();
expect(saveMcpPolicy.mock.calls.at(-1)?.[0]).toEqual({ queryTimeoutSecs });
function expectStagedTimeout(harness: ReturnType<typeof createMcpQueryTimeoutHarness>, queryTimeoutSecs: number | null) {
expect(harness.draft.value.queryTimeoutSecs).toBe(queryTimeoutSecs);
}
describe("EditorSettingsDialog MCP query timeout debounce runtime", () => {
@@ -27,117 +24,105 @@ describe("EditorSettingsDialog MCP query timeout debounce runtime", () => {
vi.useRealTimers();
});
it("saves a typed value once the debounce window elapses", () => {
const saveMcpPolicy = vi.fn();
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input: { value: "" }, policyQueryTimeoutSecs: null, saveMcpPolicy });
it("stages a typed value into the draft once the debounce window elapses, without persisting", () => {
const block = extractMcpQueryTimeoutDebounceBlock(dialogSource);
expect(block).not.toContain("saveMcpPolicy");
expect(block).not.toContain("updateMcpGlobalPolicy");
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input: { value: "" }, policyQueryTimeoutSecs: null });
harness.onMcpQueryTimeoutInput(nativeInputEvent("300"));
expect(saveMcpPolicy).not.toHaveBeenCalled();
expect(harness.draft.value.queryTimeoutSecs).toBeNull();
vi.advanceTimersByTime(299);
expect(saveMcpPolicy).not.toHaveBeenCalled();
expect(harness.draft.value.queryTimeoutSecs).toBeNull();
vi.advanceTimersByTime(1);
expect(saveMcpPolicy).toHaveBeenCalledTimes(1);
expectQueryTimeoutSave(saveMcpPolicy, 300);
expectStagedTimeout(harness, 300);
});
it("reports saving immediately and saved after the persistence callback", () => {
const setSaveStatus = vi.fn();
const saveMcpPolicy = vi.fn((_partial, callbacks?: { onSuccess?: () => void }) => callbacks?.onSuccess?.());
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input: { value: "" }, policyQueryTimeoutSecs: null, saveMcpPolicy, setSaveStatus });
harness.onMcpQueryTimeoutInput(nativeInputEvent("300"));
expect(setSaveStatus).toHaveBeenLastCalledWith("saving");
harness.flushMcpQueryTimeoutSave();
expect(setSaveStatus).toHaveBeenLastCalledWith("saved");
});
it("coalesces rapid typing into a single save", () => {
const saveMcpPolicy = vi.fn();
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input: { value: "" }, policyQueryTimeoutSecs: null, saveMcpPolicy });
it("coalesces rapid typing into a single draft update", () => {
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input: { value: "" }, policyQueryTimeoutSecs: null });
harness.onMcpQueryTimeoutInput(nativeInputEvent("1"));
vi.advanceTimersByTime(100);
harness.onMcpQueryTimeoutInput(nativeInputEvent("12"));
vi.advanceTimersByTime(100);
harness.onMcpQueryTimeoutInput(nativeInputEvent("123"));
vi.advanceTimersByTime(300);
expect(saveMcpPolicy).toHaveBeenCalledTimes(1);
expectQueryTimeoutSave(saveMcpPolicy, 123);
expectStagedTimeout(harness, 123);
});
it("flush persists a pending value before the debounce elapses", () => {
const saveMcpPolicy = vi.fn();
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input: { value: "" }, policyQueryTimeoutSecs: null, saveMcpPolicy });
it("stages a pending value before the debounce elapses", () => {
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input: { value: "" }, policyQueryTimeoutSecs: null });
harness.onMcpQueryTimeoutInput(nativeInputEvent("120"));
harness.flushMcpQueryTimeoutSave();
expect(saveMcpPolicy).toHaveBeenCalledTimes(1);
expectQueryTimeoutSave(saveMcpPolicy, 120);
// Flushing again with nothing pending is a no-op.
harness.flushMcpQueryTimeoutSave();
expect(saveMcpPolicy).toHaveBeenCalledTimes(1);
harness.stageMcpQueryTimeoutDraft();
expectStagedTimeout(harness, 120);
harness.stageMcpQueryTimeoutDraft();
expectStagedTimeout(harness, 120);
});
it("flush after the timer fired does not double-save", () => {
const saveMcpPolicy = vi.fn();
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input: { value: "" }, policyQueryTimeoutSecs: null, saveMcpPolicy });
it("staging after the timer fired does not change the draft again", () => {
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input: { value: "" }, policyQueryTimeoutSecs: null });
harness.onMcpQueryTimeoutInput(nativeInputEvent("60"));
vi.advanceTimersByTime(300);
expect(saveMcpPolicy).toHaveBeenCalledTimes(1);
harness.flushMcpQueryTimeoutSave();
expect(saveMcpPolicy).toHaveBeenCalledTimes(1);
expectStagedTimeout(harness, 60);
harness.draft.value.queryTimeoutSecs = 1;
harness.stageMcpQueryTimeoutDraft();
expect(harness.draft.value.queryTimeoutSecs).toBe(1);
});
it("retains a pending value while another MCP policy mutation holds the gate", () => {
const saveMcpPolicy = vi.fn();
const policyMutationBlocked = { value: true };
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input: { value: "" }, policyQueryTimeoutSecs: null, saveMcpPolicy, policyMutationBlocked });
harness.onMcpQueryTimeoutInput(nativeInputEvent("60"));
vi.advanceTimersByTime(300);
expect(saveMcpPolicy).not.toHaveBeenCalled();
policyMutationBlocked.value = false;
harness.flushMcpQueryTimeoutSave();
expectQueryTimeoutSave(saveMcpPolicy, 60);
});
it("empty input schedules null (inherit the connection)", () => {
const saveMcpPolicy = vi.fn();
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input: { value: "" }, policyQueryTimeoutSecs: 300, saveMcpPolicy });
it("empty input stages null (inherit the connection)", () => {
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input: { value: "" }, policyQueryTimeoutSecs: 300 });
harness.onMcpQueryTimeoutInput(nativeInputEvent(""));
harness.flushMcpQueryTimeoutSave();
expectQueryTimeoutSave(saveMcpPolicy, null);
harness.stageMcpQueryTimeoutDraft();
expectStagedTimeout(harness, null);
});
it("zero input persists 0 (no limit)", () => {
const saveMcpPolicy = vi.fn();
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input: { value: "" }, policyQueryTimeoutSecs: null, saveMcpPolicy });
it("zero input stages 0 (no limit)", () => {
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input: { value: "" }, policyQueryTimeoutSecs: null });
harness.onMcpQueryTimeoutInput(nativeInputEvent("0"));
harness.flushMcpQueryTimeoutSave();
expectQueryTimeoutSave(saveMcpPolicy, 0);
harness.stageMcpQueryTimeoutDraft();
expectStagedTimeout(harness, 0);
});
it("invalid input cancels the pending save and reverts the field", () => {
const saveMcpPolicy = vi.fn();
it("invalid input cancels the pending stage and reverts the field", () => {
const toast = vi.fn();
const input = { value: "1.5" };
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input, policyQueryTimeoutSecs: null, saveMcpPolicy, toast });
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input, policyQueryTimeoutSecs: null, toast });
const event = nativeInputEvent("1.5");
harness.onMcpQueryTimeoutInput(event);
// Invalid input must not schedule anything.
vi.advanceTimersByTime(300);
expect(saveMcpPolicy).not.toHaveBeenCalled();
expect(harness.draft.value.queryTimeoutSecs).toBeNull();
expect(toast).toHaveBeenCalledTimes(1);
// The bound ref is reverted to the persisted value ("" == inherit).
expect(input.value).toBe("");
// The native input is reverted too (one-way binding needs the DOM write).
expect((event.currentTarget as { value: string }).value).toBe("");
});
it("does not treat a bad number-input intermediate state as inherit", () => {
const saveMcpPolicy = vi.fn();
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input: { value: "" }, policyQueryTimeoutSecs: 90, saveMcpPolicy });
const harness = createMcpQueryTimeoutHarness({ source: dialogSource, input: { value: "" }, policyQueryTimeoutSecs: 90 });
harness.onMcpQueryTimeoutInput(nativeInputEvent("45"));
vi.advanceTimersByTime(100);
harness.onMcpQueryTimeoutInput(nativeInputEvent("", true));
vi.advanceTimersByTime(200);
expectQueryTimeoutSave(saveMcpPolicy, 45);
expect(saveMcpPolicy.mock.calls.some(([partial]) => partial.queryTimeoutSecs === null)).toBe(false);
expectStagedTimeout(harness, 45);
});
it("close confirmation stages the draft and does not persist, and unmount does not write", () => {
const closeStart = dialogSource.indexOf("function requestCloseSettings");
const closeEnd = dialogSource.indexOf("function onSettingsRootOpenChange");
const closeFn = dialogSource.slice(closeStart, closeEnd);
expect(closeFn).toContain("stageMcpQueryTimeoutDraft()");
expect(closeFn).not.toContain("updateMcpGlobalPolicy");
expect(closeFn).not.toContain("saveMcpGlobalPolicy");
const discardStart = dialogSource.indexOf("function discardUnsavedSettingsAndClose");
const discardFn = dialogSource.slice(discardStart, dialogSource.indexOf("interface TableColumnTemplateOverrideRow"));
expect(discardFn).not.toContain("updateMcpGlobalPolicy");
expect(discardFn).not.toContain("commitMcpPolicyDraft");
expect(discardFn).not.toContain("stageMcpQueryTimeoutDraft");
const unmountStart = dialogSource.lastIndexOf("onUnmounted(() => {");
const unmountEnd = dialogSource.indexOf("});", unmountStart);
const unmountFn = dialogSource.slice(unmountStart, unmountEnd);
expect(unmountFn).not.toContain("stageMcpQueryTimeoutDraft");
expect(unmountFn).not.toContain("updateMcpGlobalPolicy");
expect(unmountFn).not.toContain("commitMcpPolicyDraft");
});
});
@@ -6,7 +6,8 @@ import ts from "typescript";
export interface McpQueryTimeoutHarness {
onMcpQueryTimeoutInput: (event: Event) => void;
flushMcpQueryTimeoutSave: () => void;
stageMcpQueryTimeoutDraft: () => void;
draft: { value: { queryTimeoutSecs: number | null } };
}
export function extractMcpQueryTimeoutDebounceBlock(source: string): string {
@@ -16,28 +17,13 @@ export function extractMcpQueryTimeoutDebounceBlock(source: string): string {
return source.slice(start, end);
}
export function createMcpQueryTimeoutHarness(options: {
source: string;
input: { value: string };
policyQueryTimeoutSecs: number | null;
saveMcpPolicy: (partial: { queryTimeoutSecs: number | null }, callbacks?: { onSuccess?: () => void; onFailure?: () => void }) => void;
policyMutationBlocked?: { value: boolean };
setSaveStatus?: (status: "idle" | "saving" | "saved" | "failed") => void;
toast?: (message: string, duration: number) => void;
t?: (key: string) => string;
}): McpQueryTimeoutHarness {
export function createMcpQueryTimeoutHarness(options: { source: string; input: { value: string }; policyQueryTimeoutSecs: number | null; toast?: (message: string, duration: number) => void; t?: (key: string) => string }): McpQueryTimeoutHarness {
const block = extractMcpQueryTimeoutDebounceBlock(options.source);
const javascript = ts.transpileModule(block, {
compilerOptions: { module: ts.ModuleKind.None, target: ts.ScriptTarget.ES2022 },
}).outputText;
const factory = new Function("mcpQueryTimeoutInput", "settingsStore", "saveMcpPolicy", "toast", "t", "mcpPolicyControlsDisabled", "setMcpQueryTimeoutSaveStatus", `${javascript}\nreturn { onMcpQueryTimeoutInput, flushMcpQueryTimeoutSave };`);
return factory(
options.input,
{ mcpGlobalPolicy: { queryTimeoutSecs: options.policyQueryTimeoutSecs } },
options.saveMcpPolicy,
options.toast ?? (() => {}),
options.t ?? ((key: string) => key),
options.policyMutationBlocked ?? { value: false },
options.setSaveStatus ?? (() => {}),
) as McpQueryTimeoutHarness;
const draft = { value: { queryTimeoutSecs: options.policyQueryTimeoutSecs } };
const factory = new Function("mcpQueryTimeoutInput", "mcpPolicyDraft", "toast", "t", `${javascript}\nreturn { onMcpQueryTimeoutInput, stageMcpQueryTimeoutDraft };`);
const harness = factory(options.input, draft, options.toast ?? (() => {}), options.t ?? ((key: string) => key)) as Omit<McpQueryTimeoutHarness, "draft">;
return { ...harness, draft };
}