sourceServerUpdateAction.test.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { ISourceServerManagementService } from "@zcode/services";
  3. import { runSourceServerUpdate } from "@/lib/sourceServerUpdateAction.js";
  4. const profileId = "17ef20b3-622d-4a53-98c1-0cdff29f3e6d";
  5. const snapshot = { generation: 1 } as never;
  6. describe("runSourceServerUpdate", () => {
  7. it("必须先普通更新,不把版本不兼容等价为强制中断", async () => {
  8. const updateSource = vi.fn(async () => snapshot);
  9. const confirmForce = vi.fn(async () => true);
  10. await runSourceServerUpdate({
  11. management: { updateSource } as unknown as ISourceServerManagementService,
  12. profileId,
  13. confirmForce,
  14. });
  15. expect(updateSource).toHaveBeenCalledOnce();
  16. expect(updateSource).toHaveBeenCalledWith({ profileId, force: false });
  17. expect(confirmForce).not.toHaveBeenCalled();
  18. });
  19. it("只在运行中 Task guard 后二次确认并强制重试", async () => {
  20. const guard = Object.assign(new Error("running tasks"), {
  21. code: "source-server-update-running-tasks",
  22. });
  23. const updateSource = vi.fn().mockRejectedValueOnce(guard).mockResolvedValueOnce(snapshot);
  24. const confirmForce = vi.fn(async () => true);
  25. await runSourceServerUpdate({
  26. management: { updateSource } as unknown as ISourceServerManagementService,
  27. profileId,
  28. confirmForce,
  29. });
  30. expect(confirmForce).toHaveBeenCalledOnce();
  31. expect(updateSource.mock.calls).toEqual([
  32. [{ profileId, force: false }],
  33. [{ profileId, force: true }],
  34. ]);
  35. });
  36. it("用户取消强制更新时不发送 force=true", async () => {
  37. const updateSource = vi.fn(async () => {
  38. throw Object.assign(new Error("running tasks"), {
  39. code: "source-server-update-running-tasks",
  40. });
  41. });
  42. await expect(
  43. runSourceServerUpdate({
  44. management: { updateSource } as unknown as ISourceServerManagementService,
  45. profileId,
  46. confirmForce: async () => false,
  47. }),
  48. ).resolves.toBeNull();
  49. expect(updateSource).toHaveBeenCalledOnce();
  50. });
  51. });
  52. it("force 确认后继续携带原 expectedTarget", async () => {
  53. const expectedTarget = { kind: "wsl", distro: "Ubuntu", user: "dev" } as const;
  54. const updateSource = vi
  55. .fn()
  56. .mockRejectedValueOnce(
  57. Object.assign(new Error("running"), { code: "source-server-update-running-tasks" }),
  58. )
  59. .mockResolvedValue(snapshot);
  60. await runSourceServerUpdate({
  61. management: { updateSource } as unknown as ISourceServerManagementService,
  62. profileId,
  63. expectedTarget,
  64. confirmForce: async () => true,
  65. });
  66. expect(updateSource.mock.calls).toEqual([
  67. [{ profileId, expectedTarget, force: false }],
  68. [{ profileId, expectedTarget, force: true }],
  69. ]);
  70. });