Skip to content

armgit / moli

Public Imported from GitHub
0

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

3.0 KB
Raw Blame
<!DOCTYPE html>
<title>tool execution requires a JSON Object input argument; and Arrays are Objects</title>
<link rel="author" href="mailto:[email protected]">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
promise_test(async t => {
  let executeReceivedArgs = null;
  const tool = {
    name: "test-tool",
    description: "Tool for testing various JSON primitive inputs",
    execute: (args) => {
      executeReceivedArgs = args;
      return "Success";
    }
  };

  await document.modelContext.registerTool(tool);
  const [registeredTool] = await document.modelContext.getTools();

  // Pass an array. Since arrays are ECMAScript objects, this succeeds.
  const arrayArgs = [1, 2, 3];
  const result = await document.modelContext.executeTool(registeredTool, arrayArgs);
  assert_equals(result, "Success");
  assert_array_equals(executeReceivedArgs, [1, 2, 3]);

  // Now pass non-Object JSON primitives as the argument, and observe a failure.
  const stringArgs = "hello";
  await promise_rejects_js(t, TypeError,
      document.modelContext.executeTool(registeredTool, stringArgs),
      "Primitive string should be rejected");

  const numberArgs = 123;
  await promise_rejects_js(t, TypeError,
      document.modelContext.executeTool(registeredTool, numberArgs),
      "Primitive number should be rejected");

  const booleanArgs = true;
  await promise_rejects_js(t, TypeError,
      document.modelContext.executeTool(registeredTool, booleanArgs),
      "Primitive boolean should be rejected");

  const nullArgs = null;
  await promise_rejects_js(t, TypeError,
      document.modelContext.executeTool(registeredTool, nullArgs),
      "null should be rejected");

  // Pass an object whose toJSON() method returns undefined, and observe
  // a failure.
  const nullishObject = { toJSON: () => undefined };
  await promise_rejects_js(t, TypeError,
      document.modelContext.executeTool(registeredTool, nullishObject),
      "Object whose toJSON() returns undefined should be rejected");

  // Omitting input argument defaults to {}
  executeReceivedArgs = null;
  const defaultResult = await document.modelContext.executeTool(registeredTool);
  assert_equals(defaultResult, "Success");
  assert_object_equals(executeReceivedArgs, {});

  // Per Web IDL, explicitly passing undefined for an optional argument is the
  // same as omitting it, so it also defaults to {}.
  executeReceivedArgs = null;
  const undefinedResult =
      await document.modelContext.executeTool(registeredTool, undefined);
  assert_equals(undefinedResult, "Success");
  assert_object_equals(executeReceivedArgs, {});

  // Same as above, but with a subsequent argument present.
  executeReceivedArgs = null;
  const undefinedWithOptionsResult =
      await document.modelContext.executeTool(registeredTool, undefined, {});
  assert_equals(undefinedWithOptionsResult, "Success");
  assert_object_equals(executeReceivedArgs, {});
}, "tool execution requires a JSON Object input argument; and Arrays are Objects");
</script>