Automatic update from web-platform-tests [WebMCP] Make registerTool() return a promise Spec issue: https://github.com/webmachinelearning/webmcp/issues/175 Bug: 489045948 Change-Id: I254250d12ad78b95626939e0f7e22fd6fe56f70f Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7916507 Reviewed-by: Alex Moshchuk <alexmos@chromium.org> Reviewed-by: Dominic Farolino <dom@chromium.org> Reviewed-by: Khushal Sagar <khushalsagar@chromium.org> Commit-Queue: Fr <beaufort.francois@gmail.com> Cr-Commit-Position: refs/heads/main@{#1654308} -- wpt-commits: 925e2819ce321d53de6069f93dcf1e37171381b9 wpt-pr: 60974
201 lines
8.0 KiB
HTML
201 lines
8.0 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Same-origin tool exposure</title>
|
|
<link rel="author" href="mailto:dom@chromium.org">
|
|
<script src="/resources/testharness.js"></script>
|
|
<script src="/resources/testharnessreport.js"></script>
|
|
<script src="resources/helpers.js"></script>
|
|
</head>
|
|
<body>
|
|
<script>
|
|
async function setupIframe(t) {
|
|
const iframe = document.createElement('iframe');
|
|
iframe.src = 'resources/iframe-register-tool.html';
|
|
|
|
const load_promise = new Promise(resolve => iframe.onload = resolve);
|
|
document.body.appendChild(iframe);
|
|
t.add_cleanup(() => iframe.remove());
|
|
await load_promise;
|
|
return iframe;
|
|
}
|
|
|
|
promise_test(async t => {
|
|
const controller = new AbortController();
|
|
await document.modelContext.registerTool({
|
|
name: 'parent_tool_default',
|
|
description: 'Parent tool with default exposure',
|
|
inputSchema: { type: 'object', properties: { query: { type: 'string' } } },
|
|
annotations: { readOnlyHint: true, untrustedContentHint: false },
|
|
execute: async () => 'hello'
|
|
}, { signal: controller.signal });
|
|
|
|
const iframe = await setupIframe(t);
|
|
|
|
// Ask iframe what it sees.
|
|
iframe.contentWindow.postMessage('getTools', '*');
|
|
let response = await waitForIframeMessage('getToolsResponse');
|
|
|
|
const [tool] = response.tools;
|
|
assert_true(!!tool, 'Same-origin iframe should see parent tool with no exposedTo');
|
|
assert_true(toolsAreEqual(tool, {
|
|
name: 'parent_tool_default',
|
|
description: 'Parent tool with default exposure',
|
|
inputSchema: JSON.stringify({ type: 'object', properties: { query: { type: 'string' } } }),
|
|
annotations: { readOnlyHint: true, untrustedContentHint: false },
|
|
origin: self.origin
|
|
}), 'Tool details should match');
|
|
|
|
// Iframe executes parent's tool.
|
|
iframe.contentWindow.postMessage({action: 'execute', name: 'parent_tool_default'}, '*');
|
|
const exec_response = await waitForIframeMessage('executeResponse');
|
|
assert_true(exec_response.success, 'Iframe should successfully execute parent tool');
|
|
assert_equals(exec_response.result, 'hello', 'Iframe should get correct result from parent tool');
|
|
|
|
// Unregister and verify child gets event.
|
|
iframe.contentWindow.postMessage('listenForToolchange', '*');
|
|
await waitForIframeMessage('toolchange_listening_ack');
|
|
|
|
const child_unreg_promise = waitForIframeMessage('toolchange_result');
|
|
const parent_unreg_promise = new Promise(resolve => {
|
|
document.modelContext.addEventListener('toolchange', resolve, {once: true});
|
|
});
|
|
|
|
controller.abort();
|
|
|
|
const [child_response] = await Promise.all([child_unreg_promise, parent_unreg_promise]);
|
|
assert_equals(child_response.result, 'fired', 'Same-origin iframe should receive toolchange event on unregistration');
|
|
|
|
iframe.contentWindow.postMessage('getTools', '*');
|
|
response = await waitForIframeMessage('getToolsResponse');
|
|
assert_array_equals(response.tools, [], 'Same-origin iframe should no longer see any tools after unregistration');
|
|
}, 'Parent tool with missing `exposedTo` array is visible to same-origin iframe');
|
|
|
|
promise_test(async t => {
|
|
const controller = new AbortController();
|
|
await document.modelContext.registerTool({
|
|
name: 'parent_tool_empty',
|
|
description: 'Parent tool with empty exposedTo',
|
|
inputSchema: { type: 'object', properties: { query: { type: 'string' } } },
|
|
execute: async () => 'hello'
|
|
}, { exposedTo: [], signal: controller.signal });
|
|
|
|
const iframe = await setupIframe(t);
|
|
|
|
iframe.contentWindow.postMessage('getTools', '*');
|
|
let response = await waitForIframeMessage('getToolsResponse');
|
|
|
|
const [tool] = response.tools;
|
|
assert_true(!!tool, 'Same-origin iframe should see parent tool with empty exposedTo');
|
|
assert_true(toolsAreEqual(tool, {
|
|
name: 'parent_tool_empty',
|
|
description: 'Parent tool with empty exposedTo',
|
|
inputSchema: JSON.stringify({ type: 'object', properties: { query: { type: 'string' } } }),
|
|
origin: self.origin
|
|
}), 'Tool details should match');
|
|
|
|
// Iframe executes parent's tool.
|
|
iframe.contentWindow.postMessage({action: 'execute', name: 'parent_tool_empty'}, '*');
|
|
const exec_response = await waitForIframeMessage('executeResponse');
|
|
assert_true(exec_response.success, 'Iframe should successfully execute parent tool');
|
|
assert_equals(exec_response.result, 'hello', 'Iframe should get correct result from parent tool');
|
|
|
|
// Unregister and verify child gets event.
|
|
iframe.contentWindow.postMessage('listenForToolchange', '*');
|
|
await waitForIframeMessage('toolchange_listening_ack');
|
|
|
|
const child_unreg_promise = waitForIframeMessage('toolchange_result');
|
|
const parent_unreg_promise = new Promise(resolve => {
|
|
document.modelContext.addEventListener('toolchange', resolve, {once: true});
|
|
});
|
|
|
|
controller.abort();
|
|
|
|
const [child_response] = await Promise.all([child_unreg_promise, parent_unreg_promise]);
|
|
assert_equals(child_response.result, 'fired', 'Same-origin iframe should receive toolchange event on unregistration');
|
|
|
|
iframe.contentWindow.postMessage('getTools', '*');
|
|
response = await waitForIframeMessage('getToolsResponse');
|
|
assert_array_equals(response.tools, [], 'Same-origin iframe should no longer see any tools after unregistration');
|
|
}, 'Parent tool with empty `exposedTo` array is visible to same-origin iframe');
|
|
|
|
promise_test(async t => {
|
|
const iframe = await setupIframe(t);
|
|
|
|
iframe.contentWindow.postMessage({
|
|
action: 'register',
|
|
tool: {
|
|
name: 'iframe_tool_default',
|
|
description: 'Iframe tool with default exposure'
|
|
}
|
|
}, '*');
|
|
|
|
await new Promise(resolve => {
|
|
document.modelContext.addEventListener('toolchange', resolve, { once: true });
|
|
});
|
|
|
|
let tools = await document.modelContext.getTools();
|
|
const [tool] = tools;
|
|
assert_true(toolsAreEqual(tool, {
|
|
name: 'iframe_tool_default',
|
|
description: 'Iframe tool with default exposure',
|
|
origin: iframe.contentWindow.origin
|
|
}), 'Tool details should match');
|
|
|
|
// Parent executes iframe's tool.
|
|
const result = await document.modelContext.executeTool(tool, '{}');
|
|
assert_equals(result, 'hello from iframe', 'Parent should get correct result from iframe tool');
|
|
|
|
// Unregister tool.
|
|
const unregister_promise = new Promise(resolve => {
|
|
document.modelContext.addEventListener('toolchange', resolve, { once: true });
|
|
});
|
|
iframe.contentWindow.postMessage({ action: 'unregister', name: 'iframe_tool_default' }, '*');
|
|
await unregister_promise;
|
|
|
|
tools = await document.modelContext.getTools();
|
|
assert_false(tools.some(t => t.name === 'iframe_tool_default'), 'Same-origin parent should no longer see iframe tool after unregistration');
|
|
}, 'Same-origin iframe tool with default exposure is visible to parent');
|
|
|
|
promise_test(async t => {
|
|
const iframe = await setupIframe(t);
|
|
|
|
iframe.contentWindow.postMessage({
|
|
action: 'register',
|
|
tool: {
|
|
name: 'iframe_tool_empty',
|
|
description: 'Iframe tool with empty exposedTo'
|
|
},
|
|
options: { exposedTo: [] }
|
|
}, '*');
|
|
|
|
await new Promise(resolve => {
|
|
document.modelContext.addEventListener('toolchange', resolve, { once: true });
|
|
});
|
|
|
|
let tools = await document.modelContext.getTools();
|
|
const [tool] = tools;
|
|
assert_true(toolsAreEqual(tool, {
|
|
name: 'iframe_tool_empty',
|
|
description: 'Iframe tool with empty exposedTo',
|
|
origin: iframe.contentWindow.origin
|
|
}), 'Tool details should match');
|
|
|
|
// Parent executes iframe's tool.
|
|
const result = await document.modelContext.executeTool(tool, '{}');
|
|
assert_equals(result, 'hello from iframe', 'Parent should get correct result from iframe tool');
|
|
|
|
// Unregister tool.
|
|
const unregister_promise = new Promise(resolve => {
|
|
document.modelContext.addEventListener('toolchange', resolve, { once: true });
|
|
});
|
|
iframe.contentWindow.postMessage({ action: 'unregister', name: 'iframe_tool_empty' }, '*');
|
|
await unregister_promise;
|
|
|
|
tools = await document.modelContext.getTools();
|
|
assert_false(tools.some(t => t.name === 'iframe_tool_empty'), 'Same-origin parent should no longer see iframe tool after unregistration');
|
|
}, 'Same-origin iframe tool with empty exposedTo array is visible to parent');
|
|
</script>
|
|
</body>
|
|
</html>
|