forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.test.ts
More file actions
170 lines (143 loc) · 5.59 KB
/
Copy pathroute.test.ts
File metadata and controls
170 lines (143 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* Unit tests for the runtime /api/ops proxy Route Handler.
*
* These lock in the behaviors the production overlay depends on:
* - OPS_BASE_URL is read at REQUEST time (per-call), not frozen — the
* whole reason this handler exists instead of a next.config rewrite.
* - The path mapping `/api/ops/<segments>` → `${OPS_BASE_URL}/api/<segments>`
* (single `/api`, never doubled/dropped) plus the original query string.
* - Upstream status + body are relayed through.
* - A missing OPS_BASE_URL yields a clear 503, not a throw/500.
*/
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { NextRequest } from "next/server";
import { GET, POST } from "./route";
const ORIGINAL_OPS_BASE_URL = process.env.OPS_BASE_URL;
let fetchSpy: ReturnType<typeof vi.fn>;
function makeRequest(url: string, init?: RequestInit): NextRequest {
return new NextRequest(new Request(url, init));
}
function ctx(path: string[]): { params: Promise<{ path: string[] }> } {
return { params: Promise.resolve({ path }) };
}
beforeEach(() => {
fetchSpy = vi.fn();
vi.stubGlobal("fetch", fetchSpy);
});
afterEach(() => {
vi.unstubAllGlobals();
vi.restoreAllMocks();
if (ORIGINAL_OPS_BASE_URL === undefined) {
delete process.env.OPS_BASE_URL;
} else {
process.env.OPS_BASE_URL = ORIGINAL_OPS_BASE_URL;
}
});
describe("/api/ops proxy Route Handler", () => {
it("proxies GET /api/ops/probes → ${OPS_BASE_URL}/api/probes", async () => {
process.env.OPS_BASE_URL = "https://harness.example.com";
fetchSpy.mockResolvedValue(
new Response(JSON.stringify({ probes: [] }), {
status: 200,
headers: { "content-type": "application/json" },
}),
);
const res = await GET(
makeRequest("http://dashboard.local/api/ops/probes"),
ctx(["probes"]),
);
expect(fetchSpy).toHaveBeenCalledTimes(1);
const calledUrl = String(fetchSpy.mock.calls[0]![0]);
expect(calledUrl).toBe("https://harness.example.com/api/probes");
expect(res.status).toBe(200);
await expect(res.json()).resolves.toEqual({ probes: [] });
});
it("forwards the query string and nested path segments", async () => {
process.env.OPS_BASE_URL = "https://harness.example.com/";
fetchSpy.mockResolvedValue(new Response("{}", { status: 200 }));
await GET(
makeRequest("http://dashboard.local/api/ops/probes/abc?limit=5"),
ctx(["probes", "abc"]),
);
const calledUrl = String(fetchSpy.mock.calls[0]![0]);
// Trailing slash on base normalized away; single /api; query preserved.
expect(calledUrl).toBe(
"https://harness.example.com/api/probes/abc?limit=5",
);
});
it("reads OPS_BASE_URL at REQUEST time, not at module load", async () => {
fetchSpy.mockResolvedValue(new Response("{}", { status: 200 }));
process.env.OPS_BASE_URL = "https://first.example.com";
await GET(
makeRequest("http://dashboard.local/api/ops/probes"),
ctx(["probes"]),
);
expect(String(fetchSpy.mock.calls[0]![0])).toBe(
"https://first.example.com/api/probes",
);
// Change the env between requests — the second call must pick up the
// new value, proving per-request resolution (no build/module freeze).
process.env.OPS_BASE_URL = "https://second.example.com";
await GET(
makeRequest("http://dashboard.local/api/ops/probes"),
ctx(["probes"]),
);
expect(String(fetchSpy.mock.calls[1]![0])).toBe(
"https://second.example.com/api/probes",
);
});
it("relays the upstream non-2xx status and body through", async () => {
process.env.OPS_BASE_URL = "https://harness.example.com";
fetchSpy.mockResolvedValue(
new Response("boom", { status: 502, statusText: "Bad Gateway" }),
);
const res = await GET(
makeRequest("http://dashboard.local/api/ops/probes"),
ctx(["probes"]),
);
expect(res.status).toBe(502);
await expect(res.text()).resolves.toBe("boom");
});
it("forwards POST method + body to the harness", async () => {
process.env.OPS_BASE_URL = "https://harness.example.com";
fetchSpy.mockResolvedValue(
new Response(JSON.stringify({ runId: "r1" }), { status: 200 }),
);
await POST(
makeRequest("http://dashboard.local/api/ops/probes/smoke/trigger", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ slugs: ["a"] }),
}),
ctx(["probes", "smoke", "trigger"]),
);
const [calledUrl, calledInit] = fetchSpy.mock.calls[0]!;
expect(String(calledUrl)).toBe(
"https://harness.example.com/api/probes/smoke/trigger",
);
expect((calledInit as RequestInit).method).toBe("POST");
expect((calledInit as RequestInit).body).toBeDefined();
});
it("returns 503 (not a throw/500) when OPS_BASE_URL is unset", async () => {
delete process.env.OPS_BASE_URL;
const res = await GET(
makeRequest("http://dashboard.local/api/ops/probes"),
ctx(["probes"]),
);
expect(res.status).toBe(503);
expect(fetchSpy).not.toHaveBeenCalled();
const body = (await res.json()) as { error: string };
expect(body.error).toMatch(/OPS_BASE_URL/);
});
it("returns 502 when the upstream fetch rejects (harness unreachable)", async () => {
process.env.OPS_BASE_URL = "https://harness.example.com";
fetchSpy.mockRejectedValue(new Error("ENOTFOUND"));
const res = await GET(
makeRequest("http://dashboard.local/api/ops/probes"),
ctx(["probes"]),
);
expect(res.status).toBe(502);
const body = (await res.json()) as { error: string };
expect(body.error).toMatch(/ENOTFOUND/);
});
});