Файли
openidea/src/platform/context.test.ts
T
Arnaud Ritti a3d5336642 feat(plugins): let plugin code set context keys for its when clauses (#1762)
Plugin API 1.2 adds `ui.set-context(key, value)` and `ui.remove-context(key)`: a plugin sets `plugin.<id>.<key>` for `when` clauses, typed as `context-value` (flag, number or text), in the window the call came from or every window outside a command or event; a window's value wins over the global one. Keys are 1-64 of `A-Za-z0-9_-`, at most 100 per plugin, kept across window reloads and idle reclaim, dropped when the plugin stops or the window closes; remote windows get them through the dispatcher. The Rust SDK takes `impl Into<ContextValue>`. All components are rebuilt for the WIT change.

Closes #1622

Co-authored-by: arnaud-ritti <[email protected]>
2026-11-06 23:02:34 +08:00

44 рядки
1.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { clearContext, evaluateWhen, getContext, setContext } from "./context";
describe("when clauses", () => {
const ctx = { editorFocus: true, lang: "rust", count: 2, debug: false };
it.each([
["", true],
["editorFocus", true],
["!editorFocus", false],
["editorFocus && debug", false],
["editorFocus || debug", true],
["lang == rust", true],
["lang == 'rust'", true],
["lang != rust", false],
["count == 2", true],
["!(debug || !editorFocus)", true],
["missing", false],
])("%s → %s", (expr, expected) => {
expect(evaluateWhen(expr, ctx)).toBe(expected);
});
it("rejects malformed clauses", () => {
expect(() => evaluateWhen("a &&", ctx)).toThrow();
expect(() => evaluateWhen("(a", ctx)).toThrow();
});
});
describe("clearContext", () => {
it("deletes the key and keeps the others", () => {
setContext("a.x", 1);
setContext("a.y", 2);
clearContext("a.x");
expect("a.x" in getContext()).toBe(false);
expect(getContext()["a.y"]).toBe(2);
clearContext("a.y");
});
it("does nothing for an absent key", () => {
const before = getContext();
clearContext("a.never");
expect(getContext()).toBe(before);
});
});