|
| 1 | +import { expect, use } from 'chai' |
| 2 | +import sinonChai from 'sinon-chai' |
| 3 | +import sinon from 'sinon' |
| 4 | +import proxyquire from 'proxyquire' |
| 5 | +import pDefer, { DeferredPromise } from 'p-defer' |
| 6 | + |
| 7 | +import { SimpleGit } from 'simple-git' |
| 8 | +import type { GitDataSource, GitDataSourceConfig } from '../../../src/sources/GitDataSource' |
| 9 | +import Chokidar from 'chokidar' |
| 10 | + |
| 11 | +use(sinonChai) |
| 12 | + |
| 13 | +type P<F extends keyof SimpleGit> = Parameters<SimpleGit[F]> |
| 14 | +type R<F extends keyof SimpleGit> = ReturnType<SimpleGit[F]> |
| 15 | + |
| 16 | +interface GitDataSourceConstructor { |
| 17 | + new (config: GitDataSourceConfig): GitDataSource |
| 18 | +} |
| 19 | + |
| 20 | +type GDSImport = { |
| 21 | + GitDataSource: GitDataSourceConstructor |
| 22 | +} |
| 23 | + |
| 24 | +describe('GitDataSource', () => { |
| 25 | + let stubbedSimpleGit: { |
| 26 | + // Parameters<> only gets the last overload defined, which is |
| 27 | + // supposed to be the most permissive. However, SimpleGit defines |
| 28 | + // overloads in the opposite order, and we need the one that takes |
| 29 | + // a string. |
| 30 | + revparse: sinon.SinonStub<[option: string], R<'revparse'>> |
| 31 | + branch: sinon.SinonStub<P<'branch'>, R<'branch'>> |
| 32 | + status: sinon.SinonStub<P<'status'>, R<'status'>> |
| 33 | + log: sinon.SinonStub<P<'log'>, R<'log'>> |
| 34 | + } |
| 35 | + let stubbedWatchInstance: sinon.SinonStubbedInstance<Chokidar.FSWatcher> |
| 36 | + |
| 37 | + let gitDataSourceImport: GDSImport |
| 38 | + let fakeTimers: sinon.SinonFakeTimers |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + fakeTimers = sinon.useFakeTimers() |
| 42 | + stubbedSimpleGit = { |
| 43 | + revparse: sinon.stub<[option: string], R<'revparse'>>(), |
| 44 | + branch: sinon.stub<P<'branch'>, R<'branch'>>(), |
| 45 | + status: sinon.stub<P<'status'>, R<'status'>>(), |
| 46 | + log: sinon.stub<P<'log'>, R<'log'>>(), |
| 47 | + } |
| 48 | + |
| 49 | + stubbedWatchInstance = sinon.createStubInstance(Chokidar.FSWatcher) |
| 50 | + sinon.stub(Chokidar, 'watch').returns(stubbedWatchInstance) |
| 51 | + |
| 52 | + gitDataSourceImport = proxyquire.noCallThru()('../../../src/sources/GitDataSource', { |
| 53 | + 'simple-git' () { |
| 54 | + return stubbedSimpleGit |
| 55 | + }, |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + afterEach(() => { |
| 60 | + sinon.restore() |
| 61 | + fakeTimers.restore() |
| 62 | + }) |
| 63 | + |
| 64 | + describe('Unit', () => { |
| 65 | + describe('in open mode', () => { |
| 66 | + let gds: GitDataSource |
| 67 | + let projectRoot: string |
| 68 | + let branchName: string |
| 69 | + let onBranchChange: sinon.SinonStub<[branch: string | null], void> |
| 70 | + let onGitInfoChange: sinon.SinonStub<[specPath: string[]], void> |
| 71 | + let onError: sinon.SinonStub<[err: any], void> |
| 72 | + let onGitLogChange: sinon.SinonStub<[shas: string[]], void> |
| 73 | + const firstHashes = [ |
| 74 | + { hash: 'abc' }, |
| 75 | + ] |
| 76 | + const firstHashesReturnValue = ['abc'] |
| 77 | + const secondHashes = [...firstHashes, { hash: 'efg' }] |
| 78 | + const secondHashesReturnValue = [...firstHashesReturnValue, 'efg'] |
| 79 | + let firstGitLogCall: DeferredPromise<void> |
| 80 | + let secondGitLogCall: DeferredPromise<void> |
| 81 | + |
| 82 | + beforeEach(async () => { |
| 83 | + firstGitLogCall = pDefer() |
| 84 | + secondGitLogCall = pDefer() |
| 85 | + branchName = 'main' |
| 86 | + onBranchChange = sinon.stub() |
| 87 | + onGitInfoChange = sinon.stub() |
| 88 | + onError = sinon.stub() |
| 89 | + onGitLogChange = sinon.stub() |
| 90 | + |
| 91 | + projectRoot = '/root' |
| 92 | + |
| 93 | + // @ts-ignore |
| 94 | + stubbedSimpleGit.log.onFirstCall() |
| 95 | + // @ts-expect-error |
| 96 | + .callsFake(() => { |
| 97 | + firstGitLogCall.resolve() |
| 98 | + |
| 99 | + return { all: firstHashes } |
| 100 | + }) |
| 101 | + .onSecondCall() |
| 102 | + // @ts-expect-error |
| 103 | + .callsFake(() => { |
| 104 | + secondGitLogCall.resolve() |
| 105 | + |
| 106 | + return { all: secondHashes } |
| 107 | + }) |
| 108 | + |
| 109 | + // #verifyGitRepo |
| 110 | + |
| 111 | + // constructor verifies the repo in open mode via #refreshAllGitData, but does not wait for it :womp: |
| 112 | + const revparseP = pDefer<void>() |
| 113 | + |
| 114 | + // SimpleGit returns a chainable, but we only care about the promise |
| 115 | + // @ts-expect-error |
| 116 | + stubbedSimpleGit.revparse.callsFake(() => { |
| 117 | + revparseP.resolve() |
| 118 | + |
| 119 | + return Promise.resolve(projectRoot) |
| 120 | + }) |
| 121 | + |
| 122 | + // wait for revparse to be called, so we can be assured that GitDataSource has initialized |
| 123 | + // up to this point |
| 124 | + |
| 125 | + // #loadAndWatchCurrentBranch |
| 126 | + |
| 127 | + // next in initialization, it loads the current branch |
| 128 | + const branchP = pDefer<void>() |
| 129 | + |
| 130 | + // again, ignoring type warning re: chaining |
| 131 | + // @ts-expect-error |
| 132 | + stubbedSimpleGit.branch.callsFake(() => { |
| 133 | + branchP.resolve() |
| 134 | + |
| 135 | + return Promise.resolve({ current: branchName }) |
| 136 | + }) |
| 137 | + |
| 138 | + const onBranchChangeP = pDefer<void>() |
| 139 | + |
| 140 | + onBranchChange.callsFake(() => onBranchChangeP.resolve()) |
| 141 | + |
| 142 | + gds = new gitDataSourceImport.GitDataSource({ |
| 143 | + isRunMode: false, |
| 144 | + projectRoot, |
| 145 | + onBranchChange, |
| 146 | + onGitInfoChange, |
| 147 | + onError, |
| 148 | + onGitLogChange, |
| 149 | + }) |
| 150 | + |
| 151 | + await revparseP.promise |
| 152 | + await branchP.promise |
| 153 | + await onBranchChangeP.promise |
| 154 | + expect(onBranchChange).to.be.calledWith(branchName) |
| 155 | + }) |
| 156 | + |
| 157 | + describe('.get currentHashes', () => { |
| 158 | + describe('after first load', () => { |
| 159 | + beforeEach(async () => { |
| 160 | + await firstGitLogCall.promise |
| 161 | + }) |
| 162 | + |
| 163 | + it('returns the current hashes', () => { |
| 164 | + expect(gds.currentHashes).to.have.same.members(firstHashesReturnValue) |
| 165 | + }) |
| 166 | + }) |
| 167 | + |
| 168 | + describe('after sixty seconds, when there are additional hashes', () => { |
| 169 | + it('returns the current hashes', async () => { |
| 170 | + await fakeTimers.tickAsync(60001) |
| 171 | + await secondGitLogCall.promise |
| 172 | + expect(gds.currentHashes).to.have.same.members(secondHashesReturnValue) |
| 173 | + }) |
| 174 | + }) |
| 175 | + }) |
| 176 | + }) |
| 177 | + }) |
| 178 | +}) |
0 commit comments