|
| 1 | +<!DOCTYPE html> |
| 2 | +<meta charset="utf-8"> |
| 3 | +<title>WebMCP: toolchange event and getTools() update on form attribute mutation</title> |
| 4 | +<link rel="author" href="mailto:dom@chromium.org"> |
| 5 | +<script src="/resources/testharness.js"></script> |
| 6 | +<script src="/resources/testharnessreport.js"></script> |
| 7 | +<script src="../resources/helpers.js"></script> |
| 8 | +<body> |
| 9 | + |
| 10 | +<form id="f1" toolname="my_tool" tooltitle="My Title" tooldescription="desc"> |
| 11 | + <input id="input1" type="text" name="input_name"> |
| 12 | +</form> |
| 13 | + |
| 14 | +<script> |
| 15 | +function waitForToolChange() { |
| 16 | + return new Promise(resolve => { |
| 17 | + navigator.modelContext.addEventListener('toolchange', resolve, { once: true }); |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +promise_test(async t => { |
| 22 | + const form = document.getElementById('f1'); |
| 23 | + |
| 24 | + // Wait for initial registration to complete. |
| 25 | + await waitForTool('my_tool'); |
| 26 | + |
| 27 | + let tools = await navigator.modelContext.getTools(); |
| 28 | + assert_equals(tools.length, 1, "Initially registered tool"); |
| 29 | + assert_equals(tools[0].name, 'my_tool'); |
| 30 | + assert_equals(tools[0].title, 'My Title'); |
| 31 | + assert_equals(tools[0].description, 'desc'); |
| 32 | + |
| 33 | + // Test updating tooltitle attribute |
| 34 | + { |
| 35 | + const changePromise = waitForToolChange(); |
| 36 | + form.setAttribute('tooltitle', 'New Title'); |
| 37 | + await changePromise; |
| 38 | + |
| 39 | + let tools = await navigator.modelContext.getTools(); |
| 40 | + assert_equals(tools.length, 1, "Still one tool"); |
| 41 | + assert_equals(tools[0].title, 'New Title', "Title is updated"); |
| 42 | + } |
| 43 | + |
| 44 | + // Test updating tooldescription attribute |
| 45 | + { |
| 46 | + const changePromise = waitForToolChange(); |
| 47 | + form.setAttribute('tooldescription', 'New Description'); |
| 48 | + await changePromise; |
| 49 | + |
| 50 | + let tools = await navigator.modelContext.getTools(); |
| 51 | + assert_equals(tools.length, 1); |
| 52 | + assert_equals(tools[0].description, 'New Description', "Description is updated"); |
| 53 | + } |
| 54 | + |
| 55 | + // Test updating toolname attribute |
| 56 | + { |
| 57 | + const changePromise = waitForToolChange(); |
| 58 | + form.setAttribute('toolname', 'new_tool_name'); |
| 59 | + await changePromise; |
| 60 | + |
| 61 | + let tools = await navigator.modelContext.getTools(); |
| 62 | + assert_equals(tools.length, 1); |
| 63 | + assert_equals(tools[0].name, 'new_tool_name', "Name is updated"); |
| 64 | + } |
| 65 | + |
| 66 | + // Test removing tooltitle attribute (should fall back to empty string) |
| 67 | + { |
| 68 | + const changePromise = waitForToolChange(); |
| 69 | + form.removeAttribute('tooltitle'); |
| 70 | + await changePromise; |
| 71 | + |
| 72 | + let tools = await navigator.modelContext.getTools(); |
| 73 | + assert_equals(tools.length, 1); |
| 74 | + assert_equals(tools[0].title, '', "Title should fall back to empty string"); |
| 75 | + } |
| 76 | + |
| 77 | + // Test removing tooldescription attribute (should unregister the tool since it's required) |
| 78 | + { |
| 79 | + const changePromise = waitForToolChange(); |
| 80 | + form.removeAttribute('tooldescription'); |
| 81 | + await changePromise; |
| 82 | + |
| 83 | + let tools = await navigator.modelContext.getTools(); |
| 84 | + assert_equals(tools.length, 0, "Tool should be unregistered when description is removed"); |
| 85 | + } |
| 86 | + |
| 87 | + // Restore the description attribute to re-register the tool |
| 88 | + { |
| 89 | + const changePromise = waitForToolChange(); |
| 90 | + form.setAttribute('tooldescription', 'Restored Description'); |
| 91 | + await changePromise; |
| 92 | + |
| 93 | + let tools = await navigator.modelContext.getTools(); |
| 94 | + assert_equals(tools.length, 1); |
| 95 | + assert_equals(tools[0].name, 'new_tool_name'); |
| 96 | + assert_equals(tools[0].description, 'Restored Description'); |
| 97 | + } |
| 98 | + |
| 99 | + // Test removing toolname attribute (should unregister the tool since it's required) |
| 100 | + { |
| 101 | + const changePromise = waitForToolChange(); |
| 102 | + form.removeAttribute('toolname'); |
| 103 | + await changePromise; |
| 104 | + |
| 105 | + let tools = await navigator.modelContext.getTools(); |
| 106 | + assert_equals(tools.length, 0, "Tool should be unregistered when name is removed"); |
| 107 | + } |
| 108 | +}, "Test that mutating or removing form attributes (toolname, tooldescription, tooltitle) fires toolchange and updates getTools() correctly"); |
| 109 | +</script> |
| 110 | +</body> |
0 commit comments