| 
 | 1 | +import {Issue} from '../src/classes/issue';  | 
 | 2 | +import {IIssuesProcessorOptions} from '../src/interfaces/issues-processor-options';  | 
 | 3 | +import {IsoDateString} from '../src/types/iso-date-string';  | 
 | 4 | +import {IssuesProcessorMock} from './classes/issues-processor-mock';  | 
 | 5 | +import {DefaultProcessorOptions} from './constants/default-processor-options';  | 
 | 6 | +import {generateIssue} from './functions/generate-issue';  | 
 | 7 | + | 
 | 8 | +describe('operations per run option', (): void => {  | 
 | 9 | +  let sut: SUT;  | 
 | 10 | + | 
 | 11 | +  beforeEach((): void => {  | 
 | 12 | +    sut = new SUT();  | 
 | 13 | +  });  | 
 | 14 | + | 
 | 15 | +  describe('when one issue should be stale within 10 days and updated 20 days ago', (): void => {  | 
 | 16 | +    beforeEach((): void => {  | 
 | 17 | +      sut.staleIn(10).newIssue().updated(20);  | 
 | 18 | +    });  | 
 | 19 | + | 
 | 20 | +    describe('when the operations per run option is set to 1', (): void => {  | 
 | 21 | +      beforeEach((): void => {  | 
 | 22 | +        sut.operationsPerRun(1);  | 
 | 23 | +      });  | 
 | 24 | + | 
 | 25 | +      it('should consume 1 operation (stale label)', async () => {  | 
 | 26 | +        expect.assertions(2);  | 
 | 27 | + | 
 | 28 | +        await sut.test();  | 
 | 29 | + | 
 | 30 | +        expect(sut.processor.staleIssues).toHaveLength(1);  | 
 | 31 | +        expect(  | 
 | 32 | +          sut.processor.operations.getConsumedOperationsCount()  | 
 | 33 | +        ).toStrictEqual(1);  | 
 | 34 | +      });  | 
 | 35 | +    });  | 
 | 36 | +  });  | 
 | 37 | + | 
 | 38 | +  describe('when one issue should be stale within 10 days and updated 20 days ago and a comment should be added when stale', (): void => {  | 
 | 39 | +    beforeEach((): void => {  | 
 | 40 | +      sut.staleIn(10).commentOnStale().newIssue().updated(20);  | 
 | 41 | +    });  | 
 | 42 | + | 
 | 43 | +    describe('when the operations per run option is set to 2', (): void => {  | 
 | 44 | +      beforeEach((): void => {  | 
 | 45 | +        sut.operationsPerRun(2);  | 
 | 46 | +      });  | 
 | 47 | + | 
 | 48 | +      it('should consume 2 operations (stale label, comment)', async () => {  | 
 | 49 | +        expect.assertions(2);  | 
 | 50 | + | 
 | 51 | +        await sut.test();  | 
 | 52 | + | 
 | 53 | +        expect(sut.processor.staleIssues).toHaveLength(1);  | 
 | 54 | +        expect(  | 
 | 55 | +          sut.processor.operations.getConsumedOperationsCount()  | 
 | 56 | +        ).toStrictEqual(2);  | 
 | 57 | +      });  | 
 | 58 | +    });  | 
 | 59 | + | 
 | 60 | +    // Special case were we continue the issue processing even if the operations per run is reached  | 
 | 61 | +    describe('when the operations per run option is set to 1', (): void => {  | 
 | 62 | +      beforeEach((): void => {  | 
 | 63 | +        sut.operationsPerRun(1);  | 
 | 64 | +      });  | 
 | 65 | + | 
 | 66 | +      it('should consume 2 operations (stale label, comment)', async () => {  | 
 | 67 | +        expect.assertions(2);  | 
 | 68 | + | 
 | 69 | +        await sut.test();  | 
 | 70 | + | 
 | 71 | +        expect(sut.processor.staleIssues).toHaveLength(1);  | 
 | 72 | +        expect(  | 
 | 73 | +          sut.processor.operations.getConsumedOperationsCount()  | 
 | 74 | +        ).toStrictEqual(2);  | 
 | 75 | +      });  | 
 | 76 | +    });  | 
 | 77 | +  });  | 
 | 78 | + | 
 | 79 | +  describe('when two issues should be stale within 10 days and updated 20 days ago and a comment should be added when stale', (): void => {  | 
 | 80 | +    beforeEach((): void => {  | 
 | 81 | +      sut.staleIn(10).commentOnStale();  | 
 | 82 | +      sut.newIssue().updated(20);  | 
 | 83 | +      sut.newIssue().updated(20);  | 
 | 84 | +    });  | 
 | 85 | + | 
 | 86 | +    describe('when the operations per run option is set to 3', (): void => {  | 
 | 87 | +      beforeEach((): void => {  | 
 | 88 | +        sut.operationsPerRun(3);  | 
 | 89 | +      });  | 
 | 90 | + | 
 | 91 | +      it('should consume 4 operations (stale label, comment)', async () => {  | 
 | 92 | +        expect.assertions(2);  | 
 | 93 | + | 
 | 94 | +        await sut.test();  | 
 | 95 | + | 
 | 96 | +        expect(sut.processor.staleIssues).toHaveLength(2);  | 
 | 97 | +        expect(  | 
 | 98 | +          sut.processor.operations.getConsumedOperationsCount()  | 
 | 99 | +        ).toStrictEqual(4);  | 
 | 100 | +      });  | 
 | 101 | +    });  | 
 | 102 | + | 
 | 103 | +    describe('when the operations per run option is set to 2', (): void => {  | 
 | 104 | +      beforeEach((): void => {  | 
 | 105 | +        sut.operationsPerRun(2);  | 
 | 106 | +      });  | 
 | 107 | + | 
 | 108 | +      it('should consume 2 operations (stale label, comment) and stop', async () => {  | 
 | 109 | +        expect.assertions(2);  | 
 | 110 | + | 
 | 111 | +        await sut.test();  | 
 | 112 | + | 
 | 113 | +        expect(sut.processor.staleIssues).toHaveLength(1);  | 
 | 114 | +        expect(  | 
 | 115 | +          sut.processor.operations.getConsumedOperationsCount()  | 
 | 116 | +        ).toStrictEqual(2);  | 
 | 117 | +      });  | 
 | 118 | +    });  | 
 | 119 | + | 
 | 120 | +    // Special case were we continue the issue processing even if the operations per run is reached  | 
 | 121 | +    describe('when the operations per run option is set to 1', (): void => {  | 
 | 122 | +      beforeEach((): void => {  | 
 | 123 | +        sut.operationsPerRun(1);  | 
 | 124 | +      });  | 
 | 125 | + | 
 | 126 | +      it('should consume 2 operations (stale label, comment) and stop', async () => {  | 
 | 127 | +        expect.assertions(2);  | 
 | 128 | + | 
 | 129 | +        await sut.test();  | 
 | 130 | + | 
 | 131 | +        expect(sut.processor.staleIssues).toHaveLength(1);  | 
 | 132 | +        expect(  | 
 | 133 | +          sut.processor.operations.getConsumedOperationsCount()  | 
 | 134 | +        ).toStrictEqual(2);  | 
 | 135 | +      });  | 
 | 136 | +    });  | 
 | 137 | +  });  | 
 | 138 | +});  | 
 | 139 | + | 
 | 140 | +class SUT {  | 
 | 141 | +  processor!: IssuesProcessorMock;  | 
 | 142 | +  private _opts: IIssuesProcessorOptions = {  | 
 | 143 | +    ...DefaultProcessorOptions,  | 
 | 144 | +    staleIssueMessage: ''  | 
 | 145 | +  };  | 
 | 146 | +  private _testIssueList: Issue[] = [];  | 
 | 147 | +  private _sutIssues: SUTIssue[] = [];  | 
 | 148 | + | 
 | 149 | +  newIssue(): SUTIssue {  | 
 | 150 | +    const sutIssue: SUTIssue = new SUTIssue();  | 
 | 151 | +    this._sutIssues.push(sutIssue);  | 
 | 152 | + | 
 | 153 | +    return sutIssue;  | 
 | 154 | +  }  | 
 | 155 | + | 
 | 156 | +  staleIn(days: number): SUT {  | 
 | 157 | +    this._updateOptions({  | 
 | 158 | +      daysBeforeIssueStale: days  | 
 | 159 | +    });  | 
 | 160 | + | 
 | 161 | +    return this;  | 
 | 162 | +  }  | 
 | 163 | + | 
 | 164 | +  commentOnStale(): SUT {  | 
 | 165 | +    this._updateOptions({  | 
 | 166 | +      staleIssueMessage: 'Dummy stale issue message'  | 
 | 167 | +    });  | 
 | 168 | + | 
 | 169 | +    return this;  | 
 | 170 | +  }  | 
 | 171 | + | 
 | 172 | +  operationsPerRun(count: number): SUT {  | 
 | 173 | +    this._updateOptions({  | 
 | 174 | +      operationsPerRun: count  | 
 | 175 | +    });  | 
 | 176 | + | 
 | 177 | +    return this;  | 
 | 178 | +  }  | 
 | 179 | + | 
 | 180 | +  async test(): Promise<number> {  | 
 | 181 | +    return this._setTestIssueList()._setProcessor();  | 
 | 182 | +  }  | 
 | 183 | + | 
 | 184 | +  private _updateOptions(opts: Partial<IIssuesProcessorOptions>): SUT {  | 
 | 185 | +    this._opts = {...this._opts, ...opts};  | 
 | 186 | + | 
 | 187 | +    return this;  | 
 | 188 | +  }  | 
 | 189 | + | 
 | 190 | +  private _setTestIssueList(): SUT {  | 
 | 191 | +    this._testIssueList = this._sutIssues.map(  | 
 | 192 | +      (sutIssue: SUTIssue): Issue => {  | 
 | 193 | +        return generateIssue(  | 
 | 194 | +          this._opts,  | 
 | 195 | +          1,  | 
 | 196 | +          'My first issue',  | 
 | 197 | +          sutIssue.updatedAt,  | 
 | 198 | +          sutIssue.updatedAt,  | 
 | 199 | +          false  | 
 | 200 | +        );  | 
 | 201 | +      }  | 
 | 202 | +    );  | 
 | 203 | + | 
 | 204 | +    return this;  | 
 | 205 | +  }  | 
 | 206 | + | 
 | 207 | +  private async _setProcessor(): Promise<number> {  | 
 | 208 | +    this.processor = new IssuesProcessorMock(  | 
 | 209 | +      this._opts,  | 
 | 210 | +      async () => 'abot',  | 
 | 211 | +      async p => (p === 1 ? this._testIssueList : []),  | 
 | 212 | +      async () => [],  | 
 | 213 | +      async () => new Date().toDateString()  | 
 | 214 | +    );  | 
 | 215 | + | 
 | 216 | +    return this.processor.processIssues(1);  | 
 | 217 | +  }  | 
 | 218 | +}  | 
 | 219 | + | 
 | 220 | +class SUTIssue {  | 
 | 221 | +  updatedAt: IsoDateString = '2020-01-01T17:00:00Z';  | 
 | 222 | + | 
 | 223 | +  updated(daysAgo: number): SUTIssue {  | 
 | 224 | +    const today = new Date();  | 
 | 225 | +    today.setDate(today.getDate() - daysAgo);  | 
 | 226 | +    this.updatedAt = today.toISOString();  | 
 | 227 | + | 
 | 228 | +    return this;  | 
 | 229 | +  }  | 
 | 230 | +}  | 
0 commit comments