Skip to content

test: add fake Kimi services to testkit and migrate title and feedback tests - #5445

Merged
RealKai42 merged 4 commits into
mainfrom
test/fake-kimi-services
Nov 25, 2026
Merged

RealKai42 merged 4 commits into
mainfrom
test/fake-kimi-services

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

这是「理清测试基建」的第 3 轮(前两轮是 #5422、#5437)。会话标题和反馈这两个 Kimi 托管服务,在引擎、kap-server、node-sdk 的测试里各自手写了一份:有的写在 fetch 桩里,有的写成一个临时 HTTP 网关。地址、请求体、返回形状各写各的,产品改了接口,这些桩也不会一起失败。这一轮在 testkit 里做一个假 Kimi 服务,用产品里真实的客户端核对它,再把这些测试迁过去。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

一块一个提交。

块 改法
testkit 假 Kimi 服务 新子路径 @moonshot-ai/testkit/services,和假模型共用 fetch 与 HTTP 两套传输(传输层为二进制上传加了原始字节 bytes)。会话标题按剧本回答,也可以回服务错误。反馈有状态:提交、更新、申请上传地址、存储、完成上传。holdNext(endpoint) 挂起下一个打到该端点的请求。所有请求都有记录,verify() 报告意外请求、没写剧本的标题请求、没用完的剧本、没到达或没放行的挂起点、上传顺序或 etag 不对、存储请求带了令牌或用错方法
oauth 对照测试 packages/oauth/test/kimi-services-contract.test.ts 用 oauth 包里真实的 fetchChatTitle、反馈提交和更新、上传地址和完成上传,经 HTTP 请求假服务。oauth 新增 testkit 开发依赖
迁移 引擎 sessionTitleService.test.ts 和 feedback.test.ts;kap-server 的标题 REST 用例和 feedback.e2e.test.ts(包括手写网关和远端主机网关);node-sdk 的两条标题竞态用例
冻结 scripts/checks/test-doubles.mjs 新增 kimi-service-stub 规则,数测试里手写的 chat_title、/coding/v1/tools、/coding/v1/feedback、upload_url、upload_complete。只有 packages/oauth/test 里客户端自己的协议测试例外。迁移后计数为 0,基线没变

关键决策

决策 选择 没选 理由
冻结什么 测试里手写的服务线协议标记 「既替换 fetch 又提到服务」的文件 后者在 app-core、remote-control 等地方误报,那里讲的是 KAP 和中继协议。线协议标记只命中真正手写了这些服务的地方,拿 main 上迁移前的 5 个文件试,全部能抓到
模型注册表 这一轮不迁 一起做进假服务 这些测试多在测注册表客户端自己的缓存和失败处理,用的是产品留的 setModelsDevUpstreamForTest({ fetchImpl }),每处只是个几行的 helper,换掉收益很小
挂起点 统一成 holdNext(endpoint),请求一到就取走它的剧本回复 只给标题回复加挂起 kap-server 要挂起存储上传;先取回复再挂起,客户端中止时结果才是确定的,不取决于服务端什么时候察觉断开
对照测试放哪 oauth 包 testkit testkit 不依赖任何产品包(test/boundaries.test.ts 检查),产品客户端只能从使用方这一侧来核对
阅读指南
  1. packages/testkit/src/services/kimi-services.ts:handle() 先记录请求、找挂起点、准备回复,再按需等放行;answer() 按端点分派。
  2. packages/oauth/test/kimi-services-contract.test.ts:真实客户端和假服务的对照。
  3. packages/kap-server/test/feedback.e2e.test.ts:原来的手写网关换成假服务,created、uploads 等状态改成读假服务的记录。
  4. scripts/checks/test-doubles.mjs 的新规则和它的自测。

其余迁移可以略读:都是把 fetch 桩换成假服务的剧本,把对请求体的断言换成读 requests()。

旧写法的功能清单

旧写法做的事 现在由什么承担
kap-server 手写网关:令牌不对回 401 假服务的 accessToken,同时记为问题,verify() 失败
存储请求带 Authorization 回 403 假服务同样回 403,并记为问题
不属于自己的反馈回 409 假服务回 409,不记为问题(用例会有意触发)
按反馈指定签名方法 uploadMethod('POST'),作用于之后的上传;用例结束时改回 PUT
顶层用例「除了 /feedback 不许有别的外发请求」 假服务的 verify() 管发给它的请求,testkit 的网络守卫管其他外发连接
远端主机网关「对所有请求回 409 并记录」 第二个假服务实例,断言它没收到任何请求
「签名上传超过一分钟不被中止」:直接断言第一次请求的 signal 断言存储只收到一次请求(被中止会触发重试)。把产品代码临时改成 60 秒超时,这条会失败
引擎标题测试断言请求体是 chat_title 假服务对不是 chat_title 的调用回 400 并记为问题;用例只断言 chat_content

Behavior Changes and Affected Users

产品代码没有改动。下表的变化只影响写测试的人。

Behavior Before After Who relies on the old behavior Escape hatch
测试里手写会话标题或反馈服务的线协议 不拦 pnpm lint 失败,提示改用 @moonshot-ai/testkit/services 写新测试的人 只是在文件间挪动已有写法时,用 --update --allow-increase 更新基线,并在 PR 里说明;packages/oauth/test 不受限
迁移后的用例里的反馈编号和 etag 各测试自己定(9、17、"test-etag" 等) 假服务从 1 开始分配,etag 是 "testkit-etag-<上传编号>" 无 —

没有 changeset:只改了测试、私有包 testkit 和 oauth 的测试依赖、仓库检查脚本和说明文件。

验证

只在 macOS 上验证过,Linux 交给 CI,Windows 没有验证。

  • packages/testkit 192 条、packages/oauth 390 条,全部通过。
  • packages/kap-server 1,578 条,失败 2 条:搜索 worker 的主线程响应用例,单独跑通过。
  • packages/node-sdk 297 条,失败 1 条:suggestFiles,用 main 上原版的测试文件跑也一样失败。
  • packages/agent-core-v2 8,346 条,失败 10 条:tool.test.ts 子 agent 截止时间 3 条,把 testkit 传输层换回 main 的版本后同样失败;其余 7 条单独跑都通过。
  • 迁过去的用例连续跑 3 次都通过。用两处变异核对过检查:删掉一个标题剧本、多写一个标题,用例都会失败。
  • 类型检查:testkit、oauth、agent-core-v2、kap-server、node-sdk 都通过。
  • node --test 'scripts/checks/*.test.mjs':106 条通过。
  • pnpm run check:duplication <main>:没有新增重复。
  • 变基到 main 3d58fad4e(含修复 resume.test.ts 固定等待的 test(agent-core-v2): synchronize resume regression with agent disposal #5442)后,pnpm lint 通过,迁移过的用例和 testkit、oauth 重跑全部通过。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

Answer session titles and feedback with signed uploads from one stateful fake, served through the same fetch and HTTP transports as the fake model. Tests script titles, hold the next request to any endpoint, and read back every request and submission; unexpected requests, unused titles, unreached or unreleased holds, and malformed uploads fail verify().
…ients

Drive fetchChatTitle, the feedback submit and update calls, and the signed upload flow against the testkit services over HTTP, so the fake keeps answering in the shapes the product parses.
The title service, feedback service, kap-server REST and e2e cases, and the SDK title races now script the fake instead of hand-writing the managed service protocol in fetch stubs and an ad hoc HTTP gateway. Each test verifies the fake, so unexpected requests, unused titles, and unreleased holds fail the test.
test-doubles now counts the managed service wire format written by hand in tests (chat_title, the coding/v1 tools and feedback paths, upload_url, upload_complete). The managed clients' own protocol tests in packages/oauth/test are exempt. Every migrated file already counts zero, so the baseline is unchanged. Document the fake services in the testkit README and the testing guide.
@changeset-bot

changeset-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 41a9d5e

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Nov 25, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@41a9d5e
npx https://pkg.pr.new/@moonshot-ai/kimi-code@41a9d5e

commit: 41a9d5e

@RealKai42
RealKai42 merged commit 11a865d into main Nov 25, 2026
26 checks passed
@RealKai42
RealKai42 deleted the test/fake-kimi-services branch November 25, 2026 16:21
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 25, 2026
Add @moonshot-ai/testkit/services: scripted session-title and feedback
endpoints, signed upload URL/storage handling, request recording and
verify, built on the shared HTTP transport. The transport now hands the
handler the request bytes as well as the decoded body.

The services suite passes 13. The consumer test migrations are recorded as
remaining: they conflict with the fork's tests, and the oauth contract test
needs the cross-package #/ mapping fixed before typecheck can pass.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 25, 2026
Port the session-title suite from MoonshotAI#5445 onto the testkit Kimi services:
title requests, OAuth retry/force-refresh, custom headers, endpoint
override and in-flight sharing all run against the scripted service.

The file is excluded from the package tsc because importing testkit source
resolves #/ through agent-core-v2's own paths; Bun runs and passes it
(33 pass). The remaining MoonshotAI#5445 consumer suites stay recorded on the row.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 26, 2026
…I#5445)

Add the oauth contract suite for the fake Kimi services, exercising the
real managed clients for session titles and feedback against the testkit
server. The kap-server/node-sdk suites keep their existing doubles because
the upstream migration relies on vitest lifecycle hooks this fork's
bun:test harness does not provide, and their coverage is unchanged.

Verified: testkit services 13 pass; oauth contract 4 pass.
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.

1 participant