zcodeSessionTaskStatus.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. deriveZCodeTaskStatusFromSessionSnapshot,
  4. type ZCodeSessionStateSnapshot,
  5. } from "../src/index.js";
  6. type SnapshotOptions = {
  7. sessionStatus?: ZCodeSessionStateSnapshot["session"]["status"];
  8. assistantCompletedAt?: number;
  9. assistantFinish?: string;
  10. activeTurnKind?: ZCodeSessionStateSnapshot["runtime"]["activeTurnKind"];
  11. activeTurnId?: string;
  12. currentTurnId?: string;
  13. lastError?: ZCodeSessionStateSnapshot["projection"]["lastError"];
  14. };
  15. function snapshot(options: SnapshotOptions = {}): ZCodeSessionStateSnapshot {
  16. const messages: ZCodeSessionStateSnapshot["messages"] = [
  17. {
  18. info: {
  19. messageId: "msg_user",
  20. role: "user",
  21. time: { created: 1, completed: 1 },
  22. },
  23. parts: [{ partId: "part_user", type: "text", text: "continue" }],
  24. },
  25. ] as ZCodeSessionStateSnapshot["messages"];
  26. if (typeof options.assistantCompletedAt === "number" || options.assistantFinish) {
  27. messages.push({
  28. info: {
  29. messageId: "msg_assistant",
  30. sessionId: "sess_status",
  31. parentMessageId: "msg_user",
  32. role: "assistant",
  33. time: {
  34. created: 2,
  35. ...(typeof options.assistantCompletedAt === "number"
  36. ? { completed: options.assistantCompletedAt }
  37. : {}),
  38. },
  39. finish: options.assistantFinish,
  40. },
  41. parts: [{ partId: "part_assistant", type: "text", text: "done" }],
  42. } as ZCodeSessionStateSnapshot["messages"][number]);
  43. }
  44. return {
  45. session: {
  46. status: options.sessionStatus ?? "idle",
  47. },
  48. projection: {
  49. target: null,
  50. lastError: options.lastError ?? null,
  51. currentTurnId: options.currentTurnId,
  52. pendingPermissions: [],
  53. activeToolCalls: [],
  54. },
  55. runtime: {
  56. activeTurnId: options.activeTurnId,
  57. activeTurnKind: options.activeTurnKind,
  58. },
  59. messages,
  60. } as unknown as ZCodeSessionStateSnapshot;
  61. }
  62. describe("deriveZCodeTaskStatusFromSessionSnapshot", () => {
  63. it("treats idle snapshots with a completed visible assistant turn as completed", () => {
  64. expect(
  65. deriveZCodeTaskStatusFromSessionSnapshot(
  66. snapshot({
  67. sessionStatus: "idle",
  68. assistantCompletedAt: 3,
  69. }),
  70. ),
  71. ).toBe("completed");
  72. });
  73. it("does not treat tool-call continuation assistant turns as completed", () => {
  74. expect(
  75. deriveZCodeTaskStatusFromSessionSnapshot(
  76. snapshot({
  77. sessionStatus: "idle",
  78. assistantCompletedAt: 3,
  79. assistantFinish: "tool_calls",
  80. }),
  81. ),
  82. ).toBeUndefined();
  83. });
  84. it("keeps active runtime snapshots running even when session status is idle", () => {
  85. expect(
  86. deriveZCodeTaskStatusFromSessionSnapshot(
  87. snapshot({
  88. sessionStatus: "idle",
  89. activeTurnId: "turn_active",
  90. }),
  91. ),
  92. ).toBe("running");
  93. });
  94. it("does not treat the last projected turn id as active runtime", () => {
  95. expect(
  96. deriveZCodeTaskStatusFromSessionSnapshot(
  97. snapshot({
  98. sessionStatus: "idle",
  99. currentTurnId: "turn_completed",
  100. }),
  101. ),
  102. ).toBeUndefined();
  103. });
  104. it("keeps completed idle snapshots completed when only currentTurnId remains", () => {
  105. expect(
  106. deriveZCodeTaskStatusFromSessionSnapshot(
  107. snapshot({
  108. sessionStatus: "idle",
  109. currentTurnId: "turn_completed",
  110. assistantCompletedAt: 3,
  111. }),
  112. ),
  113. ).toBe("completed");
  114. });
  115. it("treats a completed assistant as terminal when only stale projection currentTurnId remains", () => {
  116. expect(
  117. deriveZCodeTaskStatusFromSessionSnapshot(
  118. snapshot({
  119. sessionStatus: "running",
  120. assistantCompletedAt: 3,
  121. currentTurnId: "turn_stale",
  122. }),
  123. ),
  124. ).toBe("completed");
  125. });
  126. it("keeps real active compact snapshots running even after a previous assistant completed", () => {
  127. expect(
  128. deriveZCodeTaskStatusFromSessionSnapshot(
  129. snapshot({
  130. sessionStatus: "running",
  131. assistantCompletedAt: 3,
  132. activeTurnId: "turn_compact",
  133. activeTurnKind: "compact",
  134. }),
  135. ),
  136. ).toBe("running");
  137. });
  138. it("prefers projection lastError over a previous completed assistant message", () => {
  139. expect(
  140. deriveZCodeTaskStatusFromSessionSnapshot(
  141. snapshot({
  142. sessionStatus: "idle",
  143. assistantCompletedAt: 3,
  144. lastError: { message: "quota exceeded" },
  145. }),
  146. ),
  147. ).toBe("error");
  148. });
  149. });