-
Notifications
You must be signed in to change notification settings - Fork 0
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
6360714
commit 95f42d6
Showing
7 changed files
with
164 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import {Command} from '@oclif/core' | ||
import path from "node:path"; | ||
|
||
import {GetConfigurationUseCase, StopCurrentTimeEntryUseCase} from "../application/cases"; | ||
import {ConfigurationValidator, configFilename} from "../core"; | ||
import {FileSystemDataSource, TogglApi, http} from "../infrastructure/data-sources"; | ||
import {ConfigurationRepositoryImplementation, TimeEntryRepositoryImplementation} from "../infrastructure/repositories"; | ||
|
||
export default class Stop extends Command { | ||
static description = 'Stop running time entry.' | ||
|
||
static examples = [ | ||
'<%= config.bin %> <%= command.id %>', | ||
] | ||
|
||
public async run(): Promise<void> { | ||
const configurationRepository = new ConfigurationRepositoryImplementation(new FileSystemDataSource(path.join(this.config.configDir, configFilename))) | ||
const config = await new GetConfigurationUseCase(configurationRepository).exec() | ||
const configValidation = ConfigurationValidator.isRequiredConfigAvailable(config) | ||
if (configValidation.error) { | ||
this.error(configValidation.message) | ||
} | ||
|
||
const togglAPI = new TogglApi({ | ||
http, | ||
token: config.apiToken, | ||
workspaceId: config.workspaceId | ||
}) | ||
const timeEntryRepository = new TimeEntryRepositoryImplementation(togglAPI) | ||
const useCase = new StopCurrentTimeEntryUseCase(timeEntryRepository) | ||
|
||
try { | ||
const updatedEntry = await useCase.exec() | ||
if (updatedEntry) { | ||
this.log(`Time entry "${updatedEntry.description}" stopped for "${updatedEntry.project.name}" project.`) | ||
} else { | ||
this.log("There is no time entry running.") | ||
} | ||
} catch (error) { | ||
this.error(`Unexpected error: ${error}`) | ||
} | ||
} | ||
} |
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,59 @@ | ||
/* eslint-disable camelcase */ | ||
import {Config, ux} from '@oclif/core' | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import path from "node:path"; | ||
import {SinonSandbox, SinonStub, createSandbox} from 'sinon' | ||
|
||
import {TogglApi} from "../../dist/infrastructure/data-sources"; // here uses dist directory because here we are testing the command, which uses transpiled code. | ||
import {buildTogglProject, buildTogglTimeEntry} from "../builders"; | ||
import {configuration} from "../fixtures"; | ||
import httpMock from "../http.mock"; | ||
|
||
chai.use(chaiAsPromised); | ||
const {expect} = chai; | ||
|
||
describe('stop command runs', () => { | ||
const projects = [buildTogglProject({name: 'Evil Company'}), buildTogglProject({name: 'Good Company'})] | ||
let sandbox: SinonSandbox | ||
let config: Config | ||
let stdoutStub: SinonStub | ||
|
||
beforeEach(async () => { | ||
sandbox = createSandbox() | ||
stdoutStub = sandbox.stub(ux.write, 'stdout') | ||
config = await Config.load({root: process.cwd()}) | ||
config.configDir = path.join(process.cwd(), 'test/fixtures') | ||
}) | ||
|
||
afterEach(async () => { | ||
sandbox.restore() | ||
httpMock.reset() | ||
}) | ||
|
||
it('stopping current time entry', async () => { | ||
const [evilProject] = projects | ||
const {id, name} = evilProject | ||
const entry = buildTogglTimeEntry({project_id: id}) | ||
httpMock.onGet(`${TogglApi.baseUrl}/api/v9/me/time_entries/current`) | ||
.reply(200, entry) | ||
.onGet(`${TogglApi.baseUrl}/api/v9/workspaces/${configuration.workspaceId}/projects/${id}`) | ||
.reply(200, evilProject) | ||
.onPut(`${TogglApi.baseUrl}/api/v9/workspaces/${configuration.workspaceId}/time_entries/${entry.id}`) | ||
.reply(200, entry) | ||
|
||
await config.runCommand("stop") | ||
|
||
expect(stdoutStub.args.flat().join(',')).to.contains(`Time entry "${entry.description}" stopped for "${name}" project.`) | ||
}) | ||
|
||
it('showing there is no entry to stop if there is no current time entry', async () => { | ||
httpMock.onGet(`${TogglApi.baseUrl}/api/v9/me/time_entries/current`) | ||
.reply(200, null) | ||
|
||
await config.runCommand("stop") | ||
|
||
expect(stdoutStub.args.flat().join(',')).to.contains(`There is no time entry running.`) | ||
}) | ||
|
||
}) |
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,5 @@ | ||
import MockAdapter from "axios-mock-adapter"; | ||
|
||
import {http} from "../dist/infrastructure/data-sources/http"; | ||
|
||
export default new MockAdapter(http); |
Oops, something went wrong.