-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathtoolchange-on-attribute-mutation.https.html
More file actions
132 lines (109 loc) · 4.49 KB
/
toolchange-on-attribute-mutation.https.html
File metadata and controls
132 lines (109 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!DOCTYPE html>
<meta charset="utf-8">
<title>WebMCP: toolchange event and getTools() update on form attribute mutation</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>
<body>
<form id="f1" toolname="my_tool" tooltitle="My Title" tooldescription="desc">
<input id="input1" type="text" name="input_name">
</form>
<script>
function waitForToolChange() {
return new Promise(resolve => {
navigator.modelContext.addEventListener('toolchange', resolve, { once: true });
});
}
promise_test(async t => {
const form = document.getElementById('f1');
// Wait for initial registration to complete.
await waitForTool('my_tool');
let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 1, "Initially registered tool");
assert_equals(tools[0].name, 'my_tool');
assert_equals(tools[0].title, 'My Title');
assert_equals(tools[0].description, 'desc');
// Test updating tooltitle attribute
{
const changePromise = waitForToolChange();
form.setAttribute('tooltitle', 'New Title');
await changePromise;
let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 1, "Still one tool");
assert_equals(tools[0].title, 'New Title', "Title is updated");
}
// Test updating tooldescription attribute
{
const changePromise = waitForToolChange();
form.setAttribute('tooldescription', 'New Description');
await changePromise;
let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 1);
assert_equals(tools[0].description, 'New Description', "Description is updated");
}
// Test updating toolname attribute
{
const changePromise = waitForToolChange();
form.setAttribute('toolname', 'new_tool_name');
await changePromise;
let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 1);
assert_equals(tools[0].name, 'new_tool_name', "Name is updated");
}
// Test removing tooltitle attribute (should fall back to empty string)
{
const changePromise = waitForToolChange();
form.removeAttribute('tooltitle');
await changePromise;
let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 1);
assert_equals(tools[0].title, '', "Title should fall back to empty string");
}
// Test removing tooldescription attribute (should unregister the tool since it's required)
{
const changePromise = waitForToolChange();
form.removeAttribute('tooldescription');
await changePromise;
let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 0, "Tool should be unregistered when description is removed");
}
// Restore the description attribute to re-register the tool
{
const changePromise = waitForToolChange();
form.setAttribute('tooldescription', 'Restored Description');
await changePromise;
let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 1);
assert_equals(tools[0].name, 'new_tool_name');
assert_equals(tools[0].description, 'Restored Description');
}
// Test adding toolautosubmit attribute
{
const changePromise = waitForToolChange();
form.setAttribute('toolautosubmit', '');
await changePromise;
let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 1, "Tool still registered after adding toolautosubmit");
assert_equals(tools[0].name, 'new_tool_name', "Tool name is still the same");
}
// Test removing toolautosubmit attribute
{
const changePromise = waitForToolChange();
form.removeAttribute('toolautosubmit');
await changePromise;
let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 1, "Tool still registered after removing toolautosubmit");
assert_equals(tools[0].name, 'new_tool_name', "Tool name is still the same");
}
// Test removing toolname attribute (should unregister the tool since it's required)
{
const changePromise = waitForToolChange();
form.removeAttribute('toolname');
await changePromise;
let tools = await navigator.modelContext.getTools();
assert_equals(tools.length, 0, "Tool should be unregistered when name is removed");
}
}, "Test that mutating or removing form attributes (toolname, tooldescription, tooltitle, toolautosubmit) fires toolchange and updates getTools() correctly");
</script>
</body>