Skip to content

fix: add mock OpenAI server for E2E tests#53

Merged
best merged 1 commit into
masterfrom
fix/e2e-mock-server
Mar 27, 2026
Merged

fix: add mock OpenAI server for E2E tests#53
best merged 1 commit into
masterfrom
fix/e2e-mock-server

Conversation

@best
Copy link
Copy Markdown
Contributor

@best best commented Mar 27, 2026

Closes #52

改动说明

E2E 测试之前依赖真实 API 端点,CI 中 404 报错。本次改动添加了本地 mock OpenAI 兼容服务器,所有测试场景无需外部 API。

新增文件

  • packages/ai/__tests__/helpers/mock-openai-server.ts — mock 服务器
  • packages/agent/__tests__/helpers/mock-openai-server.ts — agent 包副本(vitest 跨包引用限制)

修改文件

  • packages/ai/__tests__/e2e/openai-compat.e2e.test.ts — 支持 MOCK=true
  • packages/agent/__tests__/e2e/tool-calling.e2e.test.ts — 支持 MOCK=true
  • packages/agent/__tests__/e2e/react-loop.e2e.test.ts — 支持 MOCK=true
  • .github/workflows/e2e.yml — 加 MOCK: true

Mock 服务器能力

  • 非流式 & 流式 chat completions
  • 单次 & 多次 tool calls
  • 工具结果摘要(ReAct 循环支持)
  • JSON/结构化输出响应

验收标准

  • MOCK=true pnpm test 全量 1018 + 7 E2E 测试通过
  • ✅ 不依赖任何外部 API
  • ✅ CI workflow 加 MOCK=true 后可跑通
  • ✅ 未修改任何 provider/adapter 源码
  • ✅ 未改变非 MOCK 模式的行为(真实 API 模式仍可用)

Closes #52

E2E tests were failing in CI because they relied on real API endpoints
that returned 404 errors. This adds a local mock OpenAI-compatible server
that handles all test scenarios without external API dependencies.

Changes:
- Add mock-openai-server.ts in packages/ai/__tests__/helpers/
- Add mock-openai-server.ts in packages/agent/__tests__/helpers/
- Update openai-compat.e2e.test.ts to support MOCK=true mode
- Update tool-calling.e2e.test.ts to support MOCK=true mode
- Update react-loop.e2e.test.ts to support MOCK=true mode
- Update e2e.yml workflow to set MOCK=true

The mock server handles:
- Non-streaming and streaming chat completions
- Tool calls (single and multiple)
- Tool result summaries (ReAct loop support)
- JSON/structured output responses

Usage: MOCK=true pnpm test (runs E2E tests against mock server)
All 7 E2E tests pass with MOCK=true, no external API needed.
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI Code Review

Automated review for PR #53informational only, does not block merge.

Checklist

Item Notes
⚠️ Type Safety unsafe as unknown cast detected
Error Handling No issues detected
Test Coverage 3 test file(s) updated
API Consistency 4 new export(s) added
Breaking Changes No breaking changes detected

Stats

  • 📁 Files changed: 6 (5 TypeScript)
  • ➕ Lines added: 1033
  • 🧪 Test files updated: 3

1 check(s) need attention.

Powered by openlinkos/agent · AI PR Review workflow

@best
Copy link
Copy Markdown
Contributor Author

best commented Mar 27, 2026

🔍 代码审查 — PR #53

关联 Issue: #52 — E2E 测试因 OpenAI API 404 导致 CI 失败

方案: 添加本地 mock OpenAI 兼容服务器,E2E 测试在 MOCK=true 模式下无需外部 API


✅ 审查通过

优点:

  1. 方案合理:彻底解决 CI 依赖外部 API 的问题
  2. 向后兼容:未改 provider/adapter 源码,真实 API 模式仍可用
  3. Mock 覆盖全:非流式/流式、单/多 tool call、ReAct 循环、JSON 输出
  4. 生命周期管理正确:beforeAll/afterAll 正确启停服务器
  5. CI 改动最小化:只加了一行 MOCK: true

注意事项(不阻塞合并):

  • mock 服务器在 ai 和 agent 两个包中各有一份副本(因 vitest 跨包引用限制),长期可考虑 workspace 共享包统一维护
  • mock 基于正则匹配用户消息决定行为,测试 prompt 措辞变更时需注意

CI 状态: Build ✅ | Type check ✅ | AI Review ✅ | Release check ✅ | Test ⏳ 等待中

等待 Test 通过后执行合并。

@best best merged commit 6280077 into master Mar 27, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] CI tests failing - E2E OpenAI API configuration issues (package exports FIXED)

1 participant