feat(git): a Fetch button in the Git status header, and an interval for it app-wide and per case

Fetch is the only git command Codeman runs that writes, and the only one that runs a
repository's own fetch-post hook, so it stays behind a click and off by default. The Git
status window's header row gains Fetch; App Settings -> Bottom bar gains the switch and
the interval, synced because the scheduler acts on them, and Session Options -> Git
overrides both per case. The scheduler reads settings fresh each tick, times the interval
from the start of a run, never overlaps runs, and re-checks the Docker and remote rules
every tick before any git runs. Nothing but remote-tracking refs moves: HEAD, the index
and the working tree are proven unchanged against real git while origin/main advances.

Tests: src/git-fetch.ts and the scheduler, the per-case settings route, the synced/display
setting split, the two new i18n rows, and the Fetch block of the Git status browser suite.
This commit is contained in:
Devvyn committed 2026-10-25 17:23:55 +08:00
1 parent 3e06f3c141
commit 9432d9f9e2
35 files changed
+2340 -39

No files matched your search

+145
View File
@@ -29,6 +29,7 @@ describe('Git status indicator in a real browser', () => {
let repoSession: string;
let plainSession: string;
const gitStatusRequests: string[] = [];
const gitFetchRequests: string[] = [];
const settingsPutStatuses: number[] = [];
const write = (rel: string, text = 'x\n') => writeFileSync(join(repo, rel), text);
@@ -91,6 +92,9 @@ describe('Git status indicator in a real browser', () => {
page.on('request', (r) => {
// The API route only: the page also loads /git-status-ui.js, whose URL contains the same words.
if (/\/api\/sessions\/[^/]+\/git-status/.test(r.url())) gitStatusRequests.push(r.url());
// Anchored: the per-case Git settings route (/git-fetch-settings) is a different route, and a bare
// /git-fetch test would count its PUTs as fetches. The fetch route carries the limits as a query.
if (/\/api\/sessions\/[^/]+\/git-fetch(\?|$)/.test(r.url())) gitFetchRequests.push(r.url());
});
page.on('response', (r) => {
if (r.request().method() === 'PUT' && r.url().endsWith('/api/settings')) settingsPutStatuses.push(r.status());
@@ -514,6 +518,147 @@ describe('Git status indicator in a real browser', () => {
await refresh();
}, 40000);
it('puts Fetch in the header row, between Refresh and Close', async () => {
const order = await page.evaluate(() =>
[...document.querySelectorAll('.git-status-actions button')].map((b) => b.getAttribute('aria-label'))
);
expect(order).toEqual(['Refresh git status', 'Fetch git status', 'Close git status']);
// A repository the panel can read is a repository Fetch can fetch, so the button is live here.
expect(await page.evaluate(() => document.getElementById('gitStatusFetchBtn')!.disabled)).toBe(false);
});
it('Fetch fetches for real, moves the behind number, and reports what it brought', async () => {
// A second clone pushes a commit this repository has not fetched, so its remote-tracking ref is stale:
// the panel cannot show a `behind` number until a fetch runs. That is what the footer means by
// "as of the last fetch", and the number only becomes current by the button.
const other = join(root, 'other-clone');
git(root, 'clone', '-q', join(root, 'origin.git'), other);
writeFileSync(join(other, 'from-elsewhere.txt'), 'y\n');
git(other, 'add', '-A');
git(other, 'commit', '-q', '-m', 'from elsewhere');
git(other, 'push', '-q');
await refresh();
expect(await page.textContent('#gitStatusBody')).not.toMatch(/behind/);
const before = gitFetchRequests.length;
await page.click('#gitStatusFetchBtn');
await page.waitForFunction(
() => /↓ 1 behind/.test(document.getElementById('gitStatusBody')?.textContent ?? ''),
null,
{ timeout: 20000 }
);
expect(gitFetchRequests.length).toBe(before + 1);
// The same limits the status route gets go with the fetch, so the two agree on which repositories count.
expect(gitFetchRequests.at(-1)).toMatch(/maxRepos=12/);
expect(await page.locator('.git-status-chip', { hasText: 'behind' }).textContent()).toBe('↓ 1 behind');
expect(await page.textContent('#gitStatusBody .git-status-fetch-summary')).toMatch(
/^Fetched .+: 1 of 1 repository\.$/
);
expect(await page.locator('#gitStatusBody .git-status-fetch-row').count()).toBe(0);
// The 15 s poll rebuilds the whole body, so the note has to come from the renderer, not from the click.
await refresh();
expect(await page.locator('#gitStatusBody .git-status-fetch-summary').count()).toBe(1);
const foot = (await page.textContent('#gitStatusFooter')) ?? '';
expect(foot).toMatch(/Read-only apart from Fetch/);
// No longer "only when you click": an interval the user turned on fetches too, and the footer says so.
expect(foot).toMatch(/interval/);
// The chip itself says how it got current, so the number is not read as live.
expect(await page.locator('.git-status-chip', { hasText: 'behind' }).getAttribute('title')).toMatch(/interval/);
rmSync(other, { recursive: true, force: true });
}, 30000);
it('a repository Fetch could not fetch keeps its row and shows why', async () => {
await page.route('**/api/sessions/*/git-fetch*', (route) =>
route.fulfill({
contentType: 'application/json',
body: JSON.stringify({
success: true,
data: {
state: 'ok',
reposTruncated: false,
fetchedAt: Date.now(),
repos: [
{ name: 'fetchable', path: 'fetchable', ok: true },
{
name: 'private',
path: '/srv/private',
ok: false,
failure: { code: 'AUTH', message: 'could not read the remote' },
},
],
},
}),
})
);
await page.click('#gitStatusFetchBtn');
await page.waitForFunction(() =>
/could not read the remote/.test(document.getElementById('gitStatusBody')?.textContent ?? '')
);
// The summary counts what landed, and the failure is named rather than folded into the count. The
// summary is the panel's first note line, so the count is read off it, not off the whole body.
expect(await page.textContent('#gitStatusBody .git-status-fetch-summary')).toMatch(
/^Fetched .+: 1 of 2 repositories\.$/
);
const row = (await page.textContent('#gitStatusBody .git-status-fetch-row')) ?? '';
expect(row).toContain('private');
expect(row).toContain('⚠ could not read the remote');
expect(await page.locator('#gitStatusBody .git-status-fetch-row').count()).toBe(1);
// Git text is untrusted: the row is text, never markup.
expect(await page.$('#gitStatusBody img')).toBeNull();
await page.unroute('**/api/sessions/*/git-fetch*');
}, 30000);
it('one click means one fetch: the button is busy while it runs and a second call is ignored', async () => {
let slow = true;
await page.route('**/api/sessions/*/git-fetch*', async (route) => {
if (slow) await new Promise((r) => setTimeout(r, 1500));
await route.continue();
});
page.setDefaultTimeout(6000);
await page.click('#gitStatusFetchBtn');
await page.waitForFunction(() => (window as any).app._gitFetchInFlight === true);
const busy = await page.evaluate(() => {
const b = document.getElementById('gitStatusFetchBtn')!;
return { ariaBusy: b.getAttribute('aria-busy'), disabled: b.disabled };
});
expect(busy.ariaBusy).toBe('true');
// Disabled is what stops a second click reaching the handler, so one click is one fetch per repository.
expect(busy.disabled).toBe(true);
const during = gitFetchRequests.length;
await page.evaluate(() => void (window as any).app.fetchGitStatusNow());
expect(gitFetchRequests.length).toBe(during);
slow = false;
// The flag goes false before the re-render that follows it, so wait for the button itself, not the flag:
// reading it right at the flip catches the panel mid-render.
await page.waitForFunction(
() => {
const b = document.getElementById('gitStatusFetchBtn')!;
return (window as any).app._gitFetchInFlight === false && !b.disabled && !b.getAttribute('aria-busy');
},
null,
{ timeout: 15000 }
);
// The handler is still asleep: unrouting without waiting fails the run as an unhandled rejection.
await page.unrouteAll({ behavior: 'wait' });
page.setDefaultTimeout(30000);
}, 30000);
it('Fetch is inert for a session nothing can be fetched for', async () => {
await select(plainSession);
await page.waitForFunction(
() => /No git repository here/.test(document.getElementById('gitStatusBody')?.textContent ?? ''),
null,
{ timeout: 15000 }
);
expect(await page.evaluate(() => document.getElementById('gitStatusFetchBtn')!.disabled)).toBe(true);
await select(repoSession);
await page.waitForFunction(() => document.getElementById('gitStatusFetchBtn')!.disabled === false, null, {
timeout: 15000,
});
await page.waitForSelector('#gitStatusPanel.visible');
}, 30000);
it('closing the panel resets it; turning the setting off hides the button, closes the panel and stops polling', async () => {
await page.click('.git-status-actions button[aria-label="Close git status"]');
expect(await page.isVisible('#gitStatusPanel')).toBe(false);