Skip to content

Commit eec21e3

Browse files
sirtimidclaude
andcommitted
fix(kernel-browser-runtime): fix test mock recursion and iframe creation error handling
- Save original document.createElement before mocking to prevent infinite recursion when creating non-iframe elements in tests - Move iframe creation inside try block so ID is removed from launchesInProgress if createIframe throws Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 4424351 commit eec21e3

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

packages/kernel-browser-runtime/src/ui/UIOrchestrator.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ const makeIframe = (): HTMLIFrameElement & {
145145
};
146146
};
147147

148+
// Save original createElement before any mocking
149+
const originalCreateElement = document.createElement.bind(document);
150+
148151
describe('UIOrchestrator', () => {
149152
let mainSlot: ReturnType<typeof makeContainer>;
150153
let orchestrator: UIOrchestrator;
@@ -163,7 +166,7 @@ describe('UIOrchestrator', () => {
163166
createdIframes.push(iframe);
164167
return iframe as unknown as HTMLElement;
165168
}
166-
return document.createElement(tagName);
169+
return originalCreateElement(tagName);
167170
},
168171
);
169172

packages/kernel-browser-runtime/src/ui/UIOrchestrator.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,17 @@ export class UIOrchestrator {
143143

144144
this.#logger.info(`Launching UI vat: ${id} in slot: ${slot}`);
145145

146-
const iframe = this.#createIframe(id, uri, title, visible);
147-
slotElement.appendChild(iframe);
148-
146+
let iframe: HTMLIFrameElement | undefined;
149147
let port: MessagePort;
150148
try {
149+
iframe = this.#createIframe(id, uri, title, visible);
150+
slotElement.appendChild(iframe);
151+
151152
// Wait for iframe to load and establish MessageChannel
152153
port = await this.#establishConnection(iframe);
153154
} catch (error) {
154-
// Clean up iframe if connection establishment fails
155-
iframe.remove();
155+
// Clean up iframe if it was created and appended
156+
iframe?.remove();
156157
this.#launchesInProgress.delete(id);
157158
throw error;
158159
}

0 commit comments

Comments
 (0)