aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2020-03-31 21:40:03 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2020-04-09 10:38:51 +0900
commit34a569d73638dd10162050047d23cd04d286f4bc (patch)
treeb37b15f9ee3888015c633fc445b6e6b412316ca4 /test
parent1656d52d2cefb3846d968c6117484e6aefe7dabe (diff)
Prefetch completion items and store them to cache
Diffstat (limited to 'test')
-rw-r--r--test/background/completion/impl/PrefetchAndCache.test.ts74
-rw-r--r--test/background/completion/impl/filters.test.ts30
2 files changed, 76 insertions, 28 deletions
diff --git a/test/background/completion/impl/PrefetchAndCache.test.ts b/test/background/completion/impl/PrefetchAndCache.test.ts
new file mode 100644
index 0000000..23e3879
--- /dev/null
+++ b/test/background/completion/impl/PrefetchAndCache.test.ts
@@ -0,0 +1,74 @@
+import PrefetchAndCache, {shortKey} from "../../../../src/background/completion/impl/PrefetchAndCache";
+import { expect } from 'chai';
+
+class MockRepository {
+ public history: string[] = [];
+
+ constructor(
+ private items: string[],
+ ) {
+ }
+
+ get(query: string): Promise<string[]> {
+ this.history.push(query);
+ if (query.length === 0) {
+ return Promise.resolve(this.items);
+ } else {
+ return Promise.resolve(this.items.filter(item => item.includes(query)));
+ }
+ }
+}
+const filter = (items: string[], query: string) => query.length === 0 ? items : items.filter(item => item.includes(query));
+
+describe('shortKey', () => {
+ it('returns query excluding the last word', () => {
+ const query = "hello\t world good bye";
+ const shorten = shortKey(query);
+ expect(shorten).to.equal("hello world good")
+ });
+
+ it('returns half-length of the query', () => {
+ const query = "the-query-with-super-long-word";
+ const shorten = shortKey(query);
+ expect(shorten).to.equal("the-query-with-")
+ });
+
+ it('returns shorten URL', () => {
+ let query = "https://example.com/path/to/resource?q=hello";
+ let shorten = shortKey(query);
+ expect(shorten).to.equal("https://example.com/path/to/");
+
+ query = "https://example.com/path/to/resource/#id1";
+ shorten = shortKey(query);
+ expect(shorten).to.equal("https://example.com/path/to/");
+
+ query = "https://www.google.c";
+ shorten = shortKey(query);
+ expect(shorten).to.equal("https://ww");
+ })
+});
+
+describe('PrefetchAndCache', () => {
+ describe('get', () => {
+ it('returns cached request', async() => {
+ const repo = new MockRepository(["apple", "apple pie", "apple juice", "banana", "banana pudding", "cherry"]);
+ const sut = new PrefetchAndCache(repo.get.bind(repo), filter);
+
+ expect(await sut.get("apple pie")).deep.equal(["apple pie"]);
+ expect(await sut.get("apple ")).deep.equal(["apple", "apple pie", "apple juice"]);
+ expect(await sut.get("apple")).deep.equal(["apple", "apple pie", "apple juice"]);
+ expect(await sut.get("appl")).deep.equal(["apple", "apple pie", "apple juice"]);
+ expect(repo.history).to.deep.equal(["apple", 'ap']);
+
+ expect(await sut.get("banana")).deep.equal(["banana", "banana pudding"]);
+ expect(repo.history).to.deep.equal(["apple", "ap", "ban"]);
+ expect(await sut.get("banana p")).deep.equal(["banana pudding"]);
+ expect(repo.history).to.deep.equal(["apple", "ap", "ban", "banana"]);
+ expect(await sut.get("ba")).deep.equal(["banana", "banana pudding"]);
+ expect(repo.history).to.deep.equal(["apple", "ap", "ban", "banana", "b"]);
+
+ expect(await sut.get("")).to.have.lengthOf(6);
+ expect(repo.history).to.deep.equal(["apple", "ap", "ban", "banana", "b", ""]);
+ });
+ });
+}); \ No newline at end of file
diff --git a/test/background/completion/impl/filters.test.ts b/test/background/completion/impl/filters.test.ts
index 2b15a9b..a181f60 100644
--- a/test/background/completion/impl/filters.test.ts
+++ b/test/background/completion/impl/filters.test.ts
@@ -52,19 +52,6 @@ describe('background/usecases/filters', () => {
})
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' },
@@ -74,7 +61,7 @@ describe('background/usecases/filters', () => {
{ 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);
+ const filtered = filters.filterByPathname(pages);
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' },
@@ -83,19 +70,6 @@ describe('background/usecases/filters', () => {
});
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' },
@@ -105,7 +79,7 @@ describe('background/usecases/filters', () => {
{ 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);
+ const filtered = filters.filterByOrigin(pages);
expect(filtered).to.deep.equal([
{ id: '0', url: 'http://i-beam.org/search?q=apple' },
]);