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
+ usePathname: () => "/reference/api-reference/tasks",
9
+}));
10
+
11
+const { ApiEndpointsTable } = await import("../../components/api-endpoints-table");
12
+
13
+const DOCS_DIR = join(import.meta.dir, "../..");
14
+const GENERATOR_PATH = join(DOCS_DIR, "scripts/generate-api-index.ts");
15
+const fixtureDirectories: string[] = [];
16
+
17
+interface SerializedEndpoint {
18
+ method: string;
19
+ pathV31: string;
20
+ pathV3: string;
21
+ summary: string;
22
+ href: string;
23
+ legacy?: boolean;
24
+}
25
+
26
+function createSpec(version: "v3.1" | "v3") {
27
+ return {
28
+ tags: [{ name: "Tasks", description: "Task endpoints" }],
29
+ paths: {
30
+ [`/${version}/tasks/deprecated`]: {
31
+ get: {
32
+ tags: ["Tasks"],
33
+ summary: "Deprecated task endpoint",
34
+ operationId: "getDeprecatedTask",
35
+ deprecated: true,
36
+ },
37
+ },
38
+ [`/${version}/tasks/active`]: {
39
+ post: {
40
+ tags: ["Tasks"],
41
+ summary: "Active task endpoint",
42
+ operationId: "createTask",
43
+ },
44
+ },
45
+ },
46
+ };
47
+}
48
+
49
+function parseEndpoints(content: string): SerializedEndpoint[] {
50
+ const prefix = "<ApiEndpointsTable endpoints={";
51
+ const start = content.indexOf(prefix);
52
+ const end = content.indexOf("} />", start);
53
+
54
+ expect(start).toBeGreaterThanOrEqual(0);
55
+ expect(end).toBeGreaterThan(start);
56
+
57
+ return JSON.parse(content.slice(start + prefix.length, end));
58
+}
59
+
60
+function expectedEndpoints(hrefPrefix: string): SerializedEndpoint[] {
61
+ return [
62
+ {
63
+ method: "GET",
64
+ pathV31: "/v3.1/tasks/deprecated",
65
+ pathV3: "/v3/tasks/deprecated",
66
+ summary: "Deprecated task endpoint",
67
+ href: `${hrefPrefix}/tasks/getDeprecatedTask`,
68
+ legacy: true,
69
+ },
70
+ {
71
+ method: "POST",
72
+ pathV31: "/v3.1/tasks/active",
73
+ pathV3: "/v3/tasks/active",
74
+ summary: "Active task endpoint",
75
+ href: `${hrefPrefix}/tasks/createTask`,
76
+ },
77
+ ];
78
+}
79
+
80
+async function generateFixture(): Promise<string> {
81
+ const fixtureDir = await mkdtemp(join(tmpdir(), "composio-api-index-"));
82
+ fixtureDirectories.push(fixtureDir);
83
+
84
+ await mkdir(join(fixtureDir, "public"), { recursive: true });
85
+ await writeFile(
86
+ join(fixtureDir, "public/openapi.json"),
87
+ JSON.stringify(createSpec("v3.1")),
88
+ );
89
+ await writeFile(
90
+ join(fixtureDir, "public/openapi-v3.json"),
91
+ JSON.stringify(createSpec("v3")),
92
+ );
93
+
94
+ const process = Bun.spawn([Bun.argv[0], GENERATOR_PATH], {
95
+ cwd: fixtureDir,
96
+ stdout: "pipe",
97
+ stderr: "pipe",
98
+ });
99
+ const [exitCode, stderr] = await Promise.all([
100
+ process.exited,
101
+ new Response(process.stderr).text(),
102
+ ]);
103
+
104
+ if (exitCode !== 0) {
105
+ throw new Error(`API index generator failed with exit code ${exitCode}: ${stderr}`);
106
+ }
107
+
108
+ return fixtureDir;
109
+}
110
+
111
+afterEach(async () => {
112
+ await Promise.all(
113
+ fixtureDirectories.splice(0).map(path => rm(path, { recursive: true })),
114
+ );
115
+});
116
+
117
+describe("deprecated API endpoints", () => {
118
+ test("serializes legacy only for deprecated v3.1 and v3 operations", async () => {
119
+ const fixtureDir = await generateFixture();
120
+ const v31Content = await readFile(
121
+ join(fixtureDir, "content/reference/api-reference/tasks/index.mdx"),
122
+ "utf-8",
123
+ );
124
+ const v3Content = await readFile(
125
+ join(fixtureDir, "content/reference/v3/api-reference/tasks/index.mdx"),
126
+ "utf-8",
127
+ );
128
+
129
+ const v31Endpoints = parseEndpoints(v31Content);
130
+ const v3Endpoints = parseEndpoints(v3Content);
131
+
132
+ expect(v31Endpoints).toEqual(expectedEndpoints("/reference/api-reference"));
133
+ expect(v3Endpoints).toEqual(expectedEndpoints("/reference/v3/api-reference"));
134
+
135
+ for (const endpoints of [v31Endpoints, v3Endpoints]) {
136
+ expect(
137
+ endpoints.find(endpoint => endpoint.summary === "Active task endpoint"),
138
+ ).not.toHaveProperty("legacy");
139
+ }
140
+ });
141
+
142
+ test("renders one Legacy badge for a deprecated endpoint and none for an active endpoint", () => {
143
+ const html = renderToStaticMarkup(
144
+ <ApiEndpointsTable
145
+ endpoints={[
146
+ {
147
+ method: "GET",
148
+ pathV31: "/v3.1/tasks/deprecated",
149
+ pathV3: "/v3/tasks/deprecated",
150
+ summary: "Deprecated task endpoint",
151
+ href: "/reference/api-reference/tasks/getDeprecatedTask",
152
+ legacy: true,
153
+ },
154
+ {
155
+ method: "POST",
156
+ pathV31: "/v3.1/tasks/active",
157
+ pathV3: "/v3/tasks/active",
158
+ summary: "Active task endpoint",
159
+ href: "/reference/api-reference/tasks/createTask",
160
+ },
161
+ ]}
162
+ />,
163
+ );
164
+
165
+ expect(
166
+ html.match(
167
+ /title="Deprecated API endpoint; kept for existing integrations and may be removed in a future release"/g,
168
+ ),
169
+ ).toHaveLength(1);
170
+ expect(html.match(/>Legacy<\/span>/g)).toHaveLength(1);
171
+ });
172
+});