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 | import { afterEach, describe, expect, mock, test } from "bun:test"; |
| 2 | import { mkdtemp, mkdir, readFile, rm, writeFile } from "fs/promises"; |
| 3 | import { tmpdir } from "os"; |
| 4 | import { join } from "path"; |
| 5 | import { renderToStaticMarkup } from "react-dom/server"; |
| 6 | |
| 7 | mock.module("next/navigation", () => ({ |
| 8 | notFound: () => { |
| 9 | throw new Error("not found"); |
| 10 | }, |
| 11 | usePathname: () => "/reference/api-reference/tasks", |
| 12 | useSearchParams: () => new URLSearchParams(), |
| 13 | })); |
| 14 | |
| 15 | const { ApiEndpointsTable } = await import("../../components/api-endpoints-table"); |
| 16 | const { ApiPageTitle } = await import("../../components/api-page-title"); |
| 17 | const { DEPRECATED_API_LEGACY_TITLE } = await import("../../components/legacy-badge"); |
| 18 | const { getDeprecatedApiSidebarName } = await import("../../lib/deprecated-api-sidebar"); |
| 19 | const { getApiDisplayTitle, isApiPageDeprecated } = await import("../../lib/api-deprecation"); |
| 20 | |
| 21 | const DOCS_DIR = join(import.meta.dir, "../.."); |
| 22 | const GENERATOR_PATH = join(DOCS_DIR, "scripts/generate-api-index.ts"); |
| 23 | const fixtureDirectories: string[] = []; |
| 24 | |
| 25 | interface SerializedEndpoint { |
| 26 | method: string; |
| 27 | pathV31: string; |
| 28 | pathV3: string; |
| 29 | summary: string; |
| 30 | href: string; |
| 31 | legacy?: boolean; |
| 32 | } |
| 33 | |
| 34 | function createSpec(version: "v3.1" | "v3") { |
| 35 | return { |
| 36 | tags: [ |
| 37 | { |
| 38 | name: "Tasks", |
| 39 | description: version === "v3.1" ? "Current task endpoints" : "Legacy task endpoints", |
| 40 | }, |
| 41 | ], |
| 42 | paths: { |
| 43 | [`/${version}/tasks/deprecated`]: { |
| 44 | get: { |
| 45 | tags: ["Tasks"], |
| 46 | summary: "Deprecated task endpoint", |
| 47 | operationId: "getDeprecatedTask", |
| 48 | deprecated: true, |
| 49 | }, |
| 50 | }, |
| 51 | [`/${version}/tasks/active`]: { |
| 52 | post: { |
| 53 | tags: ["Tasks"], |
| 54 | summary: "Active task endpoint", |
| 55 | operationId: "createTask", |
| 56 | }, |
| 57 | }, |
| 58 | }, |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | function createWebhookSpec() { |
| 63 | return { |
| 64 | openapi: "3.1.0", |
| 65 | tags: [ |
| 66 | { name: "Unrelated Tag", description: "Declared first on purpose" }, |
| 67 | { name: "Webhook Events", description: "Webhook event payloads" }, |
| 68 | ], |
| 69 | paths: {}, |
| 70 | webhooks: { |
| 71 | "composio.test.event": { |
| 72 | post: { |
| 73 | tags: ["Webhook Events"], |
| 74 | summary: "Test event", |
| 75 | operationId: "composio_test_event", |
| 76 | }, |
| 77 | }, |
| 78 | }, |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | function parseEndpoints(content: string): SerializedEndpoint[] { |
| 83 | const prefix = "<ApiEndpointsTable endpoints={"; |
| 84 | const start = content.indexOf(prefix); |
| 85 | const end = content.indexOf("} />", start); |
| 86 | |
| 87 | expect(start).toBeGreaterThanOrEqual(0); |
| 88 | expect(end).toBeGreaterThan(start); |
| 89 | |
| 90 | return JSON.parse(content.slice(start + prefix.length, end)); |
| 91 | } |
| 92 | |
| 93 | function expectedEndpoints(hrefPrefix: string): SerializedEndpoint[] { |
| 94 | return [ |
| 95 | { |
| 96 | method: "GET", |
| 97 | pathV31: "/v3.1/tasks/deprecated", |
| 98 | pathV3: "/v3/tasks/deprecated", |
| 99 | summary: "Deprecated task endpoint", |
| 100 | href: `${hrefPrefix}/tasks/getDeprecatedTask`, |
| 101 | legacy: true, |
| 102 | }, |
| 103 | { |
| 104 | method: "POST", |
| 105 | pathV31: "/v3.1/tasks/active", |
| 106 | pathV3: "/v3/tasks/active", |
| 107 | summary: "Active task endpoint", |
| 108 | href: `${hrefPrefix}/tasks/createTask`, |
| 109 | }, |
| 110 | ]; |
| 111 | } |
| 112 | |
| 113 | async function generateFixture(options?: { webhookSpec?: ReturnType<typeof createWebhookSpec> }): Promise<string> { |
| 114 | const fixtureDir = await mkdtemp(join(tmpdir(), "composio-api-index-")); |
| 115 | fixtureDirectories.push(fixtureDir); |
| 116 | |
| 117 | await mkdir(join(fixtureDir, "public"), { recursive: true }); |
| 118 | await writeFile( |
| 119 | join(fixtureDir, "public/openapi.json"), |
| 120 | JSON.stringify(createSpec("v3.1")), |
| 121 | ); |
| 122 | await writeFile( |
| 123 | join(fixtureDir, "public/openapi-v3.json"), |
| 124 | JSON.stringify(createSpec("v3")), |
| 125 | ); |
| 126 | await mkdir(join(fixtureDir, "api-overviews"), { recursive: true }); |
| 127 | await writeFile( |
| 128 | join(fixtureDir, "api-overviews/tasks.mdx"), |
| 129 | "Current task overview with POST /api/v3.1/tasks/active", |
| 130 | ); |
| 131 | if (options?.webhookSpec) { |
| 132 | await writeFile( |
| 133 | join(fixtureDir, "public/openapi-webhooks.json"), |
| 134 | JSON.stringify(options.webhookSpec), |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | const process = Bun.spawn([Bun.argv[0], GENERATOR_PATH], { |
| 139 | cwd: fixtureDir, |
| 140 | stdout: "pipe", |
| 141 | stderr: "pipe", |
| 142 | }); |
| 143 | const [exitCode, stderr] = await Promise.all([ |
| 144 | process.exited, |
| 145 | new Response(process.stderr).text(), |
| 146 | ]); |
| 147 | |
| 148 | if (exitCode !== 0) { |
| 149 | throw new Error(`API index generator failed with exit code ${exitCode}: ${stderr}`); |
| 150 | } |
| 151 | |
| 152 | return fixtureDir; |
| 153 | } |
| 154 | |
| 155 | afterEach(async () => { |
| 156 | await Promise.all( |
| 157 | fixtureDirectories.splice(0).map(path => rm(path, { recursive: true })), |
| 158 | ); |
| 159 | }); |
| 160 | |
| 161 | describe("deprecated API endpoints", () => { |
| 162 | test("removes only a trailing deprecation marker from deprecated display titles", () => { |
| 163 | expect(getApiDisplayTitle("Deprecated task endpoint (DEPRECATED)", true)).toBe( |
| 164 | "Deprecated task endpoint", |
| 165 | ); |
| 166 | expect(getApiDisplayTitle("Deprecated task endpoint (deprecated)", true)).toBe( |
| 167 | "Deprecated task endpoint", |
| 168 | ); |
| 169 | expect(getApiDisplayTitle("Active task endpoint (DEPRECATED)", false)).toBe( |
| 170 | "Active task endpoint (DEPRECATED)", |
| 171 | ); |
| 172 | expect(getApiDisplayTitle("Deprecated endpoint details", true)).toBe( |
| 173 | "Deprecated endpoint details", |
| 174 | ); |
| 175 | }); |
| 176 | |
| 177 | test("renders Legacy on a deprecated API endpoint detail title", () => { |
| 178 | const operation = { |
| 179 | method: "get", |
| 180 | path: "/v3.1/tasks/deprecated", |
| 181 | }; |
| 182 | const pageData = { |
| 183 | getSchema: () => ({ |
| 184 | bundled: { |
| 185 | paths: { |
| 186 | [operation.path]: { |
| 187 | [operation.method]: { deprecated: true }, |
| 188 | }, |
| 189 | }, |
| 190 | }, |
| 191 | }), |
| 192 | }; |
| 193 | |
| 194 | const deprecated = isApiPageDeprecated(pageData, [operation]); |
| 195 | const html = renderToStaticMarkup( |
| 196 | <ApiPageTitle |
| 197 | title="Deprecated task endpoint (DEPRECATED)" |
| 198 | version="3.1" |
| 199 | deprecated={deprecated} |
| 200 | />, |
| 201 | ); |
| 202 | |
| 203 | expect(deprecated).toBe(true); |
| 204 | expect(html).toContain("<span>Deprecated task endpoint</span>"); |
| 205 | expect(html).not.toContain("Deprecated task endpoint (DEPRECATED)"); |
| 206 | expect(html).toContain( |
| 207 | 'class="flex min-w-0 flex-wrap items-center gap-x-2 gap-y-1 text-2xl font-semibold"', |
| 208 | ); |
| 209 | expect(html).toContain('class="inline-flex shrink-0 items-center gap-2"'); |
| 210 | expect(html.match(/>Legacy<\/span>/g)).toHaveLength(1); |
| 211 | expect(html).toContain( |
| 212 | 'title="Deprecated API endpoint; kept for existing integrations and may be removed in a future release"', |
| 213 | ); |
| 214 | }); |
| 215 | |
| 216 | test("omits Legacy from an active API endpoint detail title", () => { |
| 217 | const operation = { |
| 218 | method: "post", |
| 219 | path: "/v3.1/tasks/active", |
| 220 | }; |
| 221 | const pageData = { |
| 222 | getSchema: () => ({ |
| 223 | bundled: { |
| 224 | paths: { |
| 225 | [operation.path]: { |
| 226 | [operation.method]: {}, |
| 227 | }, |
| 228 | }, |
| 229 | }, |
| 230 | }), |
| 231 | }; |
| 232 | |
| 233 | const deprecated = isApiPageDeprecated(pageData, [operation]); |
| 234 | const html = renderToStaticMarkup( |
| 235 | <ApiPageTitle |
| 236 | title="Active task endpoint" |
| 237 | version="3.1" |
| 238 | deprecated={deprecated} |
| 239 | />, |
| 240 | ); |
| 241 | |
| 242 | expect(deprecated).toBe(false); |
| 243 | expect(html).not.toContain(">Legacy</span>"); |
| 244 | }); |
| 245 | |
| 246 | test("replaces the sidebar title marker with a compact Legacy badge", () => { |
| 247 | const operation = { |
| 248 | method: "get", |
| 249 | path: "/v3.1/tasks/deprecated", |
| 250 | }; |
| 251 | const pageData = { |
| 252 | getSchema: () => ({ |
| 253 | bundled: { |
| 254 | paths: { |
| 255 | [operation.path]: { |
| 256 | [operation.method]: { deprecated: true }, |
| 257 | }, |
| 258 | }, |
| 259 | }, |
| 260 | }), |
| 261 | }; |
| 262 | |
| 263 | const html = renderToStaticMarkup( |
| 264 | <> |
| 265 | {getDeprecatedApiSidebarName("Deprecated task endpoint (DEPRECATED)", pageData, [ |
| 266 | operation, |
| 267 | ])} |
| 268 | </>, |
| 269 | ); |
| 270 | |
| 271 | expect(html).toContain("Deprecated task endpoint"); |
| 272 | expect(html).not.toContain("(DEPRECATED)"); |
| 273 | expect(html.match(/>Legacy<\/span>/g)).toHaveLength(1); |
| 274 | expect(html).toContain("text-[10px]"); |
| 275 | expect(html).toContain(DEPRECATED_API_LEGACY_TITLE); |
| 276 | }); |
| 277 | |
| 278 | test("keeps an unflagged sidebar title unchanged", () => { |
| 279 | const operation = { |
| 280 | method: "post", |
| 281 | path: "/v3.1/tasks/active", |
| 282 | }; |
| 283 | const pageData = { |
| 284 | getSchema: () => ({ |
| 285 | bundled: { |
| 286 | paths: { |
| 287 | [operation.path]: { |
| 288 | [operation.method]: {}, |
| 289 | }, |
| 290 | }, |
| 291 | }, |
| 292 | }), |
| 293 | }; |
| 294 | |
| 295 | const html = renderToStaticMarkup( |
| 296 | <> |
| 297 | {getDeprecatedApiSidebarName("Active task endpoint (DEPRECATED)", pageData, [operation])} |
| 298 | </>, |
| 299 | ); |
| 300 | |
| 301 | expect(html).toBe("Active task endpoint (DEPRECATED)"); |
| 302 | expect(html).not.toContain(">Legacy</span>"); |
| 303 | }); |
| 304 | |
| 305 | test("detects deprecation from a bundled-only document", () => { |
| 306 | const operation = { method: "get", path: "/v3.1/tasks/deprecated" }; |
| 307 | const pageData = { |
| 308 | getSchema: () => ({ |
| 309 | bundled: { |
| 310 | paths: { |
| 311 | [operation.path]: { |
| 312 | [operation.method]: { deprecated: true }, |
| 313 | }, |
| 314 | }, |
| 315 | }, |
| 316 | }), |
| 317 | }; |
| 318 | |
| 319 | expect(isApiPageDeprecated(pageData, [operation])).toBe(true); |
| 320 | }); |
| 321 | |
| 322 | test("reports an active operation from a bundled-only document", () => { |
| 323 | const operation = { method: "post", path: "/v3.1/tasks/active" }; |
| 324 | const pageData = { |
| 325 | getSchema: () => ({ |
| 326 | bundled: { |
| 327 | paths: { |
| 328 | [operation.path]: { |
| 329 | [operation.method]: {}, |
| 330 | }, |
| 331 | }, |
| 332 | }, |
| 333 | }), |
| 334 | }; |
| 335 | |
| 336 | expect(isApiPageDeprecated(pageData, [operation])).toBe(false); |
| 337 | }); |
| 338 | |
| 339 | test("returns false when the document has no bundled paths", () => { |
| 340 | const operation = { method: "get", path: "/v3.1/tasks/deprecated" }; |
| 341 | const pageData = { getSchema: () => ({ bundled: {} }) }; |
| 342 | |
| 343 | expect(isApiPageDeprecated(pageData, [operation])).toBe(false); |
| 344 | }); |
| 345 | |
| 346 | test("serializes legacy only for deprecated v3.1 and v3 operations", async () => { |
| 347 | const fixtureDir = await generateFixture(); |
| 348 | const v31Content = await readFile( |
| 349 | join(fixtureDir, "content/reference/api-reference/tasks/index.mdx"), |
| 350 | "utf-8", |
| 351 | ); |
| 352 | const v3Content = await readFile( |
| 353 | join(fixtureDir, "content/reference/v3/api-reference/tasks/index.mdx"), |
| 354 | "utf-8", |
| 355 | ); |
| 356 | |
| 357 | const v31Endpoints = parseEndpoints(v31Content); |
| 358 | const v3Endpoints = parseEndpoints(v3Content); |
| 359 | |
| 360 | expect(v31Endpoints).toEqual(expectedEndpoints("/reference/api-reference")); |
| 361 | expect(v3Endpoints).toEqual(expectedEndpoints("/reference/v3/api-reference")); |
| 362 | |
| 363 | for (const endpoints of [v31Endpoints, v3Endpoints]) { |
| 364 | expect( |
| 365 | endpoints.find(endpoint => endpoint.summary === "Active task endpoint"), |
| 366 | ).not.toHaveProperty("legacy"); |
| 367 | } |
| 368 | }); |
| 369 | |
| 370 | test("preserves the shared overview on current and legacy indexes", async () => { |
| 371 | const fixtureDir = await generateFixture(); |
| 372 | const v31Content = await readFile( |
| 373 | join(fixtureDir, "content/reference/api-reference/tasks/index.mdx"), |
| 374 | "utf-8", |
| 375 | ); |
| 376 | const v3Content = await readFile( |
| 377 | join(fixtureDir, "content/reference/v3/api-reference/tasks/index.mdx"), |
| 378 | "utf-8", |
| 379 | ); |
| 380 | |
| 381 | expect(v31Content).toContain("Current task overview with POST /api/v3.1/tasks/active"); |
| 382 | expect(v3Content).toContain("Current task overview with POST /api/v3.1/tasks/active"); |
| 383 | }); |
| 384 | |
| 385 | test("renders one Legacy badge for a deprecated endpoint and none for an active endpoint", () => { |
| 386 | const html = renderToStaticMarkup( |
| 387 | <ApiEndpointsTable |
| 388 | endpoints={[ |
| 389 | { |
| 390 | method: "GET", |
| 391 | pathV31: "/v3.1/tasks/deprecated", |
| 392 | pathV3: "/v3/tasks/deprecated", |
| 393 | summary: "Deprecated task endpoint (DEPRECATED)", |
| 394 | href: "/reference/api-reference/tasks/getDeprecatedTask", |
| 395 | legacy: true, |
| 396 | }, |
| 397 | { |
| 398 | method: "POST", |
| 399 | pathV31: "/v3.1/tasks/active", |
| 400 | pathV3: "/v3/tasks/active", |
| 401 | summary: "Active task endpoint", |
| 402 | href: "/reference/api-reference/tasks/createTask", |
| 403 | }, |
| 404 | ]} |
| 405 | />, |
| 406 | ); |
| 407 | |
| 408 | expect(html).toContain( |
| 409 | '<a href="/reference/api-reference/tasks/getDeprecatedTask">Deprecated task endpoint</a>', |
| 410 | ); |
| 411 | expect(html).not.toContain("Deprecated task endpoint (DEPRECATED)"); |
| 412 | expect( |
| 413 | html.match( |
| 414 | /title="Deprecated API endpoint; kept for existing integrations and may be removed in a future release"/g, |
| 415 | ), |
| 416 | ).toHaveLength(1); |
| 417 | expect(html.match(/>Legacy<\/span>/g)).toHaveLength(1); |
| 418 | }); |
| 419 | }); |
| 420 | |
| 421 | describe("webhook API index", () => { |
| 422 | test("routes the index from the operation tag instead of tag declaration order", async () => { |
| 423 | const fixtureDir = await generateFixture({ webhookSpec: createWebhookSpec() }); |
| 424 | const content = await readFile( |
| 425 | join( |
| 426 | fixtureDir, |
| 427 | "content/reference/api-reference/webhook-events/index.mdx", |
| 428 | ), |
| 429 | "utf-8", |
| 430 | ); |
| 431 | |
| 432 | expect(content).toContain("title: Webhook Events"); |
| 433 | expect(content).toContain( |
| 434 | "[Test event](/reference/api-reference/webhook-events/composio_test_event)", |
| 435 | ); |
| 436 | expect( |
| 437 | await Bun.file( |
| 438 | join( |
| 439 | fixtureDir, |
| 440 | "content/reference/api-reference/unrelated-tag/index.mdx", |
| 441 | ), |
| 442 | ).exists(), |
| 443 | ).toBe(false); |
| 444 | }); |
| 445 | |
| 446 | test("generates one index per webhook tag, each listing only its own events", async () => { |
| 447 | const webhookSpec = createWebhookSpec(); |
| 448 | webhookSpec.tags.push({ name: "Session Webhooks", description: "Session webhook requests" }); |
| 449 | Object.assign(webhookSpec.webhooks, { |
| 450 | "composio.session_hook.before_execute": { |
| 451 | post: { |
| 452 | tags: ["Session Webhooks"], |
| 453 | summary: "Before execute", |
| 454 | operationId: "composio_session_hook_before_execute", |
| 455 | }, |
| 456 | }, |
| 457 | }); |
| 458 | const fixtureDir = await generateFixture({ webhookSpec }); |
| 459 | const readIndex = (tagSlug: string) => |
| 460 | readFile(join(fixtureDir, `content/reference/api-reference/${tagSlug}/index.mdx`), "utf-8"); |
| 461 | |
| 462 | const webhookEvents = await readIndex("webhook-events"); |
| 463 | expect(webhookEvents).toContain( |
| 464 | "[Test event](/reference/api-reference/webhook-events/composio_test_event)", |
| 465 | ); |
| 466 | expect(webhookEvents).not.toContain("composio_session_hook_before_execute"); |
| 467 | |
| 468 | const sessionWebhooks = await readIndex("session-webhooks"); |
| 469 | expect(sessionWebhooks).toContain("title: Session Webhooks"); |
| 470 | expect(sessionWebhooks).toContain( |
| 471 | "[Before execute](/reference/api-reference/session-webhooks/composio_session_hook_before_execute)", |
| 472 | ); |
| 473 | expect(sessionWebhooks).not.toContain("composio_test_event"); |
| 474 | }); |
| 475 | }); |