From ffcdcb1db0ea090d3cd9808469912fbbd36b5497 Mon Sep 17 00:00:00 2001 From: Memo Chou Date: Sun, 18 Dec 2022 03:21:54 +0800 Subject: [PATCH] Refactor tests --- README.md | 14 +++++---- tests/ai.test.js | 57 +++++++++++++----------------------- tests/default.test.js | 18 ++++-------- tests/image.test.js | 37 +++++++++++++++++++++++ tests/version.test.js | 18 ++++-------- utils/generate-completion.js | 14 +++++---- utils/generate-image.js | 2 ++ utils/reply-message.js | 6 ++-- 8 files changed, 91 insertions(+), 75 deletions(-) create mode 100644 tests/image.test.js diff --git a/README.md b/README.md index 85691260..412d95b6 100644 --- a/README.md +++ b/README.md @@ -159,10 +159,14 @@ APP_ENV=local APP_DEBUG=true APP_PORT=3000 -OPENAI_API_KEY= +# VERCEL_GIT_REPO_SLUG=gpt-ai-assistant +# VERCEL_ACCESS_TOKEN= +# VERCEL_DEPLOY_HOOK_URL= -LINE_CHANNEL_ACCESS_TOKEN= -LINE_CHANNEL_SECRET= +# OPENAI_API_KEY= + +# LINE_CHANNEL_ACCESS_TOKEN= +# LINE_CHANNEL_SECRET= ``` ### 測試 @@ -184,7 +188,7 @@ npm run test AI: 嗨!我可以怎麼幫助你? Human: 嗨? - AI: 你好!有什麼可以幫助你的嗎? + AI: at Assistant.info [as debug] (assistant/assistant.js:55:28) @@ -276,7 +280,7 @@ curl --request POST \ AI: 嗨!我可以怎麼幫助你? Human: 我是誰? -AI: 你是一個人,一個有意識的生物! +AI: ``` ## 相關專案 diff --git a/tests/ai.test.js b/tests/ai.test.js index 3379cb30..fb35f771 100644 --- a/tests/ai.test.js +++ b/tests/ai.test.js @@ -27,18 +27,13 @@ test('COMMAND_AI', async () => { } catch (err) { console.error(err); } - expect(getSession(USER_ID).lines.length).toEqual(3); // ['AI', 'Human', 'AI'] - expect(results.length).toEqual(2); // ['Command', 'AI'] - expect(results).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - messages: expect.arrayContaining([ - expect.objectContaining({ - text: 'off', - }), - ]), - }), - ]), + expect(getSession(USER_ID).lines.length).toEqual(3); + const replies = results.map(({ messages }) => messages.map(({ text }) => text)); + expect(replies).toEqual( + [ + ['off'], + [''], + ], ); }, TIMEOUT); @@ -55,18 +50,14 @@ test('COMMAND_AI_AUTO_REPLY_ON', async () => { } catch (err) { console.error(err); } - expect(getSession(USER_ID).lines.length).toEqual(3); // ['AI', 'Human', 'AI'] - expect(results.length).toEqual(3); // ['Command', 'Command', 'AI'] - expect(results).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - messages: expect.arrayContaining([ - expect.objectContaining({ - text: 'off', - }), - ]), - }), - ]), + expect(getSession(USER_ID).lines.length).toEqual(3); + const replies = results.map(({ messages }) => messages.map(({ text }) => text)); + expect(replies).toEqual( + [ + ['off'], + ['on'], + [''], + ], ); }, TIMEOUT); @@ -81,17 +72,11 @@ test('COMMAND_AI_AUTO_REPLY_OFF', async () => { } catch (err) { console.error(err); } - expect(getSession(USER_ID).lines.length).toEqual(1); // ['AI'] - expect(results.length).toEqual(1); // ['Command'] - expect(results).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - messages: expect.arrayContaining([ - expect.objectContaining({ - text: 'off', - }), - ]), - }), - ]), + expect(getSession(USER_ID).lines.length).toEqual(1); + const replies = results.map(({ messages }) => messages.map(({ text }) => text)); + expect(replies).toEqual( + [ + ['off'], + ], ); }, TIMEOUT); diff --git a/tests/default.test.js b/tests/default.test.js index 386ca65f..b38a8139 100644 --- a/tests/default.test.js +++ b/tests/default.test.js @@ -26,18 +26,12 @@ test('DEFAULT', async () => { } catch (err) { console.error(err); } - expect(getSession(USER_ID).lines.length).toEqual(3); // ['AI', 'Human', 'AI'] - expect(results.length).toEqual(1); // ['Command', 'AI'] - expect(results).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - messages: expect.arrayContaining([ - expect.objectContaining({ - text: expect.any(String), - }), - ]), - }), - ]), + expect(getSession(USER_ID).lines.length).toEqual(3); + const replies = results.map(({ messages }) => messages.map(({ text }) => text)); + expect(replies).toEqual( + [ + [''], + ], ); if (config.APP_DEBUG) printSessions(); }, TIMEOUT); diff --git a/tests/image.test.js b/tests/image.test.js new file mode 100644 index 00000000..551155af --- /dev/null +++ b/tests/image.test.js @@ -0,0 +1,37 @@ +import { + afterEach, + beforeEach, expect, test, +} from '@jest/globals'; +import { + settings, handleEvents, getSession, removeSession, +} from '../app/index.js'; +import storage from '../storage/index.js'; +import { createEvents, TIMEOUT, USER_ID } from './utils.js'; + +beforeEach(() => { + storage.initialize(settings); +}); + +afterEach(() => { + removeSession(USER_ID); +}); + +test('COMMAND_IMAGE', async () => { + const events = createEvents([ + 'image 動物', + ]); + let results; + try { + results = await handleEvents(events); + } catch (err) { + console.error(err); + } + expect(getSession(USER_ID).lines.length).toEqual(1); + const replies = results.map(({ messages }) => messages + .map(({ originalContentUrl }) => originalContentUrl)); + expect(replies).toEqual( + [ + [''], + ], + ); +}, TIMEOUT); diff --git a/tests/version.test.js b/tests/version.test.js index 42fe9ea1..1e6a6495 100644 --- a/tests/version.test.js +++ b/tests/version.test.js @@ -28,17 +28,11 @@ test('COMMAND_VERSION', async () => { console.error(err); } const version = getVersion(); - expect(getSession(USER_ID).lines.length).toEqual(1); // ['AI'] - expect(results.length).toEqual(1); // ['Command'] - expect(results).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - messages: expect.arrayContaining([ - expect.objectContaining({ - text: version, - }), - ]), - }), - ]), + expect(getSession(USER_ID).lines.length).toEqual(1); + const replies = results.map(({ messages }) => messages.map(({ text }) => text)); + expect(replies).toEqual( + [ + [version], + ], ); }, TIMEOUT); diff --git a/utils/generate-completion.js b/utils/generate-completion.js index d346ac26..571e6baa 100644 --- a/utils/generate-completion.js +++ b/utils/generate-completion.js @@ -1,3 +1,4 @@ +import config from '../config/index.js'; import { createCompletion, FINISH_REASON_STOP, PARTICIPANT_AI } from '../services/openai.js'; class Completion { @@ -13,16 +14,17 @@ class Completion { * @param {string} param.text * @returns {Promise} */ -const generateComplete = async ({ +const generateCompletion = async ({ prompt, text = '', }) => { + const completion = new Completion({ prompt, text }); + if (config.APP_ENV !== 'production') return completion; const { data } = await createCompletion({ prompt }); const [choice] = data.choices; - prompt += choice.text.trim(); - text += choice.text.replace(PARTICIPANT_AI, '').replace(':', '').replace(':', '').trim(); - const completion = new Completion({ prompt, text }); - return choice.finish_reason === FINISH_REASON_STOP ? completion : generateComplete(completion); + completion.prompt += choice.text.trim(); + completion.text += choice.text.replace(PARTICIPANT_AI, '').replace(':', '').replace(':', '').trim(); + return choice.finish_reason === FINISH_REASON_STOP ? completion : generateCompletion(completion); }; -export default generateComplete; +export default generateCompletion; diff --git a/utils/generate-image.js b/utils/generate-image.js index c07973ee..aef14852 100644 --- a/utils/generate-image.js +++ b/utils/generate-image.js @@ -1,3 +1,4 @@ +import config from '../config/index.js'; import { createImage } from '../services/openai.js'; class Image { @@ -14,6 +15,7 @@ class Image { const generateImage = async ({ prompt, }) => { + if (config.APP_ENV !== 'production') return new Image({ url: '' }); const { data } = await createImage({ prompt }); const [image] = data.data; return new Image(image); diff --git a/utils/reply-message.js b/utils/reply-message.js index dcb6da11..43af62b9 100644 --- a/utils/reply-message.js +++ b/utils/reply-message.js @@ -5,10 +5,8 @@ const replyMessage = ({ replyToken, messages, }) => { - if (config.APP_ENV === 'production') { - return reply({ replyToken, messages }); - } - return { replyToken, messages }; + if (config.APP_ENV !== 'production') return { replyToken, messages }; + return reply({ replyToken, messages }); }; export default replyMessage;