Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

<!DOCTYPE html>
<meta charset="utf-8">
<link rel="author" href="mailto:beaufort.francois@gmail.com">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/webmcp/resources/helpers.js"></script>
<form id="form1" toolname="submit_test" tooldescription="Submit test form1">
<button id="button1" type=submit>Submit</button>
</form>
<form id="form2" toolname="submit_test2" tooldescription="Submit test form2">
<button id="button2" type=submit>Submit</button>
</form>
<script>
promise_test(async t => {
await waitForTool('submit_test');
const form1 = document.querySelector('#form1');
const form2 = document.querySelector('#form2');
const button1 = document.querySelector('#button1');
const button2 = document.querySelector('#button2');
assert_false(form1.matches(':tool-form-active'));
assert_false(form2.matches(':tool-form-active'));
assert_false(button1.matches(':tool-submit-active'));
assert_false(button2.matches(':tool-submit-active'));
const assertActiveDuringToolactivated = e => {
assert_equals(e.toolName, 'submit_test');
assert_true(form1.matches(':tool-form-active'));
assert_false(form2.matches(':tool-form-active'));
assert_true(button1.matches(':tool-submit-active'));
assert_false(button2.matches(':tool-submit-active'));
};
const events = [];
const modelContextEvents = [];
const modelContextToolActivatedPromise = new Promise(r => {
const handler = e => {
modelContextEvents.push(e);
assertActiveDuringToolactivated(e);
r();
};
document.modelContext.addEventListener('toolactivated', handler);
t.add_cleanup(() => document.modelContext.removeEventListener('toolactivated', handler));
});
const tools = await document.modelContext.getTools();
const tool = tools.find(t => t.name === 'submit_test');
const controller = new AbortController();
const executionPromise = document.modelContext.executeTool(tool, {}, { signal: controller.signal });
await modelContextToolActivatedPromise;
assert_equals(modelContextEvents.length, 1);
const modelContextEvent = modelContextEvents[0];
assert_true(modelContextEvent instanceof ToolActivatedEvent);
assert_equals(modelContextEvent.target, document.modelContext);
const assertInactiveDuringToolcancel = e => {
assert_equals(e.toolName, 'submit_test');
assert_false(form1.matches(':tool-form-active'), "form1 matches during toolcancel");
assert_false(form2.matches(':tool-form-active'), "form2 matches during toolcancel");
assert_false(button1.matches(':tool-submit-active'), "button1 matches during toolcancel");
assert_false(button2.matches(':tool-submit-active'), "button2 matches during toolcancel");
};
const modelContextCancelEvents = [];
const modelContextToolCancelPromise = new Promise(r => {
const handler = e => {
modelContextCancelEvents.push(e);
assertInactiveDuringToolcancel(e);
r();
};
document.modelContext.addEventListener('toolcancel', handler);
t.add_cleanup(() => document.modelContext.removeEventListener('toolcancel', handler));
});
controller.abort();
let executionRejectionValue = null;
await Promise.all([
executionPromise.catch(e => {events.push('execution_rejected'); executionRejectionValue = e}),
modelContextToolCancelPromise.then(() => events.push('toolcancel')),
]);
assert_array_equals(events, ['execution_rejected', 'toolcancel'],
'the execution rejects before toolcancel is dispatched');
assert_array_equals(modelContextEvents, [modelContextEvent], 'toolactivated is dispatched once per execution');
assert_equals(modelContextCancelEvents.length, 1, 'toolcancel is dispatched once per execution');
assert_true(modelContextCancelEvents[0] instanceof ToolCancelEvent);
assert_equals(modelContextCancelEvents[0].target, document.modelContext);
assert_true(executionRejectionValue instanceof DOMException);
assert_equals(executionRejectionValue.name, "AbortError");
assert_false(form1.matches(':tool-form-active'), "form1 matches");
assert_false(form2.matches(':tool-form-active'), "form2 matches");
assert_false(button1.matches(':tool-submit-active'), "button1 matches");
assert_false(button2.matches(':tool-submit-active'), "button2 matches");
}, 'executeTool signal successfully resets tool pseudo-classes');
</script>