Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test has a WPT meta file that expects 4 subtest issues.
- This WPT test may be referenced by the following Test IDs:
- /webmcp/imperative/executeTool-target-detachment.https.html - WPT Dashboard Interop Dashboard
<!DOCTYPE html>
<html>
<head>
<title>WebMCP executeTool with Target Frame Detachment</title>
<link rel="author" href="mailto:dom@chromium.org">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
</head>
<body>
<script>
const host_info = get_host_info();
promise_test(async t => {
const iframe = document.createElement('iframe');
iframe.src = host_info.HTTPS_REMOTE_ORIGIN + '/webmcp/imperative/resources/iframe-register-tool.html';
iframe.allow = 'tools *';
const load_promise = new Promise(resolve => iframe.onload = resolve);
document.body.appendChild(iframe);
t.add_cleanup(() => iframe.remove());
await load_promise;
const toolchange_promise = new Promise(resolve => {
document.modelContext.addEventListener('toolchange', resolve, { once: true });
});
// Have the iframe register a tool (it will never execute though).
iframe.contentWindow.postMessage({
action: 'register',
tool: {
name: 'iframe_tool',
description: 'Iframe tool description'
},
options: {
exposedTo: [self.origin]
}
}, '*');
await toolchange_promise;
const tools = await document.modelContext.getTools(
{fromOrigins: [host_info.HTTPS_REMOTE_ORIGIN]});
const tool = tools.find(t => t.name === 'iframe_tool');
assert_true(!!tool, 'Tool should be retrieved successfully');
// Detach the frame before executing the tool.
iframe.remove();
// Calling executeTool() on the detached tool will internally fail to locate
// the frame, and thus reject the caller's Promise.
//
await promise_rejects_dom(
t,
'InvalidStateError',
document.modelContext.executeTool(tool),
'Promise should be rejected with InvalidStateError when target frame is detached before execution'
);
}, 'executeTool() rejects when target frame is detached before execution');
promise_test(async t => {
const iframe = document.createElement('iframe');
iframe.src = host_info.HTTPS_REMOTE_ORIGIN + '/webmcp/imperative/resources/iframe-register-tool.html';
iframe.allow = 'tools *';
const load_promise = new Promise(resolve => iframe.onload = resolve);
document.body.appendChild(iframe);
t.add_cleanup(() => iframe.remove());
await load_promise;
const toolchange_promise = new Promise(resolve => {
document.modelContext.addEventListener('toolchange', resolve, { once: true });
});
// The iframe tool hangs forever, because it must be executing *while* we
// detach the iframe.
iframe.contentWindow.postMessage({
action: 'register',
hangsForever: true,
tool: {
name: 'iframe_tool_hanging',
description: 'Iframe tool description'
},
options: {
exposedTo: [self.origin]
}
}, '*');
await toolchange_promise;
const tools = await document.modelContext.getTools(
{fromOrigins: [host_info.HTTPS_REMOTE_ORIGIN]});
const tool = tools.find(t => t.name === 'iframe_tool_hanging');
assert_true(!!tool, 'Tool should be retrieved successfully');
// Execute the tool (hangs forever).
const execute_promise = document.modelContext.executeTool(tool);
iframe.remove();
// The execute promise rejects because the target document died
// mid-execution. See the bug referenced from the above test, about making
// the reason more specific.
await promise_rejects_dom(
t,
'UnknownError',
execute_promise,
'Promise should be rejected with UnknownError when target frame is detached mid-execution'
);
}, 'executeTool() rejects when target frame is detached mid-execution');
promise_test(async t => {
const iframe = document.createElement('iframe');
iframe.src = '/common/blank.html';
iframe.allow = 'tools *';
const load_promise = new Promise(resolve => iframe.onload = resolve);
document.body.appendChild(iframe);
t.add_cleanup(() => iframe.remove());
await load_promise;
const toolchange_promise = new Promise(resolve => {
document.modelContext.addEventListener('toolchange', resolve, { once: true });
});
await iframe.contentWindow.document.modelContext.registerTool({
name: 'sync_detach_on_execute',
description: 'Synchronously detaches its own frame during execute()',
execute: () => {
iframe.remove();
return new Promise(() => {});
}
});
await toolchange_promise;
const tools = await document.modelContext.getTools();
const tool = tools.find(t => t.name === 'sync_detach_on_execute');
assert_true(!!tool, 'Tool should be retrieved successfully');
await promise_rejects_dom(
t,
'UnknownError',
document.modelContext.executeTool(tool),
'Promise should reject when target frame synchronously detaches itself during execute()'
);
}, 'executeTool() rejects cleanly when tool callback synchronously detaches its own frame');
promise_test(async t => {
const iframe = document.createElement('iframe');
iframe.src = '/common/blank.html';
iframe.allow = 'tools *';
const load_promise = new Promise(resolve => iframe.onload = resolve);
document.body.appendChild(iframe);
t.add_cleanup(() => iframe.remove());
await load_promise;
const toolchange_promise = new Promise(resolve => {
document.modelContext.addEventListener('toolchange', resolve, { once: true });
});
const { promise: toolStarted, resolve: resolveToolStarted } = Promise.withResolvers();
await iframe.contentWindow.document.modelContext.registerTool({
name: 'sync_detach_on_abort',
description: 'Synchronously detaches its own frame when options.signal aborts',
execute: (input, options) => {
options.signal.addEventListener('abort', () => {
iframe.remove();
});
resolveToolStarted();
return new Promise(() => {});
}
});
await toolchange_promise;
const tools = await document.modelContext.getTools();
const tool = tools.find(t => t.name === 'sync_detach_on_abort');
assert_true(!!tool, 'Tool should be retrieved successfully');
const controller = new AbortController();
const execute_promise = document.modelContext.executeTool(
tool, {}, { signal: controller.signal });
await toolStarted;
controller.abort('abort and detach');
await promise_rejects_exactly(
t,
'abort and detach',
execute_promise,
'Promise should reject with abort reason when options.signal abort handler detaches frame'
);
}, 'executeTool() abort cleanly handles synchronous frame detachment inside options.signal abort listener');
</script>
</body>
</html>