Skip to content

Commit 014feb6

Browse files
test(server): add running-server evidence harness for PR base fallback
Drives git.runStackedAction create_pr against a real server with a stubbed GitHub Enterprise forge (HTTPS CONNECT proxy for github.invalid only), fake gh/codex, and per-row git fixtures backed by a local bare repo. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 607524e commit 014feb6

5 files changed

Lines changed: 521 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
# Fake codex: answers the probes T3 makes and writes a fixed PR title/body for `exec`.
3+
dir="${EV_LOG_DIR:-/tmp}"
4+
echo "codex $*" >> "$dir/codex.log"
5+
case "$1" in
6+
--version) echo "codex-cli 0.0.0-fake"; exit 0 ;;
7+
mcp) echo "[]"; exit 0 ;;
8+
exec)
9+
out=""
10+
prev=""
11+
for arg in "$@"; do
12+
if [ "$prev" = "--output-last-message" ] || [ "$prev" = "-o" ]; then out="$arg"; fi
13+
prev="$arg"
14+
done
15+
n=$(ls "$dir"/prompt-*.txt 2>/dev/null | wc -l)
16+
cat > "$dir/prompt-$((n + 1)).txt"
17+
if [ -n "$out" ]; then printf '%s' '{"title":"Fixture PR","body":"fixture body"}' > "$out"; fi
18+
exit 0 ;;
19+
esac
20+
exit 0
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
# Fake gh: only hands out a token for the stub forge. Logs argv.
3+
echo "gh $*" >> "${EV_LOG_DIR:-/tmp}/gh.log"
4+
if [ "$1" = "auth" ] && [ "$2" = "token" ]; then echo fake-token; exit 0; fi
5+
exit 1
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
# Builds one row's git fixture: a local bare repo acting as the remote, and a work clone
3+
# whose origin URL is https://github.invalid/acme/demo.git (pushes rewritten to the bare repo).
4+
# Usage: fixture.sh <row-dir> <R1|R2a|R2b|R3|R4a|R4b>
5+
set -euo pipefail
6+
R=$1; ROW=$2
7+
B=$R/remote.git; W=$R/work; U=https://github.invalid/acme/demo.git
8+
export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
9+
rm -rf "$R"; mkdir -p "$R"
10+
DEF=main; case $ROW in R4*) DEF=trunk ;; esac
11+
git init -q --bare -b main "$B"
12+
git init -q -b "$DEF" "$W"; cd "$W"
13+
git config user.name Fixture; git config user.email [email protected]
14+
git remote add origin "$U"
15+
git config url."$B".pushInsteadOf "$U"
16+
F() { git -c url."$B".insteadOf="$U" "$@"; }
17+
echo m > m; git add m; git commit -qm "Main commit"; git push -q -u origin "$DEF"
18+
case $ROW in R4*) ;; *) git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main ;; esac
19+
git switch -qc parent; echo p > p; git add p; git commit -qm "Parent commit"
20+
case $ROW in R1|R2a|R2b) git push -q -u origin parent ;; esac
21+
git switch -qc child; echo c > c; git add c; git commit -qm "Child commit"; git push -q -u origin child
22+
git config branch.child.gh-merge-base parent
23+
case $ROW in
24+
R2a) git -C "$B" branch -q -D parent; F fetch -q --prune origin ;;
25+
R2b) git -C "$B" branch -q -D parent ;;
26+
R4b) git config --unset branch.child.gh-merge-base ;;
27+
esac
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
// HTTPS CONNECT proxy that only admits github.invalid:443 and serves a minimal GitHub
2+
// Enterprise API there. Every request (and every refused CONNECT) is appended to a JSONL log.
3+
import * as NodeChildProcess from "node:child_process";
4+
import * as NodeFS from "node:fs";
5+
import * as NodeHttp from "node:http";
6+
import * as NodeHttps from "node:https";
7+
import * as NodePath from "node:path";
8+
9+
const HOST = "github.invalid";
10+
11+
export function ensureCertificates(caDir) {
12+
const leafKey = NodePath.join(caDir, "leaf.key");
13+
if (NodeFS.existsSync(leafKey)) return;
14+
NodeFS.mkdirSync(caDir, { recursive: true });
15+
const openssl = (...args) => NodeChildProcess.execFileSync("openssl", args, { stdio: "pipe" });
16+
openssl(
17+
"req",
18+
"-x509",
19+
"-newkey",
20+
"rsa:2048",
21+
"-nodes",
22+
"-days",
23+
"2",
24+
"-subj",
25+
"/CN=pr-22943 throwaway CA",
26+
"-keyout",
27+
NodePath.join(caDir, "ca.key"),
28+
"-out",
29+
NodePath.join(caDir, "ca.pem"),
30+
);
31+
NodeFS.writeFileSync(NodePath.join(caDir, "leaf.ext"), `subjectAltName=DNS:${HOST}\n`);
32+
openssl(
33+
"req",
34+
"-newkey",
35+
"rsa:2048",
36+
"-nodes",
37+
"-subj",
38+
`/CN=${HOST}`,
39+
"-keyout",
40+
leafKey,
41+
"-out",
42+
NodePath.join(caDir, "leaf.csr"),
43+
);
44+
openssl(
45+
"x509",
46+
"-req",
47+
"-in",
48+
NodePath.join(caDir, "leaf.csr"),
49+
"-CA",
50+
NodePath.join(caDir, "ca.pem"),
51+
"-CAkey",
52+
NodePath.join(caDir, "ca.key"),
53+
"-CAcreateserial",
54+
"-days",
55+
"2",
56+
"-extfile",
57+
NodePath.join(caDir, "leaf.ext"),
58+
"-out",
59+
NodePath.join(caDir, "leaf.pem"),
60+
);
61+
}
62+
63+
/** @param {{ caDir: string, logFile: string, repoInfo: "ok" | "404" }} options */
64+
export async function startForgeStub({ caDir, logFile, repoInfo }) {
65+
ensureCertificates(caDir);
66+
const log = (entry) =>
67+
NodeFS.appendFileSync(
68+
logFile,
69+
JSON.stringify({ at: new Date().toISOString(), ...entry }) + "\n",
70+
);
71+
let created = null;
72+
73+
const api = NodeHttps.createServer(
74+
{
75+
key: NodeFS.readFileSync(NodePath.join(caDir, "leaf.key")),
76+
cert: NodeFS.readFileSync(NodePath.join(caDir, "leaf.pem")),
77+
},
78+
(req, res) => {
79+
let raw = "";
80+
req.on("data", (chunk) => (raw += chunk));
81+
req.on("end", () => {
82+
let body = null;
83+
try {
84+
body = raw ? JSON.parse(raw) : null;
85+
} catch {
86+
body = raw;
87+
}
88+
const json = (status, value) => {
89+
log({ kind: "request", method: req.method, url: req.url, body, status, response: value });
90+
res.writeHead(status, { "content-type": "application/json" }).end(JSON.stringify(value));
91+
};
92+
const path = (req.url ?? "").split("?")[0];
93+
if (req.method === "GET" && path === "/api/v3/repos/acme/demo") {
94+
return repoInfo === "404"
95+
? json(404, { message: "Not Found" })
96+
: json(200, {
97+
id: 1,
98+
node_id: "R_1",
99+
name: "demo",
100+
full_name: "acme/demo",
101+
private: false,
102+
owner: { login: "acme", type: "Organization" },
103+
html_url: `https://${HOST}/acme/demo`,
104+
clone_url: `https://${HOST}/acme/demo.git`,
105+
ssh_url: `git@${HOST}:acme/demo.git`,
106+
default_branch: "main",
107+
});
108+
}
109+
if (req.method === "POST" && path === "/api/graphql") {
110+
const query = String(body?.query ?? "");
111+
if (query.includes("pullRequests(")) {
112+
// Batched queries alias each field as `_<n>_repository` with `_<n>_head` variables.
113+
const prefixes = [
114+
...query.matchAll(/(_\d+_)?repository:repository\(|repository\(/g),
115+
].map((match) => match[1] ?? "");
116+
const data = {};
117+
for (const prefix of new Set(prefixes)) {
118+
const head = body?.variables?.[`${prefix}head`];
119+
const nodes = created && created.head === head ? [created.node] : [];
120+
data[`${prefix}repository`] = {
121+
pullRequests: { pageInfo: { hasNextPage: false, endCursor: null }, nodes },
122+
};
123+
}
124+
return json(200, { data });
125+
}
126+
return json(200, { errors: [{ message: "stub: unsupported query" }] });
127+
}
128+
if (req.method === "POST" && path === "/api/v3/repos/acme/demo/pulls") {
129+
const node = {
130+
number: 7,
131+
title: body.title,
132+
url: `https://${HOST}/acme/demo/pull/7`,
133+
baseRefName: body.base,
134+
headRefName: body.head,
135+
headRefOid: "0".repeat(40),
136+
state: "OPEN",
137+
isDraft: false,
138+
mergedAt: null,
139+
closedAt: null,
140+
updatedAt: new Date().toISOString(),
141+
isCrossRepository: false,
142+
headRepository: { name: "demo", nameWithOwner: "acme/demo" },
143+
headRepositoryOwner: { __typename: "Organization", login: "acme" },
144+
};
145+
created = { head: body.head, node };
146+
log({ kind: "forge-create-pr", body });
147+
return json(201, {
148+
number: 7,
149+
html_url: node.url,
150+
base: { ref: body.base },
151+
head: { ref: body.head },
152+
title: body.title,
153+
});
154+
}
155+
return json(404, { message: "stub: not handled" });
156+
});
157+
},
158+
);
159+
api.on("tlsClientError", (error) => log({ kind: "tls-error", message: error.message }));
160+
161+
const proxy = NodeHttp.createServer((req, res) => {
162+
log({ kind: "plain-http-refused", method: req.method, url: req.url });
163+
res.writeHead(403).end();
164+
});
165+
proxy.on("connect", (req, socket, head) => {
166+
if (req.url !== `${HOST}:443`) {
167+
log({ kind: "connect-refused", target: req.url });
168+
socket.end("HTTP/1.1 403 Forbidden\r\n\r\n");
169+
return;
170+
}
171+
log({ kind: "connect", target: req.url });
172+
socket.write("HTTP/1.1 200 Connection Established\r\n\r\n");
173+
if (head.length > 0) socket.unshift(head);
174+
api.emit("connection", socket);
175+
});
176+
proxy.listen(0, "127.0.0.1");
177+
await new Promise((done) => proxy.once("listening", done));
178+
return {
179+
port: proxy.address().port,
180+
close: () =>
181+
new Promise((done) => {
182+
proxy.closeAllConnections?.();
183+
api.closeAllConnections?.();
184+
proxy.close(() => done());
185+
}),
186+
};
187+
}

0 commit comments

Comments
 (0)