Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | /** |
| 2 | * Regression tests for the raw-API-to-ParameterSchema transformation. |
| 3 | * |
| 4 | * The zod-based rewrite of processSchema/toolFromApi parses raw JSON-Schema |
| 5 | * nodes recursively and rebuilds them field by field, so these tests pin the |
| 6 | * shapes the toolkit detail UI depends on (toolkit-detail.tsx reads |
| 7 | * additionalProperties and items.additionalProperties, including their |
| 8 | * nested properties, to synthesize the "[key: string]" row). |
| 9 | */ |
| 10 | import { describe, test, expect } from "bun:test"; |
| 11 | import { processSchema, toolFromApi, triggerFromApi } from "../../lib/toolkit-schema"; |
| 12 | |
| 13 | describe("processSchema", () => { |
| 14 | test("dictionary-of-object param keeps nested additionalProperties subschema", () => { |
| 15 | const params = processSchema({ |
| 16 | type: "object", |
| 17 | properties: { |
| 18 | metadata: { |
| 19 | type: "object", |
| 20 | additionalProperties: { |
| 21 | type: "object", |
| 22 | required: ["label"], |
| 23 | properties: { |
| 24 | label: { type: "string", description: "Display label" }, |
| 25 | weight: { type: "number" }, |
| 26 | }, |
| 27 | }, |
| 28 | }, |
| 29 | }, |
| 30 | }); |
| 31 | |
| 32 | const additional = params?.metadata.additionalProperties; |
| 33 | expect(additional).toBeDefined(); |
| 34 | expect(typeof additional).toBe("object"); |
| 35 | if (typeof additional === "object") { |
| 36 | expect(additional.type).toBe("object"); |
| 37 | expect(additional.requiredFields).toEqual(["label"]); |
| 38 | expect(additional.properties?.label.type).toBe("string"); |
| 39 | expect(additional.properties?.label.description).toBe("Display label"); |
| 40 | expect(additional.properties?.weight.type).toBe("number"); |
| 41 | } |
| 42 | }); |
| 43 | |
| 44 | test("array-of-dictionary param keeps items.additionalProperties", () => { |
| 45 | const params = processSchema({ |
| 46 | type: "object", |
| 47 | properties: { |
| 48 | rows: { |
| 49 | type: "array", |
| 50 | items: { |
| 51 | type: "object", |
| 52 | additionalProperties: { type: "string", description: "cell value" }, |
| 53 | }, |
| 54 | }, |
| 55 | }, |
| 56 | }); |
| 57 | |
| 58 | const itemsAdditional = params?.rows.items?.additionalProperties; |
| 59 | expect(itemsAdditional).toBeDefined(); |
| 60 | expect(typeof itemsAdditional).toBe("object"); |
| 61 | if (typeof itemsAdditional === "object") { |
| 62 | expect(itemsAdditional.type).toBe("string"); |
| 63 | expect(itemsAdditional.description).toBe("cell value"); |
| 64 | } |
| 65 | }); |
| 66 | |
| 67 | test("boolean additionalProperties passes through unchanged", () => { |
| 68 | const params = processSchema({ |
| 69 | type: "object", |
| 70 | properties: { |
| 71 | strict: { type: "object", additionalProperties: false }, |
| 72 | }, |
| 73 | }); |
| 74 | |
| 75 | expect(params?.strict.additionalProperties).toBe(false); |
| 76 | }); |
| 77 | |
| 78 | test("array items keep the required-to-requiredFields remap and nested properties", () => { |
| 79 | const params = processSchema({ |
| 80 | type: "object", |
| 81 | required: ["records"], |
| 82 | properties: { |
| 83 | records: { |
| 84 | type: "array", |
| 85 | items: { |
| 86 | type: "object", |
| 87 | required: ["id"], |
| 88 | properties: { |
| 89 | id: { type: "string" }, |
| 90 | note: { type: "string" }, |
| 91 | }, |
| 92 | }, |
| 93 | }, |
| 94 | }, |
| 95 | }); |
| 96 | |
| 97 | expect(params?.records.required).toBe(true); |
| 98 | expect(params?.records.items?.requiredFields).toEqual(["id"]); |
| 99 | expect(params?.records.items?.properties?.id.type).toBe("string"); |
| 100 | }); |
| 101 | |
| 102 | test("tuple-form items array is not spread into numeric keys", () => { |
| 103 | const params = processSchema({ |
| 104 | type: "object", |
| 105 | properties: { |
| 106 | pair: { type: "array", items: [{ type: "string" }, { type: "number" }] }, |
| 107 | }, |
| 108 | }); |
| 109 | |
| 110 | expect(params?.pair.items).toBeUndefined(); |
| 111 | }); |
| 112 | |
| 113 | test("scalar enum values are preserved as strings", () => { |
| 114 | const params = processSchema({ |
| 115 | type: "object", |
| 116 | properties: { |
| 117 | priority: { type: "integer", enum: [1, 2, 3, true, { invalid: "object" }] }, |
| 118 | }, |
| 119 | }); |
| 120 | |
| 121 | expect(params?.priority.enum).toEqual(["1", "2", "3", "true"]); |
| 122 | }); |
| 123 | |
| 124 | test("omits empty enum arrays from the serialized parameter shape", () => { |
| 125 | const params = processSchema({ |
| 126 | type: "object", |
| 127 | properties: { query: { type: "string" } }, |
| 128 | }); |
| 129 | |
| 130 | expect(params?.query.enum).toBeUndefined(); |
| 131 | expect(JSON.stringify(params?.query)).not.toContain('"enum"'); |
| 132 | }); |
| 133 | }); |
| 134 | |
| 135 | describe("toolFromApi", () => { |
| 136 | test("maps slug fallback and nested schemas end to end", () => { |
| 137 | const tool = toolFromApi({ |
| 138 | slug: "TEST_TOOL", |
| 139 | description: "A test tool", |
| 140 | input_parameters: { |
| 141 | type: "object", |
| 142 | properties: { |
| 143 | config: { |
| 144 | type: "object", |
| 145 | additionalProperties: { |
| 146 | type: "object", |
| 147 | properties: { enabled: { type: "boolean" } }, |
| 148 | }, |
| 149 | }, |
| 150 | }, |
| 151 | }, |
| 152 | }); |
| 153 | |
| 154 | expect(tool.name).toBe("TEST_TOOL"); |
| 155 | const additional = tool.input_parameters?.config.additionalProperties; |
| 156 | expect(typeof additional).toBe("object"); |
| 157 | if (typeof additional === "object") { |
| 158 | expect(additional.properties?.enabled.type).toBe("boolean"); |
| 159 | } |
| 160 | }); |
| 161 | |
| 162 | test("empty names fall back to display names", () => { |
| 163 | expect( |
| 164 | toolFromApi({ |
| 165 | slug: "TEST_TOOL", |
| 166 | name: "", |
| 167 | display_name: "Test tool", |
| 168 | description: "A test tool", |
| 169 | }).name |
| 170 | ).toBe("Test tool"); |
| 171 | |
| 172 | expect( |
| 173 | triggerFromApi({ |
| 174 | slug: "TEST_TRIGGER", |
| 175 | name: "", |
| 176 | display_name: "Test trigger", |
| 177 | description: "A test trigger", |
| 178 | }).name |
| 179 | ).toBe("Test trigger"); |
| 180 | }); |
| 181 | |
| 182 | test("omits empty scope and tag arrays from the serialized tool shape", () => { |
| 183 | const tool = toolFromApi({ slug: "TEST_TOOL" }); |
| 184 | |
| 185 | expect(tool.scopes).toBeUndefined(); |
| 186 | expect(tool.tags).toBeUndefined(); |
| 187 | expect(JSON.stringify(tool)).not.toContain('"scopes"'); |
| 188 | expect(JSON.stringify(tool)).not.toContain('"tags"'); |
| 189 | }); |
| 190 | }); |