Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/pipi/__tests__/output-schema.test.ts

Raw
import { validateToolArguments } from "@earendil-works/pi-ai";
import { describe, expect, it } from "vitest";
import { json, outputParameters } from "../runtime.ts";

function validate(schema: Record<string, unknown>, output: unknown) {
	return validateToolArguments(
		{
			name: "submit",
			description: "Submit",
			parameters: outputParameters(schema),
		},
		{
			type: "toolCall",
			id: "submission",
			name: "submit",
			arguments: { output: json(output) },
		},
	);
}
describe("native output schema resource boundary", () => {
	it.each([
		{
			schema: {
				$defs: { A: { $ref: "#/$defs/B" }, B: { type: "integer" } },
				type: "object",
				properties: { value: { $ref: "#/$defs/A" } },
				required: ["value"],
			},
			good: { value: 42 },
			bad: { value: "not a number" },
		},
		{
			schema: {
				type: "object",
				properties: {
					original: { type: "integer" },
					copy: { $ref: "#/properties/original" },
				},
				required: ["copy"],
			},
			good: { copy: 42 },
			bad: { copy: "invalid" },
		},
		{
			schema: {
				type: "object",
				properties: { value: { type: "integer" }, child: { $ref: "#" } },
				required: ["value"],
			},
			good: { value: 1, child: { value: 2 } },
			bad: { value: 1, child: { value: "invalid" } },
		},
		{
			schema: { const: { $ref: "#/literal-data", $id: "literal-data" } },
			good: { $ref: "#/literal-data", $id: "literal-data" },
			bad: { $ref: "#/changed", $id: "literal-data" },
		},
		{
			schema: {
				$id: "https://example.test/output",
				$defs: { number: { type: "integer" } },
				$ref: "#/$defs/number",
			},
			good: 42,
			bad: "invalid",
		},
		{
			schema: { type: "array", items: { type: "integer" } },
			good: [1, 2],
			bad: ["invalid"],
		},
	])("preserves standalone schema semantics %#", ({ schema, good, bad }) => {
		const original = structuredClone(schema);
		expect(validate(schema, good)).toEqual({ output: good });
		expect(() => validate(schema, bad)).toThrow();
		expect(schema).toEqual(original);
	});
});