windowHostControllerService.test.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. import { describe, expect, it, vi } from "vitest";
  2. import { Emitter } from "@zcode/rpc";
  3. import type { ZCodeTaskMeta } from "@zcode/shared";
  4. import type {
  5. IZCodeAgentService,
  6. IZCodeTaskService,
  7. WindowHostControllerFrame,
  8. } from "@zcode/services";
  9. import {
  10. CONTROLLER_TASKS_INDEX_TOPIC,
  11. type SessionsIndexTopicWireCandidate,
  12. } from "@zcode/shared/zcode-protocol-v4";
  13. import { createWindowHostControllerRuntime } from "../src/host/windowHostControllerService.js";
  14. function meta(params: {
  15. taskId: string;
  16. workspacePath?: string;
  17. workspaceIdentity?: string;
  18. createdAt?: number;
  19. }): ZCodeTaskMeta {
  20. return {
  21. taskId: params.taskId,
  22. traceId: `trace-${params.taskId}`,
  23. title: params.taskId,
  24. workspacePath: params.workspacePath ?? "/work/demo",
  25. ...(params.workspaceIdentity ? { workspaceIdentity: params.workspaceIdentity } : {}),
  26. createdAt: params.createdAt ?? 1,
  27. updatedAt: params.createdAt ?? 1,
  28. mode: "default",
  29. };
  30. }
  31. function taskService(params: {
  32. active?: ZCodeTaskMeta[];
  33. pinned?: ZCodeTaskMeta[];
  34. archived?: ZCodeTaskMeta[];
  35. search?: ZCodeTaskMeta[];
  36. }) {
  37. return {
  38. listTasks: vi.fn(async () => params.active ?? []),
  39. listPinnedTasks: vi.fn(async () => params.pinned ?? []),
  40. listArchivedTasks: vi.fn(async () => params.archived ?? []),
  41. listTaskList: vi.fn(async () => ({
  42. items: params.search ?? [],
  43. total: params.search?.length ?? 0,
  44. hasMore: false,
  45. })),
  46. setTaskPinned: vi.fn(async ({ pinned }: { pinned: boolean }) =>
  47. meta({ taskId: pinned ? "pinned" : "unpinned" }),
  48. ),
  49. archiveTask: vi.fn(async () => meta({ taskId: "archived" })),
  50. unarchiveTask: vi.fn(async () => meta({ taskId: "unarchived" })),
  51. deleteTask: vi.fn(async () => undefined),
  52. setTaskUnread: vi.fn(async () => meta({ taskId: "unread" })),
  53. } as unknown as IZCodeTaskService;
  54. }
  55. const remoteIdentity = "remote:ssh:dev:22:user:/work/demo";
  56. describe("WindowHostControllerService", () => {
  57. it("从 source Agent 的 existing-only sessions-index 读取 live activity", async () => {
  58. const frames = new Emitter<SessionsIndexTopicWireCandidate>();
  59. const runtimeLifecycle = new Emitter<{
  60. workspaceKey: string;
  61. workspacePath: string;
  62. workspaceIdentity?: string;
  63. state: "available" | "unavailable";
  64. runtimeIdentity: { workspaceKey: string; generation: number };
  65. }>();
  66. const agentService = {
  67. onDynamicSessionsIndexFrame: vi.fn(() => frames.event),
  68. onAgentRuntimeLifecycle: runtimeLifecycle.event,
  69. onAgentRuntimeRestarted: vi.fn(() => ({ dispose: vi.fn() })),
  70. subscribeSessionsIndexV4: vi.fn(async () => {
  71. frames.fire({
  72. wireVersion: 3,
  73. kind: "complete",
  74. deliveryKind: "initial",
  75. logicalFrameId: "initial-1",
  76. logicalFrameOrdinal: 1,
  77. topic: "sessions-index//work/demo",
  78. subscriptionId: "sessions-sub-1",
  79. frame: {
  80. topic: "sessions-index//work/demo",
  81. subscriptionId: "sessions-sub-1",
  82. fromSeq: 0,
  83. toSeq: 1,
  84. sentAt: 1,
  85. payload: {
  86. kind: "snapshot",
  87. snapshot: {
  88. protocolVersion: 1,
  89. workspaceId: "/work/demo",
  90. logEpoch: "sessions-epoch-1",
  91. sessions: [
  92. {
  93. sessionId: "live",
  94. workspaceId: "/work/demo",
  95. title: "正在运行",
  96. phase: "running",
  97. sessionEnded: false,
  98. hasBackgroundWork: true,
  99. workflowActivity: {
  100. runs: [
  101. {
  102. runId: "run-1",
  103. toolCallId: "tool-1",
  104. name: "Deep research",
  105. status: "running",
  106. phases: [{ name: "Research", status: "running" }],
  107. currentPhase: "Research",
  108. agentsWorking: 1,
  109. },
  110. ],
  111. },
  112. pendingInteraction: {
  113. interactionId: "ask-1",
  114. kind: "userInput",
  115. toolName: "AskUserQuestion",
  116. },
  117. pendingInteractionSummary: {
  118. permissionCount: 0,
  119. userInputCount: 1,
  120. },
  121. lastActivityAt: 20,
  122. createdAt: 1,
  123. },
  124. ],
  125. },
  126. },
  127. },
  128. });
  129. return {
  130. ack: { subscriptionId: "sessions-sub-1", mode: "snapshot", logEpoch: "sessions-epoch-1" },
  131. };
  132. }),
  133. resyncSessionsIndexV4: vi.fn(),
  134. unsubscribeSessionsIndexV4: vi.fn(async () => undefined),
  135. } as unknown as IZCodeAgentService;
  136. const local = taskService({ active: [meta({ taskId: "live" })] });
  137. const runtime = createWindowHostControllerRuntime({
  138. createId: () => "id",
  139. resolveSource: (scope) => ({
  140. scope: { kind: "local" as const, ...scope },
  141. taskService: local,
  142. agentService,
  143. sourceAvailability: "online" as const,
  144. }),
  145. });
  146. const result = await runtime.service.listTaskList({
  147. kind: "active",
  148. workspaceScopes: [{ workspacePath: "/work/demo" }],
  149. sortBy: "updated",
  150. });
  151. expect(agentService.subscribeSessionsIndexV4).toHaveBeenCalledWith(
  152. expect.objectContaining({
  153. workspacePath: "/work/demo",
  154. subscriberScope: "window-controller",
  155. runtimePolicy: "existing-only",
  156. }),
  157. );
  158. expect(result.items).toEqual([
  159. expect.objectContaining({
  160. taskId: "live",
  161. title: "正在运行",
  162. updatedAt: 20,
  163. liveStatus: "waiting",
  164. pendingInteraction: expect.objectContaining({ interactionId: "ask-1" }),
  165. activity: {
  166. phase: "running",
  167. lastActivityAt: 20,
  168. hasBackgroundWork: true,
  169. workflowActivity: {
  170. runs: [
  171. {
  172. runId: "run-1",
  173. toolCallId: "tool-1",
  174. name: "Deep research",
  175. status: "running",
  176. phases: [{ name: "Research", status: "running" }],
  177. currentPhase: "Research",
  178. agentsWorking: 1,
  179. },
  180. ],
  181. },
  182. pendingInteractions: { permissionCount: 0, userInputCount: 1 },
  183. },
  184. }),
  185. ]);
  186. frames.fire({
  187. wireVersion: 3,
  188. kind: "complete",
  189. deliveryKind: "online",
  190. logicalFrameId: "delta-2",
  191. logicalFrameOrdinal: 2,
  192. topic: "sessions-index//work/demo",
  193. subscriptionId: "sessions-sub-1",
  194. frame: {
  195. topic: "sessions-index//work/demo",
  196. subscriptionId: "sessions-sub-1",
  197. fromSeq: 1,
  198. toSeq: 2,
  199. sentAt: 2,
  200. payload: {
  201. kind: "deltas",
  202. deltas: [
  203. {
  204. op: "session.upserted",
  205. session: {
  206. sessionId: "live",
  207. workspaceId: "/work/demo",
  208. title: "已经完成",
  209. phase: "completedSuccess",
  210. sessionEnded: true,
  211. hasBackgroundWork: false,
  212. lastActivityAt: 30,
  213. createdAt: 1,
  214. },
  215. },
  216. ],
  217. },
  218. },
  219. });
  220. const completed = await runtime.service.listTaskList({
  221. kind: "active",
  222. workspaceScopes: [{ workspacePath: "/work/demo" }],
  223. sortBy: "updated",
  224. });
  225. expect(completed.items[0]).toMatchObject({
  226. liveStatus: "completed",
  227. activity: { phase: "completedSuccess", lastActivityAt: 30 },
  228. });
  229. expect(local.listTasks).toHaveBeenCalledTimes(1);
  230. expect(local.listPinnedTasks).toHaveBeenCalledTimes(1);
  231. expect(local.listArchivedTasks).toHaveBeenCalledTimes(1);
  232. runtime.dispose();
  233. });
  234. it("在 Host 内聚合 local 与 remote task rows,并为结果附加 source 地址", async () => {
  235. const local = taskService({ active: [meta({ taskId: "local", createdAt: 1 })] });
  236. const remote = taskService({
  237. pinned: [meta({ taskId: "remote", workspaceIdentity: remoteIdentity, createdAt: 2 })],
  238. });
  239. const runtime = createWindowHostControllerRuntime({
  240. createId: () => "id",
  241. resolveSource: (scope) =>
  242. scope.workspaceIdentity
  243. ? {
  244. scope: {
  245. kind: "remote" as const,
  246. remoteSessionId: "remote-1",
  247. ...scope,
  248. workspaceIdentity: scope.workspaceIdentity,
  249. },
  250. taskService: remote,
  251. sourceAvailability: "online" as const,
  252. }
  253. : {
  254. scope: { kind: "local" as const, ...scope },
  255. taskService: local,
  256. sourceAvailability: "online" as const,
  257. },
  258. });
  259. const result = await runtime.service.listTaskList({
  260. kind: "active",
  261. workspaceScopes: [
  262. { workspacePath: "/work/demo" },
  263. { workspacePath: "/work/demo", workspaceIdentity: remoteIdentity },
  264. ],
  265. sortBy: "created",
  266. });
  267. expect(result.items.map((item) => item.taskId)).toEqual(["remote", "local"]);
  268. expect(result.items[0]).toMatchObject({
  269. remoteSessionId: "remote-1",
  270. sourceAvailability: "online",
  271. });
  272. });
  273. it("同一 source 的并发列表查询共享一次 task-index refresh", async () => {
  274. const local = taskService({ active: [meta({ taskId: "local" })] });
  275. const runtime = createWindowHostControllerRuntime({
  276. createId: () => "id",
  277. resolveSource: (scope) => ({
  278. scope: { kind: "local" as const, ...scope },
  279. taskService: local,
  280. sourceAvailability: "online" as const,
  281. }),
  282. });
  283. await Promise.all([
  284. runtime.service.listTaskList({
  285. kind: "active",
  286. workspaceScopes: [{ workspacePath: "/work/demo" }],
  287. sortBy: "updated",
  288. }),
  289. runtime.service.listTaskList({
  290. kind: "timeline",
  291. workspaceScopes: [{ workspacePath: "/work/demo" }],
  292. sortBy: "updated",
  293. }),
  294. ]);
  295. expect(local.listTasks).toHaveBeenCalledTimes(1);
  296. expect(local.listPinnedTasks).toHaveBeenCalledTimes(1);
  297. expect(local.listArchivedTasks).toHaveBeenCalledTimes(1);
  298. });
  299. it("断连后保留最后可信投影,非搜索可读但写入 fail-closed", async () => {
  300. const remote = taskService({
  301. active: [meta({ taskId: "remote", workspaceIdentity: remoteIdentity })],
  302. });
  303. let online = true;
  304. const runtime = createWindowHostControllerRuntime({
  305. createId: () => "id",
  306. resolveSource: (scope) => ({
  307. scope: {
  308. kind: "remote" as const,
  309. remoteSessionId: "remote-1",
  310. ...scope,
  311. workspaceIdentity: scope.workspaceIdentity!,
  312. },
  313. taskService: online ? remote : undefined,
  314. sourceAvailability: online ? "online" : "offline",
  315. }),
  316. });
  317. const query = {
  318. kind: "active" as const,
  319. workspaceScopes: [{ workspacePath: "/work/demo", workspaceIdentity: remoteIdentity }],
  320. sortBy: "updated" as const,
  321. };
  322. await runtime.service.listTaskList(query);
  323. online = false;
  324. runtime.disconnectSource({
  325. kind: "remote",
  326. remoteSessionId: "remote-1",
  327. workspacePath: "/work/demo",
  328. workspaceIdentity: remoteIdentity,
  329. });
  330. const offline = await runtime.service.listTaskList(query);
  331. expect(offline.items).toEqual([
  332. expect.objectContaining({ taskId: "remote", sourceAvailability: "offline" }),
  333. ]);
  334. await expect(
  335. runtime.service.mutateTask({
  336. address: {
  337. remoteSessionId: "remote-1",
  338. workspacePath: "/work/demo",
  339. workspaceIdentity: remoteIdentity,
  340. taskId: "remote",
  341. },
  342. mutation: { kind: "delete" },
  343. }),
  344. ).rejects.toThrow("远程 source 当前离线");
  345. expect(remote.deleteTask).not.toHaveBeenCalled();
  346. });
  347. it("全文搜索只查询在线 source,不用离线摘要伪造结果", async () => {
  348. const runtime = createWindowHostControllerRuntime({
  349. createId: () => "id",
  350. resolveSource: (scope) => ({
  351. scope: {
  352. kind: "remote" as const,
  353. remoteSessionId: "remote-1",
  354. ...scope,
  355. workspaceIdentity: scope.workspaceIdentity!,
  356. },
  357. sourceAvailability: "offline" as const,
  358. }),
  359. });
  360. const result = await runtime.service.listTaskList({
  361. kind: "active",
  362. workspaceScopes: [{ workspacePath: "/work/demo", workspaceIdentity: remoteIdentity }],
  363. sortBy: "updated",
  364. search: "needle",
  365. });
  366. expect(result).toEqual({ items: [], total: 0, hasMore: false });
  367. });
  368. it("列表 mutation 按完整 task address 路由到唯一 task service", async () => {
  369. const remote = taskService({
  370. active: [meta({ taskId: "remote", workspaceIdentity: remoteIdentity })],
  371. });
  372. const runtime = createWindowHostControllerRuntime({
  373. createId: () => "id",
  374. resolveSource: (scope) => ({
  375. scope: {
  376. kind: "remote" as const,
  377. remoteSessionId: "remote-1",
  378. ...scope,
  379. workspaceIdentity: scope.workspaceIdentity!,
  380. },
  381. taskService: remote,
  382. sourceAvailability: "online" as const,
  383. }),
  384. });
  385. await runtime.service.listTaskList({
  386. kind: "active",
  387. workspaceScopes: [{ workspacePath: "/work/demo", workspaceIdentity: remoteIdentity }],
  388. sortBy: "updated",
  389. });
  390. await runtime.service.mutateTask({
  391. address: {
  392. remoteSessionId: "remote-1",
  393. workspacePath: "/work/demo",
  394. workspaceIdentity: remoteIdentity,
  395. taskId: "remote",
  396. },
  397. mutation: { kind: "pin", pinned: true },
  398. });
  399. expect(remote.setTaskPinned).toHaveBeenCalledWith({
  400. taskId: "remote",
  401. workspacePath: "/work/demo",
  402. workspaceIdentity: remoteIdentity,
  403. pinned: true,
  404. });
  405. });
  406. it("归档条件删除保留 remote 地址与跳过结果,离线 source 禁止写入", async () => {
  407. const remote = taskService({
  408. archived: [meta({ taskId: "remote", workspaceIdentity: remoteIdentity })],
  409. });
  410. remote.deleteArchivedTask = vi.fn(async () => false);
  411. let online = true;
  412. const runtime = createWindowHostControllerRuntime({
  413. createId: () => "id",
  414. resolveSource: (scope) => ({
  415. scope: {
  416. kind: "remote" as const,
  417. remoteSessionId: "remote-1",
  418. ...scope,
  419. workspaceIdentity: scope.workspaceIdentity!,
  420. },
  421. taskService: remote,
  422. sourceAvailability: online ? "online" : "offline",
  423. }),
  424. });
  425. const address = await runtime.resolveTaskAddress({
  426. workspacePath: "/work/demo",
  427. workspaceIdentity: remoteIdentity,
  428. taskId: "remote",
  429. });
  430. expect(await runtime.service.deleteArchivedTask({ address })).toBe(false);
  431. expect(remote.deleteArchivedTask).toHaveBeenCalledWith({
  432. workspacePath: "/work/demo",
  433. workspaceIdentity: remoteIdentity,
  434. taskId: "remote",
  435. });
  436. online = false;
  437. await expect(runtime.service.deleteArchivedTask({ address })).rejects.toThrow("离线");
  438. expect(remote.deleteArchivedTask).toHaveBeenCalledTimes(1);
  439. });
  440. it("remote attachment 首次列表写入会先建立 Controller source", async () => {
  441. const remote = taskService({
  442. active: [meta({ taskId: "remote", workspaceIdentity: remoteIdentity })],
  443. });
  444. const runtime = createWindowHostControllerRuntime({
  445. createId: () => "id",
  446. resolveSource: (scope) => ({
  447. scope: {
  448. kind: "remote" as const,
  449. remoteSessionId: "remote-1",
  450. ...scope,
  451. workspaceIdentity: scope.workspaceIdentity!,
  452. },
  453. taskService: remote,
  454. sourceAvailability: "online" as const,
  455. }),
  456. });
  457. const address = await runtime.resolveTaskAddress({
  458. taskId: "remote",
  459. workspacePath: "/work/demo",
  460. workspaceIdentity: remoteIdentity,
  461. attachmentScope: {
  462. kind: "remote",
  463. remoteSessionId: "remote-1",
  464. workspacePath: "/work/demo",
  465. workspaceIdentity: remoteIdentity,
  466. },
  467. });
  468. await runtime.service.mutateTask({
  469. address,
  470. mutation: { kind: "mark-unread" },
  471. });
  472. expect(remote.setTaskUnread).toHaveBeenCalledWith({
  473. taskId: "remote",
  474. workspacePath: "/work/demo",
  475. workspaceIdentity: remoteIdentity,
  476. unread: true,
  477. });
  478. });
  479. it("remote attachment 已过期时不刷新或写入当前同 workspace source", async () => {
  480. const remote = taskService({
  481. active: [meta({ taskId: "remote", workspaceIdentity: remoteIdentity })],
  482. });
  483. const runtime = createWindowHostControllerRuntime({
  484. createId: () => "id",
  485. resolveSource: (scope) => ({
  486. scope: {
  487. kind: "remote" as const,
  488. remoteSessionId: "remote-current",
  489. ...scope,
  490. workspaceIdentity: scope.workspaceIdentity!,
  491. },
  492. taskService: remote,
  493. sourceAvailability: "online" as const,
  494. }),
  495. });
  496. await expect(
  497. runtime.resolveTaskAddress({
  498. taskId: "remote",
  499. workspacePath: "/work/demo",
  500. workspaceIdentity: remoteIdentity,
  501. attachmentScope: {
  502. kind: "remote",
  503. remoteSessionId: "remote-stale",
  504. workspacePath: "/work/demo",
  505. workspaceIdentity: remoteIdentity,
  506. },
  507. }),
  508. ).rejects.toThrow("列表 mutation 与 remote attachment source 不匹配");
  509. expect(remote.listTasks).not.toHaveBeenCalled();
  510. expect(remote.setTaskUnread).not.toHaveBeenCalled();
  511. });
  512. it("单个 remote source 读取失败不影响 local 与其他 source 列表", async () => {
  513. const local = taskService({ active: [meta({ taskId: "local" })] });
  514. const brokenRemote = taskService({});
  515. brokenRemote.listTasks = vi.fn(async () => {
  516. throw new Error("remote unavailable");
  517. });
  518. const sourceErrors = vi.fn();
  519. const runtime = createWindowHostControllerRuntime({
  520. createId: () => "id",
  521. onSourceError: sourceErrors,
  522. resolveSource: (scope) =>
  523. scope.workspaceIdentity
  524. ? {
  525. scope: {
  526. kind: "remote" as const,
  527. remoteSessionId: "remote-1",
  528. ...scope,
  529. workspaceIdentity: scope.workspaceIdentity,
  530. },
  531. taskService: brokenRemote,
  532. sourceAvailability: "online" as const,
  533. }
  534. : {
  535. scope: { kind: "local" as const, ...scope },
  536. taskService: local,
  537. sourceAvailability: "online" as const,
  538. },
  539. });
  540. const result = await runtime.service.listTaskList({
  541. kind: "active",
  542. workspaceScopes: [
  543. { workspacePath: "/work/demo" },
  544. { workspacePath: "/work/demo", workspaceIdentity: remoteIdentity },
  545. ],
  546. sortBy: "updated",
  547. });
  548. expect(result.items.map((item) => item.taskId)).toEqual(["local"]);
  549. expect(sourceErrors).toHaveBeenCalledWith(
  550. expect.objectContaining({ kind: "remote" }),
  551. "refresh",
  552. expect.any(Error),
  553. );
  554. });
  555. it("重连 snapshot 读取失败时保留旧离线投影,后续成功后再原子替换", async () => {
  556. const oldService = taskService({
  557. active: [meta({ taskId: "old", workspaceIdentity: remoteIdentity })],
  558. });
  559. const nextService = taskService({
  560. active: [meta({ taskId: "new", workspaceIdentity: remoteIdentity })],
  561. });
  562. let sessionId = "remote-old";
  563. let service: IZCodeTaskService = oldService;
  564. const runtime = createWindowHostControllerRuntime({
  565. createId: () => "id",
  566. resolveSource: (scope) => ({
  567. scope: {
  568. kind: "remote" as const,
  569. remoteSessionId: sessionId,
  570. ...scope,
  571. workspaceIdentity: scope.workspaceIdentity!,
  572. },
  573. taskService: service,
  574. sourceAvailability: "online" as const,
  575. }),
  576. });
  577. const query = {
  578. kind: "active" as const,
  579. workspaceScopes: [{ workspacePath: "/work/demo", workspaceIdentity: remoteIdentity }],
  580. sortBy: "updated" as const,
  581. };
  582. await runtime.service.listTaskList(query);
  583. runtime.disconnectSource({
  584. kind: "remote",
  585. remoteSessionId: "remote-old",
  586. workspacePath: "/work/demo",
  587. workspaceIdentity: remoteIdentity,
  588. });
  589. sessionId = "remote-new";
  590. service = nextService;
  591. nextService.listTasks = vi.fn(async () => {
  592. throw new Error("snapshot unavailable");
  593. });
  594. await expect(
  595. runtime.replaceDisconnectedSource(
  596. {
  597. kind: "remote",
  598. remoteSessionId: "remote-old",
  599. workspacePath: "/work/demo",
  600. workspaceIdentity: remoteIdentity,
  601. },
  602. {
  603. scope: {
  604. kind: "remote",
  605. remoteSessionId: "remote-new",
  606. workspacePath: "/work/demo",
  607. workspaceIdentity: remoteIdentity,
  608. },
  609. taskService: nextService,
  610. sourceAvailability: "online",
  611. },
  612. ),
  613. ).rejects.toThrow("snapshot unavailable");
  614. expect(await runtime.service.listTaskList(query)).toEqual(
  615. expect.objectContaining({
  616. items: [
  617. expect.objectContaining({
  618. taskId: "old",
  619. remoteSessionId: "remote-old",
  620. sourceAvailability: "offline",
  621. }),
  622. ],
  623. }),
  624. );
  625. runtime.disconnectSource({
  626. kind: "remote",
  627. remoteSessionId: "remote-new",
  628. workspacePath: "/work/demo",
  629. workspaceIdentity: remoteIdentity,
  630. });
  631. const finalService = taskService({
  632. active: [meta({ taskId: "new", workspaceIdentity: remoteIdentity })],
  633. });
  634. sessionId = "remote-final";
  635. service = finalService;
  636. await runtime.replaceDisconnectedSource(
  637. {
  638. kind: "remote",
  639. remoteSessionId: "remote-new",
  640. workspacePath: "/work/demo",
  641. workspaceIdentity: remoteIdentity,
  642. },
  643. {
  644. scope: {
  645. kind: "remote",
  646. remoteSessionId: "remote-final",
  647. workspacePath: "/work/demo",
  648. workspaceIdentity: remoteIdentity,
  649. },
  650. taskService: finalService,
  651. sourceAvailability: "online",
  652. },
  653. );
  654. const replaced = await runtime.service.listTaskList(query);
  655. expect(replaced.items).toEqual([
  656. expect.objectContaining({
  657. taskId: "new",
  658. remoteSessionId: "remote-final",
  659. sourceAvailability: "online",
  660. }),
  661. ]);
  662. const snapshots: WindowHostControllerFrame[] = [];
  663. const frameSubscription = runtime.service.onDynamicControllerFrame()((frame) => {
  664. snapshots.push(frame);
  665. });
  666. await runtime.service.subscribeControllerV4({
  667. topic: CONTROLLER_TASKS_INDEX_TOPIC,
  668. visibility: "foreground",
  669. });
  670. const snapshot = snapshots.at(-1);
  671. expect(snapshot?.topic).toBe(CONTROLLER_TASKS_INDEX_TOPIC);
  672. expect(
  673. snapshot?.topic === CONTROLLER_TASKS_INDEX_TOPIC && snapshot.payload.kind === "snapshot"
  674. ? snapshot.payload.snapshot.tasks.map((task) => task.address.remoteSessionId)
  675. : [],
  676. ).toEqual(["remote-final"]);
  677. frameSubscription.dispose();
  678. });
  679. });