Files
lvmh/e2e/fake-gitlab.mjs
T

67 lines
2.2 KiB
JavaScript

// fake-gitlab.mjs — minimal Gitea v1 API stand-in on an ephemeral port.
// Serves exactly the two endpoints the daemon calls (daemon/gitlab.go):
// GET /api/v1/user (token validation)
// GET /api/v1/user/repos (repo listing)
// Repos are returned UNSORTED so the harness proves the daemon sorts by
// updated_at.
import * as http from "node:http";
export const GOOD_PAT = "gitea-e2e-0123456789abcdef";
const HTTP_OK = 200;
const HTTP_UNAUTHORIZED = 401;
const HTTP_NOT_FOUND = 404;
export function startFakeGitLab() {
const seen = { userAuths: [], repoAuths: [] };
const server = http.createServer((req, res) => {
const auth = String(req.headers["authorization"] ?? "");
const send = (code, obj) => {
res.writeHead(code, { "Content-Type": "application/json" });
res.end(JSON.stringify(obj));
};
if (req.url.startsWith("/api/v1/user/repos")) {
seen.repoAuths.push(auth);
if (auth !== `token ${GOOD_PAT}`) return send(HTTP_UNAUTHORIZED, { message: "401 Unauthorized" });
const base = `http://127.0.0.1:${server.address().port}`;
return send(HTTP_OK, [
{
full_name: "lvmh/beta",
name: "beta",
owner: { login: "lvmh" },
updated_at: "2025-07-01T10:00:00Z",
html_url: `${base}/lvmh/beta`,
default_branch: "main",
},
{
full_name: "lvmh/alpha",
name: "alpha",
owner: { login: "lvmh" },
updated_at: "2025-08-01T10:00:00Z",
html_url: `${base}/lvmh/alpha`,
default_branch: "trunk",
},
]);
}
if (req.url.startsWith("/api/v1/user")) {
seen.userAuths.push(auth);
return auth === `token ${GOOD_PAT}`
? send(HTTP_OK, { id: 1, login: "e2e-user", name: "E2E User" })
: send(HTTP_UNAUTHORIZED, { message: "401 Unauthorized" });
}
return send(HTTP_NOT_FOUND, { message: "404 Not Found" });
});
return new Promise((resolve) => {
server.listen(0, "127.0.0.1", () => {
resolve({
url: `http://127.0.0.1:${server.address().port}`,
pat: GOOD_PAT,
seen,
close() {
server.close();
},
});
});
});
}