Skip to content

Commit

Permalink
fix(server): unsplash path mapping (#9159)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Dec 16, 2024
1 parent 9a66c97 commit 5cc4011
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/backend/server/src/plugins/copilot/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,8 @@ export class CopilotController {
}
}

@CallMetric('ai', 'unsplash')
@Get('/unsplash/photos')
@CallMetric('ai', 'unsplash')
async unsplashPhotos(
@Req() req: Request,
@Res() res: Response,
Expand Down
9 changes: 9 additions & 0 deletions packages/backend/server/tests/copilot.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
MockCopilotTestProvider,
sse2array,
textToEventStream,
unsplashSearch,
} from './utils/copilot';

const test = ava as TestFn<{
Expand All @@ -62,6 +63,7 @@ test.beforeEach(async t => {
fal: {
apiKey: '1',
},
unsplashKey: process.env.UNSPLASH_ACCESS_KEY || '1',
},
},
}),
Expand Down Expand Up @@ -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');
});
12 changes: 12 additions & 0 deletions packages/backend/server/tests/utils/copilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {}
) {
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 =>
Expand Down

0 comments on commit 5cc4011

Please sign in to comment.