Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
48 changes: 47 additions & 1 deletion src/engine/SingleMacroEngine.member-access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,52 @@ describe("SingleMacroEngine member access", () => {
expect(mockGetUserScript).toHaveBeenCalledWith(scriptB, app);
});

it("loads a selector-targeted script after pre-commands run", async () => {
const preCommand = {
id: "wait-1",
name: "Wait",
type: CommandType.Wait,
} as ICommand;
const scriptA = createUserScript("script-a", "a.js", {
name: "Alpha Script",
});
const scriptB = createUserScript("script-b", "b.js", {
name: "Beta Script",
});

let ready = false;
const engineInstance = macroEngineFactory();
engineInstance.runSubset = vi.fn().mockImplementation(async () => {
ready = true;
});
macroEngineFactory = () => engineInstance;

mockGetUserScript.mockImplementation(async (command: IUserScript) => {
if (command.id !== "script-b") {
return { alpha: vi.fn() };
}

return ready
? { beta: () => "late-bound-result" }
: { alpha: vi.fn() };
});

const engine = new SingleMacroEngine(
app,
plugin,
[baseMacroChoice([preCommand, scriptA, scriptB])],
choiceExecutor,
);

const result = await engine.runAndGetOutput(
"My Macro::Beta Script::beta",
);

expect(result).toBe("late-bound-result");
expect(mockGetUserScript).toHaveBeenCalledTimes(1);
expect(mockGetUserScript).toHaveBeenCalledWith(scriptB, app);
});

it("aborts when a selector matches duplicate script names", async () => {
const scriptA = createUserScript("script-a", "a.js", {
name: "Shared Script",
Expand Down Expand Up @@ -411,7 +457,7 @@ describe("SingleMacroEngine member access", () => {

await expect(
engine.runAndGetOutput("My Macro::Alpha Script::beta"),
).rejects.toThrow("targeted script 'Alpha Script'");
).rejects.toThrow("routes member access to 'Alpha Script'");
});

it("propagates aborts when the export aborts", async () => {
Expand Down
50 changes: 7 additions & 43 deletions src/engine/SingleMacroEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type UserScriptCandidate = {
command: IUserScript;
index: number;
exportsRef?: unknown;
resolvedMember: { found: boolean; value?: unknown };
resolvedMember?: { found: boolean; value?: unknown };
};

type MemberAccessSelection = {
Expand Down Expand Up @@ -98,16 +98,14 @@ export class SingleMacroEngine {
throw new Error(`macro '${macroName}' does not exist.`);
}

const preloadedScripts = new Map<string, unknown>();

// Create a dedicated engine for this macro
const engine = new MacroChoiceEngine(
this.app,
this.plugin,
macroChoice,
this.choiceExecutor,
this.variables,
preloadedScripts,
undefined,
context?.label,
);

Expand All @@ -116,7 +114,6 @@ export class SingleMacroEngine {
engine,
macroChoice,
memberAccess,
preloadedScripts,
);

this.ensureNotAborted();
Expand Down Expand Up @@ -154,7 +151,6 @@ export class SingleMacroEngine {
engine: MacroChoiceEngine,
macroChoice: IMacroChoice,
memberAccess: string[],
preloadedScripts: Map<string, unknown>,
): Promise<{ executed: boolean; result?: unknown }> {
const originalCommands = macroChoice.macro?.commands;
if (!originalCommands?.length) {
Expand All @@ -176,7 +172,6 @@ export class SingleMacroEngine {
macroChoice,
userScriptCommands,
memberAccess,
preloadedScripts,
);
const preCommands = originalCommands.slice(0, selection.candidate.index);

Expand Down Expand Up @@ -210,11 +205,6 @@ export class SingleMacroEngine {
);
}

const cacheKey = userScriptCommand.path ?? userScriptCommand.id;
if (cacheKey && exportsRef !== undefined && exportsRef !== null) {
preloadedScripts.set(cacheKey, exportsRef);
}

const settingsExport =
typeof exportsRef === "object" || typeof exportsRef === "function"
? (exportsRef as Record<string, unknown>).settings
Expand All @@ -228,9 +218,8 @@ export class SingleMacroEngine {
}

const resolvedMember =
selection.candidate.exportsRef !== undefined
? selection.candidate.resolvedMember
: this.resolveMemberAccess(exportsRef, selection.memberAccess);
selection.candidate.resolvedMember ??
this.resolveMemberAccess(exportsRef, selection.memberAccess);

if (!resolvedMember.found) {
throw new MacroAbortError(
Expand Down Expand Up @@ -327,14 +316,12 @@ export class SingleMacroEngine {
macroChoice: IMacroChoice,
userScriptCommands: Array<{ command: IUserScript; index: number }>,
memberAccess: string[],
preloadedScripts: Map<string, unknown>,
): Promise<MemberAccessSelection> {
if (userScriptCommands.length === 1) {
return {
candidate: {
command: userScriptCommands[0].command,
index: userScriptCommands[0].index,
resolvedMember: { found: false },
},
memberAccess,
};
Expand All @@ -347,44 +334,21 @@ export class SingleMacroEngine {
);

if (selectorMatch) {
const exportsRef = await getUserScript(selectorMatch.command, this.app);
const cacheKey = selectorMatch.command.path ?? selectorMatch.command.id;
if (cacheKey && exportsRef !== undefined && exportsRef !== null) {
preloadedScripts.set(cacheKey, exportsRef);
}

const resolvedMember = this.resolveMemberAccess(
exportsRef,
selectorMatch.memberAccess,
);
if (!resolvedMember.found) {
throw new MacroAbortError(
`Macro '${macroChoice.name}' targeted script '${selectorMatch.command.name}', but that script does not export '${selectorMatch.memberAccess.join(
"::",
)}'.`,
);
}

return {
candidate: {
command: selectorMatch.command,
index: selectorMatch.index,
exportsRef,
resolvedMember,
},
memberAccess: selectorMatch.memberAccess,
};
}

const candidates: UserScriptCandidate[] = [];
const candidates: Array<
UserScriptCandidate & { resolvedMember: { found: boolean; value?: unknown } }
> = [];

for (const entry of userScriptCommands) {
const exportsRef = await getUserScript(entry.command, this.app);
const cacheKey = entry.command.path ?? entry.command.id;
if (cacheKey && exportsRef !== undefined && exportsRef !== null) {
preloadedScripts.set(cacheKey, exportsRef);
}

candidates.push({
command: entry.command,
index: entry.index,
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/macro-member-access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import type {

const VAULT = "dev";
const PLUGIN_ID = "quickadd";
const WAIT_OPTS = { timeoutMs: 10_000, intervalMs: 200 };
const waitTimeoutMs = Number(process.env.E2E_TIMEOUT_MS) || 15_000;
const WAIT_OPTS = { timeoutMs: waitTimeoutMs, intervalMs: 200 };
const TEST_PREFIX = "__qa-test-964-";

let obsidian: ObsidianClient;
Expand Down
Loading