zcodeProtocolV4ConnectionScope.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { describe, expect, it } from "vitest";
  2. import { hostAttachServicePortMessageSchema } from "../src/index.js";
  3. import {
  4. subscribeParamsSchema,
  5. V4_METHODS,
  6. conversationResyncParamsSchema,
  7. v4ConnectionFlowParamsSchema,
  8. v4ConversationResyncParamsSchema,
  9. v4ConversationResyncResultSchema,
  10. v4ConversationSubscribeParamsSchema,
  11. v4ConversationSubscribeResultSchema,
  12. v4ConversationUnsubscribeParamsSchema,
  13. v4SessionsIndexSubscribeResultSchema,
  14. v4WorkspaceConfigSubscribeResultSchema,
  15. } from "../src/zcode-protocol-v4/index.js";
  16. describe("ZCode Protocol V4 trusted connection scope", () => {
  17. it("connection flow RPC 只接受 trusted connectionId 与三态控制", () => {
  18. expect(V4_METHODS.connectionFlow).toBe("v4/connection/flow");
  19. for (const state of ["saturated", "drained", "closed"] as const) {
  20. expect(
  21. v4ConnectionFlowParamsSchema.safeParse({ connectionId: "connection-1", state }).success,
  22. ).toBe(true);
  23. }
  24. expect(
  25. v4ConnectionFlowParamsSchema.safeParse({
  26. connectionId: "connection-1",
  27. state: "paused-by-ui",
  28. }).success,
  29. ).toBe(false);
  30. expect(
  31. v4ConnectionFlowParamsSchema.safeParse({
  32. connectionId: "connection-1",
  33. state: "saturated",
  34. topic: "conversation/forged",
  35. }).success,
  36. ).toBe(false);
  37. });
  38. it("UI-facing subscribe 不能选择 delivery profile 或 client mode", () => {
  39. expect(
  40. subscribeParamsSchema.safeParse({
  41. topic: "conversation/session-1",
  42. deliveryProfile: "replayable",
  43. }).success,
  44. ).toBe(false);
  45. expect(
  46. subscribeParamsSchema.safeParse({
  47. topic: "conversation/session-1",
  48. clientMode: "web-remote-replayable",
  49. }).success,
  50. ).toBe(false);
  51. });
  52. it("CLI-facing subscribe 必须带 host 注入的 connectionId/clientMode", () => {
  53. expect(
  54. v4ConversationSubscribeParamsSchema.safeParse({
  55. topic: "conversation/session-1",
  56. connectionId: "connection-1",
  57. }).success,
  58. ).toBe(false);
  59. expect(
  60. v4ConversationSubscribeParamsSchema.safeParse({
  61. topic: "conversation/session-1",
  62. connectionId: "connection-1",
  63. clientMode: "desktop-continuous",
  64. }).success,
  65. ).toBe(true);
  66. expect(
  67. v4ConversationSubscribeParamsSchema.safeParse({
  68. topic: "conversation/session-1",
  69. connectionId: "connection-1",
  70. clientMode: "mobile-self-declared",
  71. }).success,
  72. ).toBe(false);
  73. });
  74. it("AttachServicePort 只接受 host 可信的两种 clientMode", () => {
  75. for (const clientMode of ["desktop-continuous", "web-remote-replayable"] as const) {
  76. expect(
  77. hostAttachServicePortMessageSchema.safeParse({
  78. type: "attach-service-port",
  79. requestId: `request-${clientMode}`,
  80. attachmentId: `attachment-${clientMode}`,
  81. clientMode,
  82. scope: { kind: "local" },
  83. }).success,
  84. ).toBe(true);
  85. }
  86. expect(
  87. hostAttachServicePortMessageSchema.safeParse({
  88. type: "attach-service-port",
  89. requestId: "request-invalid",
  90. attachmentId: "attachment-invalid",
  91. clientMode: "mobile-self-declared",
  92. scope: { kind: "local" },
  93. }).success,
  94. ).toBe(false);
  95. expect(
  96. hostAttachServicePortMessageSchema.safeParse({
  97. type: "attach-service-port",
  98. }).success,
  99. ).toBe(false);
  100. });
  101. it("CLI-facing unsubscribe 必须严格携带 topic/subscriptionId/connectionId", () => {
  102. expect(
  103. v4ConversationUnsubscribeParamsSchema.safeParse({
  104. subscriptionId: "sub-1",
  105. }).success,
  106. ).toBe(false);
  107. expect(
  108. v4ConversationUnsubscribeParamsSchema.safeParse({
  109. topic: "conversation/session-1",
  110. subscriptionId: "sub-1",
  111. connectionId: "connection-1",
  112. }).success,
  113. ).toBe(true);
  114. expect(
  115. v4ConversationUnsubscribeParamsSchema.safeParse({
  116. topic: "conversation/session-1",
  117. subscriptionId: "sub-1",
  118. connectionId: "connection-1",
  119. forged: true,
  120. }).success,
  121. ).toBe(false);
  122. });
  123. it("same-sub resync 只接受 subscriptionId/base/forceSnapshot 且结果 strict ACK", () => {
  124. expect(V4_METHODS.conversationResync).toBe("v4/conversation/resync");
  125. expect(
  126. conversationResyncParamsSchema.safeParse({
  127. subscriptionId: "sub-1",
  128. base: { logEpoch: "epoch-1", seq: 7 },
  129. }).success,
  130. ).toBe(true);
  131. expect(
  132. conversationResyncParamsSchema.safeParse({
  133. subscriptionId: "sub-1",
  134. base: null,
  135. forceSnapshot: true,
  136. }).success,
  137. ).toBe(true);
  138. expect(
  139. conversationResyncParamsSchema.safeParse({
  140. subscriptionId: "sub-1",
  141. base: null,
  142. topic: "conversation/forged",
  143. }).success,
  144. ).toBe(false);
  145. expect(
  146. v4ConversationResyncParamsSchema.safeParse({
  147. topic: "conversation/session-1",
  148. connectionId: "connection-1",
  149. subscriptionId: "sub-1",
  150. base: null,
  151. }).success,
  152. ).toBe(true);
  153. expect(
  154. v4ConversationResyncParamsSchema.safeParse({
  155. subscriptionId: "sub-1",
  156. base: null,
  157. }).success,
  158. ).toBe(false);
  159. const ack = { subscriptionId: "sub-1", mode: "resume" as const, logEpoch: "epoch-1" };
  160. expect(v4ConversationResyncResultSchema.safeParse({ ack }).success).toBe(true);
  161. expect(v4ConversationResyncResultSchema.safeParse({ ack, frame: null }).success).toBe(false);
  162. });
  163. it.each([
  164. ["conversation", v4ConversationSubscribeResultSchema],
  165. ["sessions-index", v4SessionsIndexSubscribeResultSchema],
  166. ["workspace-config", v4WorkspaceConfigSubscribeResultSchema],
  167. ])("%s subscribe result 只接受 strict ACK", (_topic, schema) => {
  168. const ack = {
  169. subscriptionId: "sub-1",
  170. mode: "snapshot" as const,
  171. logEpoch: "epoch-1",
  172. };
  173. expect(schema.safeParse({ ack }).success).toBe(true);
  174. expect(schema.safeParse({ ack, frame: null }).success).toBe(false);
  175. });
  176. });