-
-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23f97ac
commit 5cf302d
Showing
7 changed files
with
190 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import createMockInstance from 'jest-create-mock-instance'; | ||
|
||
import { FakeCommand } from '../fixtures/fake-command'; | ||
import { Logger } from '../logger'; | ||
import { LoggerPadding } from './logger-padding'; | ||
|
||
let logger: jest.Mocked<Logger>; | ||
let controller: LoggerPadding; | ||
let commands: FakeCommand[]; | ||
|
||
beforeEach(() => { | ||
commands = [new FakeCommand(), new FakeCommand()]; | ||
logger = createMockInstance(Logger); | ||
controller = new LoggerPadding({ logger }); | ||
}); | ||
|
||
it('returns same commands', () => { | ||
expect(controller.handle(commands)).toMatchObject({ commands }); | ||
}); | ||
|
||
it('sets the prefix length on handle', () => { | ||
controller.handle(commands); | ||
expect(logger.setPrefixLength).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('updates the prefix length when commands emit a start timer', () => { | ||
controller.handle(commands); | ||
commands[0].timer.next({ startDate: new Date() }); | ||
expect(logger.setPrefixLength).toHaveBeenCalledTimes(2); | ||
|
||
commands[1].timer.next({ startDate: new Date() }); | ||
expect(logger.setPrefixLength).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('sets prefix length to the longest prefix of all commands', () => { | ||
logger.getPrefixContent | ||
.mockReturnValueOnce({ type: 'default', value: 'foobar' }) | ||
.mockReturnValueOnce({ type: 'default', value: 'baz' }); | ||
|
||
controller.handle(commands); | ||
expect(logger.setPrefixLength).toHaveBeenCalledWith(6); | ||
}); | ||
|
||
it('does not shorten the prefix length', () => { | ||
logger.getPrefixContent | ||
.mockReturnValueOnce({ type: 'default', value: '100' }) | ||
.mockReturnValueOnce({ type: 'default', value: '1' }); | ||
|
||
controller.handle(commands); | ||
commands[0].timer.next({ startDate: new Date() }); | ||
expect(logger.setPrefixLength).toHaveBeenCalledWith(3); | ||
|
||
commands[0].timer.next({ startDate: new Date() }); | ||
expect(logger.setPrefixLength).toHaveBeenCalledWith(3); | ||
}); | ||
|
||
it('unsubscribes from start timers on finish', () => { | ||
logger.getPrefixContent.mockReturnValue({ type: 'default', value: '1' }); | ||
|
||
const { onFinish } = controller.handle(commands); | ||
commands[0].timer.next({ startDate: new Date() }); | ||
expect(logger.setPrefixLength).toHaveBeenCalledTimes(2); | ||
|
||
onFinish(); | ||
commands[0].timer.next({ startDate: new Date() }); | ||
expect(logger.setPrefixLength).toHaveBeenCalledTimes(2); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Command } from '../command'; | ||
import { Logger } from '../logger'; | ||
import { FlowController } from './flow-controller'; | ||
|
||
export class LoggerPadding implements FlowController { | ||
private readonly logger: Logger; | ||
|
||
constructor({ logger }: { logger: Logger }) { | ||
this.logger = logger; | ||
} | ||
|
||
handle(commands: Command[]): { commands: Command[]; onFinish: () => void } { | ||
// Sometimes there's limited concurrency, so not all commands will spawn straight away. | ||
// Compute the prefix length now, which works for all styles but those with a PID. | ||
let length = commands.reduce((length, command) => { | ||
const content = this.logger.getPrefixContent(command); | ||
return Math.max(length, content?.value.length || 0); | ||
}, 0); | ||
this.logger.setPrefixLength(length); | ||
|
||
// The length of prefixes is somewhat stable, except for PIDs, which might change when a | ||
// process spawns (e.g. PIDs might look like 1, 10 or 100), therefore listen to command starts | ||
// and update the prefix length when this happens. | ||
const subs = commands.map((command) => | ||
command.timer.subscribe((event) => { | ||
if (!event.endDate) { | ||
const content = this.logger.getPrefixContent(command); | ||
length = Math.max(length, content?.value.length || 0); | ||
this.logger.setPrefixLength(length); | ||
} | ||
}), | ||
); | ||
|
||
return { | ||
commands, | ||
onFinish() { | ||
subs.forEach((sub) => sub.unsubscribe()); | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters