-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
184 lines (169 loc) · 6.14 KB
/
index.test.ts
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/** @license AGPL-3.0-or-later
*
* Copyright(C) 2025 Hong Xu <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const hostAddress = "https://localhost:3001" as const;
const delpaGitHubRawBaseUrl =
"https://raw.githubusercontent.com/delpa-org" as const;
test("/ redirects to Delpa homepage", async () => {
const response = await fetch(`${hostAddress}/`, {
redirect: "manual",
});
expect(response.status).toBe(301);
expect(response.headers.get("location")).toBe("https://delpa.org");
});
test("/health-check returns 200 with OK", async () => {
const response = await fetch(`${hostAddress}/health-check`, {
redirect: "manual",
});
expect(response.status).toBe(200);
expect(await response.text()).toBe("OK");
});
describe("/melpa/snapshot", () => {
const snapshotLeadingPathComp = "melpa/snapshot" as const;
for (const [name, path] of [
["valid with a one-level subdir", "2024-01-01/a/b"],
["valid with a one-level subdir with a trailing slash", "2024-02-02/a/b/"],
["valid with a two-level subdir", "2024-01-01/a/b/c"],
[
"valid with a two-level subdir with a trailing slash",
"2024-03-03/a/b/c/",
],
] as const) {
test(`Redirect with valid URL under /shapshot: ${name}`, async () => {
const response = await fetch(
`${hostAddress}/${snapshotLeadingPathComp}/${path}`,
{
redirect: "manual",
},
);
expect(response.status).toBe(301);
expect(response.headers.get("location")).toBe(
delpaGitHubRawBaseUrl +
`/melpa-snapshot-${path.split("/")[0]}/refs/heads/master/packages/` +
path.slice(
path.indexOf("/") + 1, // Remove the top-level folder in path
),
);
});
}
for (const [name, path] of [
["without a trailing slash", "2024-01-01"],
["with a trailing slash", "2024-02-02/"],
] as const) {
test(`Report OK with valid snapshot version at a root dir of /melpa/shapshot: ${name}`, async () => {
const response = await fetch(
`${hostAddress}/${snapshotLeadingPathComp}/${path}`,
{
redirect: "manual",
},
);
expect(response.status).toBe(200);
const responseText = await response.text();
expect(responseText).toContain("valid snapshot version");
expect(responseText).toContain(path.split("/")[0]);
});
}
for (const [name, path] of [
["non-existing snapshot", "2025-01-01"],
["non-existing partially-matched snapshot", "2024-01"],
["non-existing with no subdir", "non-existing"],
["non-existing with no subdir but with a trailing slash", "non-existing/"],
["non-existing with a one-level subdir", "non-existing/a"],
[
"non-existing with a one-level subdir with a trailing slash",
"non-existing/a/",
],
["non-existing with a two-level subdir", "non-existing/a/b"],
[
"non-existing with a two-level subdir with a trailing slash",
"non-existing/a/b/",
],
] as const) {
test(`Return 404 with invalid URL under /melpa/shapshot: ${name}`, async () => {
const response = await fetch(
`${hostAddress}/${snapshotLeadingPathComp}/${path}`,
{
redirect: "manual",
},
);
expect(response.status).toBe(404);
expect(response.headers.get("content-type")).toContain("text/plain");
const responseText = await response.text();
expect(responseText).toContain("404 Not Found");
expect(responseText).toContain(
`Invalid snapshot version: ${path.split("/")[0]}`,
);
});
}
});
describe("/melpa/at-least-days-old", () => {
const ageLeadingPathComp = "melpa/at-least-days-old" as const;
for (const [name, path, snapshotVersion] of [
["without a trailing slash", "1", "2024-10-10"],
["with a trailing slash", "270/", "2024-03-03"],
] as const) {
test(`Report OK with valid snapshot version at a root dir of /melpa/at-least-days-old: ${name}`, async () => {
const response = await fetch(
`${hostAddress}/${ageLeadingPathComp}/${path}`,
{
redirect: "manual",
},
);
expect(response.status).toBe(200);
const responseText = await response.text();
expect(responseText).toBe(
`${path.split("/")[0]} days old points to snapshot version ${snapshotVersion}.`,
);
});
}
for (const [name, path, snapshotVersion] of [
["without a trailing slash", "1/a/b", "2024-10-10"],
["with a trailing slash", "270/a/b/", "2024-03-03"],
] as const) {
test(`Redirect to the corresponding snapshot given a file under /melpa/at-least-days-old: ${name}`, async () => {
const response = await fetch(
`${hostAddress}/${ageLeadingPathComp}/${path}`,
{
redirect: "manual",
},
);
expect(response.status).toBe(301);
expect(response.headers.get("location")).toBe(
delpaGitHubRawBaseUrl +
`/melpa-snapshot-${snapshotVersion}/refs/heads/master/packages/` +
path.slice(
path.indexOf("/") + 1, // Remove the top-level folder in path
),
);
});
}
for (const [name, path] of [
["0 days", "0/a/b"],
["more than 270 days", "271/a/b/"],
] as const) {
test(`404 for out-of-range number of days under /melpa/at-least-days-old: ${name}`, async () => {
const response = await fetch(
`${hostAddress}/${ageLeadingPathComp}/${path}`,
{
redirect: "manual",
},
);
expect(response.status).toBe(404);
expect(await response.text()).toBe("404 Not Found");
});
}
});