sourceServerStartupReconnect.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { describe, expect, it, vi } from "vitest";
  2. import { runStartupReconnectBatch } from "../src/host/sourceServerStartupReconnect.js";
  3. import type { SourceServerReconnectCandidate } from "../src/host/sourceServerDirectory.js";
  4. const candidate = {
  5. profile: {
  6. profileId: "17ef20b3-622d-4a53-98c1-0cdff29f3e6d",
  7. transport: "docker",
  8. target: { kind: "docker", container: "zcode-dev" },
  9. createdAt: 1,
  10. },
  11. target: { kind: "docker", container: "zcode-dev" },
  12. } as unknown as SourceServerReconnectCandidate;
  13. function createDeps(remoteStartupAutoReconnectEnabled?: boolean) {
  14. const listCandidates = vi.fn(async () => [candidate]);
  15. const reconnect = vi.fn(async (candidates: SourceServerReconnectCandidate[]) =>
  16. candidates.map(({ profile }) => ({
  17. profileId: profile.profileId,
  18. status: "online" as const,
  19. })),
  20. );
  21. return {
  22. deps: {
  23. getSettings: vi.fn(async () => ({ remoteStartupAutoReconnectEnabled })),
  24. listCandidates,
  25. reconnect,
  26. },
  27. listCandidates,
  28. reconnect,
  29. };
  30. }
  31. describe("runStartupReconnectBatch", () => {
  32. it("skips the whole startup batch when the preference is disabled", async () => {
  33. const { deps, listCandidates, reconnect } = createDeps(false);
  34. await expect(runStartupReconnectBatch(deps)).resolves.toEqual([]);
  35. // 合同 §8.2:关闭 = 跳过整个启动批次,候选枚举都不得执行;
  36. // 手动连接与断线恢复不经过本入口,因此不受该设置影响。
  37. expect(listCandidates).not.toHaveBeenCalled();
  38. expect(reconnect).not.toHaveBeenCalled();
  39. });
  40. it("runs the batch when the preference is explicitly enabled", async () => {
  41. const { deps, reconnect } = createDeps(true);
  42. await expect(runStartupReconnectBatch(deps)).resolves.toEqual([
  43. { profileId: candidate.profile.profileId, status: "online" },
  44. ]);
  45. expect(reconnect).toHaveBeenCalledWith([candidate]);
  46. });
  47. it("treats a missing preference as enabled to keep old settings behavior", async () => {
  48. const { deps, reconnect } = createDeps(undefined);
  49. await expect(runStartupReconnectBatch(deps)).resolves.toEqual([
  50. { profileId: candidate.profile.profileId, status: "online" },
  51. ]);
  52. expect(reconnect).toHaveBeenCalledWith([candidate]);
  53. });
  54. });