From bf4e5dd78ffcef07ac6e2cf2588c26dfbf9c38ee Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sun, 31 Mar 2024 23:13:22 +0800 Subject: [PATCH] test: fix caces --- lib/middleware/cache.test.ts | 10 +++++----- lib/middleware/parameter.test.ts | 30 +++++++----------------------- lib/middleware/parameter.ts | 2 +- lib/setup.test.ts | 2 +- lib/utils/got.test.ts | 4 ++-- vitest.config.ts | 1 + 6 files changed, 17 insertions(+), 32 deletions(-) diff --git a/lib/middleware/cache.test.ts b/lib/middleware/cache.test.ts index 7edce9fc5..393d8153e 100644 --- a/lib/middleware/cache.test.ts +++ b/lib/middleware/cache.test.ts @@ -3,7 +3,7 @@ import Parser from 'rss-parser'; import wait from '@/utils/wait'; process.env.CACHE_EXPIRE = '1'; -process.env.CACHE_CONTENT_EXPIRE = '3'; +process.env.CACHE_CONTENT_EXPIRE = '2'; const parser = new Parser(); @@ -38,7 +38,7 @@ describe('cache', () => { expect(response3.headers).not.toHaveProperty('rsshub-cache-status'); const parsed3 = await parser.parseString(await response3.text()); - await wait(3 * 1000 + 100); + await wait(2 * 1000 + 100); const response4 = await app.request('/test/cache'); const parsed4 = await parser.parseString(await response4.text()); @@ -51,7 +51,7 @@ describe('cache', () => { await wait(1 * 1000 + 100); const response5 = await app.request('/test/refreshCache'); const parsed5 = await parser.parseString(await response5.text()); - await wait(2 * 1000 + 100); + await wait(1 * 1000 + 100); const response6 = await app.request('/test/refreshCache'); const parsed6 = await parser.parseString(await response6.text()); @@ -86,7 +86,7 @@ describe('cache', () => { expect(response3.headers).not.toHaveProperty('rsshub-cache-status'); const parsed3 = await parser.parseString(await response3.text()); - await wait(3 * 1000 + 100); + await wait(2 * 1000 + 100); const response4 = await app.request('/test/cache'); const parsed4 = await parser.parseString(await response4.text()); @@ -99,7 +99,7 @@ describe('cache', () => { await wait(1 * 1000 + 100); const response5 = await app.request('/test/refreshCache'); const parsed5 = await parser.parseString(await response5.text()); - await wait(2 * 1000 + 100); + await wait(1 * 1000 + 100); const response6 = await app.request('/test/refreshCache'); const parsed6 = await parser.parseString(await response6.text()); diff --git a/lib/middleware/parameter.test.ts b/lib/middleware/parameter.test.ts index 2a31436d3..6fb8788bb 100644 --- a/lib/middleware/parameter.test.ts +++ b/lib/middleware/parameter.test.ts @@ -1,8 +1,12 @@ import { describe, expect, it, vi } from 'vitest'; -import app from '@/app'; import Parser from 'rss-parser'; -import { config } from '@/config'; -import nock from 'nock'; + +process.env.OPENAI_API_KEY = 'sk-1234567890'; +process.env.OPENAI_API_ENDPOINT = 'https://api.openai.mock/v1'; + +vi.mock('@/utils/request-rewriter', () => ({ default: null })); +const { config } = await import('@/config'); +const { default: app } = await import('@/app'); const parser = new Parser(); @@ -424,26 +428,6 @@ describe('multi parameter', () => { describe('openai', () => { it(`chatgpt`, async () => { - vi.resetModules(); - - process.env.OPENAI_API_KEY = 'sk-1234567890'; - const app = (await import('@/app')).default; - const { config } = await import('@/config'); - nock(config.openai.endpoint) - .post('/chat/completions') - .reply(() => [ - 200, - { - choices: [ - { - message: { - content: 'Summary of the article.', - }, - }, - ], - }, - ]); - const responseWithGpt = await app.request('/test/gpt?chatgpt=true'); const responseNormal = await app.request('/test/gpt'); diff --git a/lib/middleware/parameter.ts b/lib/middleware/parameter.ts index 025f6b405..5985d4bd8 100644 --- a/lib/middleware/parameter.ts +++ b/lib/middleware/parameter.ts @@ -50,7 +50,7 @@ const summarizeArticle = async (articleText: string) => { }, }); - return response.data.choices[0].message.content; + return response.choices[0].message.content; }; const getAuthorString = (item) => { diff --git a/lib/setup.test.ts b/lib/setup.test.ts index 90170e0c7..192d9fe82 100644 --- a/lib/setup.test.ts +++ b/lib/setup.test.ts @@ -3,7 +3,7 @@ import { setupServer } from 'msw/node'; import { http, HttpResponse } from 'msw'; const server = setupServer( - http.post(`https://api.openai.com/v1/chat/completions`, () => + http.post(`https://api.openai.mock/v1/chat/completions`, () => HttpResponse.json({ choices: [ { diff --git a/lib/utils/got.test.ts b/lib/utils/got.test.ts index 5b65725fc..b09ff977e 100644 --- a/lib/utils/got.test.ts +++ b/lib/utils/got.test.ts @@ -5,8 +5,8 @@ import { config } from '@/config'; describe('got', () => { it('headers', async () => { - const { data } = await got('http://rsshub.test/ua'); - expect(data.ua).toBe(config.ua); + const { data } = await got('http://rsshub.test/headers'); + expect(data['user-agent']).toBe(config.ua); }); it('retry', async () => { diff --git a/vitest.config.ts b/vitest.config.ts index bc2deb5f5..883c92d55 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -11,5 +11,6 @@ export default defineConfig({ }, testTimeout: 10000, setupFiles: ['./lib/setup.test.ts'], + exclude: ['./lib/setup.test.ts'], }, });