From 5cc40114fc2f97602a84a33349734611fbde94f8 Mon Sep 17 00:00:00 2001 From: darkskygit Date: Mon, 16 Dec 2024 09:35:18 +0000 Subject: [PATCH] fix(server): unsplash path mapping (#9159) --- .../backend/server/src/plugins/copilot/controller.ts | 2 +- packages/backend/server/tests/copilot.e2e.ts | 9 +++++++++ packages/backend/server/tests/utils/copilot.ts | 12 ++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/backend/server/src/plugins/copilot/controller.ts b/packages/backend/server/src/plugins/copilot/controller.ts index c2ecb054b64e1..fba8f0dee1eeb 100644 --- a/packages/backend/server/src/plugins/copilot/controller.ts +++ b/packages/backend/server/src/plugins/copilot/controller.ts @@ -466,8 +466,8 @@ export class CopilotController { } } - @CallMetric('ai', 'unsplash') @Get('/unsplash/photos') + @CallMetric('ai', 'unsplash') async unsplashPhotos( @Req() req: Request, @Res() res: Response, diff --git a/packages/backend/server/tests/copilot.e2e.ts b/packages/backend/server/tests/copilot.e2e.ts index ec1ffc834b264..1cb8b95f757d1 100644 --- a/packages/backend/server/tests/copilot.e2e.ts +++ b/packages/backend/server/tests/copilot.e2e.ts @@ -40,6 +40,7 @@ import { MockCopilotTestProvider, sse2array, textToEventStream, + unsplashSearch, } from './utils/copilot'; const test = ava as TestFn<{ @@ -62,6 +63,7 @@ test.beforeEach(async t => { fal: { apiKey: '1', }, + unsplashKey: process.env.UNSPLASH_ACCESS_KEY || '1', }, }, }), @@ -645,3 +647,10 @@ test('should reject request that user have not permission', async t => { ); } }); + +test('should be able to search image from unsplash', async t => { + const { app } = t.context; + + const resp = await unsplashSearch(app, token); + t.not(resp.status, 404, 'route should be exists'); +}); diff --git a/packages/backend/server/tests/utils/copilot.ts b/packages/backend/server/tests/utils/copilot.ts index 5bd0088754be5..07bce549a2535 100644 --- a/packages/backend/server/tests/utils/copilot.ts +++ b/packages/backend/server/tests/utils/copilot.ts @@ -286,6 +286,18 @@ export async function chatWithImages( return chatWithText(app, userToken, sessionId, messageId, '/images'); } +export async function unsplashSearch( + app: INestApplication, + userToken: string, + params: Record = {} +) { + const query = new URLSearchParams(params); + const res = await request(app.getHttpServer()) + .get(`/api/copilot/unsplash/photos?${query}`) + .auth(userToken, { type: 'bearer' }); + return res; +} + export function sse2array(eventSource: string) { const blocks = eventSource.replace(/^\n(.*?)\n$/, '$1').split(/\n\n+/); return blocks.map(block =>