Skip to content

Commit

Permalink
feat: implement progress api (#159)
Browse files Browse the repository at this point in the history
* feat: implement progress api

* feat: localized progress message

* feat: retrievingTestRunSummary,queryingForAggregateCodeCoverage progress
  • Loading branch information
Xiaoyi Chen authored and rcoringrato-sfdc committed Mar 18, 2021
1 parent d4a9d72 commit b9a5460
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 7 deletions.
3 changes: 3 additions & 0 deletions packages/apex-node/src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export const messages = {
streamingTransportUp: 'Listening for streaming state changes...',
streamingTransportDown: 'Faye client generated a transport:down event.',
streamingProcessingTestRun: 'Processing test run %s',
retrievingTestRunSummary: 'Retrieving test run summary record',
queryingForAggregateCodeCoverage:
'Querying for aggregate code coverage results',
failRate: 'Fail Rate',
testsRan: 'Tests Ran',
orgId: 'Org Id',
Expand Down
1 change: 1 addition & 0 deletions packages/apex-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export {
export { LogService, ApexLogGetOptions, LogRecord, LogResult } from './logs';
export { JUnitReporter, TapReporter, HumanReporter } from './reporters';
export {
ApexTestProgressValue,
ApexTestResultData,
ApexTestResultOutcome,
AsyncTestArrayConfiguration,
Expand Down
21 changes: 21 additions & 0 deletions packages/apex-node/src/streaming/streamingClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,20 @@ export class StreamingClient {
});

this.client.on('transport:up', () => {
this.progress?.report({
type: 'StreamingClientProgress',
value: 'streamingTransportUp',
message: nls.localize('streamingTransportUp')
});
console.log(nls.localize('streamingTransportUp'));
});

this.client.on('transport:down', () => {
this.progress?.report({
type: 'StreamingClientProgress',
value: 'streamingTransportDown',
message: nls.localize('streamingTransportDown')
});
console.log(nls.localize('streamingTransportDown'));
});

Expand Down Expand Up @@ -167,6 +177,12 @@ export class StreamingClient {
return result;
}

this.progress?.report({
type: 'StreamingClientProgress',
value: 'streamingProcessingTestRun',
message: nls.localize('streamingProcessingTestRun', testRunId),
testRunId
});
console.log(nls.localize('streamingProcessingTestRun', testRunId));
return null;
}
Expand All @@ -183,6 +199,11 @@ export class StreamingClient {
throw new Error(nls.localize('noTestQueueResults', testRunId));
}

this.progress?.report({
type: 'TestQueueProgress',
value: result
});

for (let i = 0; i < result.records.length; i++) {
const item = result.records[i];
if (
Expand Down
1 change: 1 addition & 0 deletions packages/apex-node/src/tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

export { TestService } from './testService';
export {
ApexTestProgressValue,
AsyncTestConfiguration,
AsyncTestArrayConfiguration,
SyncTestConfiguration,
Expand Down
18 changes: 16 additions & 2 deletions packages/apex-node/src/tests/testService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ export class TestService {
asyncRunResult.queueItem,
asyncRunResult.runId,
getCurrentTime(),
codeCoverage
codeCoverage,
progress
);
}

Expand Down Expand Up @@ -439,7 +440,8 @@ export class TestService {
testQueueResult: ApexTestQueueItem,
testRunId: string,
commandStartTime: number,
codeCoverage = false
codeCoverage = false,
progress?: Progress<ApexTestProgressValue>
): Promise<TestResult> {
if (!this.isValidTestRunID(testRunId)) {
throw new Error(nls.localize('invalidTestRunIdErr', testRunId));
Expand All @@ -450,6 +452,13 @@ export class TestService {
testRunSummaryQuery +=
'MethodsEnqueued, StartTime, EndTime, TestTime, UserId ';
testRunSummaryQuery += `FROM ApexTestRunResult WHERE AsyncApexJobId = '${testRunId}'`;

progress?.report({
type: 'FormatTestResultProgress',
value: 'retrievingTestRunSummary',
message: nls.localize('retrievingTestRunSummary')
});

const testRunSummaryResults = (await this.connection.tooling.query(
testRunSummaryQuery
)) as ApexTestRunResult;
Expand Down Expand Up @@ -526,6 +535,11 @@ export class TestService {
}
});

progress?.report({
type: 'FormatTestResultProgress',
value: 'queryingForAggregateCodeCoverage',
message: nls.localize('queryingForAggregateCodeCoverage')
});
const {
codeCoverageResults,
totalLines,
Expand Down
22 changes: 21 additions & 1 deletion packages/apex-node/src/tests/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,24 @@ export type NamespaceQueryResult = {
records: NamespaceRecord[];
};

export type ApexTestProgressValue = ApexTestRunResultRecord;
export type ApexTestProgressValue =
| {
type: 'StreamingClientProgress';
value: 'streamingTransportUp' | 'streamingTransportDown';
message: string;
}
| {
type: 'StreamingClientProgress';
value: 'streamingProcessingTestRun';
testRunId: string;
message: string;
}
| {
type: 'TestQueueProgress';
value: ApexTestQueueItem;
}
| {
type: 'FormatTestResultProgress';
value: 'retrievingTestRunSummary' | 'queryingForAggregateCodeCoverage';
message: string;
};
118 changes: 116 additions & 2 deletions packages/apex-node/test/streaming/streamingClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@

import { AuthInfo, Connection } from '@salesforce/core';
import { MockTestOrgData, testSetup } from '@salesforce/core/lib/testSetup';
import { createSandbox, SinonSandbox } from 'sinon';
import { assert, createSandbox, SinonSandbox } from 'sinon';
import { StreamingClient } from '../../src/streaming';
import { expect } from 'chai';
import { Client as FayeClient, Subscription } from 'faye';
import { fail } from 'assert';
import { Progress } from '../../src';
import { TestResultMessage } from '../../src/streaming/types';
import { ApexTestQueueItemStatus } from '../../src/tests/types';
import {
ApexTestQueueItemStatus,
ApexTestProgressValue
} from '../../src/tests/types';
import { nls } from '../../src/i18n';
import { EventEmitter } from 'events';

const $$ = testSetup();
let mockConnection: Connection;
Expand Down Expand Up @@ -233,4 +238,113 @@ describe('Streaming API Client', () => {
expect(mockToolingQuery.calledOnce).to.equal(true);
expect(results).to.equal(null);
});

it('should report streamingTransportUp progress', () => {
const reportStub = sandboxStub.stub();
const progressReporter: Progress<ApexTestProgressValue> = {
report: reportStub
};
const mockFayeClient = new EventEmitter();
const stubOn = sandboxStub.stub(FayeClient.prototype, 'on');
stubOn.callsFake(mockFayeClient.on.bind(mockFayeClient));

new StreamingClient(mockConnection, progressReporter);
mockFayeClient.emit('transport:up');

assert.calledOnce(reportStub);
assert.calledWith(reportStub, {
type: 'StreamingClientProgress',
value: 'streamingTransportUp',
message: nls.localize('streamingTransportUp')
});
});

it('should report streamingTransportDown progress', () => {
const reportStub = sandboxStub.stub();
const progressReporter: Progress<ApexTestProgressValue> = {
report: reportStub
};
const mockFayeClient = new EventEmitter();
const stubOn = sandboxStub.stub(FayeClient.prototype, 'on');
stubOn.callsFake(mockFayeClient.on.bind(mockFayeClient));

new StreamingClient(mockConnection, progressReporter);
mockFayeClient.emit('transport:down');

assert.calledOnce(reportStub);
assert.calledWith(reportStub, {
type: 'StreamingClientProgress',
value: 'streamingTransportDown',
message: nls.localize('streamingTransportDown')
});
});

it('should report streamingProcessingTestRun progress', async () => {
const mockToolingQuery = sandboxStub.stub(mockConnection.tooling, 'query');
mockToolingQuery.resolves({
done: true,
totalSize: 0,
records: [
{
Id: '707xx0000AGQ3jbQQD',
Status: ApexTestQueueItemStatus.Processing,
ApexClassId: '01pxx00000O6tXZQAx',
TestRunResultId: '05mxx000000TgYuQAw'
}
]
});
const reportStub = sandboxStub.stub();
const progressReporter: Progress<ApexTestProgressValue> = {
report: reportStub
};

const streamClient = new StreamingClient(mockConnection, progressReporter);
await streamClient.handler(testResultMsg);

assert.calledWith(reportStub, {
type: 'StreamingClientProgress',
value: 'streamingProcessingTestRun',
message: nls.localize('streamingProcessingTestRun', '707xx0000AGQ3jbQQD'),
testRunId: '707xx0000AGQ3jbQQD'
});
});

it('should report test queue progress', async () => {
const mockToolingQuery = sandboxStub.stub(mockConnection.tooling, 'query');
mockToolingQuery.resolves({
done: true,
totalSize: 0,
records: [
{
Id: '707xx0000AGQ3jbQQD',
Status: ApexTestQueueItemStatus.Processing,
ApexClassId: '01pxx00000O6tXZQAx',
TestRunResultId: '05mxx000000TgYuQAw'
}
]
});
const reportStub = sandboxStub.stub();
const progressReporter: Progress<ApexTestProgressValue> = {
report: reportStub
};

const streamClient = new StreamingClient(mockConnection, progressReporter);
await streamClient.handler(testResultMsg);

assert.calledWith(reportStub, {
type: 'TestQueueProgress',
value: {
done: true,
totalSize: 0,
records: [
{
Id: '707xx0000AGQ3jbQQD',
Status: ApexTestQueueItemStatus.Processing,
ApexClassId: '01pxx00000O6tXZQAx',
TestRunResultId: '05mxx000000TgYuQAw'
}
]
}
});
});
});
Loading

0 comments on commit b9a5460

Please sign in to comment.