Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🔨 chore: improve test workflow (#8876)

Arvin Xud07e2acae89a
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 783a4435e82..a0e92386005 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -2,8 +2,82 @@ name: Test CI
on: [push, pull_request]
+permissions:
+ contents: read
+
jobs:
- test:
+ # Package tests - using each package's own test script
+ test-packages:
+ runs-on: ubuntu-latest
+ strategy:
+ matrix:
+ package: [file-loaders, prompts, model-runtime, web-crawler, electron-server-ipc]
+
+ name: Test package ${{ matrix.package }}
+
+ steps:
+ - uses: actions/checkout@v5
+
+ - name: Setup Node.js
+ uses: actions/setup-node@v4
+ with:
+ node-version: 22
+
+ - uses: pnpm/action-setup@v4
+
+ - name: Install bun
+ uses: oven-sh/setup-bun@v1
+ with:
+ bun-version: ${{ secrets.BUN_VERSION }}
+
+ - name: Install deps
+ run: bun i
+
+ - name: Test ${{ matrix.package }} package with coverage
+ run: pnpm --filter @lobechat/${{ matrix.package }} test:coverage
+
+ - name: Upload ${{ matrix.package }} coverage to Codecov
+ uses: codecov/codecov-action@v4
+ with:
+ token: ${{ secrets.CODECOV_TOKEN }}
+ files: ./packages/${{ matrix.package }}/coverage/lcov.info
+ flags: packages/${{ matrix.package }}
+
+ # App tests
+ test-website:
+ name: Test Website
+
+ runs-on: ubuntu-latest
+
+ steps:
+ - uses: actions/checkout@v5
+
+ - name: Setup Node.js
+ uses: actions/setup-node@v4
+ with:
+ node-version: 22
+
+ - name: Install bun
+ uses: oven-sh/setup-bun@v1
+ with:
+ bun-version: ${{ secrets.BUN_VERSION }}
+
+ - name: Install deps
+ run: bun i
+
+ - name: Test App Coverage
+ run: bun run test-app:coverage
+
+ - name: Upload App Coverage to Codecov
+ uses: codecov/codecov-action@v4
+ with:
+ token: ${{ secrets.CODECOV_TOKEN }}
+ files: ./coverage/app/lcov.info
+ flags: app
+
+ test-server:
+ name: Test Database
+
runs-on: ubuntu-latest
services:
@@ -13,8 +87,6 @@ jobs:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
-
-
ports:
- 5432:5432
@@ -53,13 +125,3 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage/server/lcov.info
flags: server
-
- - name: Test App Coverage
- run: bun run test-app:coverage
-
- - name: Upload App Coverage to Codecov
- uses: codecov/codecov-action@v4
- with:
- token: ${{ secrets.CODECOV_TOKEN }}
- files: ./coverage/app/lcov.info
- flags: app
diff --git a/packages/electron-server-ipc/package.json b/packages/electron-server-ipc/package.json
index b46b5e0de77..fb7ca847c2f 100644
--- a/packages/electron-server-ipc/package.json
+++ b/packages/electron-server-ipc/package.json
@@ -5,7 +5,8 @@
"main": "src/index.ts",
"types": "src/index.ts",
"scripts": {
- "test": "vitest"
+ "test": "vitest",
+ "test:coverage": "vitest --coverage"
},
"dependencies": {
"debug": "^4.3.4"
diff --git a/packages/electron-server-ipc/vitest.config.ts b/packages/electron-server-ipc/vitest.config.ts
new file mode 100644
index 00000000000..5b40bc079f3
--- /dev/null
+++ b/packages/electron-server-ipc/vitest.config.ts
@@ -0,0 +1,10 @@
+import { defineConfig } from 'vitest/config';
+
+export default defineConfig({
+ test: {
+ coverage: {
+ all: false,
+ },
+ environment: 'node',
+ },
+});
diff --git a/packages/file-loaders/package.json b/packages/file-loaders/package.json
index 3ccf37ba097..32d0a087b99 100644
--- a/packages/file-loaders/package.json
+++ b/packages/file-loaders/package.json
@@ -21,7 +21,8 @@
"sideEffects": false,
"main": "./src/index.ts",
"scripts": {
- "test": "vitest"
+ "test": "vitest",
+ "test:coverage": "vitest --coverage"
},
"dependencies": {
"@langchain/community": "^0.3.41",
diff --git a/packages/model-runtime/package.json b/packages/model-runtime/package.json
index d2ecd18c64a..03c1ac5c3fb 100644
--- a/packages/model-runtime/package.json
+++ b/packages/model-runtime/package.json
@@ -3,6 +3,10 @@
"version": "1.0.0",
"private": true,
"main": "./src/index.ts",
+ "scripts": {
+ "test": "vitest",
+ "test:coverage": "vitest --coverage"
+ },
"dependencies": {
"@aws-sdk/client-bedrock-runtime": "^3.862.0",
"@lobechat/types": "workspace:*",
diff --git a/packages/model-runtime/src/baichuan/index.test.ts b/packages/model-runtime/src/baichuan/index.test.ts
index 148b34ebf57..b0f2c15713c 100644
--- a/packages/model-runtime/src/baichuan/index.test.ts
+++ b/packages/model-runtime/src/baichuan/index.test.ts
@@ -1,5 +1,5 @@
// @vitest-environment node
-import { Mock } from 'vitest';
+import { Mock, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { LobeOpenAICompatibleRuntime, ModelProvider } from '@/libs/model-runtime';
diff --git a/packages/model-runtime/src/mistral/index.test.ts b/packages/model-runtime/src/mistral/index.test.ts
index e32654b870b..adcdadada24 100644
--- a/packages/model-runtime/src/mistral/index.test.ts
+++ b/packages/model-runtime/src/mistral/index.test.ts
@@ -1,5 +1,5 @@
// @vitest-environment node
-import { Mock, afterEach, beforeEach, expect, it, vi } from 'vitest';
+import { Mock, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { LobeOpenAICompatibleRuntime } from '@/libs/model-runtime';
import { testProvider } from '@/libs/model-runtime/providerTestUtils';
diff --git a/packages/model-runtime/src/utils/anthropicHelpers.test.ts b/packages/model-runtime/src/utils/anthropicHelpers.test.ts
index 0cd63c44254..1ac6ca8ee88 100644
--- a/packages/model-runtime/src/utils/anthropicHelpers.test.ts
+++ b/packages/model-runtime/src/utils/anthropicHelpers.test.ts
@@ -1,5 +1,5 @@
import { OpenAI } from 'openai';
-import { describe, expect, it } from 'vitest';
+import { describe, expect, it, vi } from 'vitest';
import { imageUrlToBase64 } from '@/utils/imageToBase64';
diff --git a/packages/model-runtime/vitest.config.ts b/packages/model-runtime/vitest.config.ts
new file mode 100644
index 00000000000..e2cc1c7668d
--- /dev/null
+++ b/packages/model-runtime/vitest.config.ts
@@ -0,0 +1,16 @@
+import { resolve } from 'node:path';
+import { defineConfig } from 'vitest/config';
+
+export default defineConfig({
+ test: {
+ alias: {
+ /* eslint-disable sort-keys-fix/sort-keys-fix */
+ '@/libs/model-runtime': resolve(__dirname, './src'),
+ '@/types': resolve(__dirname, '../types/src'),
+ '@/const': resolve(__dirname, '../const/src'),
+ '@': resolve(__dirname, '../../src'),
+ /* eslint-enable */
+ },
+ environment: 'happy-dom',
+ },
+});
diff --git a/packages/prompts/package.json b/packages/prompts/package.json
index 73a823ffc26..bcc2e958a67 100644
--- a/packages/prompts/package.json
+++ b/packages/prompts/package.json
@@ -3,6 +3,10 @@
"version": "1.0.0",
"private": true,
"main": "./src/index.ts",
+ "scripts": {
+ "test": "vitest",
+ "test:coverage": "vitest --coverage"
+ },
"dependencies": {
"@lobechat/const": "workspace:*",
"@lobechat/types": "workspace:*"
diff --git a/packages/prompts/src/chains/__tests__/summaryDescription.test.ts b/packages/prompts/src/chains/__tests__/summaryDescription.test.ts
index 198d10738f6..24330fb0755 100644
--- a/packages/prompts/src/chains/__tests__/summaryDescription.test.ts
+++ b/packages/prompts/src/chains/__tests__/summaryDescription.test.ts
@@ -1,6 +1,4 @@
-import { Mock, describe, expect, it, vi } from 'vitest';
-
-import { globalHelpers } from '@/store/global/helpers';
+import { describe, expect, it } from 'vitest';
import { chainSummaryDescription } from '../summaryDescription';
@@ -10,7 +8,7 @@ describe('chainSummaryDescription', () => {
const expectedOutput = {
messages: [
{
- content: `你是一名擅长技能总结的助理,你需要将用户的输入的内容总结为一个角色技能简介,不超过 20 个字。内容需要确保信息清晰、逻辑清晰,并有效地传达角色的技能和经验,需要并翻译为目标语言:${globalHelpers.getCurrentLanguage()}。格式要求如下:\n输入: {文本作为JSON引用字符串} [locale]\n输出: {简介}`,
+ content: `你是一名擅长技能总结的助理,你需要将用户的输入的内容总结为一个角色技能简介,不超过 20 个字。内容需要确保信息清晰、逻辑清晰,并有效地传达角色的技能和经验,需要并翻译为目标语言:en-US。格式要求如下:\n输入: {文本作为JSON引用字符串} [locale]\n输出: {简介}`,
role: 'system',
},
{
diff --git a/packages/prompts/src/chains/__tests__/summaryTitle.test.ts b/packages/prompts/src/chains/__tests__/summaryTitle.test.ts
index 0de9b8aaa5e..ec46bf829e0 100644
--- a/packages/prompts/src/chains/__tests__/summaryTitle.test.ts
+++ b/packages/prompts/src/chains/__tests__/summaryTitle.test.ts
@@ -1,17 +1,8 @@
import { OpenAIChatMessage } from '@lobechat/types';
-import { Mock, describe, expect, it, vi } from 'vitest';
-
-import { chatHelpers } from '@/store/chat/helpers';
+import { describe, expect, it, vi } from 'vitest';
import { chainSummaryTitle } from '../summaryTitle';
-// Mock the chatHelpers.getMessagesTokenCount function
-vi.mock('@/store/chat/helpers', () => ({
- chatHelpers: {
- getMessagesTokenCount: vi.fn(),
- },
-}));
-
describe('chainSummaryTitle', () => {
it('should use the default model if the token count is below the GPT-3.5 limit', async () => {
// Arrange
@@ -21,7 +12,6 @@ describe('chainSummaryTitle', () => {
];
const currentLanguage = 'en-US';
const tokenCount = 10000; // Arbitrary token count below the GPT-3.5 limit
- (chatHelpers.getMessagesTokenCount as Mock).mockResolvedValue(tokenCount);
// Act
const result = await chainSummaryTitle(messages, currentLanguage);
diff --git a/packages/prompts/src/prompts/plugin/index.test.ts b/packages/prompts/src/prompts/plugin/index.test.ts
index d42c3b08c21..ec0e39c32ad 100644
--- a/packages/prompts/src/prompts/plugin/index.test.ts
+++ b/packages/prompts/src/prompts/plugin/index.test.ts
@@ -1,3 +1,5 @@
+import { describe, expect, it } from 'vitest';
+
import { pluginPrompts } from './index';
import { Tool } from './tools';
diff --git a/packages/prompts/vitest.config.ts b/packages/prompts/vitest.config.ts
new file mode 100644
index 00000000000..a9f5456ffd2
--- /dev/null
+++ b/packages/prompts/vitest.config.ts
@@ -0,0 +1,7 @@
+import { defineConfig } from 'vitest/config';
+
+export default defineConfig({
+ test: {
+ environment: 'happy-dom',
+ },
+});
diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts
index 5afe689721d..363ae823d01 100644
--- a/packages/types/src/index.ts
+++ b/packages/types/src/index.ts
@@ -1,5 +1,6 @@
export * from './artifact';
export * from './chunk';
+export * from './clientDB';
export * from './fetch';
export * from './knowledgeBase';
export * from './message';
diff --git a/packages/web-crawler/package.json b/packages/web-crawler/package.json
index 692c0b02781..93a99765096 100644
--- a/packages/web-crawler/package.json
+++ b/packages/web-crawler/package.json
@@ -4,6 +4,10 @@
"private": true,
"main": "src/index.ts",
"types": "src/index.ts",
+ "scripts": {
+ "test": "vitest",
+ "test:coverage": "vitest --coverage"
+ },
"dependencies": {
"@mozilla/readability": "^0.6.0",
"happy-dom": "^17.0.0",
diff --git a/packages/web-crawler/src/crawImpl/__tests__/jina.test.ts b/packages/web-crawler/src/crawImpl/__tests__/jina.test.ts
index cb96a969e1d..6798c2896c1 100644
--- a/packages/web-crawler/src/crawImpl/__tests__/jina.test.ts
+++ b/packages/web-crawler/src/crawImpl/__tests__/jina.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it, vi } from 'vitest';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
import { jina } from '../jina';
diff --git a/packages/web-crawler/src/crawImpl/__tests__/search1api.test.ts b/packages/web-crawler/src/crawImpl/__tests__/search1api.test.ts
index cd6e3464cff..7c227281546 100644
--- a/packages/web-crawler/src/crawImpl/__tests__/search1api.test.ts
+++ b/packages/web-crawler/src/crawImpl/__tests__/search1api.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it, vi } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { NetworkConnectionError, PageNotFoundError, TimeoutError } from '../../utils/errorType';
import * as withTimeoutModule from '../../utils/withTimeout';
diff --git a/packages/web-crawler/src/utils/__tests__/withTimeout.test.ts b/packages/web-crawler/src/utils/__tests__/withTimeout.test.ts
index 2aef2e151fd..3b52aee4cf2 100644
--- a/packages/web-crawler/src/utils/__tests__/withTimeout.test.ts
+++ b/packages/web-crawler/src/utils/__tests__/withTimeout.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it, vi } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { TimeoutError } from '../errorType';
import { DEFAULT_TIMEOUT, withTimeout } from '../withTimeout';
diff --git a/packages/web-crawler/src/utils/appUrlRules.test.ts b/packages/web-crawler/src/utils/appUrlRules.test.ts
index dea545df036..abc1ff971a6 100644
--- a/packages/web-crawler/src/utils/appUrlRules.test.ts
+++ b/packages/web-crawler/src/utils/appUrlRules.test.ts
@@ -1,3 +1,5 @@
+import { describe, expect, it } from 'vitest';
+
import { applyUrlRules } from './appUrlRules';
describe('applyUrlRules', () => {
diff --git a/packages/web-crawler/src/utils/htmlToMarkdown.test.ts b/packages/web-crawler/src/utils/htmlToMarkdown.test.ts
index 69b982708e0..2b57a7a8228 100644
--- a/packages/web-crawler/src/utils/htmlToMarkdown.test.ts
+++ b/packages/web-crawler/src/utils/htmlToMarkdown.test.ts
@@ -1,6 +1,6 @@
import { readFileSync } from 'node:fs';
import * as path from 'node:path';
-import { expect } from 'vitest';
+import { describe, expect, it } from 'vitest';
import { FilterOptions } from '../type';
import { htmlToMarkdown } from './htmlToMarkdown';
diff --git a/packages/web-crawler/vitest.config.ts b/packages/web-crawler/vitest.config.ts
new file mode 100644
index 00000000000..4ac6027d578
--- /dev/null
+++ b/packages/web-crawler/vitest.config.ts
@@ -0,0 +1,7 @@
+import { defineConfig } from 'vitest/config';
+
+export default defineConfig({
+ test: {
+ environment: 'node',
+ },
+});
diff --git a/vitest.config.ts b/vitest.config.ts
index 0a7e60509c0..a8d8c53659d 100644
--- a/vitest.config.ts
+++ b/vitest.config.ts
@@ -22,6 +22,7 @@ export default defineConfig({
// https://github.com/lobehub/lobe-chat/pull/7265
...coverageConfigDefaults.exclude,
'__mocks__/**',
+ '**/packages/**',
// just ignore the migration code
// we will use pglite in the future
// so the coverage of this file is not important
@@ -38,6 +39,7 @@ export default defineConfig({
'**/dist/**',
'**/build/**',
'**/apps/desktop/**',
+ '**/packages/**',
'src/database/server/**/**',
'src/database/repositories/dataImporter/deprecated/**/**',
],