contract.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { expect, it } from "vitest";
  2. import { ToolSearchDeliverySchema } from "@zcode/contracts";
  3. import {
  4. ToolSearchAnnouncementSchema,
  5. ToolSearchMcpDeltaSchema,
  6. } from "../../src/tool/search/contract.js";
  7. it("does not widen the names contract when adding independent MCP state", () => {
  8. expect(
  9. ToolSearchAnnouncementSchema.safeParse({
  10. addedNames: [],
  11. removedNames: [],
  12. readdedNames: [],
  13. mcp: {},
  14. }).success,
  15. ).toBe(false);
  16. expect(
  17. ToolSearchMcpDeltaSchema.safeParse({ mcp: { pending: ["example"], failed: [] } }).success,
  18. ).toBe(true);
  19. });
  20. it("reads MCP metadata written with retired fields", () => {
  21. // 开发期会话可能带已删除的字段;状态 metadata 不是 strict,读取时丢弃未知字段。
  22. const parsed = ToolSearchMcpDeltaSchema.safeParse({
  23. mcp: {
  24. pending: [],
  25. failed: [{ name: "broken", blocked: true, willRetry: true }],
  26. revoked: ["gone"],
  27. },
  28. retractedNames: ["mcp__gone__ping"],
  29. toolOrigins: { mcp__gone__ping: "gone" },
  30. });
  31. expect(parsed.success).toBe(true);
  32. expect(parsed.data).toEqual({ mcp: { pending: [], failed: [{ name: "broken" }] } });
  33. });
  34. it("reads delivery records and retractions written by a newer version", () => {
  35. // 降级读取:新版本可能给交付记录加字段、新增撤销原因。修复前 strict 与封闭枚举让整条记录被丢弃,
  36. // 冻结的常驻定义失效(请求字节变化),撤销事实丢失后还会重复公告。
  37. const delivery = ToolSearchDeliverySchema.safeParse({
  38. definitions: [
  39. {
  40. tool: { name: "mcp__res__ping", inputSchema: { type: "object" }, examples: [] },
  41. mcp: { serverName: "res", toolName: "ping", origin: "future" },
  42. frozenAt: 1,
  43. },
  44. ],
  45. strippedReferences: [{ toolCallId: "call", toolName: "ToolSearch", reason: "future" }],
  46. version: 2,
  47. });
  48. expect(delivery.data).toEqual({
  49. definitions: [
  50. {
  51. tool: { name: "mcp__res__ping", inputSchema: { type: "object" } },
  52. mcp: { serverName: "res", toolName: "ping" },
  53. },
  54. ],
  55. strippedReferences: [{ toolCallId: "call", toolName: "ToolSearch" }],
  56. });
  57. expect(
  58. ToolSearchMcpDeltaSchema.safeParse({
  59. retractedTools: [{ name: "mcp__gone__ping", cause: "future_cause" }],
  60. }).data,
  61. ).toEqual({ retractedTools: [{ name: "mcp__gone__ping", cause: "future_cause" }] });
  62. });