Commit 3d157f3 1 parent b59a0b1 commit 3d157f3 Copy full SHA for 3d157f3
File tree 6 files changed +94
-24
lines changed
6 files changed +94
-24
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,10 @@ clean:
14
14
test :
15
15
yarn test
16
16
17
+ .PHONY : test_watch
18
+ test_watch :
19
+ yarn test:watch
20
+
17
21
.PHONY : test_coverage
18
22
test_coverage :
19
23
yarn test:coverage
Original file line number Diff line number Diff line change 1
1
import { Button , createFrames } from 'frames.js/next' ;
2
- import { generateImageUrl } from '../../../service/stampService' ;
2
+ import { StampService } from '../../../service/stampService' ;
3
3
4
4
const frames = createFrames ( {
5
5
basePath : '/api/frames/createStamp' ,
6
6
} ) ;
7
7
8
8
const handleRequest = frames ( async ( ctx ) => {
9
- const image_url = await generateImageUrl ( ctx . message ?. inputText ) ;
9
+ const template =
10
+ 'スタイルはキティちゃんのようにシンプルで愛らしいキャラクターのLINEスタンプを生成してください。ポーズはセリフに合わせて適切に変化させてください。セリフ:' ;
11
+ const service = new StampService ( template ) ;
12
+ const image_url = await service . generateImageUrl ( ctx . message ?. inputText ) ;
10
13
11
14
return {
12
15
image : image_url ,
Original file line number Diff line number Diff line change 1
1
import OpenAI from 'openai' ;
2
2
3
- const openai = new OpenAI ( ) ;
3
+ export class StampRepository {
4
+ constructor ( private openai = new OpenAI ( ) ) { }
4
5
5
- export async function generateImage ( prompt : string ) {
6
- try {
7
- const imageResponse = await openai . images . generate ( {
8
- model : 'dall-e-3' ,
9
- prompt : prompt ,
10
- n : 1 ,
11
- size : '1024x1024' ,
12
- } ) ;
13
- const imageUrl = imageResponse . data [ 0 ] ?. url || '' ;
14
- return imageUrl ;
15
- } catch ( error ) {
16
- console . error ( 'Error generating image:' , error ) ;
17
- throw error ;
6
+ async generateImage ( prompt : string ) : Promise < string > {
7
+ try {
8
+ const imageResponse = await this . openai . images . generate ( {
9
+ model : 'dall-e-3' ,
10
+ prompt : prompt ,
11
+ n : 1 ,
12
+ size : '1024x1024' ,
13
+ } ) ;
14
+ const imageUrl = imageResponse . data [ 0 ] ?. url || '' ;
15
+ return imageUrl ;
16
+ } catch ( error ) {
17
+ console . error ( 'Error generating image:' , error ) ;
18
+ throw new Error ( 'Image generation failed' ) ;
19
+ }
18
20
}
19
21
}
Original file line number Diff line number Diff line change 1
- import { generateImage } from '../repository/stampRepository' ;
1
+ // stampService.js
2
+ import { StampRepository } from '../repository/stampRepository' ;
2
3
3
- export async function generateImageUrl ( input : string ) : Promise < string > {
4
- const template =
5
- 'スタイルはキティちゃんのようにシンプルで愛らしいキャラクターのLINEスタンプを生成してください。ポーズはセリフに合わせて適切に変化させてください。セリフ:' ;
4
+ export class StampService {
5
+ private template : string ;
6
6
7
- const prompt = template + input ;
7
+ constructor (
8
+ template : string ,
9
+ private stampRepository : StampRepository = new StampRepository ( )
10
+ ) {
11
+ this . template = template ;
12
+ }
8
13
9
- const imageUrl = await generateImage ( prompt ) ;
10
-
11
- return imageUrl ;
14
+ async generateImageUrl ( input : string ) : Promise < string > {
15
+ const prompt = this . template + input ;
16
+ const imageUrl = await this . stampRepository . generateImage ( prompt ) ;
17
+ return imageUrl ;
18
+ }
12
19
}
Original file line number Diff line number Diff line change
1
+ // import OpenAI from 'openai';
2
+ // import { StampRepository } from '../app/repository/stampRepository';
3
+
4
+ // jest.mock('openai', () => {
5
+ // return {
6
+ // OpenAI: jest.fn().mockImplementation(() => {
7
+ // return {
8
+ // images: {
9
+ // generate: jest.fn().mockResolvedValue({
10
+ // data: [
11
+ // {
12
+ // url: 'mockImageUrl',
13
+ // },
14
+ // ],
15
+ // }),
16
+ // },
17
+ // };
18
+ // }),
19
+ // };
20
+ // });
21
+
22
+ // describe('StampRepository', () => {
23
+ // let stampRepository: StampRepository;
24
+
25
+ // beforeEach(() => {
26
+ // stampRepository = new StampRepository();
27
+ // });
28
+
29
+ // it('should return an image URL when generateImage is called', async () => {
30
+ // const prompt = 'test prompt';
31
+ // const imageUrl = await stampRepository.generateImage(prompt);
32
+
33
+ // // 正しい画像URLが返されることを確認
34
+ // expect(imageUrl).toBe('mockImageUrl');
35
+ // // OpenAI.images.generateが適切な引数で呼び出されたことを確認
36
+ // expect(OpenAI.prototype.images.generate).toHaveBeenCalledWith({
37
+ // model: 'dall-e-3',
38
+ // prompt: prompt,
39
+ // n: 1,
40
+ // size: '1024x1024',
41
+ // });
42
+ // });
43
+
44
+ // it('should throw an error if the OpenAI API call fails', async () => {
45
+ // // OpenAI.images.generate メソッドを失敗させるようにモックを設定
46
+ // OpenAI.prototype.images.generate.mockRejectedValue(
47
+ // new Error('API call failed')
48
+ // );
49
+
50
+ // await expect(stampRepository.generateImage('test prompt')).rejects.toThrow(
51
+ // 'Image generation failed'
52
+ // );
53
+ // });
54
+ // });
You can’t perform that action at this time.
0 commit comments