zcodeProtocolV4CommandQuery.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. V4_METHODS,
  4. commandsQueryParamsSchema,
  5. commandsQueryResultSchema,
  6. conversationInputIntentSchema,
  7. queueItemSchema,
  8. } from "../src/zcode-protocol-v4/index.js";
  9. describe("ZCode Protocol v4 authoritative input intent", () => {
  10. const baseIntent = {
  11. sourceCommandId: "cmd-input-1",
  12. queueItemId: "queue-1",
  13. clientId: "client-a",
  14. kind: "sendText" as const,
  15. text: "原文",
  16. delivery: { requested: "guide" as const, admitted: "queue" as const },
  17. order: { admissionSeq: 3, queuePosition: 1 },
  18. steer: { state: "fellBack" as const, reasonCode: "guide.attachmentsUnsupported" },
  19. dispatch: { state: "queued" as const },
  20. admittedAt: 1000,
  21. };
  22. it("完整字段通过,缺省 attachments 规范化为 []", () => {
  23. expect(conversationInputIntentSchema.parse(baseIntent)).toEqual({
  24. ...baseIntent,
  25. attachments: [],
  26. });
  27. const parsed = conversationInputIntentSchema.parse({
  28. ...baseIntent,
  29. attachments: [
  30. {
  31. ref: "artifact:1",
  32. fileName: "note.txt",
  33. mime: "text/plain",
  34. bytes: 4,
  35. },
  36. ],
  37. dispatch: { state: "reserved", reservationId: "reservation-1" },
  38. });
  39. expect(parsed.attachments).toHaveLength(1);
  40. expect(parsed.dispatch).toEqual({ state: "reserved", reservationId: "reservation-1" });
  41. });
  42. it("严格拒绝额外字段和非法顺序", () => {
  43. expect(
  44. conversationInputIntentSchema.safeParse({ ...baseIntent, unknownField: true }).success,
  45. ).toBe(false);
  46. expect(
  47. conversationInputIntentSchema.safeParse({
  48. ...baseIntent,
  49. order: { admissionSeq: -1 },
  50. }).success,
  51. ).toBe(false);
  52. });
  53. it("QueueItem 复用同一 intent,并把 dispatch 收窄为 queue lifecycle", () => {
  54. expect(
  55. queueItemSchema.parse({
  56. ...baseIntent,
  57. attachments: [],
  58. dispatch: { state: "queued" },
  59. }),
  60. ).toMatchObject({
  61. sourceCommandId: "cmd-input-1",
  62. queueItemId: "queue-1",
  63. clientId: "client-a",
  64. attachments: [],
  65. });
  66. expect(
  67. queueItemSchema.safeParse({
  68. ...baseIntent,
  69. attachments: [],
  70. dispatch: { state: "drained" },
  71. }).success,
  72. ).toBe(false);
  73. expect(
  74. queueItemSchema.safeParse({
  75. queueItemId: "queue-legacy",
  76. kind: "sendText",
  77. text: "lost fields",
  78. sourceCommandId: "cmd-legacy",
  79. submittedByClientId: "legacy",
  80. enqueuedAt: 1,
  81. }).success,
  82. ).toBe(false);
  83. });
  84. });
  85. describe("ZCode Protocol v4 commands/query", () => {
  86. const key = (index: number, sessionId: string | null = "session-1") => ({
  87. sessionId,
  88. commandId: `command-${index}`,
  89. });
  90. it("方法名冻结,1/64 个 key 与 null global key 通过", () => {
  91. expect(V4_METHODS.commandsQuery).toBe("v4/commands/query");
  92. expect(commandsQueryParamsSchema.parse({ commands: [key(1, null)] }).commands).toHaveLength(1);
  93. expect(
  94. commandsQueryParamsSchema.parse({
  95. commands: Array.from({ length: 64 }, (_, index) => key(index)),
  96. }).commands,
  97. ).toHaveLength(64);
  98. });
  99. it("0/65 个 key 与额外字段拒绝", () => {
  100. expect(commandsQueryParamsSchema.safeParse({ commands: [] }).success).toBe(false);
  101. expect(
  102. commandsQueryParamsSchema.safeParse({
  103. commands: Array.from({ length: 65 }, (_, index) => key(index)),
  104. }).success,
  105. ).toBe(false);
  106. expect(
  107. commandsQueryParamsSchema.safeParse({ commands: [key(1)], callerWorkspace: "/forged" })
  108. .success,
  109. ).toBe(false);
  110. });
  111. it("result 接受 ordered ack/unknown 且严格校验 envelope", () => {
  112. const parsed = commandsQueryResultSchema.parse({
  113. results: [
  114. { key: key(1), result: "unknown" },
  115. {
  116. key: key(2, null),
  117. result: {
  118. commandId: "command-2",
  119. status: "failed",
  120. reasonCode: "fault.command.inputDiscardedOnRestart",
  121. revisionAtDecision: 0,
  122. },
  123. },
  124. ],
  125. });
  126. expect(parsed.results.map((item) => item.key.commandId)).toEqual(["command-1", "command-2"]);
  127. expect(
  128. commandsQueryResultSchema.safeParse({
  129. results: [{ key: key(1), result: "unknown", extra: 1 }],
  130. }).success,
  131. ).toBe(false);
  132. });
  133. });