Skip to content

Commit

Permalink
Added block flag for searchers
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Nov 7, 2018
1 parent 321f7ab commit 5c73832
Show file tree
Hide file tree
Showing 22 changed files with 111 additions and 50 deletions.
5 changes: 4 additions & 1 deletion src/tests/unit/fake-searcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { Searcher } from "../../ts/searcher/searcher";
import { SearchResultItem } from "../../ts/search-result-item";

export class FakeSearcher implements Searcher {
public readonly blockOthers: boolean;

private readonly searchResultItems: SearchResultItem[];

constructor(searchResultItems: SearchResultItem[]) {
constructor(shouldBlockOthers: boolean, searchResultItems: SearchResultItem[]) {
this.blockOthers = shouldBlockOthers;
this.searchResultItems = searchResultItems;
}

Expand Down
76 changes: 55 additions & 21 deletions src/tests/unit/input-validation-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { WebSearchBuilder } from "../../ts/builders/web-search-builder";
describe(InputValidationService.name, (): void => {
const config = {
fallbackWebSearches: [] as string[],
webSearches: [] as WebSearch[],
} as UserConfigOptions;

describe("getSearchResults", () => {
Expand All @@ -21,17 +22,19 @@ describe(InputValidationService.name, (): void => {
null,
];

const shouldBlockOtherSearchers = false;

const combinations = [
{
searcher: new FakeSearcher([{ name: "Search Result 1" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(true),
},
{
searcher: new FakeSearcher([{ name: "Search Result 2" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 2" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
{
searcher: new FakeSearcher([{ name: "Search Result 3" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 3" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
];
Expand All @@ -51,17 +54,18 @@ describe(InputValidationService.name, (): void => {
});

it("should return an empty array if user input matches none of the searchers", (): void => {
const shouldBlockOtherSearchers = false;
const combinations = [
{
searcher: new FakeSearcher([{ name: "Search Result 1" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
{
searcher: new FakeSearcher([{ name: "Search Result 2" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 2" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
{
searcher: new FakeSearcher([{ name: "Search Result 3" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 3" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
];
Expand All @@ -74,17 +78,18 @@ describe(InputValidationService.name, (): void => {
});

it("should return all items if user input matches all searchers", (): void => {
const shouldBlockOtherSearchers = false;
const combinations = [
{
searcher: new FakeSearcher([{ name: "Search Result 1" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(true),
},
{
searcher: new FakeSearcher([{ name: "Search Result 2" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 2" }] as SearchResultItem[]),
validator: new FakeInputValidator(true),
},
{
searcher: new FakeSearcher([{ name: "Search Result 3" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 3" }] as SearchResultItem[]),
validator: new FakeInputValidator(true),
},
];
Expand All @@ -96,17 +101,18 @@ describe(InputValidationService.name, (): void => {
});

it("should return only the items that match the user input", (): void => {
const shouldBlockOtherSearchers = false;
const combinations = [
{
searcher: new FakeSearcher([{ name: "Search Result 1" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
{
searcher: new FakeSearcher([{ name: "Search Result 2" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 2" }] as SearchResultItem[]),
validator: new FakeInputValidator(true),
},
{
searcher: new FakeSearcher([{ name: "Search Result 3" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 3" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
];
Expand All @@ -120,17 +126,18 @@ describe(InputValidationService.name, (): void => {
});

it("should return empty search result if no fallback search results are defined and user input does not match any searcher", (): void => {
const shouldBlockOtherSearchers = false;
const combinations = [
{
searcher: new FakeSearcher([{ name: "Search Result 1" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
{
searcher: new FakeSearcher([{ name: "Search Result 2" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 2" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
{
searcher: new FakeSearcher([{ name: "Search Result 3" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 3" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
];
Expand All @@ -155,17 +162,19 @@ describe(InputValidationService.name, (): void => {
config.fallbackWebSearches = [fallBackWebSearchName];
config.webSearches = [webSearch];

const shouldBlockOtherSearchers = false;

const combinations = [
{
searcher: new FakeSearcher([{ name: "Search Result 1" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
{
searcher: new FakeSearcher([{ name: "Search Result 2" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 2" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
{
searcher: new FakeSearcher([{ name: "Search Result 3" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 3" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
];
Expand All @@ -189,17 +198,18 @@ describe(InputValidationService.name, (): void => {
config.fallbackWebSearches = [fallBackWebSearchName];
config.webSearches = [webSearch];

const shouldBlockOtherSearchers = false;
const combinations = [
{
searcher: new FakeSearcher([{ name: "Search Result 1" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
{
searcher: new FakeSearcher([{ name: "Search Result 2" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 2" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
{
searcher: new FakeSearcher([{ name: "Search Result 3" }] as SearchResultItem[]),
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 3" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
];
Expand All @@ -212,5 +222,29 @@ describe(InputValidationService.name, (): void => {
expect(actual[0].name).toBe(WebSearchBuilder.buildSearchResultItem(userInput, webSearch).name);
expect(actual[0].executionArgument).toBe(`${fallBackWebSearchUrl}${userInput}`);
});

it("should only return search results from first blocking searcher", (): void => {
const combinations = [
{
searcher: new FakeSearcher(false, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(true),
},
{
searcher: new FakeSearcher(false, [{ name: "Search Result 2" }] as SearchResultItem[]),
validator: new FakeInputValidator(true),
},
{
searcher: new FakeSearcher(true, [{ name: "Search Result 3" }] as SearchResultItem[]),
validator: new FakeInputValidator(true),
},
];

const userInput = "search";

const searchResults = new InputValidationService(config, combinations).getSearchResult(userInput);

expect(searchResults.length).toBe(1);
expect(searchResults[0].name).toBe("Search Result 3");
});
});
});
6 changes: 3 additions & 3 deletions src/ts/executors/calculator-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { StringHelpers } from "../helpers/string-helpers";
import { clipboard } from "electron";

export class CalculatorExecutor implements Executor {
public hideAfterExecution = true;
public resetUserInputAfterExecution = true;
public logExecution = true;
public readonly hideAfterExecution = true;
public readonly resetUserInputAfterExecution = true;
public readonly logExecution = true;

public execute(executionArgument: string): void {
const prefix = CalculatorHelper.getExecutionArgumentPrefix;
Expand Down
6 changes: 3 additions & 3 deletions src/ts/executors/command-line-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { Executor } from "./executor";
import { IpcChannels } from "../ipc-channels";

export class CommandLineExecutor implements Executor {
public hideAfterExecution = false;
public resetUserInputAfterExecution = true;
public logExecution = false;
public readonly hideAfterExecution = false;
public readonly resetUserInputAfterExecution = true;
public readonly logExecution = false;

public execute(executionArgument: string): void {
const command = CommandLineHelpers.buildCommand(executionArgument);
Expand Down
6 changes: 3 additions & 3 deletions src/ts/executors/custom-command-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { exec } from "child_process";
import { UeliHelpers } from "../helpers/ueli-helpers";

export class CustomCommandExecutor implements Executor {
public hideAfterExecution = true;
public resetUserInputAfterExecution = true;
public logExecution = true;
public readonly hideAfterExecution = true;
public readonly resetUserInputAfterExecution = true;
public readonly logExecution = true;

public execute(executionArgument: string): void {
executionArgument = executionArgument.replace(UeliHelpers.shortcutPrefix, "");
Expand Down
6 changes: 3 additions & 3 deletions src/ts/executors/file-path-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { Executor } from "./executor";
import { platform } from "os";

export class FilePathExecutor implements Executor {
public hideAfterExecution = true;
public resetUserInputAfterExecution = true;
public logExecution = true;
public readonly hideAfterExecution = true;
public readonly resetUserInputAfterExecution = true;
public readonly logExecution = true;

public execute(filePath: string): void {
const command = Injector.getFileExecutionCommand(platform(), filePath);
Expand Down
6 changes: 3 additions & 3 deletions src/ts/executors/mac-os-settings-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { MacOsSettingsHelpers } from "../helpers/mac-os-settings.helpers";
import { exec } from "child_process";

export class MacOsSettingsExecutor implements Executor {
public hideAfterExecution = true;
public resetUserInputAfterExecution = true;
public logExecution = true;
public readonly hideAfterExecution = true;
public readonly resetUserInputAfterExecution = true;
public readonly logExecution = true;

public execute(executionArgument: string): void {
const command = this.replacePrefix(executionArgument);
Expand Down
6 changes: 3 additions & 3 deletions src/ts/executors/ueli-command-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { ipcMain } from "electron";
import { Executor } from "./executor";

export class UeliCommandExecutor implements Executor {
public hideAfterExecution = false;
public logExecution = true;
public resetUserInputAfterExecution = true;
public readonly hideAfterExecution = false;
public readonly logExecution = true;
public readonly resetUserInputAfterExecution = true;

public execute(command: string): void {
ipcMain.emit(command);
Expand Down
6 changes: 3 additions & 3 deletions src/ts/executors/web-search-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { WebSearchHelpers } from "../helpers/web-search-helper";
import { WebSearch } from "../web-search";

export class WebSearchExecutor implements Executor {
public hideAfterExecution = true;
public resetUserInputAfterExecution = true;
public logExecution = false;
public readonly hideAfterExecution = true;
public readonly resetUserInputAfterExecution = true;
public readonly logExecution = false;

private readonly webSearches: WebSearch[];

Expand Down
6 changes: 3 additions & 3 deletions src/ts/executors/web-url-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { Executor } from "./executor";
import { platform } from "os";

export class WebUrlExecutor implements Executor {
public hideAfterExecution = true;
public resetUserInputAfterExecution = true;
public logExecution = false;
public readonly hideAfterExecution = true;
public readonly resetUserInputAfterExecution = true;
public readonly logExecution = false;

public execute(url: string): void {
const command = Injector.getOpenUrlWithDefaultBrowserCommand(platform(), url);
Expand Down
6 changes: 3 additions & 3 deletions src/ts/executors/windows-settings-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Executor } from "./executor";
import { WindowsSettingsHelpers } from "../helpers/windows-settings-helpers";

export class WindowsSettingsExecutor implements Executor {
public hideAfterExecution = true;
public resetUserInputAfterExecution = true;
public logExecution = true;
public readonly hideAfterExecution = true;
public readonly resetUserInputAfterExecution = true;
public readonly logExecution = true;

public execute(executionArgument: string): void {
const command = this.replacePrefix(executionArgument);
Expand Down
7 changes: 6 additions & 1 deletion src/ts/input-validation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ export class InputValidationService {

for (const combination of this.validatorSearcherCombinations) {
if (combination.validator.isValidForSearchResults(trimmedUserInput)) {
result = result.concat(combination.searcher.getSearchResult(trimmedUserInput));
if (combination.searcher.blockOthers) {
result = combination.searcher.getSearchResult(trimmedUserInput);
break;
} else {
result = result.concat(combination.searcher.getSearchResult(trimmedUserInput));
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/ts/searcher/calculator-searcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as math from "mathjs";
import { IconSet } from "../icon-sets/icon-set";

export class CalculatorSearcher implements Searcher {
public readonly blockOthers = false;

private readonly iconSet: IconSet;

constructor(iconSet: IconSet) {
Expand Down
2 changes: 2 additions & 0 deletions src/ts/searcher/command-line-searcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { CommandLineHelpers } from "../helpers/command-line-helpers";
import { IconSet } from "../icon-sets/icon-set";

export class CommandLineSearcher implements Searcher {
public readonly blockOthers = true;

private readonly iconSet: IconSet;

constructor(iconSet: IconSet) {
Expand Down
2 changes: 2 additions & 0 deletions src/ts/searcher/custom-command-searcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { UeliHelpers } from "../helpers/ueli-helpers";
import { StringHelpers } from "../helpers/string-helpers";

export class CustomCommandSearcher implements Searcher {
public readonly blockOthers = false;

private readonly customCommands: CustomCommand[];
private readonly defaultIcon: string;

Expand Down
2 changes: 2 additions & 0 deletions src/ts/searcher/email-address-searcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { SearchResultItem } from "../search-result-item";
import { IconSet } from "../icon-sets/icon-set";

export class EmailAddressSearcher implements Searcher {
public readonly blockOthers = false;

private readonly iconSet: IconSet;

constructor(iconSet: IconSet) {
Expand Down
2 changes: 2 additions & 0 deletions src/ts/searcher/fallback-web-search-searcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { WebSearch } from "../web-search";
import { WebSearchBuilder } from "../builders/web-search-builder";

export class FallbackWebSearchSercher implements Searcher {
public readonly blockOthers = false;

private readonly fallbackWebSearches: string[];
private readonly webSearches: WebSearch[];

Expand Down
2 changes: 2 additions & 0 deletions src/ts/searcher/file-path-searcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { FilePathDescriptionBuilder } from "../builders/file-path-description-bu
import { IconSet } from "../icon-sets/icon-set";

export class FilePathSearcher implements Searcher {
public readonly blockOthers = true;

private readonly iconSet: IconSet;
private readonly searchEngineThreshold: number;
private readonly searchEngineLimit: number;
Expand Down
Loading

0 comments on commit 5c73832

Please sign in to comment.