backgroundTaskNotifications.test.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. parseZCodeBackgroundTaskNotificationText,
  4. zcodeBackgroundTaskNotificationToolUpdateStatus,
  5. } from "../src/background-task-notifications.js";
  6. describe("background task notifications", () => {
  7. it("preserves stopped task-notification status and normalizes killed to stopped", () => {
  8. expect(zcodeBackgroundTaskNotificationToolUpdateStatus("stopped")).toBe("stopped");
  9. expect(zcodeBackgroundTaskNotificationToolUpdateStatus("killed")).toBe("stopped");
  10. });
  11. it("normalizes terminal task-notification status", () => {
  12. expect(zcodeBackgroundTaskNotificationToolUpdateStatus("completed")).toBe("completed");
  13. expect(zcodeBackgroundTaskNotificationToolUpdateStatus("failed")).toBe("failed");
  14. expect(zcodeBackgroundTaskNotificationToolUpdateStatus("lost")).toBe("failed");
  15. expect(zcodeBackgroundTaskNotificationToolUpdateStatus("unexpected")).toBe("completed");
  16. expect(zcodeBackgroundTaskNotificationToolUpdateStatus(undefined)).toBe("completed");
  17. });
  18. it("parses failed notification error and decodes XML entities exactly once", () => {
  19. const providerMessage = "429 & retry; literal & marker";
  20. const summary = `Agent general-purpose task "Review" failed. ${providerMessage}`;
  21. const parsed = parseZCodeBackgroundTaskNotificationText(
  22. [
  23. "<task-notification>",
  24. "<task-id>agent_429</task-id>",
  25. "<tool-use-id>toolu_429</tool-use-id>",
  26. "<status>failed</status>",
  27. "<summary>Agent general-purpose task &quot;Review&quot; failed. 429 &amp; retry; literal &amp;amp; marker</summary>",
  28. "<error>429 &amp; retry; literal &amp;amp; marker</error>",
  29. "</task-notification>",
  30. ].join("\n"),
  31. );
  32. expect(parsed).toMatchObject({
  33. toolUseId: "toolu_429",
  34. notification: {
  35. taskId: "agent_429",
  36. status: "failed",
  37. summary,
  38. error: providerMessage,
  39. },
  40. });
  41. });
  42. });