zcodeTaskMetaSchema.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { describe, expect, it } from "vitest";
  2. import { zcodeSessionFileSchema } from "../src/validation.js";
  3. describe("zcodeTaskMetaSchema", () => {
  4. it("会保留 unread 持久化字段", () => {
  5. const parsed = zcodeSessionFileSchema.parse({
  6. meta: {
  7. taskId: "task-schema",
  8. traceId: "trace-schema",
  9. title: "Schema roundtrip",
  10. workspacePath: "/tmp/workspace",
  11. createdAt: 1,
  12. updatedAt: 2,
  13. mode: "default",
  14. provider: "codex",
  15. unreadAt: 12,
  16. },
  17. messages: [],
  18. });
  19. expect(parsed.meta.unreadAt).toBe(12);
  20. });
  21. it("会保留 zcode target 持久化字段", () => {
  22. const parsed = zcodeSessionFileSchema.parse({
  23. meta: {
  24. taskId: "task-target",
  25. traceId: "trace-target",
  26. title: "Target roundtrip",
  27. workspacePath: "/tmp/workspace",
  28. createdAt: 1,
  29. updatedAt: 2,
  30. mode: "default",
  31. provider: "glm",
  32. target: {
  33. sessionID: "session-target",
  34. targetID: "target-1",
  35. objective: "finish target integration",
  36. summaryTitle: "Finish target integration",
  37. status: "active",
  38. tokenBudget: 1000,
  39. tokensUsed: 12,
  40. timeUsedSeconds: 3,
  41. time: {
  42. created: 1,
  43. updated: 2,
  44. },
  45. },
  46. },
  47. messages: [],
  48. });
  49. expect(parsed.meta.target?.objective).toBe("finish target integration");
  50. expect(parsed.meta.target?.tokensUsed).toBe(12);
  51. });
  52. it("兼容旧 task target 缺失 summaryTitle", () => {
  53. const parsed = zcodeSessionFileSchema.parse({
  54. meta: {
  55. taskId: "task-target-legacy",
  56. traceId: "trace-target-legacy",
  57. title: "Legacy target",
  58. workspacePath: "/tmp/workspace",
  59. createdAt: 1,
  60. updatedAt: 2,
  61. mode: "default",
  62. provider: "glm",
  63. target: {
  64. sessionID: "session-target",
  65. targetID: "target-1",
  66. objective: "finish target integration",
  67. status: "complete",
  68. tokenBudget: null,
  69. tokensUsed: 12,
  70. timeUsedSeconds: 3,
  71. time: {
  72. created: 1,
  73. updated: 2,
  74. },
  75. },
  76. },
  77. messages: [],
  78. });
  79. expect(parsed.meta.target?.summaryTitle).toBeNull();
  80. });
  81. it("会保留 task 最后失败原因", () => {
  82. const parsed = zcodeSessionFileSchema.parse({
  83. meta: {
  84. taskId: "task-last-error",
  85. traceId: "trace-last-error",
  86. title: "Last error roundtrip",
  87. workspacePath: "/tmp/workspace",
  88. createdAt: 1,
  89. updatedAt: 2,
  90. mode: "default",
  91. provider: "claude",
  92. status: "error",
  93. lastError: {
  94. code: "EMPTY_REPLY",
  95. message: "Agent 未产生任何回复,请重试。",
  96. traceId: "trace-last-error",
  97. taskId: "task-last-error",
  98. attribution: {
  99. source: "runtime",
  100. reason: "model_config_missing",
  101. },
  102. },
  103. },
  104. messages: [],
  105. });
  106. expect(parsed.meta.lastError).toMatchObject({
  107. code: "EMPTY_REPLY",
  108. message: "Agent 未产生任何回复,请重试。",
  109. attribution: {
  110. source: "runtime",
  111. reason: "model_config_missing",
  112. },
  113. });
  114. });
  115. it("会拒绝非法 zcode target 状态", () => {
  116. expect(() =>
  117. zcodeSessionFileSchema.parse({
  118. meta: {
  119. taskId: "task-target-invalid",
  120. traceId: "trace-target-invalid",
  121. title: "Target invalid",
  122. workspacePath: "/tmp/workspace",
  123. createdAt: 1,
  124. updatedAt: 2,
  125. mode: "default",
  126. provider: "glm",
  127. target: {
  128. sessionID: "session-target",
  129. targetID: "target-1",
  130. objective: "finish target integration",
  131. summaryTitle: "Finish target integration",
  132. status: "sleeping",
  133. tokenBudget: 1000,
  134. tokensUsed: 12,
  135. timeUsedSeconds: 3,
  136. time: {
  137. created: 1,
  138. updated: 2,
  139. },
  140. },
  141. },
  142. messages: [],
  143. }),
  144. ).toThrow();
  145. });
  146. });