-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.test.ts
More file actions
145 lines (131 loc) · 6.2 KB
/
Copy pathmiddleware.test.ts
File metadata and controls
145 lines (131 loc) · 6.2 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
import { describe, expect, test } from "bun:test";
import { NextRequest } from "next/server";
import { CANONICAL_MARKDOWN_REQUEST_HEADER } from "@/lib/markdown-http";
import { middleware } from "./middleware";
function request(
path: string,
headers?: Record<string, string>,
): NextRequest {
return new NextRequest(new URL(path, "https://aicharts.io"), { headers });
}
describe("markdown content negotiation", () => {
test("rewrites Accept: text/markdown to the markdown handler and sets Vary", () => {
const response = middleware(request("/", { Accept: "text/markdown" }));
expect(response.status).toBe(200);
expect(response.headers.get("x-middleware-rewrite")).toContain("/api/markdown");
expect(response.headers.get(`x-middleware-request-${CANONICAL_MARKDOWN_REQUEST_HEADER}`))
.toBe("1");
expect(response.headers.get("Vary")).toContain("Accept");
});
test("rewrites explicit .md URLs regardless of Accept", () => {
for (const [path, expected] of [
["/data.md", "/api/markdown/data"],
["/coding.md", "/api/markdown/coding"],
["/benchmarks.md", "/api/markdown/benchmarks"],
["/calculator.md", "/api/markdown/calculator"],
["/blog/terminal-bench-science.md", "/api/markdown/blog/terminal-bench-science"],
["/models/openai/gpt-5.6-sol/max.md", "/api/markdown/models/openai/gpt-5.6-sol/max"],
] as const) {
const response = middleware(request(path, { Accept: "text/html" }));
expect(response.headers.get("x-middleware-rewrite")).toContain(expected);
expect(response.headers.get(`x-middleware-request-${CANONICAL_MARKDOWN_REQUEST_HEADER}`))
.toBeNull();
expect(response.headers.get("Vary")).toContain("Accept");
}
});
test("returns 406 when no produced type matches", () => {
const response = middleware(request("/", { Accept: "application/pdf" }));
expect(response.status).toBe(406);
expect(response.headers.get("Content-Type")).toBe("text/plain; charset=utf-8");
expect(response.headers.get("Vary")).toBe("Accept");
return expect(response.text()).resolves.toContain("Available: text/html, text/markdown");
});
test("leaves HTML and Next.js data requests alone while adding Vary on HTML", () => {
const html = middleware(request("/", { Accept: "text/html" }));
expect(html.headers.get("x-middleware-rewrite")).toBeNull();
expect(html.headers.get("Vary")).toContain("Accept");
const rsc = middleware(request("/", {
Accept: "text/x-component",
RSC: "1",
}));
expect(rsc.headers.get("x-middleware-rewrite")).toBeNull();
expect(rsc.status).not.toBe(406);
});
test("negotiates only known HTML page routes", () => {
for (const path of [
"/",
"/data",
"/coding",
"/benchmarks",
"/calculator",
"/models",
"/models/openai/gpt-5.6-sol/max",
"/blog",
"/blog/terminal-bench-science",
]) {
const response = middleware(request(path, { Accept: "text/markdown" }));
expect(response.headers.get("x-middleware-rewrite")).toContain("/api/markdown");
}
});
test("leaves every non-HTML public representation untouched", () => {
const representations = [
["/data/benchmark-atlas.json", "application/json"],
["/data/benchmark-atlas/terminal-bench-4", "application/json"],
["/data/benchmark-atlas/arc-agi-2", "application/json"],
["/data/artificial-analysis-intelligence.json", "application/json"],
["/data/coding-agents.json", "application/json"],
["/data/terminal-bench-4.json", "application/json"],
["/data/terminal-bench-science-0-1.json", "application/json"],
["/blog/feed.xml", "application/atom+xml"],
["/llms.txt", "text/plain"],
["/robots.txt", "text/plain"],
["/sitemap.xml", "application/xml"],
["/icon.svg", "image/svg+xml"],
["/images/blog/terminal-bench-science.webp", "image/webp"],
["/opengraph-image", "image/png"],
["/models/opengraph-image-v7", "image/png"],
["/models/openai/gpt-5.6-sol/max/card.png", "image/png"],
["/models/openai/gpt-5.6-sol/max/opengraph-image", "image/png"],
] as const;
for (const [path, accept] of representations) {
for (const requestedType of [accept, "text/markdown", "application/pdf"]) {
const response = middleware(request(path, { Accept: requestedType }));
expect(response.headers.get("x-middleware-rewrite")).toBeNull();
expect(response.status).toBe(200);
}
}
});
test("leaves inert Hraness previews on their single HTML representation", () => {
for (const path of ["/preview", "/models/preview"]) {
const markdown = middleware(request(path, {
Accept: "text/markdown",
}));
const unsupported = middleware(request(path, {
Accept: "application/pdf",
}));
expect(markdown.headers.get("x-middleware-rewrite")).toBeNull();
expect(markdown.status).toBe(200);
expect(unsupported.headers.get("x-middleware-rewrite")).toBeNull();
expect(unsupported.status).toBe(200);
}
});
});
describe("old homepage comparison links", () => {
test("redirects legacy chart state before negotiating its new canonical page", () => {
for (const accept of ["text/html", "text/markdown", "text/x-component"]) {
const response = middleware(request("/?atlas=arc-agi-2&atlasCompare=a&atlasCompare=b", { Accept: accept }));
expect(response.status).toBe(308);
expect(response.headers.get("location")).toBe("https://aicharts.io/benchmarks?atlas=arc-agi-2&atlasCompare=a&atlasCompare=b");
expect(response.headers.get("x-middleware-rewrite")).toBeNull();
}
expect(middleware(request("/?benchmark=deepSwe&compare=costUsd&point=id")).headers.get("location"))
.toBe("https://aicharts.io/coding?benchmark=deepSwe&compare=costUsd&point=id");
});
test("does not redirect new destinations, normal home visits, or posted content", () => {
for (const path of ["/", "/?utm_source=notes", "/coding?benchmark=deepSwe", "/benchmarks?atlas=arc-agi-2"]) {
expect(middleware(request(path)).headers.get("location")).toBeNull();
}
const post = new NextRequest("https://aicharts.io/?atlas=arc-agi-2", { method: "POST" });
expect(middleware(post).headers.get("location")).toBeNull();
});
});