workspaceSessionReloadPlan.test.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildWorkspaceSessionReloadDraftError,
  4. buildWorkspaceSessionReloadPlan,
  5. shouldDebounceWorkspaceSessionReload,
  6. WORKSPACE_SESSION_RELOAD_DEBOUNCE_MS,
  7. } from "@/lib/workspaceSessionReloadPlan.js";
  8. describe("buildWorkspaceSessionReloadPlan", () => {
  9. it("有 activeTaskId 时返回 resumeTaskId,并跳过预热", () => {
  10. expect(buildWorkspaceSessionReloadPlan("task-123")).toEqual({
  11. resumeTaskId: "task-123",
  12. shouldPrepareWorkspace: false,
  13. });
  14. });
  15. it("activeTaskId 为空或仅空白时,不续接 task 且需要预热", () => {
  16. expect(buildWorkspaceSessionReloadPlan(null)).toEqual({
  17. resumeTaskId: undefined,
  18. shouldPrepareWorkspace: true,
  19. });
  20. expect(buildWorkspaceSessionReloadPlan(" ")).toEqual({
  21. resumeTaskId: undefined,
  22. shouldPrepareWorkspace: true,
  23. });
  24. });
  25. it("reload 失败时会生成可展示的草稿态错误", () => {
  26. const error = buildWorkspaceSessionReloadDraftError(
  27. { code: -32000, message: "Authentication required" },
  28. {
  29. workspacePath: "/repo/demo",
  30. provider: "glm",
  31. },
  32. );
  33. expect(error).toMatchObject({
  34. code: "WORKSPACE_PREPARE_FAILED",
  35. message: "Authentication required",
  36. detail: "workspace=/repo/demo provider=glm reason=reload-session attempt=1/1",
  37. });
  38. });
  39. it("短时间内重复触发 reload 会命中防抖", () => {
  40. expect(
  41. shouldDebounceWorkspaceSessionReload(
  42. 1_000,
  43. 1_000 + WORKSPACE_SESSION_RELOAD_DEBOUNCE_MS - 1,
  44. ),
  45. ).toBe(true);
  46. expect(
  47. shouldDebounceWorkspaceSessionReload(
  48. 1_000,
  49. 1_000 + WORKSPACE_SESSION_RELOAD_DEBOUNCE_MS,
  50. ),
  51. ).toBe(false);
  52. });
  53. });