Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for notebook search #179989

Merged
merged 3 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 2 additions & 43 deletions src/vs/workbench/contrib/search/test/browser/searchActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,19 @@

import * as assert from 'assert';
import { Keybinding } from 'vs/base/common/keybindings';
import { isWindows, OS } from 'vs/base/common/platform';
import { OS } from 'vs/base/common/platform';
import { URI } from 'vs/base/common/uri';
import { IModelService } from 'vs/editor/common/services/model';
import { ModelService } from 'vs/editor/common/services/modelService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { USLayoutResolvedKeybinding } from 'vs/platform/keybinding/common/usLayoutResolvedKeybinding';
import { IFileMatch, QueryType } from 'vs/workbench/services/search/common/search';
import { getElementToFocusAfterRemoved, getLastNodeFromSameType } from 'vs/workbench/contrib/search/browser/searchActionsRemoveReplace';
import { FileMatch, FileMatchOrMatch, FolderMatch, Match, SearchModel } from 'vs/workbench/contrib/search/browser/searchModel';
import { MockObjectTree } from 'vs/workbench/contrib/search/test/browser/mockSearchTree';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { TestThemeService } from 'vs/platform/theme/test/common/testThemeService';
import { ILabelService } from 'vs/platform/label/common/label';
import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/services/notebookEditorService';
import { NotebookEditorWidgetService } from 'vs/workbench/contrib/notebook/browser/services/notebookEditorServiceImpl';
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { TestEditorGroupsService } from 'vs/workbench/test/browser/workbenchTestServices';
import { createFileUriFromPathFromRoot, stubModelService, stubNotebookEditorService } from 'vs/workbench/contrib/search/test/browser/searchTestCommon';

suite('Search Actions', () => {

Expand Down Expand Up @@ -126,27 +119,6 @@ suite('Search Actions', () => {
}, undefined, undefined, folderMatch, rawMatch, null);
}

function createFileUriFromPathFromRoot(path?: string): URI {
const rootName = getRootName();
if (path) {
return URI.file(`${rootName}${path}`);
} else {
if (isWindows) {
return URI.file(`${rootName}/`);
} else {
return URI.file(rootName);
}
}
}

function getRootName(): string {
if (isWindows) {
return 'c:';
} else {
return '';
}
}

function aMatch(fileMatch: FileMatch): Match {
const line = ++counter;
const match = new Match(
Expand All @@ -172,17 +144,4 @@ suite('Search Actions', () => {
function aTree(elements: FileMatchOrMatch[]): any {
return new MockObjectTree(elements);
}

function stubModelService(instantiationService: TestInstantiationService): IModelService {
instantiationService.stub(IThemeService, new TestThemeService());
const config = new TestConfigurationService();
config.setUserConfiguration('search', { searchOnType: true, experimental: { notebookSearch: false } });
instantiationService.stub(IConfigurationService, config);
return instantiationService.createInstance(ModelService);
}

function stubNotebookEditorService(instantiationService: TestInstantiationService): INotebookEditorService {
instantiationService.stub(IEditorGroupsService, new TestEditorGroupsService());
return instantiationService.createInstance(NotebookEditorWidgetService);
}
});
178 changes: 155 additions & 23 deletions src/vs/workbench/contrib/search/test/browser/searchModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ import { TestInstantiationService } from 'vs/platform/instantiation/test/common/
import { IFileMatch, IFileSearchStats, IFolderQuery, ISearchComplete, ISearchProgressItem, ISearchQuery, ISearchService, ITextSearchMatch, OneLineRange, QueryType, TextSearchMatch } from 'vs/workbench/services/search/common/search';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { NullTelemetryService } from 'vs/platform/telemetry/common/telemetryUtils';
import { SearchModel } from 'vs/workbench/contrib/search/browser/searchModel';
import { CellMatch, MatchInNotebook, SearchModel } from 'vs/workbench/contrib/search/browser/searchModel';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { TestThemeService } from 'vs/platform/theme/test/common/testThemeService';
import { FileService } from 'vs/platform/files/common/fileService';
import { ILogService, NullLogService } from 'vs/platform/log/common/log';
import { IUriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentity';
import { UriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentityService';
import { isWindows } from 'vs/base/common/platform';
import { ILabelService } from 'vs/platform/label/common/label';
import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/services/notebookEditorService';
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { TestEditorGroupsService } from 'vs/workbench/test/browser/workbenchTestServices';
import { NotebookEditorWidgetService } from 'vs/workbench/contrib/notebook/browser/services/notebookEditorServiceImpl';
import { createFileUriFromPathFromRoot, getRootName } from 'vs/workbench/contrib/search/test/browser/searchTestCommon';
import { ICellMatch, IFileMatchWithCells, contentMatchesToTextSearchMatches, webviewMatchesToTextSearchMatches } from 'vs/workbench/contrib/search/browser/searchNotebookHelpers';
import { CellKind } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { ICellViewModel } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { FindMatch, IReadonlyTextBuffer } from 'vs/editor/common/model';
import { ResourceMap, ResourceSet } from 'vs/base/common/map';

const nullEvent = new class {
id: number = -1;
Expand Down Expand Up @@ -94,7 +99,7 @@ suite('SearchModel', () => {

function searchServiceWithResults(results: IFileMatch[], complete: ISearchComplete | null = null): ISearchService {
return <ISearchService>{
textSearch(query: ISearchQuery, token?: CancellationToken, onProgress?: (result: ISearchProgressItem) => void): Promise<ISearchComplete> {
textSearch(query: ISearchQuery, token?: CancellationToken, onProgress?: (result: ISearchProgressItem) => void, notebookURIs?: ResourceSet): Promise<ISearchComplete> {
return new Promise(resolve => {
queueMicrotask(() => {
results.forEach(onProgress!);
Expand Down Expand Up @@ -158,6 +163,141 @@ suite('SearchModel', () => {
assert.ok(new Range(2, 1, 2, 2).equalsRange(actuaMatches[0].range()));
});


test('Search Model: Search can return notebook results', async () => {
const notebookUri = createFileUriFromPathFromRoot('/1');

const results = [
aRawMatch('/2',
new TextSearchMatch('test', new OneLineRange(1, 1, 5)),
new TextSearchMatch('this is a test', new OneLineRange(1, 11, 15))),
aRawMatch('/3', new TextSearchMatch('test', lineOneRange))];
const searchService = instantiationService.stub(ISearchService, searchServiceWithResults(results));
const addContext = sinon.stub(CellMatch.prototype, 'addContext');
restoreStubs.push(addContext);

const textSearch = sinon.spy(searchService, 'textSearch');
const mdInputCell = {
cellKind: CellKind.Markup, textBuffer: <IReadonlyTextBuffer>{
getLineContent(lineNumber: number): string {
if (lineNumber === 1) {
return '# Test';
} else {
return '';
}
}
},
id: 'mdInputCell'
} as ICellViewModel;

const findMatchMds = [new FindMatch(new Range(1, 3, 1, 7), ['Test'])];

const codeCell = {
cellKind: CellKind.Code, textBuffer: <IReadonlyTextBuffer>{
getLineContent(lineNumber: number): string {
if (lineNumber === 1) {
return 'print("test! testing!!")';
} else {
return '';
}
}
},
id: 'codeCell'
} as ICellViewModel;

const findMatchCodeCells =
[new FindMatch(new Range(1, 8, 1, 12), ['test']),
new FindMatch(new Range(1, 14, 1, 18), ['test']),
];
const webviewMatches = [{
index: 0,
searchPreviewInfo: {
line: 'test! testing!!',
range: {
start: 1,
end: 5
}
}
},
{
index: 1,
searchPreviewInfo: {
line: 'test! testing!!',
range: {
start: 7,
end: 11
}
}
}
];
const cellMatchMd: ICellMatch = {
cell: mdInputCell,
index: 0,
contentResults: contentMatchesToTextSearchMatches(findMatchMds, mdInputCell),
webviewResults: []
};

const cellMatchCode: ICellMatch = {
cell: codeCell,
index: 1,
contentResults: contentMatchesToTextSearchMatches(findMatchCodeCells, codeCell),
webviewResults: webviewMatchesToTextSearchMatches(webviewMatches),
};

const model: SearchModel = instantiationService.createInstance(SearchModel);
const notebookSearch = sinon.stub(model, <any>"getLocalNotebookResults").callsFake(() => {
const localResults = new ResourceMap<IFileMatchWithCells | null>(uri => uri.path);
const fileMatch = aRawMatchWithCells('/1', cellMatchMd, cellMatchCode);
localResults.set(notebookUri, fileMatch);
return Promise.resolve(
{
results: localResults,
limitHit: false
});
});

await model.search({ contentPattern: { pattern: 'test' }, type: QueryType.Text, folderQueries });
const actual = model.searchResult.matches();

assert(notebookSearch.calledOnce);
assert(textSearch.getCall(0).args[3]?.size === 1);
assert(textSearch.getCall(0).args[3]?.has(notebookUri)); // ensure that the textsearch knows not to re-source the notebooks

assert.strictEqual(3, actual.length);
assert.strictEqual(URI.file(`${getRootName()}/1`).toString(), actual[0].resource.toString());
const notebookFileMatches = actual[0].matches();

assert.ok(notebookFileMatches[0].range().equalsRange(new Range(1, 3, 1, 7)));
assert.ok(notebookFileMatches[1].range().equalsRange(new Range(1, 8, 1, 12)));
assert.ok(notebookFileMatches[2].range().equalsRange(new Range(1, 14, 1, 18)));
assert.ok(notebookFileMatches[3].range().equalsRange(new Range(1, 2, 1, 6)));
assert.ok(notebookFileMatches[4].range().equalsRange(new Range(1, 8, 1, 12)));

notebookFileMatches.forEach(match => match instanceof MatchInNotebook);
// assert(notebookFileMatches[0] instanceof MatchInNotebook);
assert((notebookFileMatches[0] as MatchInNotebook).cell.id === 'mdInputCell');
assert((notebookFileMatches[1] as MatchInNotebook).cell.id === 'codeCell');
assert((notebookFileMatches[2] as MatchInNotebook).cell.id === 'codeCell');
assert((notebookFileMatches[3] as MatchInNotebook).cell.id === 'codeCell');
assert((notebookFileMatches[4] as MatchInNotebook).cell.id === 'codeCell');

const mdCellMatchProcessed = (notebookFileMatches[0] as MatchInNotebook).cellParent;
const codeCellMatchProcessed = (notebookFileMatches[1] as MatchInNotebook).cellParent;

assert(mdCellMatchProcessed.contentMatches.length === 1);
assert(codeCellMatchProcessed.contentMatches.length === 2);
assert(codeCellMatchProcessed.webviewMatches.length === 2);

assert(mdCellMatchProcessed.contentMatches[0] === notebookFileMatches[0]);
assert(codeCellMatchProcessed.contentMatches[0] === notebookFileMatches[1]);
assert(codeCellMatchProcessed.contentMatches[1] === notebookFileMatches[2]);
assert(codeCellMatchProcessed.webviewMatches[0] === notebookFileMatches[3]);
assert(codeCellMatchProcessed.webviewMatches[1] === notebookFileMatches[4]);

assert.strictEqual(URI.file(`${getRootName()}/2`).toString(), actual[1].resource.toString());
assert.strictEqual(URI.file(`${getRootName()}/3`).toString(), actual[2].resource.toString());
});

test('Search Model: Search reports telemetry on search completed', async () => {
const target = instantiationService.spy(ITelemetryService, 'publicLog');
const results = [
Expand Down Expand Up @@ -284,6 +424,15 @@ suite('SearchModel', () => {
const tokenSource = new CancellationTokenSource();
instantiationService.stub(ISearchService, canceleableSearchService(tokenSource));
const testObject: SearchModel = instantiationService.createInstance(SearchModel);
sinon.stub(testObject, "notebookSearch").callsFake((_, token) => {
token?.onCancellationRequested(() => tokenSource.cancel());

return new Promise(resolve => {
queueMicrotask(() => {
resolve(<any>{});
});
});
});

testObject.search({ contentPattern: { pattern: 'somestring' }, type: QueryType.Text, folderQueries });
instantiationService.stub(ISearchService, searchServiceWithResults([]));
Expand Down Expand Up @@ -327,25 +476,8 @@ suite('SearchModel', () => {
return { resource: createFileUriFromPathFromRoot(resource), results };
}

function createFileUriFromPathFromRoot(path?: string): URI {
const rootName = getRootName();
if (path) {
return URI.file(`${rootName}${path}`);
} else {
if (isWindows) {
return URI.file(`${rootName}/`);
} else {
return URI.file(rootName);
}
}
}

function getRootName(): string {
if (isWindows) {
return 'c:';
} else {
return '';
}
function aRawMatchWithCells(resource: string, ...cells: ICellMatch[]) {
return { resource: createFileUriFromPathFromRoot(resource), cellResults: cells };
}

function stub(arg1: any, arg2: any, arg3: any): sinon.SinonStub {
Expand All @@ -357,7 +489,7 @@ suite('SearchModel', () => {
function stubModelService(instantiationService: TestInstantiationService): IModelService {
instantiationService.stub(IThemeService, new TestThemeService());
const config = new TestConfigurationService();
config.setUserConfiguration('search', { searchOnType: true, experimental: { notebookSearch: false } });
config.setUserConfiguration('search', { searchOnType: true, experimental: { notebookSearch: true } });
instantiationService.stub(IConfigurationService, config);
return instantiationService.createInstance(ModelService);
}
Expand Down
Loading