zcodeProtocolV4InputPrediction.test.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // 手机远控输入预测线格式(docs/web-remote-control/mobile-input-prediction.md)。
  2. import { describe, expect, it } from "vitest";
  3. import {
  4. V4_METHODS,
  5. emptyInputPredictionResult,
  6. helloMessageSchema,
  7. v4ConversationInputPredictionParamsSchema,
  8. v4ConversationInputPredictionResultSchema,
  9. } from "../src/zcode-protocol-v4/index.js";
  10. describe("v4/conversation/inputPrediction 线格式", () => {
  11. it("方法名固定", () => {
  12. expect(V4_METHODS.conversationInputPrediction).toBe("v4/conversation/inputPrediction");
  13. });
  14. it("params 严格校验", () => {
  15. const parse = (value: unknown) => v4ConversationInputPredictionParamsSchema.safeParse(value);
  16. expect(parse({ sessionId: "s", revision: 0 }).success).toBe(true);
  17. expect(parse({ sessionId: "s", revision: 3, draft: "帮我", requestId: "r1" }).success).toBe(
  18. true,
  19. );
  20. expect(parse({ sessionId: "", revision: 0 }).success).toBe(false);
  21. expect(parse({ sessionId: "s", revision: -1 }).success).toBe(false);
  22. expect(parse({ sessionId: "s", revision: 1.5 }).success).toBe(false);
  23. expect(parse({ sessionId: "s", revision: 0, draft: "x".repeat(4001) }).success).toBe(false);
  24. expect(parse({ sessionId: "s", revision: 0, requestId: "" }).success).toBe(false);
  25. expect(parse({ sessionId: "s", revision: 0, unknown: true }).success).toBe(false);
  26. });
  27. it("result:候选不 trim 但不能纯空白 / 换行 / 超长,最多 3 个", () => {
  28. const parse = (candidates: unknown[]) =>
  29. v4ConversationInputPredictionResultSchema.safeParse({
  30. decision: "show",
  31. candidates,
  32. topConfidence: 0.9,
  33. });
  34. expect(parse([{ text: " tests", confidence: 0.9 }]).success).toBe(true);
  35. expect(parse([{ text: " ", confidence: 0.9 }]).success).toBe(false);
  36. expect(parse([{ text: "a\nb", confidence: 0.9 }]).success).toBe(false);
  37. expect(parse([{ text: "x".repeat(41), confidence: 0.9 }]).success).toBe(false);
  38. expect(parse([{ text: "a", confidence: 1.1 }]).success).toBe(false);
  39. expect(parse(Array.from({ length: 4 }, () => ({ text: "a", confidence: 0.5 }))).success).toBe(
  40. false,
  41. );
  42. });
  43. it("空结果构造器只回显给定 requestId", () => {
  44. expect(emptyInputPredictionResult("unavailable")).toEqual({
  45. decision: "unavailable",
  46. candidates: [],
  47. topConfidence: 0,
  48. });
  49. expect(
  50. v4ConversationInputPredictionResultSchema.parse(emptyInputPredictionResult("cancelled", "r")),
  51. ).toEqual({ decision: "cancelled", candidates: [], topConfidence: 0, requestId: "r" });
  52. });
  53. it("hello.capabilities.inputPrediction 可选:老 Host 缺席仍能解析", () => {
  54. const hello = {
  55. kind: "hello",
  56. protocolVersion: 3,
  57. connectionId: "c",
  58. clientMode: "web-remote-replayable",
  59. deliveryProfile: "replayable",
  60. serverTime: 1,
  61. capabilities: {
  62. nativeDialogs: false,
  63. localTerminal: false,
  64. binaryFrames: false,
  65. compression: "none",
  66. },
  67. auth: {},
  68. };
  69. expect(helloMessageSchema.parse(hello).capabilities.inputPrediction).toBeUndefined();
  70. expect(
  71. helloMessageSchema.parse({
  72. ...hello,
  73. capabilities: { ...hello.capabilities, inputPrediction: true },
  74. }).capabilities.inputPrediction,
  75. ).toBe(true);
  76. });
  77. });