| /** | |
| * @license | |
| * Copyright 2021 Google LLC | |
| * SPDX-License-Identifier: Apache-2.0 | |
| */ | |
| import * as sinon from 'sinon'; | |
| import '../../test/common-test-setup'; | |
| import './checks-model'; | |
| import { | |
| CheckResult, | |
| ChecksModel, | |
| ChecksPatchset, | |
| ChecksProviderState, | |
| collectRunResults, | |
| RunResult, | |
| } from './checks-model'; | |
| import { | |
| Action, | |
| Category, | |
| ChangeData, | |
| CheckRun, | |
| ChecksApiConfig, | |
| ChecksProvider, | |
| ResponseCode, | |
| RunStatus, | |
| } from '../../api/checks'; | |
| import {getAppContext} from '../../services/app-context'; | |
| import { | |
| createCheckResult, | |
| createParsedChange, | |
| createRevisions, | |
| createRun, | |
| getCurrentRevision, | |
| } from '../../test/test-data-generators'; | |
| import {waitEventLoop, waitUntil, waitUntilCalled} from '../../test/test-utils'; | |
| import {ParsedChangeInfo} from '../../types/types'; | |
| import { | |
| changeModelToken, | |
| updateRevisionsWithCommitShas, | |
| } from '../change/change-model'; | |
| import {assert} from '@open-wc/testing'; | |
| import {testResolver} from '../../test/common-test-setup'; | |
| import {changeViewModelToken} from '../views/change'; | |
| import { | |
| NumericChangeId, | |
| PatchSetNumber, | |
| RevisionPatchSetNum, | |
| } from '../../api/rest-api'; | |
| import {pluginLoaderToken} from '../../elements/shared/gr-js-api-interface/gr-plugin-loader'; | |
| import {deepEqual} from '../../utils/deep-util'; | |
| const PLUGIN_NAME = 'test-plugin'; | |
| const RUNS: CheckRun[] = [ | |
| { | |
| checkName: 'MacCheck', | |
| change: 123, | |
| patchset: 1, | |
| attempt: 1, | |
| status: RunStatus.COMPLETED, | |
| results: [ | |
| { | |
| externalId: 'id-314', | |
| category: Category.WARNING, | |
| summary: 'Meddle cheddle check and you are weg.', | |
| }, | |
| ], | |
| }, | |
| ]; | |
| const CONFIG_POLLING_5S: ChecksApiConfig = { | |
| fetchPollingIntervalSeconds: 5, | |
| }; | |
| const CONFIG_POLLING_NONE: ChecksApiConfig = { | |
| fetchPollingIntervalSeconds: 0, | |
| }; | |
| function createProvider(): ChecksProvider { | |
| return { | |
| fetch: () => | |
| Promise.resolve({ | |
| responseCode: ResponseCode.OK, | |
| runs: [], | |
| }), | |
| }; | |
| } | |
| /** | |
| * A provider that echoes back the patchset it was asked to fetch, so that tests | |
| * can assert which patchset a tab is populated with. | |
| */ | |
| function createPatchsetTaggingProvider(): ChecksProvider { | |
| return { | |
| fetch: (data: ChangeData) => | |
| Promise.resolve({ | |
| responseCode: ResponseCode.OK, | |
| runs: [createRun({patchset: data.patchsetNumber})], | |
| }), | |
| }; | |
| } | |
| /** A change with two patchsets, so latest (2) and older (1) are distinct. */ | |
| function createTwoPatchsetChange(): ParsedChangeInfo { | |
| return updateRevisionsWithCommitShas({ | |
| ...createParsedChange(), | |
| revisions: createRevisions(2), | |
| current_revision: getCurrentRevision(1), | |
| })!; | |
| } | |
| suite('checks-model tests', () => { | |
| let model: ChecksModel; | |
| let current: ChecksProviderState; | |
| setup(() => { | |
| model = new ChecksModel( | |
| testResolver(changeViewModelToken), | |
| testResolver(changeModelToken), | |
| getAppContext().reportingService, | |
| testResolver(pluginLoaderToken).pluginsModel | |
| ); | |
| model.checksLatest$.subscribe(c => (current = c[PLUGIN_NAME])); | |
| }); | |
| teardown(() => { | |
| model.finalize(); | |
| }); | |
| test('register and fetch', async () => { | |
| const clock = sinon.useFakeTimers({shouldClearNativeTimers: true}); | |
| let change: ParsedChangeInfo | undefined = undefined; | |
| testResolver(changeModelToken).change$.subscribe(c => (change = c)); | |
| const provider = createProvider(); | |
| const fetchSpy = sinon.spy(provider, 'fetch'); | |
| model.register({ | |
| pluginName: 'test-plugin', | |
| provider, | |
| config: CONFIG_POLLING_NONE, | |
| }); | |
| await waitUntil(() => change === undefined); | |
| const testChange = updateRevisionsWithCommitShas(createParsedChange()); | |
| testResolver(changeModelToken).updateStateChange(testChange); | |
| await waitUntil(() => deepEqual(change, testChange)); | |
| // Advance time by 600ms to trigger the throttled fetch | |
| clock.tick(600); | |
| await waitUntilCalled(fetchSpy, 'fetch'); | |
| assert.equal( | |
| model.latestPatchNum, | |
| testChange!.revisions[testChange!.current_revision] | |
| ._number as PatchSetNumber | |
| ); | |
| assert.equal(model.changeNum, testChange!._number); | |
| clock.restore(); | |
| }); | |
| test('no duplicate fetch when viewing latest patchset (no selection)', async () => { | |
| const clock = sinon.useFakeTimers({shouldClearNativeTimers: true}); | |
| let change: ParsedChangeInfo | undefined = undefined; | |
| testResolver(changeModelToken).change$.subscribe(c => (change = c)); | |
| let latestRuns: CheckRun[] = []; | |
| let selectedRuns: CheckRun[] = []; | |
| model.allRunsLatestPatchset$.subscribe(r => (latestRuns = r)); | |
| model.allRunsSelectedPatchset$.subscribe(r => (selectedRuns = r)); | |
| const provider = createPatchsetTaggingProvider(); | |
| const fetchSpy = sinon.spy(provider, 'fetch'); | |
| model.register({ | |
| pluginName: PLUGIN_NAME, | |
| provider, | |
| config: CONFIG_POLLING_NONE, | |
| }); | |
| await waitUntil(() => change === undefined); | |
| // Viewing the latest patchset (2), no explicit checks patchset override. | |
| testResolver(changeViewModelToken).updateState({ | |
| patchNum: 2 as RevisionPatchSetNum, | |
| }); | |
| const testChange = createTwoPatchsetChange(); | |
| testResolver(changeModelToken).updateStateChange(testChange); | |
| await waitUntil(() => deepEqual(change, testChange)); | |
| // Fire the throttled emission, then flush the fetch promise into state. | |
| clock.tick(600); | |
| await waitEventLoop(); | |
| // The SELECTED patchset equals LATEST, so only a single fetch is needed. | |
| assert.equal(fetchSpy.callCount, 1); | |
| // Both tabs show data for the latest patchset (2). | |
| assert.isNotEmpty(latestRuns); | |
| assert.isNotEmpty(selectedRuns); | |
| assert.isTrue(latestRuns.every(r => r.patchset === 2)); | |
| assert.isTrue(selectedRuns.every(r => r.patchset === 2)); | |
| clock.restore(); | |
| }); | |
| test('no duplicate fetch when latest patchset is explicitly selected', async () => { | |
| const clock = sinon.useFakeTimers({shouldClearNativeTimers: true}); | |
| let change: ParsedChangeInfo | undefined = undefined; | |
| testResolver(changeModelToken).change$.subscribe(c => (change = c)); | |
| let latestRuns: CheckRun[] = []; | |
| let selectedRuns: CheckRun[] = []; | |
| model.allRunsLatestPatchset$.subscribe(r => (latestRuns = r)); | |
| model.allRunsSelectedPatchset$.subscribe(r => (selectedRuns = r)); | |
| const provider = createPatchsetTaggingProvider(); | |
| const fetchSpy = sinon.spy(provider, 'fetch'); | |
| model.register({ | |
| pluginName: PLUGIN_NAME, | |
| provider, | |
| config: CONFIG_POLLING_NONE, | |
| }); | |
| await waitUntil(() => change === undefined); | |
| // Viewing patchset 1 but explicitly selecting the latest patchset (2) in the | |
| // checks tab. checksPatchset differs from patchNum, so it is not reset. | |
| testResolver(changeViewModelToken).updateState({ | |
| patchNum: 1 as RevisionPatchSetNum, | |
| checksPatchset: 2 as PatchSetNumber, | |
| }); | |
| const testChange = createTwoPatchsetChange(); | |
| testResolver(changeModelToken).updateStateChange(testChange); | |
| await waitUntil(() => deepEqual(change, testChange)); | |
| // Fire the throttled emission, then flush the fetch promise into state. | |
| clock.tick(600); | |
| await waitEventLoop(); | |
| // Selected patchset (2) equals latest, so still only a single fetch. | |
| assert.equal(fetchSpy.callCount, 1); | |
| assert.isNotEmpty(latestRuns); | |
| assert.isNotEmpty(selectedRuns); | |
| assert.isTrue(latestRuns.every(r => r.patchset === 2)); | |
| assert.isTrue(selectedRuns.every(r => r.patchset === 2)); | |
| clock.restore(); | |
| }); | |
| test('fetches both patchsets when an older one is selected', async () => { | |
| const clock = sinon.useFakeTimers({shouldClearNativeTimers: true}); | |
| let change: ParsedChangeInfo | undefined = undefined; | |
| testResolver(changeModelToken).change$.subscribe(c => (change = c)); | |
| let latestRuns: CheckRun[] = []; | |
| let selectedRuns: CheckRun[] = []; | |
| model.allRunsLatestPatchset$.subscribe(r => (latestRuns = r)); | |
| model.allRunsSelectedPatchset$.subscribe(r => (selectedRuns = r)); | |
| const provider = createPatchsetTaggingProvider(); | |
| const fetchSpy = sinon.spy(provider, 'fetch'); | |
| model.register({ | |
| pluginName: PLUGIN_NAME, | |
| provider, | |
| config: CONFIG_POLLING_NONE, | |
| }); | |
| await waitUntil(() => change === undefined); | |
| // Explicitly selecting the older patchset (1); latest is 2. | |
| testResolver(changeViewModelToken).updateState({ | |
| checksPatchset: 1 as PatchSetNumber, | |
| }); | |
| const testChange = createTwoPatchsetChange(); | |
| testResolver(changeModelToken).updateStateChange(testChange); | |
| await waitUntil(() => deepEqual(change, testChange)); | |
| // Fire the throttled emission, then flush the fetch promise into state. | |
| clock.tick(600); | |
| await waitEventLoop(); | |
| // Distinct patchsets require two fetches: one for latest, one for selected. | |
| assert.equal(fetchSpy.callCount, 2); | |
| // The latest tab shows patchset 2, the selected tab shows the older 1. | |
| assert.isNotEmpty(latestRuns); | |
| assert.isNotEmpty(selectedRuns); | |
| assert.isTrue(latestRuns.every(r => r.patchset === 2)); | |
| assert.isTrue(selectedRuns.every(r => r.patchset === 1)); | |
| clock.restore(); | |
| }); | |
| test('fetch throttle', async () => { | |
| const clock = sinon.useFakeTimers({shouldClearNativeTimers: true}); | |
| let change: ParsedChangeInfo | undefined = undefined; | |
| testResolver(changeModelToken).change$.subscribe(c => (change = c)); | |
| const provider = createProvider(); | |
| const fetchSpy = sinon.spy(provider, 'fetch'); | |
| model.register({ | |
| pluginName: 'test-plugin', | |
| provider, | |
| config: CONFIG_POLLING_NONE, | |
| }); | |
| await waitUntil(() => change === undefined); | |
| const testChange = updateRevisionsWithCommitShas(createParsedChange()); | |
| testResolver(changeModelToken).updateStateChange(testChange); | |
| await waitUntil(() => deepEqual(change, testChange)); | |
| model.reload('test-plugin'); | |
| model.reload('test-plugin'); | |
| model.reload('test-plugin'); | |
| // Does not emit at 'leading' of throttle interval, | |
| // because fetch() is not called when change is undefined. | |
| assert.equal(fetchSpy.callCount, 0); | |
| // 600 ms is greater than the 500 ms throttle time. | |
| clock.tick(600); | |
| // emits at 'trailing' of throttle interval | |
| assert.equal(fetchSpy.callCount, 1); | |
| model.reload('test-plugin'); | |
| model.reload('test-plugin'); | |
| model.reload('test-plugin'); | |
| model.reload('test-plugin'); | |
| // emits at 'leading' of throttle interval | |
| assert.equal(fetchSpy.callCount, 2); | |
| // 600 ms is greater than the 500 ms throttle time. | |
| clock.tick(600); | |
| // emits at 'trailing' of throttle interval | |
| assert.equal(fetchSpy.callCount, 3); | |
| }); | |
| test('triggerAction', async () => { | |
| model.changeNum = 314 as NumericChangeId; | |
| model.latestPatchNum = 13 as PatchSetNumber; | |
| const action: Action = { | |
| name: 'test action', | |
| callback: () => undefined, | |
| }; | |
| const spy = sinon.spy(action, 'callback'); | |
| model.triggerAction(action, undefined, 'none'); | |
| assert.isTrue(spy.calledOnce); | |
| assert.equal(spy.lastCall.args[0], 314); | |
| assert.equal(spy.lastCall.args[1], 13); | |
| }); | |
| test('model.updateStateSetProvider', () => { | |
| model.updateStateSetProvider(PLUGIN_NAME, ChecksPatchset.LATEST); | |
| assert.deepEqual(current, { | |
| pluginName: PLUGIN_NAME, | |
| loading: false, | |
| firstTimeLoad: true, | |
| runs: [], | |
| actions: [], | |
| links: [], | |
| }); | |
| }); | |
| test('checksSelected$ selector does not mutate state', () => { | |
| model.updateStateSetProvider(PLUGIN_NAME, ChecksPatchset.LATEST); | |
| const pluginStateLatestRef = model.getState().pluginStateLatest; | |
| // Fires the selector synchronously via BehaviorSubject replay. | |
| model.checksSelected$.subscribe(() => {}); | |
| assert.strictEqual( | |
| model.getState().pluginStateLatest, | |
| pluginStateLatestRef, | |
| 'checksSelected$ must not mutate state.pluginStateLatest' | |
| ); | |
| }); | |
| test('loading and first time load', () => { | |
| model.updateStateSetProvider(PLUGIN_NAME, ChecksPatchset.LATEST); | |
| assert.isFalse(current.loading); | |
| assert.isTrue(current.firstTimeLoad); | |
| model.updateStateSetLoading(PLUGIN_NAME, ChecksPatchset.LATEST); | |
| assert.isTrue(current.loading); | |
| assert.isTrue(current.firstTimeLoad); | |
| model.updateStateSetResults( | |
| PLUGIN_NAME, | |
| RUNS, | |
| [], | |
| [], | |
| undefined, | |
| ChecksPatchset.LATEST | |
| ); | |
| assert.isFalse(current.loading); | |
| assert.isFalse(current.firstTimeLoad); | |
| model.updateStateSetLoading(PLUGIN_NAME, ChecksPatchset.LATEST); | |
| assert.isTrue(current.loading); | |
| assert.isFalse(current.firstTimeLoad); | |
| model.updateStateSetResults( | |
| PLUGIN_NAME, | |
| RUNS, | |
| [], | |
| [], | |
| undefined, | |
| ChecksPatchset.LATEST | |
| ); | |
| assert.isFalse(current.loading); | |
| assert.isFalse(current.firstTimeLoad); | |
| }); | |
| test('model.updateStateSetResults', () => { | |
| model.updateStateSetProvider(PLUGIN_NAME, ChecksPatchset.LATEST); | |
| model.updateStateSetResults( | |
| PLUGIN_NAME, | |
| RUNS, | |
| [], | |
| [], | |
| undefined, | |
| ChecksPatchset.LATEST | |
| ); | |
| assert.lengthOf(current.runs, 1); | |
| assert.lengthOf(current.runs[0].results!, 1); | |
| }); | |
| test('model.updateStateSetResults ignore empty name or status', () => { | |
| model.updateStateSetProvider(PLUGIN_NAME, ChecksPatchset.LATEST); | |
| model.updateStateSetResults( | |
| PLUGIN_NAME, | |
| [ | |
| { | |
| checkName: 'test-check-name', | |
| status: RunStatus.COMPLETED, | |
| }, | |
| // Will be ignored, because the checkName is empty. | |
| { | |
| checkName: undefined as unknown as string, | |
| status: RunStatus.COMPLETED, | |
| }, | |
| // Will be ignored, because the status is empty. | |
| { | |
| checkName: 'test-check-name', | |
| status: undefined as unknown as RunStatus, | |
| }, | |
| ], | |
| [], | |
| [], | |
| undefined, | |
| ChecksPatchset.LATEST | |
| ); | |
| // 2 out of 3 runs are ignored. | |
| assert.lengthOf(current.runs, 1); | |
| }); | |
| test('model.updateStateUpdateResult', () => { | |
| model.updateStateSetProvider(PLUGIN_NAME, ChecksPatchset.LATEST); | |
| model.updateStateSetResults( | |
| PLUGIN_NAME, | |
| RUNS, | |
| [], | |
| [], | |
| undefined, | |
| ChecksPatchset.LATEST | |
| ); | |
| assert.equal( | |
| current.runs[0].results![0].summary, | |
| RUNS[0].results![0].summary | |
| ); | |
| const result = RUNS[0].results![0]; | |
| const updatedResult = {...result, summary: 'new'}; | |
| model.updateStateUpdateResult( | |
| PLUGIN_NAME, | |
| RUNS[0], | |
| updatedResult, | |
| ChecksPatchset.LATEST | |
| ); | |
| assert.lengthOf(current.runs, 1); | |
| assert.lengthOf(current.runs[0].results!, 1); | |
| assert.equal(current.runs[0].results![0].summary, 'new'); | |
| }); | |
| test('allResults$', async () => { | |
| let results: CheckResult[] | undefined = undefined; | |
| model.allResults$.subscribe(allResults => (results = allResults)); | |
| testResolver(changeViewModelToken).updateState({ | |
| checksPatchset: 1 as PatchSetNumber, | |
| }); | |
| testResolver(changeModelToken).updateStateChange(createParsedChange()); | |
| model.updateStateSetProvider(PLUGIN_NAME, ChecksPatchset.SELECTED); | |
| model.updateStateSetProvider(PLUGIN_NAME, ChecksPatchset.LATEST); | |
| assert.equal(results!.length, 0); | |
| model.updateStateSetResults( | |
| PLUGIN_NAME, | |
| RUNS, | |
| [], | |
| [], | |
| undefined, | |
| ChecksPatchset.LATEST | |
| ); | |
| assert.equal(results!.length, 1); | |
| model.updateStateSetResults( | |
| PLUGIN_NAME, | |
| RUNS, | |
| [], | |
| [], | |
| undefined, | |
| ChecksPatchset.SELECTED | |
| ); | |
| assert.equal(results!.length, 1); | |
| }); | |
| test('polls for changes', async () => { | |
| const clock = sinon.useFakeTimers({shouldClearNativeTimers: true}); | |
| let change: ParsedChangeInfo | undefined = undefined; | |
| testResolver(changeModelToken).change$.subscribe(c => (change = c)); | |
| const provider = createProvider(); | |
| const fetchSpy = sinon.spy(provider, 'fetch'); | |
| model.register({ | |
| pluginName: 'test-plugin', | |
| provider, | |
| config: CONFIG_POLLING_5S, | |
| }); | |
| await waitUntil(() => change === undefined); | |
| clock.tick(1); | |
| const testChange = updateRevisionsWithCommitShas(createParsedChange()); | |
| testResolver(changeModelToken).updateStateChange(testChange); | |
| await waitUntil(() => deepEqual(change, testChange)); | |
| clock.tick(600); // need to wait for 500ms throttle | |
| await waitUntilCalled(fetchSpy, 'fetch'); | |
| const pollCount = fetchSpy.callCount; | |
| // polling should continue while we wait | |
| clock.tick(CONFIG_POLLING_5S.fetchPollingIntervalSeconds * 1000 * 2); | |
| assert.isTrue(fetchSpy.callCount > pollCount); | |
| }); | |
| test('does not poll when config specifies 0 seconds', async () => { | |
| const clock = sinon.useFakeTimers({shouldClearNativeTimers: true}); | |
| let change: ParsedChangeInfo | undefined = undefined; | |
| testResolver(changeModelToken).change$.subscribe(c => (change = c)); | |
| const provider = createProvider(); | |
| const fetchSpy = sinon.spy(provider, 'fetch'); | |
| model.register({ | |
| pluginName: 'test-plugin', | |
| provider, | |
| config: CONFIG_POLLING_NONE, | |
| }); | |
| await waitUntil(() => change === undefined); | |
| clock.tick(1); | |
| const testChange = updateRevisionsWithCommitShas(createParsedChange()); | |
| testResolver(changeModelToken).updateStateChange(testChange); | |
| await waitUntil(() => deepEqual(change, testChange)); | |
| clock.tick(600); // need to wait for 500ms throttle | |
| await waitUntilCalled(fetchSpy, 'fetch'); | |
| clock.tick(1); | |
| const pollCount = fetchSpy.callCount; | |
| // polling should not happen | |
| clock.tick(60 * 1000); | |
| assert.equal(fetchSpy.callCount, pollCount); | |
| }); | |
| test('collectRunResults does not incur quadratic size increase', async () => { | |
| const results: CheckResult[] = []; | |
| for (let i = 0; i < 100; i++) { | |
| results.push({ | |
| ...createCheckResult({ | |
| message: 'some message', | |
| }), | |
| }); | |
| } | |
| const run = createRun({results}); | |
| let collected: RunResult[] = []; | |
| collected = collectRunResults(collected, { | |
| runs: [run], | |
| } as ChecksProviderState); | |
| const collectedString = JSON.stringify(collected); | |
| // If the `results` property would not be removed from every check run, then | |
| // this combined string would be >1MB in size. | |
| assert.isAtMost(collectedString.length, 50000); | |
| }); | |
| }); |