diff --git a/examples/self-test-loop/package.json b/examples/self-test-loop/package.json index 5b394c9d1..cc7102af5 100644 --- a/examples/self-test-loop/package.json +++ b/examples/self-test-loop/package.json @@ -133,6 +133,7 @@ }, "system": {}, "screen": {}, + "search": {}, "keyboard": {}, "whisper": {}, "vault": {}, diff --git a/examples/self-test-loop/src/config/index.ts b/examples/self-test-loop/src/config/index.ts index eecd3dc09..89742df00 100644 --- a/examples/self-test-loop/src/config/index.ts +++ b/examples/self-test-loop/src/config/index.ts @@ -9,6 +9,7 @@ import { filesystemTestGroup } from './filesystem-group'; import { keyboardTestGroup } from './keyboard-group'; import { networkTestGroup } from './network-group'; import { processTestGroup } from './process-group'; +import { searchTestGroup } from './search-group'; import { screenTestGroup } from './screen-group'; import { systemTestGroup } from './system-group'; import { uiTestGroup } from './ui-group'; @@ -29,6 +30,7 @@ export const testConfig: { [key: string]: TestGroup } = { keyboard: keyboardTestGroup(), network: networkTestGroup(), process: processTestGroup(), + search: searchTestGroup(), screen: screenTestGroup(), system: systemTestGroup(), ui: uiTestGroup(), diff --git a/examples/self-test-loop/src/config/search-group.ts b/examples/self-test-loop/src/config/search-group.ts new file mode 100644 index 000000000..f8d5cf884 --- /dev/null +++ b/examples/self-test-loop/src/config/search-group.ts @@ -0,0 +1,37 @@ +import TestGroup from '../testingFixtures/testGroup'; +import { LoopTest } from '../testingFixtures/loopTest'; +import * as searchTests from '../tests/search'; + +export const searchTestGroup = (): TestGroup => + new TestGroup('Search Aptitude', [ + new LoopTest( + 'Search Aptitude - Create Index', + searchTests.testSearchCreateIndex, + 10000, + 'test should pass automatically.', + ), + new LoopTest( + 'Search Aptitude - Index Search', + searchTests.testSearchCreateIndexSearch, + 10000, + 'Can you see search result?', + ), + new LoopTest( + 'Search Aptitude - Index Query String Search', + searchTests.testSearchCreateIndexQueryStringSearch, + 10000, + 'Can you see search result?', + ), + new LoopTest( + 'Search Aptitude - Search Index Exists', + searchTests.testSearchIndexExsit, + 10000, + 'Test should pass if index exist', + ), + /* new LoopTest( + 'Search Aptitude - Search Index Delete', + searchTests.testSearchIndexDelete, + 10000, + 'Test should pass if index has been deleted', + ), */ + ]); diff --git a/examples/self-test-loop/src/tests/search/index.ts b/examples/self-test-loop/src/tests/search/index.ts new file mode 100644 index 000000000..3da634700 --- /dev/null +++ b/examples/self-test-loop/src/tests/search/index.ts @@ -0,0 +1,249 @@ +/* eslint-disable no-async-promise-executor */ +import { search, whisper, network, document } from '@oliveai/ldk'; +import { Worksheet, Row } from '@oliveai/ldk/dist/document/types'; +import { resolveRejectButtons } from '../whisper/utils'; + +function workSheet2Document(worksheet: Worksheet) { + const { name, rows } = worksheet; + const data = JSON.stringify( + rows.map((row: Row) => ({ + testName: row.cells[0].value, + number: row.cells[1].value, + name: row.cells[2].value, + })), + ); + + return { + name, + data, + }; +} + +export const testSearchCreateIndex = (): Promise => + new Promise(async (resolve, reject) => { + const documents: search.Document[] = [ + { + name: 'test', + data: JSON.stringify([]), + fields: [], + }, + ]; + await whisper.create({ + label: 'Create index test', + onClose: () => undefined, + components: [ + { + type: whisper.WhisperComponentType.Markdown, + body: 'testing create index', + }, + ], + }); + const index = await search.createIndex('Create Index', documents, {}); + if (index != null) { + resolve(true); + } else { + reject('failed to create index'); + } + }); + +export const testSearchCreateIndexSearch = (): Promise => + new Promise(async (resolve, reject) => { + let request = await network.httpRequest({ + url: 'https://github.com/open-olive/loop-development-kit/raw/develop/examples/self-test-loop/static/test.xlsx', + method: 'GET', + }); + let branch = 'develop'; + + // Above URL is used after this feature is finished and merged in + // Before PR is merged and branch is deleted, use the feature branch + // Can delete this block after merge + if (request.statusCode === 404) { + request = await network.httpRequest({ + url: 'https://github.com/open-olive/loop-development-kit/raw/HELPS-4731-search-aptitude-selftest/examples/self-test-loop/static/test.xlsx', + method: 'GET', + }); + branch = 'HELPS-4731-search-aptitude-selftest'; + } + const workbook = await document.xlsxDecode(request.body); + const documents = workbook.worksheets.map(workSheet2Document); + await whisper.create({ + label: 'Index search test', + onClose: () => undefined, + components: [ + { + type: whisper.WhisperComponentType.Markdown, + body: 'Document Contents: ', + }, + { + type: whisper.WhisperComponentType.Markdown, + body: JSON.stringify(documents), + }, + ], + }); + const index = await search.createIndex('Test Index', documents, {}); + try { + //console.log(JSON.stringify(index)); + if (index != null) { + const searchResult = await index.search('Dev'); + //console.log(JSON.stringify(searchResult)); + const body = + 'search result for "Dev": data:' + + JSON.stringify(searchResult.data) + + ', total:' + + searchResult.total; + await whisper.create({ + label: 'Search Result for "Dev" ', + onClose: () => undefined, + components: [ + { + type: whisper.WhisperComponentType.Markdown, + body: body, + }, + resolveRejectButtons(resolve, reject), + ], + }); + } else { + reject('failed to create index'); + } + } catch (err) { + reject(err); + } + }); + +export const testSearchCreateIndexQueryStringSearch = (): Promise => + new Promise(async (resolve, reject) => { + let request = await network.httpRequest({ + url: 'https://github.com/open-olive/loop-development-kit/raw/develop/examples/self-test-loop/static/test.xlsx', + method: 'GET', + }); + let branch = 'develop'; + + // Above URL is used after this feature is finished and merged in + // Before PR is merged and branch is deleted, use the feature branch + // Can delete this block after merge + if (request.statusCode === 404) { + request = await network.httpRequest({ + url: 'https://github.com/open-olive/loop-development-kit/raw/HELPS-4731-search-aptitude-selftest/examples/self-test-loop/static/test.xlsx', + method: 'GET', + }); + branch = 'HELPS-4731-search-aptitude-selftest'; + } + const workbook = await document.xlsxDecode(request.body); + const documents = workbook.worksheets.map(workSheet2Document); + + await whisper.create({ + label: 'Index query string search test', + onClose: () => undefined, + components: [ + { + type: whisper.WhisperComponentType.Markdown, + body: 'Document Contents: ', + }, + { + type: whisper.WhisperComponentType.Markdown, + body: JSON.stringify(documents), + }, + ], + }); + + const index = await search.createIndex('Test Index', documents, {}); + try { + if (index != null) { + const searchResult = await index.search('Olive'); + + const body = + 'search result for "Olive": data:' + + JSON.stringify(searchResult.data) + + ', total:' + + searchResult.total; + await whisper.create({ + label: 'Query String Search Result for "Olive" ', + onClose: () => undefined, + components: [ + { + type: whisper.WhisperComponentType.Markdown, + body: body, + }, + resolveRejectButtons(resolve, reject), + ], + }); + } else { + reject('failed to create index'); + } + } catch (err) { + reject(err); + } + }); + +export const testSearchIndexExsit = (): Promise => + new Promise(async (resolve, reject) => { + const documents: search.Document[] = [ + { + name: 'test', + data: JSON.stringify([]), + fields: [], + }, + ]; + await whisper.create({ + label: 'Search exist index test', + onClose: () => undefined, + components: [ + { + type: whisper.WhisperComponentType.Markdown, + body: 'testing index exist', + }, + ], + }); + + const index = await search.createIndex('Exist Index', documents, {}); + + const indexExists = await search.exists('Exist Index'); + await whisper.create({ + label: 'Search Index Exists ', + onClose: () => undefined, + components: [ + { + type: whisper.WhisperComponentType.Markdown, + body: 'Search Index Exists: ' + indexExists.toString(), + }, + resolveRejectButtons(resolve, reject), + ], + }); + }); + +//TODO: track what causes delete functon doesn't work as expected. +/* export const testSearchIndexDelete = (): Promise => + new Promise(async (resolve, reject) => { + const documents: search.Document[] = [ + { + name: 'test', + data: JSON.stringify([]), + fields: [], + }, + ]; + await whisper.create({ + label: 'Search Delete index test', + onClose: () => undefined, + components: [ + { + type: whisper.WhisperComponentType.Markdown, + body: 'testing delete index', + }, + ], + }); + + const index = await search.createIndex('Delete Index', documents, {}); + await index.delete(); + const indexExists = await search.exists('Delete Index'); + await whisper.create({ + label: 'Search Index Delete ', + onClose: () => undefined, + components: [ + { + type: whisper.WhisperComponentType.Markdown, + body: 'Search Index Exists(should be false): ' + indexExists.toString(), + }, + resolveRejectButtons(resolve, reject), + ], + }); + }); */ diff --git a/examples/self-test-loop/static/test.xlsx b/examples/self-test-loop/static/test.xlsx new file mode 100644 index 000000000..052741e31 Binary files /dev/null and b/examples/self-test-loop/static/test.xlsx differ