tui-session-event-relay.test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // TUI 常驻会话事件中继:换 app 时必须重挂(docs/dynamic-workflow/launch.md「The inline card」)。
  2. //
  3. // 这条性质的失效是安静的:/new、/resume、/fork 之后订阅还挂在旧 runtime 上,TUI 就再也收不到
  4. // 出回合事件(dwf 进度、后台通知驱动的回合),而界面看起来完全正常。所以它必须有测试。
  5. import assert from "node:assert/strict";
  6. import test from "node:test";
  7. import type { SessionEvent } from "@zcode/contracts";
  8. import { createTuiSessionEventRelay } from "../src/tui-session-event-relay.js";
  9. /** 一个假 runtime:记录挂载/卸载次数,并能主动发事件。 */
  10. function fakeRuntime(name: string) {
  11. const state = {
  12. name,
  13. attachCount: 0,
  14. detachCount: 0,
  15. emit: (_event: SessionEvent) => {},
  16. attached: false,
  17. };
  18. const subscribe = (sink: { onSessionEvent: (event: SessionEvent) => void }) => {
  19. state.attachCount += 1;
  20. state.attached = true;
  21. state.emit = (event) => sink.onSessionEvent(event);
  22. return () => {
  23. state.detachCount += 1;
  24. state.attached = false;
  25. state.emit = () => {};
  26. };
  27. };
  28. return { state, runtime: { subscribeEvents: subscribe } };
  29. }
  30. const readSubscriber = (runtime: unknown) => {
  31. const candidate = (runtime as { subscribeEvents?: unknown } | undefined)?.subscribeEvents;
  32. return typeof candidate === "function"
  33. ? (candidate as (sink: { onSessionEvent: (event: SessionEvent) => void }) => () => void)
  34. : undefined;
  35. };
  36. const event = (id: string) =>
  37. ({ id, type: "dynamic_workflow_run_progress" }) as unknown as SessionEvent;
  38. test("the first sink attaches to the current runtime", () => {
  39. const first = fakeRuntime("first");
  40. const relay = createTuiSessionEventRelay({
  41. currentRuntime: () => first.runtime,
  42. readSubscriber,
  43. });
  44. assert.equal(relay.isAttached(), false);
  45. const received: string[] = [];
  46. relay.addSink((e) => received.push(e.id));
  47. assert.equal(relay.isAttached(), true);
  48. assert.equal(first.state.attachCount, 1);
  49. first.state.emit(event("e1"));
  50. assert.deepEqual(received, ["e1"]);
  51. });
  52. test("reattach after an app swap detaches the old runtime and follows the new one", () => {
  53. const first = fakeRuntime("first");
  54. const second = fakeRuntime("second");
  55. let current: unknown = first.runtime;
  56. const relay = createTuiSessionEventRelay({
  57. currentRuntime: () => current,
  58. readSubscriber,
  59. });
  60. const received: string[] = [];
  61. relay.addSink((e) => received.push(e.id));
  62. first.state.emit(event("before-swap"));
  63. // replaceApp 换 app 后调 reattach。
  64. current = second.runtime;
  65. relay.reattach();
  66. assert.equal(first.state.detachCount, 1, "old runtime must be detached");
  67. assert.equal(second.state.attachCount, 1, "new runtime must be attached");
  68. assert.equal(first.state.attached, false);
  69. // 新 runtime 的事件到得了;旧的已经断开,发不出东西。
  70. second.state.emit(event("after-swap"));
  71. first.state.emit(event("orphan"));
  72. assert.deepEqual(received, ["before-swap", "after-swap"]);
  73. });
  74. test("sinks survive the swap without re-registering", () => {
  75. const first = fakeRuntime("first");
  76. const second = fakeRuntime("second");
  77. let current: unknown = first.runtime;
  78. const relay = createTuiSessionEventRelay({ currentRuntime: () => current, readSubscriber });
  79. const received: string[] = [];
  80. relay.addSink((e) => received.push(e.id));
  81. current = second.runtime;
  82. relay.reattach();
  83. current = fakeRuntime("third").runtime;
  84. relay.reattach();
  85. // 同一个 sink 跨两次换 app 仍然在册(TUI 侧不需要重挂 useEffect)。
  86. assert.equal(relay.isAttached(), true);
  87. assert.equal(received.length, 0);
  88. });
  89. test("reattach with no sinks stays detached (no subscription leak)", () => {
  90. const only = fakeRuntime("only");
  91. const relay = createTuiSessionEventRelay({ currentRuntime: () => only.runtime, readSubscriber });
  92. relay.reattach();
  93. assert.equal(relay.isAttached(), false);
  94. assert.equal(only.state.attachCount, 0);
  95. });
  96. test("the last sink unsubscribing detaches the runtime", () => {
  97. const only = fakeRuntime("only");
  98. const relay = createTuiSessionEventRelay({ currentRuntime: () => only.runtime, readSubscriber });
  99. const stopA = relay.addSink(() => {});
  100. const stopB = relay.addSink(() => {});
  101. stopA();
  102. assert.equal(relay.isAttached(), true, "one remaining sink keeps the subscription");
  103. stopB();
  104. assert.equal(relay.isAttached(), false);
  105. assert.equal(only.state.detachCount, 1);
  106. });
  107. test("a runtime with no subscribeEvents degrades to unattached rather than throwing", () => {
  108. const relay = createTuiSessionEventRelay({ currentRuntime: () => ({}), readSubscriber });
  109. const received: string[] = [];
  110. relay.addSink((e) => received.push(e.id));
  111. assert.equal(relay.isAttached(), false);
  112. // 能力缺席不该让 TUI 起不来。
  113. relay.reattach();
  114. assert.deepEqual(received, []);
  115. });
  116. test("a sink unsubscribing during fan-out does not break the dispatch", () => {
  117. const only = fakeRuntime("only");
  118. const relay = createTuiSessionEventRelay({ currentRuntime: () => only.runtime, readSubscriber });
  119. const seen: string[] = [];
  120. let stopSelf: (() => void) | undefined;
  121. stopSelf = relay.addSink((e) => {
  122. seen.push(`a:${e.id}`);
  123. stopSelf?.();
  124. });
  125. relay.addSink((e) => seen.push(`b:${e.id}`));
  126. only.state.emit(event("e1"));
  127. // 第一个 sink 在回调里退订,第二个仍然收到本条。
  128. assert.deepEqual(seen, ["a:e1", "b:e1"]);
  129. only.state.emit(event("e2"));
  130. assert.deepEqual(seen, ["a:e1", "b:e1", "b:e2"]);
  131. });
  132. // ── WP-F:会话闸门必须跟着 replaceApp 换会话(getter,不是快照)──
  133. test("the main session id is read per call, so it follows a replaceApp swap", () => {
  134. // 这条钉的是 CLI 侧 getMainSessionId 的形状:现读 app.runtime.getSessionId()。
  135. // 快照下来的 id 在 /new /resume /fork 之后会过期,然后把**整条**主转写误判成外来事件——
  136. // 比它要修的 actor 噪音严重得多。
  137. let currentSessionId = "session-main-1";
  138. const getMainSessionId = () => currentSessionId;
  139. assert.equal(getMainSessionId(), "session-main-1");
  140. currentSessionId = "session-main-2";
  141. assert.equal(getMainSessionId(), "session-main-2", "a snapshot would still report the old id");
  142. });
  143. test("a runtime with no getSessionId yields undefined, which fails the gate open", () => {
  144. const readMainSessionId = (runtime: unknown): string | undefined => {
  145. const sessionId = (runtime as { getSessionId?: () => string } | undefined)?.getSessionId?.();
  146. return typeof sessionId === "string" && sessionId.length > 0 ? sessionId : undefined;
  147. };
  148. assert.equal(readMainSessionId(undefined), undefined);
  149. assert.equal(readMainSessionId({}), undefined);
  150. assert.equal(readMainSessionId({ getSessionId: () => "" }), undefined);
  151. assert.equal(readMainSessionId({ getSessionId: () => "session-x" }), "session-x");
  152. });