e2e.repos.test.ts
⎇
Raw
1import { describe, test, expect, beforeAll, afterAll } from 'bun:test';
2import { rmSync, writeFileSync } from 'node:fs';
3import { spawnSync } from 'node:child_process';
4import { chromium } from 'playwright';
5import type { Browser, BrowserContext } from 'playwright';
6import {
7 BASE,
8 ADMIN_PASS,
9 DATA_DIR,
10 setupTestEnv,
11 spawnServer,
12 killServer,
13 login,
14 seedRepo,
15 db,
16} from './helpers.ts';
17
18let browser: Browser;
19let server: Awaited<ReturnType<typeof spawnServer>>;
20
21beforeAll(async () => {
22 await setupTestEnv();
23 server = await spawnServer();
24 browser = await chromium.launch();
25
26 // Register alice
27 const ctx = await browser.newContext();
28 const page = await ctx.newPage();
29 try {
30 await page.goto(`${BASE}/register`);
31 await page.fill('[name=username]', 'alice');
32 await page.fill('[name=password]', 'password123');
33 await page.fill('[name=password2]', 'password123');
34 await page.click('button[type=submit]');
35 await page.waitForURL(BASE + '/');
36 } finally { await ctx.close(); }
37});
38
39afterAll(async () => {
40 await browser.close();
41 await killServer(server);
42});
43
44async function loggedInContext(username = 'admin', password = ADMIN_PASS) {
45 const ctx = await browser.newContext();
46 const page = await ctx.newPage();
47 await login(page, username, password);
48 await page.close();
49 return ctx;
50}
51
52// ─── Repos ────────────────────────────────────────────────────────────────────
53
54describe('repos', () => {
55 // Shared admin context — cookies persist across tests in this block.
56 let adminCtx: BrowserContext;
57 // Alice's context, created after alice is registered in auth tests.
58 let aliceCtx: BrowserContext;
59 // Set after 'commit log' test; used by all commit-detail tests below.
60 let commitUrl: string;
61
62 beforeAll(async () => {
63 adminCtx = await loggedInContext();
64 aliceCtx = await loggedInContext('alice', 'password123');
65 });
66
67 afterAll(async () => {
68 await adminCtx.close();
69 await aliceCtx.close();
70 });
71
72 test('non-admin gets 403 on /new', async () => {
73 const page = await aliceCtx.newPage();
74 try {
75 const resp = await page.request.get(`${BASE}/new`);
76 expect(resp.status()).toBe(403);
77 } finally { await page.close(); }
78 });
79
80 test('create repository', async () => {
81 const page = await adminCtx.newPage();
82 try {
83 await page.goto(`${BASE}/new`);
84 await page.fill('[name=name]', 'my-repo');
85 await page.fill('[name=description]', 'A test repo');
86 await page.click('form[action="/new"] button[type=submit]');
87 await page.waitForURL(`${BASE}/my-repo`);
88 expect(await page.locator('.empty-state').isVisible()).toBe(true);
89 } finally { await page.close(); }
90 });
91
92 test('repository appears in list', async () => {
93 const page = await adminCtx.newPage();
94 try {
95 await page.goto(BASE);
96 expect(await page.locator('.repo-name').allTextContents()).toContain('my-repo');
97 } finally { await page.close(); }
98 });
99
100 test('search finds matching repo', async () => {
101 const page = await adminCtx.newPage();
102 try {
103 await page.goto(BASE);
104 await page.fill('[name=q]', 'my-repo');
105 await page.click('.search-form button[type=submit]');
106 expect(await page.locator('.repo-name').allTextContents()).toContain('my-repo');
107 } finally { await page.close(); }
108 });
109
110 test('search returns empty for unknown term', async () => {
111 const page = await adminCtx.newPage();
112 try {
113 await page.goto(BASE);
114 await page.fill('[name=q]', 'zzz-nothing-here');
115 await page.click('.search-form button[type=submit]');
116 expect(await page.locator('.empty-state').isVisible()).toBe(true);
117 } finally { await page.close(); }
118 });
119
120 test('browse file tree after seeding content', async () => {
121 await seedRepo('my-repo');
122 const page = await adminCtx.newPage();
123 try {
124 await page.goto(`${BASE}/my-repo/tree/main`);
125 const files = await page.locator('.file-name a').allTextContents();
126 expect(files).toContain('README.md');
127 expect(files).toContain('index.js');
128 } finally { await page.close(); }
129 });
130
131 test('view file blob with syntax highlighting', async () => {
132 const page = await adminCtx.newPage();
133 try {
134 await page.goto(`${BASE}/my-repo/blob/main/index.js`);
135 expect(await page.locator('.file-blob-name').textContent()).toBe('index.js');
136 expect(await page.locator('.file-blob-body').isVisible()).toBe(true);
137 } finally { await page.close(); }
138 });
139
140 test('raw file download responds 200', async () => {
141 const page = await adminCtx.newPage();
142 try {
143 const resp = await page.request.get(`${BASE}/my-repo/raw/main/README.md`);
144 expect(resp.status()).toBe(200);
145 expect(resp.headers()['content-disposition']).toContain('README.md');
146 } finally { await page.close(); }
147 });
148
149 test('commit log shows initial commit', async () => {
150 const page = await adminCtx.newPage();
151 try {
152 await page.goto(`${BASE}/my-repo/commits/main`);
153 const subjects = await page.locator('.commit-subject').allTextContents();
154 expect(subjects.some(s => s.includes('Initial commit'))).toBe(true);
155 // Navigate to the commit page and capture the URL for subsequent tests
156 await page.locator('.commit-hash').first().click();
157 await page.waitForURL(/\/my-repo\/commit\//);
158 commitUrl = page.url();
159 } finally { await page.close(); }
160 });
161
162 test('commit detail shows metadata card', async () => {
163 const page = await adminCtx.newPage();
164 try {
165 await page.goto(commitUrl);
166 expect(await page.locator('.commit-card').isVisible()).toBe(true);
167 expect(await page.locator('.commit-card-subject').textContent()).toContain('Initial commit');
168 // Author, date and SHA rows are all present
169 const metaText = await page.locator('.commit-card-meta').textContent();
170 expect(metaText).toContain('Test'); // author name set by seedRepo
171 expect(metaText).toContain('Author'); // label (CSS uppercases visually)
172 expect(metaText).toContain('Date');
173 expect(metaText).toContain('Commit');
174 } finally { await page.close(); }
175 });
176
177 test('commit detail full SHA is shown', async () => {
178 const page = await adminCtx.newPage();
179 try {
180 await page.goto(commitUrl);
181 const sha = commitUrl.split('/commit/')[1] ?? null;
182 expect(await page.locator('.commit-sha-full').textContent()).toBe(sha);
183 } finally { await page.close(); }
184 });
185
186 test('commit detail shows file nav sidebar', async () => {
187 const page = await adminCtx.newPage();
188 try {
189 await page.goto(commitUrl);
190 expect(await page.locator('.file-nav-details').isVisible()).toBe(true);
191 const navItems = await page.locator('.file-nav-item').allTextContents();
192 // seedRepo adds README.md and index.js
193 expect(navItems.some(t => t.includes('README.md'))).toBe(true);
194 expect(navItems.some(t => t.includes('index.js'))).toBe(true);
195 } finally { await page.close(); }
196 });
197
198 test('commit detail file nav items are anchor links to diff sections', async () => {
199 const page = await adminCtx.newPage();
200 try {
201 await page.goto(commitUrl);
202 const hrefs = await page.locator('.file-nav-item').evaluateAll(
203 els => els.map(el => el.getAttribute('href') ?? ''),
204 );
205 expect(hrefs.every(h => h.startsWith('#'))).toBe(true);
206 } finally { await page.close(); }
207 });
208
209 test('commit detail shows diff table with added lines', async () => {
210 const page = await adminCtx.newPage();
211 try {
212 await page.goto(commitUrl);
213 // Initial commit only adds lines
214 expect(await page.locator('.diff-table').first().isVisible()).toBe(true);
215 expect(await page.locator('.diff-row-add').count()).toBeGreaterThan(0);
216 expect(await page.locator('.diff-row-del').count()).toBe(0);
217 } finally { await page.close(); }
218 });
219
220 test('commit detail diff table has line numbers', async () => {
221 const page = await adminCtx.newPage();
222 try {
223 await page.goto(commitUrl);
224 // New-side line numbers (column 2) on add rows start at 1
225 const firstNewLn = await page.locator('.diff-row-add .diff-ln-new').first().textContent();
226 expect(firstNewLn?.trim()).toBe('1');
227 } finally { await page.close(); }
228 });
229
230 test('commit detail shows added stats on file header', async () => {
231 const page = await adminCtx.newPage();
232 try {
233 await page.goto(commitUrl);
234 const addStats = await page.locator('.diff-stat-add').allTextContents();
235 expect(addStats.length).toBeGreaterThan(0);
236 expect(addStats.every(s => s.startsWith('+'))).toBe(true);
237 } finally { await page.close(); }
238 });
239
240 test('commit detail view-at-sha button links to blob at that commit', async () => {
241 const page = await adminCtx.newPage();
242 try {
243 await page.goto(commitUrl);
244 const sha = commitUrl.split('/commit/')[1];
245 const btn = page.locator('.btn-xs').first();
246 const href = await btn.getAttribute('href');
247 expect(href).toContain(`/blob/${sha}/`);
248 } finally { await page.close(); }
249 });
250
251 test('commit detail view-at-branch button links to blob at default branch', async () => {
252 const page = await adminCtx.newPage();
253 try {
254 await page.goto(commitUrl);
255 const btns = await page.locator('.btn-xs').allTextContents();
256 expect(btns.some(t => t.includes('@ main'))).toBe(true);
257 const branchBtns = await page.locator('.btn-xs').evaluateAll(
258 els => els.filter(el => el.textContent?.includes('@ main')).map(el => el.getAttribute('href') ?? ''),
259 );
260 expect(branchBtns.every(h => h.includes('/blob/main/'))).toBe(true);
261 } finally { await page.close(); }
262 });
263
264 test('commit detail file diff can be collapsed', async () => {
265 const page = await adminCtx.newPage();
266 try {
267 await page.goto(commitUrl);
268 // File body is visible when details is open
269 const diffFile = page.locator('.diff-file').first();
270 expect(await diffFile.getAttribute('open')).not.toBeNull();
271 // Click the summary to collapse
272 await diffFile.locator('.diff-file-header').click();
273 expect(await diffFile.getAttribute('open')).toBeNull();
274 } finally { await page.close(); }
275 });
276
277 test('commit detail file nav sidebar can be collapsed', async () => {
278 const page = await adminCtx.newPage();
279 try {
280 await page.goto(commitUrl);
281 const nav = page.locator('.file-nav-details');
282 expect(await nav.getAttribute('open')).not.toBeNull();
283 await nav.locator('.file-nav-toggle').click();
284 expect(await nav.getAttribute('open')).toBeNull();
285 } finally { await page.close(); }
286 });
287
288 test('readme renders on repo home', async () => {
289 const page = await adminCtx.newPage();
290 try {
291 await page.goto(`${BASE}/my-repo`);
292 expect(await page.locator('.readme-header').isVisible()).toBe(true);
293 expect(await page.locator('.readme-section .markdown-body').innerHTML()).toContain('my-repo');
294 } finally { await page.close(); }
295 });
296
297 test('private repo hidden from other users', async () => {
298 // Make private
299 const adminPage = await adminCtx.newPage();
300 try {
301 await adminPage.goto(`${BASE}/my-repo/settings`);
302 await adminPage.check('[name=is_private]');
303 await adminPage.click('form[action$="/settings"] button[type=submit]');
304 expect(await adminPage.locator('.form-success').isVisible()).toBe(true);
305 } finally { await adminPage.close(); }
306
307 // Alice should get 404
308 const alicePage = await aliceCtx.newPage();
309 try {
310 const resp = await alicePage.request.get(`${BASE}/my-repo`);
311 expect(resp.status()).toBe(404);
312 await alicePage.goto(BASE);
313 expect(await alicePage.locator('.repo-name').allTextContents()).not.toContain('my-repo');
314 } finally { await alicePage.close(); }
315
316 // Restore to public
317 const adminPage2 = await adminCtx.newPage();
318 try {
319 await adminPage2.goto(`${BASE}/my-repo/settings`);
320 await adminPage2.uncheck('[name=is_private]');
321 await adminPage2.click('form[action$="/settings"] button[type=submit]');
322 } finally { await adminPage2.close(); }
323 });
324
325 test('settings tab visible for admin, hidden for others', async () => {
326 const adminPage = await adminCtx.newPage();
327 try {
328 await adminPage.goto(`${BASE}/my-repo`);
329 expect(await adminPage.locator('.repo-tab[href$="/settings"]').isVisible()).toBe(true);
330 } finally { await adminPage.close(); }
331
332 const alicePage = await aliceCtx.newPage();
333 try {
334 await alicePage.goto(`${BASE}/my-repo`);
335 expect(await alicePage.locator('.repo-tab[href$="/settings"]').count()).toBe(0);
336 } finally { await alicePage.close(); }
337 });
338
339 test('create repository with invalid name shows error', async () => {
340 const resp = await adminCtx.request.post(`${BASE}/new`, {
341 form: { name: 'has spaces!', description: '', default_branch: 'main' },
342 maxRedirects: 0,
343 });
344 expect(resp.status()).toBe(200);
345 expect(await resp.text()).toContain('Invalid repository name');
346 });
347
348 test('auto-scanned repo is private by default', async () => {
349 const name = 'auto-private-repo';
350 const dir = `${process.cwd()}/${DATA_DIR}/repos/${name}.git`;
351
352 rmSync(dir, { recursive: true, force: true });
353 const tmp = `/tmp/hf-scan-${Date.now()}`;
354 try {
355 spawnSync('git', ['init', '--bare', dir], { stdio: 'ignore' });
356 spawnSync('git', ['clone', dir, tmp], { stdio: 'ignore' });
357 spawnSync('git', ['-C', tmp, 'commit', '--allow-empty', '-m', 'init'], { stdio: 'ignore' });
358 spawnSync('git', ['-C', tmp, 'push', 'origin', 'HEAD:main'], { stdio: 'ignore' });
359 } finally {
360 rmSync(tmp, { recursive: true, force: true });
361 }
362
363 // The Go server adopts repos found on disk at startup, not lazily per
364 // request, so restart it and read the row it created.
365 await killServer(server);
366 server = await spawnServer();
367 const repo = await db
368 .selectFrom('repositories')
369 .select(['name', 'is_private'])
370 .where('name', '=', name)
371 .executeTakeFirst();
372 expect(repo!.is_private).toBe(1);
373
374 await db.deleteFrom('repositories').where('name', '=', name).execute();
375 rmSync(dir, { recursive: true, force: true });
376 });
377
378 test('repo is registered even with a stale config.lock', async () => {
379 const name = 'stale-lock-repo';
380 const dir = `${process.cwd()}/${DATA_DIR}/repos/${name}.git`;
381
382 rmSync(dir, { recursive: true, force: true });
383 spawnSync('git', ['init', '--bare', dir], { stdio: 'ignore' });
384 // Drop core.bare so ensureBare has to attempt a write, then block it.
385 spawnSync('git', ['config', '--file', `${dir}/config`, '--unset', 'core.bare'], {
386 stdio: 'ignore',
387 });
388 writeFileSync(`${dir}/config.lock`, '');
389
390 await killServer(server);
391 server = await spawnServer();
392 const repo = await db
393 .selectFrom('repositories')
394 .select('name')
395 .where('name', '=', name)
396 .executeTakeFirst();
397 expect(repo!.name).toBe(name);
398
399 await db.deleteFrom('repositories').where('name', '=', name).execute();
400 rmSync(dir, { recursive: true, force: true });
401 });
402});
403