-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add progress feedback for liberate/init commands #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /** | ||
| * Shared stderr progress handler for liberate operations. | ||
| */ | ||
|
|
||
| import type { ILiberateProgress } from '../application/interfaces/ILiberateService'; | ||
|
|
||
| /** | ||
| * Create a progress callback that writes liberate progress to stderr. | ||
| * Uses in-place `\r` updates when stderr is a TTY, newline-based otherwise. | ||
| */ | ||
| export function createStderrProgressHandler(): (p: ILiberateProgress) => void { | ||
| const isTTY = process.stderr.isTTY; | ||
| let lastLineLength = 0; | ||
| let inPlaceStarted = false; | ||
|
|
||
| return (p: ILiberateProgress): void => { | ||
| if (p.phase === 'triage') { | ||
| process.stderr.write(`Found ${p.total} high-interest commits to analyze.\n`); | ||
| } else if (p.phase === 'processing') { | ||
| const sha = p.sha.slice(0, 7); | ||
| const subject = p.subject.length > 60 ? p.subject.slice(0, 57) + '...' : p.subject; | ||
| const line = ` [${p.current}/${p.total}] ${sha} ${subject} (${p.factsExtracted} facts)`; | ||
|
|
||
| if (isTTY) { | ||
| const padded = line.padEnd(lastLineLength, ' '); | ||
| lastLineLength = line.length; | ||
| inPlaceStarted = true; | ||
| process.stderr.write(`\r${padded}`); | ||
| } else { | ||
| process.stderr.write(`${line}\n`); | ||
| } | ||
| } else if (p.phase === 'complete') { | ||
| if (isTTY && inPlaceStarted) { | ||
| process.stderr.write('\n'); | ||
| inPlaceStarted = false; | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Wrap a liberate call so that if it throws after writing in-place progress, | ||
| * a trailing newline is still written to stderr. | ||
| */ | ||
| export async function liberateWithProgress<T>( | ||
| fn: () => Promise<T>, | ||
| onProgress: ReturnType<typeof createStderrProgressHandler>, | ||
| ): Promise<T> { | ||
| try { | ||
| return await fn(); | ||
| } finally { | ||
| // Ensure terminal is clean if progress was mid-line when error occurred. | ||
| // The 'complete' phase handler already writes \n, but if liberate threw | ||
| // before emitting 'complete', we need a fallback. We rely on the handler's | ||
| // internal state via one final 'complete' call. | ||
| onProgress({ phase: 'complete', current: 0, total: 0, sha: '', subject: '', factsExtracted: 0 }); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { describe, it, before, after } from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
| import type { ILiberateProgress } from '../../../../src/application/interfaces/ILiberateService'; | ||
| import { execFileSync } from 'child_process'; | ||
| import { mkdtempSync, writeFileSync, rmSync } from 'fs'; | ||
| import { join } from 'path'; | ||
|
|
@@ -93,5 +94,52 @@ describe('LiberateService', () => { | |
| assert.equal(result.commitsAnnotated, 0); | ||
| assert.equal(result.factsExtracted, 0); | ||
| }); | ||
|
|
||
| it('should call onProgress with triage, processing, and complete phases', async () => { | ||
| const events: ILiberateProgress[] = []; | ||
|
|
||
| await service.liberate({ | ||
| cwd: repoDir, | ||
| dryRun: true, | ||
| threshold: 1, | ||
| onProgress: (p) => events.push({ ...p }), | ||
| }); | ||
|
|
||
| assert.ok(events.length >= 2, 'should emit at least triage + complete'); | ||
|
|
||
| // First event is triage | ||
| assert.equal(events[0].phase, 'triage'); | ||
| assert.equal(events[0].current, 0); | ||
| assert.ok(events[0].total >= 0); | ||
|
|
||
| // Last event is complete | ||
| const last = events[events.length - 1]; | ||
| assert.equal(last.phase, 'complete'); | ||
|
|
||
| // All middle events are processing | ||
| const processingEvents = events.filter(e => e.phase === 'processing'); | ||
| for (let i = 0; i < processingEvents.length; i++) { | ||
| assert.equal(processingEvents[i].current, i + 1, 'current should be 1-based'); | ||
| assert.ok(processingEvents[i].sha.length > 0, 'sha should be non-empty'); | ||
| assert.ok(processingEvents[i].subject.length > 0, 'subject should be non-empty'); | ||
| } | ||
| }); | ||
|
|
||
| it('should emit triage and complete even with zero high-interest commits', async () => { | ||
| const events: ILiberateProgress[] = []; | ||
|
|
||
| await service.liberate({ | ||
| cwd: repoDir, | ||
| dryRun: true, | ||
| threshold: 100, | ||
| onProgress: (p) => events.push({ ...p }), | ||
| }); | ||
|
|
||
| assert.equal(events.length, 2); | ||
| assert.equal(events[0].phase, 'triage'); | ||
| assert.equal(events[0].total, 0); | ||
| assert.equal(events[1].phase, 'complete'); | ||
| assert.equal(events[1].factsExtracted, 0); | ||
| }); | ||
|
Comment on lines
+98
to
+143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Unit-test isolation: consider mocking Git/FS dependencies. These new tests still exercise real git commands and filesystem via As per coding guidelines: "tests/unit/**/*.{ts,tsx}: Create unit tests with manually mocked dependencies (no testing framework) in 🤖 Prompt for AI Agents |
||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clarify
currentsemantics in progress docs.LiberateService emits
current = totalon the complete phase, but the comment says 0 for triage/complete. Align the doc (or the emitter) to avoid ambiguity for consumers.✏️ Suggested doc tweak
📝 Committable suggestion
🤖 Prompt for AI Agents