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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
import { type Page } from "@playwright/test";
import * as path from "path";
import * as os from "os";
import * as fs from "fs/promises";
export const BASE_URL = process.env.E2E_BASE_URL ?? "http://localhost:3000";
export const TEST_PASSWORD = "TestPass123!";
let _seq = 0;
export function uid(prefix = "u"): string {
_seq++;
return `${prefix}${Date.now()}${_seq}`;
}
export async function createTestUser(
page: Page,
prefix = "tuser"
): Promise<string> {
const username = uid(prefix);
const email = `${username}@test.example`;
await page.goto("/register");
await page.fill('input[name="username"]', username);
await page.fill('input[name="email"]', email);
await page.fill('input[name="password"]', TEST_PASSWORD);
await page.click('button[type="submit"]');
await page.waitForURL(/\/(dashboard|[a-z])/);
return username;
}
export async function loginUser(
page: Page,
username: string,
password = TEST_PASSWORD
): Promise<void> {
await page.goto("/login");
await page.fill('input[name="username"]', username);
await page.fill('input[name="password"]', password);
await page.click('button[type="submit"]');
await page.waitForURL(/\/(dashboard|[a-z])/);
}
export async function logoutUser(page: Page): Promise<void> {
const logoutLink = page.locator('a[href="/logout"]').first();
if (await logoutLink.isVisible()) {
await logoutLink.click();
} else {
await page.goto("/logout");
}
await page.waitForURL(/\/(login|register|$)/);
}
export async function createTestRepo(
page: Page,
owner: string,
namePrefix = "repo"
): Promise<string> {
const repoName = uid(namePrefix);
await page.goto("/new");
await page.fill('input[name="name"]', repoName);
const descField = page.locator('input[name="description"], textarea[name="description"]');
if (await descField.isVisible()) {
await descField.fill("E2E test repo");
}
await page.click('button[type="submit"]');
await page.waitForURL(new RegExp(`/${owner}/${repoName}`));
return repoName;
}
export async function pushTestCommit(opts: {
owner: string;
repo: string;
username: string;
password?: string;
fileName?: string;
fileContent?: string;
commitMsg?: string;
branch?: string;
}): Promise<string> {
const {
owner,
repo,
username,
password = TEST_PASSWORD,
fileName = "README.md",
fileContent = `# ${repo}\n\nE2E test commit.\n`,
commitMsg = "Add test file",
branch = "main",
} = opts;
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "gc-e2e-"));
const baseUrl = BASE_URL.replace("://", `://${username}:${encodeURIComponent(password)}@`);
const repoUrl = `${baseUrl}/${owner}/${repo}.git`;
await spawnGit(tmpDir, ["init", "-b", branch]);
await spawnGit(tmpDir, ["remote", "add", "origin", repoUrl]);
await spawnGit(tmpDir, ["config", "user.email", `${username}@test.example`]);
await spawnGit(tmpDir, ["config", "user.name", username]);
await fs.writeFile(path.join(tmpDir, fileName), fileContent, "utf8");
await spawnGit(tmpDir, ["add", "."]);
await spawnGit(tmpDir, ["commit", "-m", commitMsg]);
await spawnGit(tmpDir, ["push", "-u", "origin", branch]);
return tmpDir;
}
export async function pushFeatureBranch(opts: {
repoDir: string;
username: string;
branchName?: string;
fileName?: string;
fileContent?: string;
commitMsg?: string;
}): Promise<string> {
const {
repoDir,
username,
branchName = uid("feat-"),
fileName = "feature.md",
fileContent = "Feature branch file.\n",
commitMsg = "Add feature file",
} = opts;
await spawnGit(repoDir, ["config", "user.email", `${username}@test.example`]);
await spawnGit(repoDir, ["config", "user.name", username]);
await spawnGit(repoDir, ["checkout", "-b", branchName]);
await fs.writeFile(path.join(repoDir, fileName), fileContent, "utf8");
await spawnGit(repoDir, ["add", "."]);
await spawnGit(repoDir, ["commit", "-m", commitMsg]);
await spawnGit(repoDir, ["push", "-u", "origin", branchName]);
return branchName;
}
async function spawnGit(cwd: string, args: string[]): Promise<string> {
const proc = Bun.spawn(["git", ...args], {
cwd,
env: {
...process.env,
GIT_TERMINAL_PROMPT: "0",
GIT_ASKPASS: "echo",
},
stdout: "pipe",
stderr: "pipe",
});
const exitCode = await proc.exited;
const stdout = await new Response(proc.stdout).text();
const stderr = await new Response(proc.stderr).text();
if (exitCode !== 0) {
throw new Error(
`git ${args.join(" ")} failed (exit ${exitCode}):\n${stderr}\n${stdout}`
);
}
return stdout.trim();
}
export async function cleanupDir(dir: string): Promise<void> {
try {
await fs.rm(dir, { recursive: true, force: true });
} catch {
}
}
|