Skip to content

armgit / moli

Public Imported from GitHub
0

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

2.0 KB
Raw Blame
<!DOCTYPE html>
<html>
<head>
<title>WebMCP Opaque Origin Declarative Tool</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>
</head>
<body>

<form toolname="opaque_tool" tooldescription="Opaque tool description">
  <input type="text" name="param1">
</form>

<script>
promise_test(async t => {
  // Assert that our CSP header took effect.
  assert_equals(self.origin, 'null');

  // Wait for the tool to register (happens synchronously upon parsing, but
  // getTools is async)
  const tool = await waitForTool('opaque_tool');
  assert_true(!!tool, "Tool should be successfully registered from an opaque origin");

  await promise_rejects_dom(
    t,
    'NotSupportedError',
    document.modelContext.executeTool(tool, {"param1":"value"}),
    'executeTool() must reject with NotSupportedError in opaque origin documents'
  );
}, 'An opaque origin document can register but not execute its own declarative tools');

promise_test(async t => {
  const tools = await document.modelContext.getTools();
  const tool = tools.find(t => t.name === 'opaque_tool');

  let events = [];
  const p = document.modelContext.executeTool(tool);

  // Since `executeTool()` synchronously rejects the Promise for opaque origins,
  // the microtask to run the catch reaction is already queued before
  // `queueMicrotask()` is invoked below.
  p.catch(() => events.push('rejection'));

  // Queue another microtask immediately after.
  queueMicrotask(() => events.push('microtask'));

  await promise_rejects_dom(
    t,
    'NotSupportedError',
    document.modelContext.executeTool(tool, {"param1":"value"}),
    'executeTool() must reject with NotSupportedError in opaque origin documents'
  );

  assert_array_equals(events, ['rejection', 'microtask'],
    'returned promise is rejected before custom microtask is queued');
}, 'executeTool() rejects synchronously for opaque origins');
</script>
</body>
</html>