Files
adolf/packages/ai/src/providers/openai-tool-projection.test.ts
alvis bedb527145
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Vendor OpenClaw source as Adolf fork baseline
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11),
free to diverge. Tree copied sans upstream .git; upstream remote added for
future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19.
Preserves docs/ARCHITECTURE.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
2026-07-05 09:36:54 +00:00

284 lines
8.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
projectOpenAITools,
reconcileOpenAICompletionsToolChoice,
reconcileOpenAIResponsesToolChoice,
} from "./openai-tool-projection.js";
describe("OpenAI tool projection", () => {
it("keeps healthy tools when sibling descriptors or schemas are unreadable", () => {
const projection = projectOpenAITools([
{
get name(): never {
throw new Error("name exploded");
},
parameters: {},
},
{
name: "bad_schema",
parameters: {
type: "object",
get properties(): never {
throw new Error("properties exploded");
},
},
},
{
name: "lookup",
get description(): never {
throw new Error("description exploded");
},
parameters: { type: "object", properties: {} },
},
]);
expect(projection.tools).toEqual([
{
toolIndex: 2,
name: "lookup",
parameters: { type: "object", properties: {} },
},
]);
expect(projection.diagnostics).toHaveLength(2);
});
it("reads optional descriptions once before projecting them", () => {
let descriptionReads = 0;
const projection = projectOpenAITools([
{
name: "lookup",
get description() {
descriptionReads += 1;
return descriptionReads === 1 ? "Lookup" : Symbol("invalid");
},
parameters: {},
},
]);
expect(projection.tools[0]?.description).toBe("Lookup");
expect(descriptionReads).toBe(1);
});
it("keeps a healthy pinned Responses function choice", () => {
const projection = projectOpenAITools([{ name: "lookup", parameters: {} }]);
expect(
reconcileOpenAIResponsesToolChoice({ type: "function", name: "lookup" }, projection),
).toEqual({ type: "function", name: "lookup" });
});
it("materializes pinned function choices after one name read", () => {
const projection = projectOpenAITools([{ name: "lookup", parameters: {} }]);
let responsesNameReads = 0;
let completionsNameReads = 0;
const responsesChoice = {
type: "function",
get name() {
responsesNameReads += 1;
return responsesNameReads === 1 ? "lookup" : "broken";
},
};
const completionsChoice = {
type: "function",
function: {
get name() {
completionsNameReads += 1;
return completionsNameReads === 1 ? "lookup" : "broken";
},
},
};
expect(reconcileOpenAIResponsesToolChoice(responsesChoice as never, projection)).toEqual({
type: "function",
name: "lookup",
});
expect(reconcileOpenAICompletionsToolChoice(completionsChoice as never, projection)).toEqual({
type: "function",
function: { name: "lookup" },
});
expect(responsesNameReads).toBe(1);
expect(completionsNameReads).toBe(1);
});
it("normalizes omitted parameters to an empty schema", () => {
expect(projectOpenAITools([{ name: "lookup", parameters: undefined }]).tools).toEqual([
{
toolIndex: 0,
name: "lookup",
parameters: {},
},
]);
});
it("quarantines OpenAI tools with unsupported dynamic schema references", () => {
const projection = projectOpenAITools([
{
name: "dynamic",
parameters: {
type: "object",
properties: {
value: { $dynamicRef: "#value" },
},
},
},
]);
expect(projection.tools).toEqual([]);
expect(projection.diagnostics).toEqual([
{
toolIndex: 0,
toolName: "dynamic",
violations: ["dynamic.parameters.properties.value.$dynamicRef"],
},
]);
});
it("quarantines an inventory with an unreadable length", () => {
const tools = new Proxy([], {
get(target, property, receiver) {
if (property === "length") {
throw new Error("length exploded");
}
return Reflect.get(target, property, receiver);
},
});
expect(projectOpenAITools(tools)).toEqual({
inputToolCount: 0,
tools: [],
diagnostics: [{ toolIndex: 0, violations: ["tool[0] is unreadable"] }],
});
});
it("rejects pinned and required choices when their function tools are unavailable", () => {
const projection = projectOpenAITools([
{
name: "broken",
get parameters(): never {
throw new Error("parameters exploded");
},
},
]);
expect(() =>
reconcileOpenAIResponsesToolChoice({ type: "function", name: "broken" }, projection),
).toThrow('requested unavailable tool "broken"');
expect(() => reconcileOpenAIResponsesToolChoice("required", projection)).toThrow(
"no tools survived schema conversion",
);
expect(() =>
reconcileOpenAICompletionsToolChoice(
{ type: "function", function: { name: "broken" } },
projection,
),
).toThrow('requested unavailable tool "broken"');
expect(() => reconcileOpenAICompletionsToolChoice("required", projection)).toThrow(
"no tools survived schema conversion",
);
});
it("filters official Responses allowed_tools without broadening access", () => {
const projection = projectOpenAITools([{ name: "lookup", parameters: {} }]);
expect(
reconcileOpenAIResponsesToolChoice(
{
type: "allowed_tools",
mode: "required",
tools: [
{ type: "function", name: "broken" },
{ type: "function", name: "lookup" },
{ type: "web_search_preview" },
],
},
projection,
),
).toEqual({
type: "allowed_tools",
mode: "required",
tools: [{ type: "function", name: "lookup" }, { type: "web_search_preview" }],
});
});
it("disables an auto allowed_tools choice when no allowed tools survive", () => {
const projection = projectOpenAITools([{ name: "lookup", parameters: {} }]);
expect(
reconcileOpenAIResponsesToolChoice(
{
type: "allowed_tools",
mode: "auto",
tools: [{ type: "function", name: "broken" }],
},
projection,
),
).toBe("none");
});
it("filters official Chat Completions allowed_tools without broadening access", () => {
const projection = projectOpenAITools([{ name: "lookup", parameters: {} }]);
expect(
reconcileOpenAICompletionsToolChoice(
{
type: "allowed_tools",
allowed_tools: {
mode: "required",
tools: [
{ type: "function", function: { name: "broken" } },
{ type: "function", function: { name: "lookup" } },
{ type: "custom", custom: { name: "shell" } },
],
},
},
projection,
),
).toEqual({
type: "allowed_tools",
allowed_tools: {
mode: "required",
tools: [{ type: "function", function: { name: "lookup" } }],
},
});
});
it("rejects unsupported top-level Chat Completions custom choices", () => {
const projection = projectOpenAITools([{ name: "lookup", parameters: {} }]);
expect(() =>
reconcileOpenAICompletionsToolChoice(
{ type: "custom", custom: { name: "shell" } },
projection,
),
).toThrow("custom tool_choice is unsupported");
});
it("disables an auto Chat Completions allowed_tools choice when none survive", () => {
const projection = projectOpenAITools([{ name: "lookup", parameters: {} }]);
expect(
reconcileOpenAICompletionsToolChoice(
{
type: "allowed_tools",
allowed_tools: {
mode: "auto",
tools: [
{ type: "function", function: { name: "broken" } },
{ type: "custom", custom: { name: "shell" } },
],
},
},
projection,
),
).toBe("none");
});
it("preserves non-function Responses choices", () => {
const projection = projectOpenAITools([]);
expect(reconcileOpenAIResponsesToolChoice({ type: "web_search_preview" }, projection)).toEqual({
type: "web_search_preview",
});
});
});