Skip to content

Commit ca12753

Browse files
committed
feat(tests): add comprehensive test suite for core functionality
- Implemented unit tests for AI utilities, including prompt template processing and tool name validation. - Created environment utility tests to verify production and SCF detection. - Developed integration tests for core utilities, including JSON parsing and string/array operations. - Added workflow tests to validate step execution and state management. - Introduced a test runner script to streamline test execution. - Documented test structure and running instructions in README.md.
1 parent fb17ddb commit ca12753

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2993
-561
lines changed

.github/workflows/README.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# GitHub Actions for MCPC Core
2+
3+
这个目录包含为 MCPC Core 项目配置的 GitHub Actions 工作流。
4+
5+
## 工作流文件
6+
7+
### 1. `simple-test.yml` - 基础测试工作流
8+
**推荐使用** - 简洁、可靠的测试工作流
9+
10+
- **触发条件**: 推送到 `main`/`develop` 分支,PR 到 `main` 分支
11+
- **测试矩阵**:
12+
- Deno 版本: `2.4.x`, `2.x` (最新)
13+
- 操作系统: Ubuntu, macOS, Windows
14+
- **功能**:
15+
- ✅ 代码格式检查 (`deno fmt`)
16+
- ✅ 代码质量检查 (`deno lint`)
17+
- ✅ 类型检查 (`deno check`)
18+
- ✅ 运行所有测试 (`deno task test`)
19+
- ✅ 生成测试覆盖率报告
20+
- ✅ 安全检查(依赖分析)
21+
22+
### 2. `deno-test.yml` - 完整测试工作流
23+
高级功能的完整测试套件
24+
25+
- **触发条件**:
26+
- 推送到任何分支
27+
- PR 到 `main`/`develop` 分支
28+
- 手动触发(支持选择测试类型)
29+
- **功能**:
30+
- 🔄 多版本 Deno 测试矩阵
31+
- 📊 详细的覆盖率报告
32+
- ⚡ 性能测试
33+
- 🔒 安全审计
34+
- 📦 发布检查
35+
- 🚀 并发控制
36+
37+
### 3. `test.yml` - 传统测试工作流
38+
包含基准测试和多任务的扩展版本
39+
40+
## 推荐配置
41+
42+
### 主要工作流
43+
建议使用 **`simple-test.yml`** 作为主要的 CI/CD 工作流,因为它:
44+
- 快速执行(通常 < 5 分钟)
45+
- 覆盖所有必要的检查
46+
- 在多个平台上测试
47+
- 使用最新的 Deno 版本
48+
49+
### 高级需求
50+
如果需要更详细的测试和报告,可以使用 **`deno-test.yml`**
51+
52+
## Deno 设置详情
53+
54+
### 版本策略
55+
- **`2.x`**: 使用 Deno 2.x 系列的最新版本
56+
- **`2.4.x`**: 使用 Deno 2.4.x 系列的最新补丁版本
57+
58+
### 依赖缓存
59+
工作流使用 GitHub Actions 缓存来加速构建:
60+
```yaml
61+
- name: Cache Dependencies
62+
uses: actions/cache@v4
63+
with:
64+
path: ~/.cache/deno
65+
key: deno-${{ runner.os }}-${{ matrix.deno-version }}-${{ hashFiles('packages/core/deno.lock') }}
66+
```
67+
68+
### 权限设置
69+
测试只需要最小权限:
70+
- `--allow-env`: 用于环境变量测试
71+
- `--allow-read`: 用于读取测试文件
72+
73+
## 本地验证
74+
75+
在推送代码前,可以本地运行相同的检查:
76+
77+
```bash
78+
# 进入 core 包目录
79+
cd packages/core
80+
81+
# 格式检查
82+
deno fmt --check
83+
84+
# 代码质量检查
85+
deno lint
86+
87+
# 类型检查
88+
deno check mod.ts
89+
90+
# 运行测试
91+
deno task test
92+
93+
# 生成覆盖率
94+
deno test --allow-env --allow-read --coverage=coverage tests/
95+
deno coverage coverage --lcov --output=coverage.lcov
96+
```
97+
98+
## 状态徽章
99+
100+
可以在 README 中添加状态徽章:
101+
102+
```markdown
103+
[![Tests](https://github.com/mcpc-tech/mcpc/workflows/MCPC%20Core%20-%20Deno%20Tests/badge.svg)](https://github.com/mcpc-tech/mcpc/actions)
104+
[![Coverage](https://codecov.io/gh/mcpc-tech/mcpc/branch/main/graph/badge.svg)](https://codecov.io/gh/mcpc-tech/mcpc)
105+
```
106+
107+
## 故障排除
108+
109+
### 常见问题
110+
111+
1. **依赖缓存失败**
112+
- 删除 `deno.lock` 文件并重新生成
113+
- 检查 `deno.json` 中的导入路径
114+
115+
2. **权限错误**
116+
- 确保测试只使用 `--allow-env` 和 `--allow-read`
117+
- 避免在测试中访问网络或文件系统
118+
119+
3. **格式检查失败**
120+
- 运行 `deno fmt` 自动修复格式问题
121+
122+
4. **类型检查失败**
123+
- 检查导入路径和类型定义
124+
- 确保所有依赖都在 `deno.json` 中正确声明
125+
126+
### 调试技巧
127+
128+
- 使用 `continue-on-error: true` 让非关键步骤失败时不中断工作流
129+
- 添加 `timeout-minutes` 防止作业挂起
130+
- 使用矩阵策略在多个环境中测试
131+
132+
## 维护
133+
134+
### 更新 Deno 版本
135+
定期更新工作流中的 Deno 版本:
136+
1. 检查 [Deno 发布页面](https://github.com/denoland/deno/releases)
137+
2. 更新 `deno-version` 在工作流文件中
138+
3. 测试新版本的兼容性
139+
140+
### Actions 版本更新
141+
定期更新 GitHub Actions 版本:
142+
- `actions/checkout@v4` → 检查最新版本
143+
- `denoland/setup-deno@v2` → 检查最新版本
144+
- `actions/cache@v4` → 检查最新版本

.github/workflows/ci.yml

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main, develop ]
6+
paths:
7+
- 'packages/core/**'
8+
- '.github/workflows/**'
9+
pull_request:
10+
branches: [ main ]
11+
paths:
12+
- 'packages/core/**'
13+
- '.github/workflows/**'
14+
15+
jobs:
16+
test:
17+
name: Test with Deno ${{ matrix.deno-version }} on ${{ matrix.os }}
18+
runs-on: ${{ matrix.os }}
19+
timeout-minutes: 10
20+
21+
strategy:
22+
matrix:
23+
deno-version: ['2.4.x', '2.x']
24+
os: [ubuntu-latest, macos-latest, windows-latest]
25+
fail-fast: false
26+
27+
steps:
28+
- name: Git Checkout
29+
uses: actions/checkout@v4
30+
31+
- name: Use Deno ${{ matrix.deno-version }}
32+
uses: denoland/setup-deno@v2
33+
with:
34+
deno-version: ${{ matrix.deno-version }}
35+
36+
- name: Show Deno Version
37+
run: deno --version
38+
39+
- name: Cache Dependencies
40+
uses: actions/cache@v4
41+
with:
42+
path: ~/.cache/deno
43+
key: deno-${{ runner.os }}-${{ matrix.deno-version }}-${{ hashFiles('packages/core/deno.lock') }}
44+
45+
- name: Install Dependencies
46+
working-directory: packages/core
47+
run: deno cache mod.ts
48+
49+
- name: Run Linter
50+
working-directory: packages/core
51+
run: deno lint
52+
53+
- name: Check Formatting
54+
working-directory: packages/core
55+
run: deno fmt --check
56+
57+
- name: Type Check
58+
working-directory: packages/core
59+
run: deno check mod.ts
60+
61+
- name: Run Tests
62+
working-directory: packages/core
63+
run: deno task test
64+
65+
coverage:
66+
name: Generate Coverage Report
67+
runs-on: ubuntu-latest
68+
needs: test
69+
70+
steps:
71+
- name: Git Checkout
72+
uses: actions/checkout@v4
73+
74+
- name: Use Deno
75+
uses: denoland/setup-deno@v2
76+
with:
77+
deno-version: 2.x
78+
79+
- name: Run Tests with Coverage
80+
working-directory: packages/core
81+
run: deno test --allow-env --allow-read --coverage=coverage tests/
82+
83+
- name: Generate Coverage Report
84+
working-directory: packages/core
85+
run: deno coverage coverage --lcov --output=coverage.lcov
86+
87+
- name: Upload to Codecov
88+
uses: codecov/codecov-action@v4
89+
continue-on-error: true
90+
with:
91+
file: ./packages/core/coverage.lcov
92+
flags: mcpc-core
93+
94+
security:
95+
name: Security Check
96+
runs-on: ubuntu-latest
97+
98+
steps:
99+
- name: Git Checkout
100+
uses: actions/checkout@v4
101+
102+
- name: Use Deno
103+
uses: denoland/setup-deno@v2
104+
with:
105+
deno-version: 2.x
106+
107+
- name: Check Dependencies
108+
working-directory: packages/core
109+
run: |
110+
echo "=== Dependency Info ==="
111+
deno info mod.ts
112+
echo "=== Checking for HTTP imports ==="
113+
! grep -r "http://" . --include="*.ts" || (echo "Found insecure HTTP imports" && exit 1)

0 commit comments

Comments
 (0)