Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
89 changes: 89 additions & 0 deletions src/engine/MacroChoiceEngine.entry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,95 @@ describe("MacroChoiceEngine user script variable propagation", () => {
expect(engine["params"].variables.override).toBe(1);
expect(engine["choiceExecutor"].variables).toBe(providedVariables);
});

it("treats `params.variables = {...}` as replacing the backing map", async () => {
const assignScript: IUserScript = {
id: "assign-script",
name: "Assign Script",
type: CommandType.UserScript,
path: "assign-script.js",
settings: {},
};

const assignChoice: IMacroChoice = {
id: "macro-assign",
name: "Macro assign",
type: "Macro",
command: false,
runOnStartup: false,
macro: {
id: "macro-assign",
name: "Macro assign",
commands: [assignScript],
} as IMacro,
};

mockGetUserScript.mockImplementationOnce(() => {
return Promise.resolve(async (params: { variables: Record<string, unknown> }) => {
params.variables = { foo: "bar" };
});
});

variables.set("old", "value");

const engine = new MacroChoiceEngine(
app,
plugin,
assignChoice,
choiceExecutor,
variables,
);

await engine.run();

expect(choiceExecutor.variables.get("foo")).toBe("bar");
expect(choiceExecutor.variables.has("old")).toBe(false);
});

it("does not clear variables when assigned the same backing map", async () => {
const assignSameScript: IUserScript = {
id: "assign-same-script",
name: "Assign Same Script",
type: CommandType.UserScript,
path: "assign-same-script.js",
settings: {},
};

const assignSameChoice: IMacroChoice = {
id: "macro-assign-same",
name: "Macro assign same",
type: "Macro",
command: false,
runOnStartup: false,
macro: {
id: "macro-assign-same",
name: "Macro assign same",
commands: [assignSameScript],
} as IMacro,
};

mockGetUserScript.mockImplementationOnce(() => {
return Promise.resolve(async (params: { variables: Record<string, unknown> }) => {
params.variables = params.variables;
params.variables.added = 2;
});
});

variables.set("old", "value");

const engine = new MacroChoiceEngine(
app,
plugin,
assignSameChoice,
choiceExecutor,
variables,
);

await engine.run();

expect(choiceExecutor.variables.get("old")).toBe("value");
expect(choiceExecutor.variables.get("added")).toBe(2);
});
});

describe("MacroChoiceEngine choice command cancellation", () => {
Expand Down
31 changes: 28 additions & 3 deletions src/engine/MacroChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,40 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine {
choiceExecutor: IChoiceExecutor,
sharedVariables: Map<string, unknown>
) {
return {
const variablesProxy = createVariablesProxy(sharedVariables);

const params = {
app,
quickAddApi: QuickAddApi.GetApi(app, plugin, choiceExecutor),
variables: createVariablesProxy(sharedVariables),
obsidian,
abort: (message?: string) => {
throw new MacroAbortError(message);
},
};
} as unknown as typeof this.params;

// Backward compatibility: some scripts assign `QuickAdd.variables = {...}`.
// Treat that as replacing the backing Map so templates can consume them.
Object.defineProperty(params, "variables", {
get: () => variablesProxy,
set: (next: unknown) => {
if (next === sharedVariables) return;

const entries =
next instanceof Map
? Array.from(next.entries())
: next && typeof next === "object"
? Object.entries(next as Record<string, unknown>)
: null;

sharedVariables.clear();

entries?.forEach(([key, value]) => sharedVariables.set(key, value));
},
enumerable: true,
configurable: false,
});

return params;
}

private initSharedVariables(
Expand Down