Path: blob/main/extensions/copilot/test/simulation/workbench/stores/detectedTests.ts
13399 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as mobx from 'mobx';6import { Disposable, toDisposable } from '../../../../src/util/vs/base/common/lifecycle';7import { IDetectedTestOutput } from '../../shared/sharedTypes';8import { spawnSimulation } from '../utils/simulationExec';9import { genericEquals } from '../utils/utils';1011/**12* Detects tests that are available in the simulation.13*14* @param extraArgs - Additional CLI args to pass to `simulationMain` when listing tests.15* Used by NES External mode to pass `--nes=external --external-scenarios=<path>`.16*/1718export class DetectedTests extends Disposable {1920@mobx.observable21public tests: IDetectedTestOutput[] = [];2223constructor(private readonly _extraArgs?: () => string[]) {24super();25mobx.makeObservable(this);2627let timer: TimeoutHandle | undefined = undefined;28this._register(toDisposable(() => clearInterval(timer)));29const resume = () => {30this._updateTests();31timer = setInterval(() => this._updateTests(), 5000);32};33const suspend = () => {34clearInterval(timer);35timer = undefined;36};3738mobx.onBecomeObserved(this, 'tests', resume);39mobx.onBecomeUnobserved(this, 'tests', suspend);40}4142private async _updateTests(): Promise<void> {43const newTests = await this._fetchTests();44if (genericEquals(this.tests, newTests)) {45return;46}4748mobx.runInAction(() => {49this.tests = newTests;50});51}5253private async _fetchTests(): Promise<IDetectedTestOutput[]> {54const args = ['--list-tests', '--json', ...(this._extraArgs?.() ?? [])];55const result = await spawnSimulation<IDetectedTestOutput>({56args,57ignoreNonJSONLines: true58}).toPromise();59result.sort((a, b) => a.name.localeCompare(b.name));60return result;61}62}636465