diff options
Diffstat (limited to 'test/background/completion')
4 files changed, 404 insertions, 0 deletions
diff --git a/test/background/completion/OpenCompletionUseCase.test.ts b/test/background/completion/OpenCompletionUseCase.test.ts new file mode 100644 index 0000000..421ce69 --- /dev/null +++ b/test/background/completion/OpenCompletionUseCase.test.ts @@ -0,0 +1,131 @@ +import "reflect-metadata"; +import CompletionType from "../../../src/shared/CompletionType"; +import BookmarkRepository, {BookmarkItem} from "../../../src/background/completion/BookmarkRepository"; +import HistoryRepository, {HistoryItem} from "../../../src/background/completion/HistoryRepository"; +import OpenCompletionUseCase from "../../../src/background/completion/OpenCompletionUseCase"; +import CachedSettingRepository from "../../../src/background/repositories/CachedSettingRepository"; +import Settings, {DefaultSetting} from "../../../src/shared/settings/Settings"; +import { expect } from 'chai'; +import sinon from 'sinon'; +import Properties from "../../../src/shared/settings/Properties"; +import Search from "../../../src/shared/settings/Search"; + +class MockBookmarkRepository implements BookmarkRepository { + queryBookmarks(_query: string): Promise<BookmarkItem[]> { + throw new Error("not implemented") + } +} + +class MockHistoryRepository implements HistoryRepository { + queryHistories(_keywords: string): Promise<HistoryItem[]> { + throw new Error("not implemented") + } +} + +class MockSettingRepository implements CachedSettingRepository { + get(): Promise<Settings> { + throw new Error("not implemented") + } + + setProperty(_name: string, _value: string | number | boolean): Promise<void> { + throw new Error("not implemented") + } + + update(_value: Settings): Promise<void> { + throw new Error("not implemented") + } +} + +describe('OpenCompletionUseCase', () => { + let bookmarkRepository: MockBookmarkRepository; + let historyRepository: MockHistoryRepository; + let settingRepository: MockSettingRepository; + let sut: OpenCompletionUseCase; + + beforeEach(() => { + bookmarkRepository = new MockBookmarkRepository(); + historyRepository = new MockHistoryRepository(); + settingRepository = new MockSettingRepository(); + sut = new OpenCompletionUseCase(bookmarkRepository, historyRepository, settingRepository) + }); + + describe('#getCompletionTypes', () => { + it("returns completion types from the property", async () => { + sinon.stub(settingRepository, 'get').returns(Promise.resolve(new Settings({ + keymaps: DefaultSetting.keymaps, + search: DefaultSetting.search, + properties: new Properties({ complete: "shb" }), + blacklist: DefaultSetting.blacklist, + }))); + + const items = await sut.getCompletionTypes(); + expect(items).to.deep.equal([ + CompletionType.SearchEngines, + CompletionType.History, + CompletionType.Bookmarks + ]); + }); + }); + + describe('#requestSearchEngines', () => { + it("returns search engines matches by the query", async () => { + sinon.stub(settingRepository, 'get').returns(Promise.resolve(new Settings({ + keymaps: DefaultSetting.keymaps, + search: new Search("google", { + "google": "https://google.com/search?q={}", + "yahoo": "https://search.yahoo.com/search?q={}", + "bing": "https://bing.com/search?q={}", + "google_ja": "https://google.co.jp/search?q={}", + }), + properties: DefaultSetting.properties, + blacklist: DefaultSetting.blacklist, + }))); + + expect(await sut.requestSearchEngines("")).to.deep.equal([ + "google", + "yahoo", + "bing", + "google_ja", + ]); + expect(await sut.requestSearchEngines("go")).to.deep.equal([ + "google", + "google_ja", + ]); + expect(await sut.requestSearchEngines("x")).to.be.empty; + }) + }); + + describe('#requestBookmarks', () => { + it("returns bookmarks from the repository", async() => { + sinon.stub(bookmarkRepository, 'queryBookmarks') + .withArgs("site").returns(Promise.resolve([ + { title: "site1", url: "https://site1.example.com" }, + { title: "site2", url: "https://site2.example.com/" }, + ])) + .withArgs("xyz").returns(Promise.resolve([])); + + expect(await sut.requestBookmarks("site")).to.deep.equal([ + { title: "site1", url: "https://site1.example.com" }, + { title: "site2", url: "https://site2.example.com/" }, + ]); + expect(await sut.requestBookmarks("xyz")).to.be.empty; + }); + }); + + describe('#requestHistory', () => { + it("returns histories from the repository", async() => { + sinon.stub(historyRepository, 'queryHistories') + .withArgs("site").returns(Promise.resolve([ + { title: "site1", url: "https://site1.example.com" }, + { title: "site2", url: "https://site2.example.com/" }, + ])) + .withArgs("xyz").returns(Promise.resolve([])); + + expect(await sut.requestHistory("site")).to.deep.equal([ + { title: "site1", url: "https://site1.example.com" }, + { title: "site2", url: "https://site2.example.com/" }, + ]); + expect(await sut.requestHistory("xyz")).to.be.empty; + }); + }); +});
\ No newline at end of file diff --git a/test/background/completion/PropertyCompletionUseCase.test.ts b/test/background/completion/PropertyCompletionUseCase.test.ts new file mode 100644 index 0000000..57f5bff --- /dev/null +++ b/test/background/completion/PropertyCompletionUseCase.test.ts @@ -0,0 +1,15 @@ +import 'reflect-metadata'; +import PropertyCompletionUseCase from "../../../src/background/completion/PropertyCompletionUseCase"; +import { expect } from 'chai'; + +describe('PropertyCompletionUseCase', () => { + describe('getProperties', () => { + it('returns property types', async () => { + const sut = new PropertyCompletionUseCase(); + + const properties = await sut.getProperties(); + expect(properties).to.deep.contain({ name: 'smoothscroll', type: 'boolean' }); + expect(properties).to.deep.contain({ name: 'complete', type: 'string' }); + }) + }); +});
\ No newline at end of file diff --git a/test/background/completion/TabCompletionUseCase.test.ts b/test/background/completion/TabCompletionUseCase.test.ts new file mode 100644 index 0000000..b9dc60b --- /dev/null +++ b/test/background/completion/TabCompletionUseCase.test.ts @@ -0,0 +1,144 @@ +import "reflect-metadata" +import TabRepositoryImpl from "../../../src/background/completion/impl/TabRepositoryImpl"; +import {Tab} from "../../../src/background/completion/TabRepository"; +import TabPresenter from "../../../src/background/presenters/TabPresenter"; +import TabCompletionUseCase from "../../../src/background/completion/TabCompletionUseCase"; +import sinon from 'sinon'; +import { expect } from 'chai'; +import TabFlag from "../../../src/shared/TabFlag"; + +class MockTabRepository implements TabRepositoryImpl { + async queryTabs(_query: string, _excludePinned: boolean): Promise<Tab[]> { + throw new Error("not implemented") + } + + async getAllTabs(_excludePinned: boolean): Promise<Tab[]> { + throw new Error("not implemented") + } +} + +class MockTabPresenter implements TabPresenter { + create(_url: string, _opts?: object): Promise<browser.tabs.Tab> { + throw new Error("not implemented") + } + + duplicate(_id: number): Promise<browser.tabs.Tab> { + throw new Error("not implemented") + } + + getAll(): Promise<browser.tabs.Tab[]> { + throw new Error("not implemented") + } + + getByKeyword(_keyword: string, _excludePinned: boolean): Promise<browser.tabs.Tab[]> { + throw new Error("not implemented") + } + + getCurrent(): Promise<browser.tabs.Tab> { + throw new Error("not implemented") + } + + getLastSelectedId(): Promise<number | undefined> { + throw new Error("not implemented") + } + + getZoom(_tabId: number): Promise<number> { + throw new Error("not implemented") + } + + onSelected(_listener: (arg: { tabId: number; windowId: number }) => void): void { + throw new Error("not implemented") + } + + open(_url: string, _tabId?: number): Promise<browser.tabs.Tab> { + throw new Error("not implemented") + } + + reload(_tabId: number, _cache: boolean): Promise<void> { + throw new Error("not implemented") + } + + remove(_ids: number[]): Promise<void> { + throw new Error("not implemented") + } + + reopen(): Promise<any> { + throw new Error("not implemented") + } + + select(_tabId: number): Promise<void> { + throw new Error("not implemented") + } + + setPinned(_tabId: number, _pinned: boolean): Promise<void> { + throw new Error("not implemented") + } + + setZoom(_tabId: number, _factor: number): Promise<void> { + throw new Error("not implemented") + } +} + +describe('TabCompletionUseCase', () => { + let tabRepository: MockTabRepository; + let tabPresenter: TabPresenter; + let sut: TabCompletionUseCase; + + beforeEach(() => { + tabRepository = new MockTabRepository(); + tabPresenter = new MockTabPresenter(); + sut = new TabCompletionUseCase(tabRepository, tabPresenter); + + sinon.stub(tabPresenter, 'getLastSelectedId').returns(Promise.resolve(12)); + sinon.stub(tabRepository, 'getAllTabs').returns(Promise.resolve([ + { id: 10, index: 0, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', active: false }, + { id: 11, index: 1, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', active: true }, + { id: 12, index: 2, title: 'Bing', url: 'https://bing.com/', active: false }, + ])); + }); + + describe('#queryTabs', () => { + it("returns tab items", async () => { + sinon.stub(tabRepository, 'queryTabs').withArgs('', false).returns(Promise.resolve([ + { id: 10, index: 0, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', active: false }, + { id: 11, index: 1, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', active: true }, + { id: 12, index: 2, title: 'Bing', url: 'https://bing.com/', active: false }, + ])).withArgs('oo', false).returns(Promise.resolve([ + { id: 10, index: 0, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', active: false }, + { id: 11, index: 1, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', active: true }, + ])); + + expect(await sut.queryTabs('', false)).to.deep.equal([ + { index: 1, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', flag: TabFlag.None }, + { index: 2, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', flag: TabFlag.CurrentTab }, + { index: 3, title: 'Bing', url: 'https://bing.com/', faviconUrl: undefined, flag: TabFlag.LastTab }, + ]); + + expect(await sut.queryTabs('oo', false)).to.deep.equal([ + { index: 1, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', flag: TabFlag.None }, + { index: 2, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', flag: TabFlag.CurrentTab }, + ]); + }); + + it("returns a tab by the index", async () => { + expect(await sut.queryTabs('1', false)).to.deep.equal([ + { index: 1, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', flag: TabFlag.None }, + ]); + + expect(await sut.queryTabs('10', false)).to.be.empty; + expect(await sut.queryTabs('-1', false)).to.be.empty; + }); + + it("returns the current tab by % flag", async () => { + expect(await sut.queryTabs('%', false)).to.deep.equal([ + { index: 2, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', flag: TabFlag.CurrentTab }, + ]); + }); + + it("returns the current tab by # flag", async () => { + expect(await sut.queryTabs('#', false)).to.deep.equal([ + { index: 3, title: 'Bing', url: 'https://bing.com/', faviconUrl: undefined, flag: TabFlag.LastTab }, + ]); + }) + }); +});
\ No newline at end of file diff --git a/test/background/completion/impl/filters.test.ts b/test/background/completion/impl/filters.test.ts new file mode 100644 index 0000000..2b15a9b --- /dev/null +++ b/test/background/completion/impl/filters.test.ts @@ -0,0 +1,114 @@ +import * as filters from '../../../../src/background/completion/impl/filters' +import { expect } from 'chai'; + +describe('background/usecases/filters', () => { + describe('filterHttp', () => { + it('filters http URLs duplicates to https hosts', () => { + const pages = [ + { id: '0', url: 'http://i-beam.org/foo' }, + { id: '1', url: 'https://i-beam.org/bar' }, + { id: '2', url: 'http://i-beam.net/hoge' }, + { id: '3', url: 'http://i-beam.net/fuga' }, + ]; + const filtered = filters.filterHttp(pages); + + const urls = filtered.map(x => x.url); + expect(urls).to.deep.equal([ + 'https://i-beam.org/bar', 'http://i-beam.net/hoge', 'http://i-beam.net/fuga' + ]); + }) + }); + + describe('filterBlankTitle', () => { + it('filters blank titles', () => { + const pages = [ + { id: '0', title: 'hello' }, + { id: '1', title: '' }, + { id: '2' }, + ]; + const filtered = filters.filterBlankTitle(pages); + + expect(filtered).to.deep.equal([{ id: '0', title: 'hello' }]); + }); + }); + + describe('filterByTailingSlash', () => { + it('filters duplicated pathname on tailing slash', () => { + const pages = [ + { id: '0', url: 'http://i-beam.org/content' }, + { id: '1', url: 'http://i-beam.org/content/' }, + { id: '2', url: 'http://i-beam.org/search' }, + { id: '3', url: 'http://i-beam.org/search?q=apple_banana_cherry' }, + ]; + const filtered = filters.filterByTailingSlash(pages); + + const urls = filtered.map(x => x.url); + expect(urls).to.deep.equal([ + 'http://i-beam.org/content', + 'http://i-beam.org/search', + 'http://i-beam.org/search?q=apple_banana_cherry', + ]); + }); + }) + + describe('filterByPathname', () => { + it('remains items less than minimam length', () => { + const pages = [ + { id: '0', url: 'http://i-beam.org/search?q=apple' }, + { id: '1', url: 'http://i-beam.org/search?q=apple_banana' }, + { id: '2', url: 'http://i-beam.org/search?q=apple_banana_cherry' }, + { id: '3', url: 'http://i-beam.org/request?q=apple' }, + { id: '4', url: 'http://i-beam.org/request?q=apple_banana' }, + { id: '5', url: 'http://i-beam.org/request?q=apple_banana_cherry' }, + ]; + const filtered = filters.filterByPathname(pages, 10); + expect(filtered).to.have.lengthOf(6); + }); + + it('filters by length of pathname', () => { + const pages = [ + { id: '0', url: 'http://i-beam.org/search?q=apple' }, + { id: '1', url: 'http://i-beam.org/search?q=apple_banana' }, + { id: '2', url: 'http://i-beam.org/search?q=apple_banana_cherry' }, + { id: '3', url: 'http://i-beam.net/search?q=apple' }, + { id: '4', url: 'http://i-beam.net/search?q=apple_banana' }, + { id: '5', url: 'http://i-beam.net/search?q=apple_banana_cherry' }, + ]; + const filtered = filters.filterByPathname(pages, 0); + expect(filtered).to.deep.equal([ + { id: '0', url: 'http://i-beam.org/search?q=apple' }, + { id: '3', url: 'http://i-beam.net/search?q=apple' }, + ]); + }); + }); + + describe('filterByOrigin', () => { + it('remains items less than minimam length', () => { + const pages = [ + { id: '0', url: 'http://i-beam.org/search?q=apple' }, + { id: '1', url: 'http://i-beam.org/search?q=apple_banana' }, + { id: '2', url: 'http://i-beam.org/search?q=apple_banana_cherry' }, + { id: '3', url: 'http://i-beam.org/request?q=apple' }, + { id: '4', url: 'http://i-beam.org/request?q=apple_banana' }, + { id: '5', url: 'http://i-beam.org/request?q=apple_banana_cherry' }, + ]; + const filtered = filters.filterByOrigin(pages, 10); + expect(filtered).to.have.lengthOf(6); + }); + + it('filters by length of pathname', () => { + const pages = [ + { id: '0', url: 'http://i-beam.org/search?q=apple' }, + { id: '1', url: 'http://i-beam.org/search?q=apple_banana' }, + { id: '2', url: 'http://i-beam.org/search?q=apple_banana_cherry' }, + { id: '3', url: 'http://i-beam.org/request?q=apple' }, + { id: '4', url: 'http://i-beam.org/request?q=apple_banana' }, + { id: '5', url: 'http://i-beam.org/request?q=apple_banana_cherry' }, + ]; + const filtered = filters.filterByOrigin(pages, 0); + expect(filtered).to.deep.equal([ + { id: '0', url: 'http://i-beam.org/search?q=apple' }, + ]); + }); + }); +}); |