Skip to content

Commit

Permalink
Merge pull request #42 from memochou1993/refactor/tests
Browse files Browse the repository at this point in the history
Refactor tests
  • Loading branch information
memochou1993 authored Dec 17, 2022
2 parents e629836 + ffcdcb1 commit 3d3116f
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 75 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,14 @@ APP_ENV=local
APP_DEBUG=true
APP_PORT=3000
OPENAI_API_KEY=<your_openai_api_key>
# VERCEL_GIT_REPO_SLUG=gpt-ai-assistant
# VERCEL_ACCESS_TOKEN=<your_vercel_access_token>
# VERCEL_DEPLOY_HOOK_URL=<your_vercel_deploy_hook_url>
LINE_CHANNEL_ACCESS_TOKEN=<your_line_channel_access_token>
LINE_CHANNEL_SECRET=<your_line_channel_secret>
# OPENAI_API_KEY=<your_openai_api_key>
# LINE_CHANNEL_ACCESS_TOKEN=<your_line_channel_access_token>
# LINE_CHANNEL_SECRET=<your_line_channel_secret>
```

### 測試
Expand All @@ -184,7 +188,7 @@ npm run test

AI: 嗨!我可以怎麼幫助你?
Human: 嗨?
AI: 你好!有什麼可以幫助你的嗎?
AI:

at Assistant.info [as debug] (assistant/assistant.js:55:28)

Expand Down Expand Up @@ -276,7 +280,7 @@ curl --request POST \

AI: 嗨!我可以怎麼幫助你?
Human: 我是誰?
AI: 你是一個人,一個有意識的生物!
AI:
```

## 相關專案
Expand Down
57 changes: 21 additions & 36 deletions tests/ai.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);
18 changes: 6 additions & 12 deletions tests/default.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
37 changes: 37 additions & 0 deletions tests/image.test.js
Original file line number Diff line number Diff line change
@@ -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);
18 changes: 6 additions & 12 deletions tests/version.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
14 changes: 8 additions & 6 deletions utils/generate-completion.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import config from '../config/index.js';
import { createCompletion, FINISH_REASON_STOP, PARTICIPANT_AI } from '../services/openai.js';

class Completion {
Expand All @@ -13,16 +14,17 @@ class Completion {
* @param {string} param.text
* @returns {Promise<Completion>}
*/
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;
2 changes: 2 additions & 0 deletions utils/generate-image.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import config from '../config/index.js';
import { createImage } from '../services/openai.js';

class Image {
Expand All @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions utils/reply-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

1 comment on commit 3d3116f

@vercel
Copy link

@vercel vercel bot commented on 3d3116f Dec 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.