sessionsIndexRegistryEndpointMismatch.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  2. import { Emitter } from "@zcode/rpc";
  3. import type { IServiceAccessor } from "@zcode/services";
  4. const { lifecycleWarn } = vi.hoisted(() => ({ lifecycleWarn: vi.fn() }));
  5. vi.mock("@/logger.js", () => ({
  6. logger: {
  7. debug: vi.fn(),
  8. info: vi.fn(),
  9. warn: vi.fn(),
  10. error: vi.fn(),
  11. lifecycle: { info: vi.fn(), warn: lifecycleWarn, error: vi.fn() },
  12. },
  13. }));
  14. import {
  15. acquireSessionsIndex,
  16. releaseSessionsIndex,
  17. resetSessionsIndexScopeEndpointMismatchReportsForTest,
  18. sessionsIndexRegistrySize,
  19. type SessionsIndexAgentService,
  20. type SessionsIndexScope,
  21. } from "@/v4/sessionsIndexRegistry.js";
  22. import {
  23. registerRemoteWorkspaceSession,
  24. unregisterRemoteWorkspaceSession,
  25. } from "@/store/remoteWorkspaceSessionStore.js";
  26. const workspacePath = "/root/src/plancov";
  27. const workspaceIdentity = "remote:wsl:default:root:/root/src/plancov";
  28. const remoteSessionId = "remote-session-plancov";
  29. function mockAgentService(): SessionsIndexAgentService {
  30. const runtimeRestarts = new Emitter<{ workspaceKey: string }>();
  31. return {
  32. helloConversationV4: vi.fn(async () => ({
  33. kind: "hello" as const,
  34. protocolVersion: 3 as const,
  35. connectionId: "test-connection",
  36. clientMode: "desktop-continuous" as const,
  37. deliveryProfile: "continuous" as const,
  38. serverTime: 1,
  39. capabilities: {
  40. nativeDialogs: true,
  41. localTerminal: true,
  42. binaryFrames: false,
  43. compression: "none" as const,
  44. },
  45. auth: {},
  46. })),
  47. initializeConversationV4: vi.fn(async () => {}),
  48. subscribeSessionsIndexV4: vi.fn(async () => ({
  49. ack: { subscriptionId: "sub-1", mode: "snapshot" as const, logEpoch: "e1" },
  50. })),
  51. unsubscribeSessionsIndexV4: vi.fn(async () => {}),
  52. resyncSessionsIndexV4: vi.fn(async (params: { subscriptionId: string }) => ({
  53. ack: { subscriptionId: params.subscriptionId, mode: "snapshot" as const, logEpoch: "e1" },
  54. })),
  55. onDynamicSessionsIndexFrame: vi.fn(() => () => ({ dispose: () => {} })),
  56. onAgentRuntimeRestarted: runtimeRestarts.event,
  57. } as unknown as SessionsIndexAgentService;
  58. }
  59. const remoteScopeWithoutEndpoint: SessionsIndexScope = {
  60. workspaceKey: workspaceIdentity,
  61. workspacePath,
  62. workspaceIdentity,
  63. };
  64. describe("sessionsIndexRegistry scope endpoint mismatch 观测", () => {
  65. let remoteService: SessionsIndexAgentService;
  66. beforeEach(() => {
  67. lifecycleWarn.mockClear();
  68. resetSessionsIndexScopeEndpointMismatchReportsForTest();
  69. remoteService = mockAgentService();
  70. registerRemoteWorkspaceSession({
  71. sessionId: remoteSessionId,
  72. services: { zcodeAgentService: remoteService } as unknown as IServiceAccessor,
  73. });
  74. });
  75. afterEach(() => {
  76. unregisterRemoteWorkspaceSession(remoteSessionId);
  77. expect(sessionsIndexRegistrySize()).toBe(0);
  78. });
  79. it("远程代理却缺省 endpointKey 时落一条 lifecycle warn,并按 scope 去重", () => {
  80. const store = acquireSessionsIndex(remoteScopeWithoutEndpoint, remoteService);
  81. const storeAgain = acquireSessionsIndex(remoteScopeWithoutEndpoint, remoteService);
  82. expect(lifecycleWarn).toHaveBeenCalledTimes(1);
  83. expect(lifecycleWarn).toHaveBeenCalledWith(
  84. "[v4-sessions-index] scope endpoint 与远程代理归属不一致",
  85. expect.objectContaining({
  86. event: "v4.sessions_index.scope_endpoint_mismatch",
  87. endpointKey: "__base__",
  88. remoteSessionId,
  89. workspaceKey: workspaceIdentity,
  90. workspacePath,
  91. }),
  92. );
  93. releaseSessionsIndex(remoteScopeWithoutEndpoint, store);
  94. releaseSessionsIndex(remoteScopeWithoutEndpoint, storeAgain);
  95. });
  96. it("endpointKey 与远程代理归属一致时不告警", () => {
  97. const scope: SessionsIndexScope = {
  98. ...remoteScopeWithoutEndpoint,
  99. endpointKey: remoteSessionId,
  100. };
  101. const store = acquireSessionsIndex(scope, remoteService);
  102. expect(lifecycleWarn).not.toHaveBeenCalled();
  103. releaseSessionsIndex(scope, store);
  104. });
  105. it("endpointKey 指向另一个远程 session 时同样告警", () => {
  106. const scope: SessionsIndexScope = {
  107. ...remoteScopeWithoutEndpoint,
  108. endpointKey: "remote-session-other",
  109. };
  110. const store = acquireSessionsIndex(scope, remoteService);
  111. expect(lifecycleWarn).toHaveBeenCalledTimes(1);
  112. expect(lifecycleWarn).toHaveBeenCalledWith(
  113. "[v4-sessions-index] scope endpoint 与远程代理归属不一致",
  114. expect.objectContaining({ endpointKey: "remote-session-other", remoteSessionId }),
  115. );
  116. releaseSessionsIndex(scope, store);
  117. });
  118. it("本地 base 代理缺省 endpointKey 属于正常形态,不告警", () => {
  119. const localService = mockAgentService();
  120. const scope: SessionsIndexScope = { workspaceKey: "D:\\ws", workspacePath: "D:\\ws" };
  121. const store = acquireSessionsIndex(scope, localService);
  122. expect(lifecycleWarn).not.toHaveBeenCalled();
  123. releaseSessionsIndex(scope, store);
  124. });
  125. });