Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

6.1 KB
Raw Blame
<!DOCTYPE html>
<meta charset="utf-8">
<title>WebMCP: toolchange event and getTools() update on form attribute mutation</title>
<link rel="author" href="mailto:[email protected]">
<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 => {
    document.modelContext.addEventListener('toolchange', resolve, { once: true });
  });
}

promise_test(async t => {
  const form = document.getElementById('f1');

  // Wait for initial registration to complete.
  const tool = await waitForTool('my_tool');
  assert_equals(tool.name, 'my_tool');
  assert_equals(tool.title, 'My Title');
  assert_equals(tool.description, 'desc');

  // Test updating tooltitle attribute
  {
    const changePromise = waitForToolChange();
    form.setAttribute('tooltitle', 'New Title');
    await changePromise;

    let tools = await document.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 document.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 document.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 document.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 document.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 document.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 document.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 document.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 document.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");

promise_test(async t => {
  const form = document.createElement('form');
  form.setAttribute('toolname', 'attr_test_tool');
  form.setAttribute('tooldescription', 'desc');
  const input = document.createElement('input');
  input.name = 'input_name';
  form.appendChild(input);
  document.body.appendChild(form);
  t.add_cleanup(() => form.remove());

  await waitForTool('attr_test_tool');

  let toolchangeCount = 0;
  const listener = () => toolchangeCount++;
  document.modelContext.addEventListener('toolchange', listener);
  t.add_cleanup(() => document.modelContext.removeEventListener('toolchange', listener));

  input.setAttribute('data-unrelated', 'value');
  await new Promise(resolve => t.step_timeout(resolve, 50));
  assert_equals(toolchangeCount, 0, 'toolchange should not fire on unrelated attribute');
}, "No toolchange event on unrelated attribute mutation");

promise_test(async t => {
  const form = document.createElement('form');
  form.setAttribute('toolname', 'redundant_test_tool');
  form.setAttribute('tooldescription', 'desc');
  const input = document.createElement('input');
  input.name = 'input_name';
  form.appendChild(input);
  document.body.appendChild(form);
  t.add_cleanup(() => form.remove());

  await waitForTool('redundant_test_tool');

  let toolchangeCount = 0;
  const listener = () => toolchangeCount++;
  document.modelContext.addEventListener('toolchange', listener);
  t.add_cleanup(() => document.modelContext.removeEventListener('toolchange', listener));

  input.remove();
  form.appendChild(input);
  await new Promise(resolve => t.step_timeout(resolve, 50));
  assert_equals(toolchangeCount, 0, 'toolchange should not fire on redundant schema mutation');
}, "No toolchange event on redundant schema mutation");
</script>
</body>