webRemoteControlTaskIndex.test.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. import { describe, expect, it } from "vitest";
  2. import type { ZCodeTaskMeta } from "@zcode/shared";
  3. import * as webRemoteControlTaskIndexModule from "@/lib/webRemoteControlTaskIndex.js";
  4. import {
  5. applyWebRemoteControlTaskMembershipUpdate,
  6. buildWebRemoteControlTaskIndexModel,
  7. buildWebRemoteControlTaskTargets,
  8. getWebRemoteControlTaskMutationWorkspaceTargets,
  9. groupWebRemoteControlTasksByWorkspace,
  10. } from "@/lib/webRemoteControlTaskIndex.js";
  11. import type { WorkspaceTabState } from "@/store/tabStore.js";
  12. import { attachTaskListRowActivity } from "@/v4/taskListRowActivity.js";
  13. function createTab(overrides: Partial<WorkspaceTabState>): WorkspaceTabState {
  14. return {
  15. id: overrides.id ?? "tab-default",
  16. kind: "workspace",
  17. label: overrides.label ?? "Default",
  18. workspacePath: overrides.workspacePath ?? "/repo/default",
  19. workspaceIdentity: overrides.workspaceIdentity,
  20. remoteSessionId: overrides.remoteSessionId,
  21. remoteTarget: overrides.remoteTarget,
  22. };
  23. }
  24. function createTask(overrides: Partial<ZCodeTaskMeta>): ZCodeTaskMeta {
  25. return {
  26. taskId: overrides.taskId ?? "task-default",
  27. traceId: "trace-default",
  28. title: overrides.title ?? "Default task",
  29. workspacePath: overrides.workspacePath ?? "/repo/default",
  30. workspaceIdentity: overrides.workspaceIdentity,
  31. createdAt: overrides.createdAt ?? 1,
  32. updatedAt: overrides.updatedAt ?? 1,
  33. mode: "default",
  34. provider: overrides.provider ?? "codex",
  35. unreadAt: overrides.unreadAt,
  36. status: overrides.status,
  37. };
  38. }
  39. describe("buildWebRemoteControlTaskTargets", () => {
  40. it("maps task metadata to mobile-readable targets and keeps remote workspace identity", () => {
  41. const localTab = createTab({
  42. id: "local-tab",
  43. label: "Local App",
  44. workspacePath: "/workspace/local-app",
  45. });
  46. const remoteTab = createTab({
  47. id: "remote-tab",
  48. label: "Remote App",
  49. workspacePath: "/workspace/app",
  50. workspaceIdentity: "ssh://dev.example.com/workspace/app",
  51. remoteSessionId: "remote-session-1",
  52. });
  53. const tasks = [
  54. createTask({
  55. taskId: "older-local",
  56. title: "Older local",
  57. workspacePath: localTab.workspacePath,
  58. updatedAt: 10,
  59. }),
  60. createTask({
  61. taskId: "newer-remote",
  62. title: "Newer remote",
  63. workspacePath: remoteTab.workspacePath,
  64. workspaceIdentity: remoteTab.workspaceIdentity,
  65. updatedAt: 20,
  66. unreadAt: 21,
  67. }),
  68. ];
  69. const targets = buildWebRemoteControlTaskTargets({
  70. workspaceTabs: [localTab, remoteTab],
  71. tasks,
  72. });
  73. expect(targets.map((target) => target.taskId)).toEqual(["newer-remote", "older-local"]);
  74. expect(targets[0]).toMatchObject({
  75. workspaceIdentity: "ssh://dev.example.com/workspace/app",
  76. remoteSessionId: "remote-session-1",
  77. workspaceLabel: "Remote App",
  78. workspaceKind: "remote",
  79. unreadAt: 21,
  80. });
  81. });
  82. it("drops tasks whose workspace is not currently open in the desktop window", () => {
  83. const targets = buildWebRemoteControlTaskTargets({
  84. workspaceTabs: [
  85. createTab({
  86. label: "Visible",
  87. workspacePath: "/workspace/visible",
  88. }),
  89. ],
  90. tasks: [
  91. createTask({
  92. taskId: "visible-task",
  93. workspacePath: "/workspace/visible",
  94. }),
  95. createTask({
  96. taskId: "hidden-task",
  97. workspacePath: "/workspace/hidden",
  98. }),
  99. ],
  100. });
  101. expect(targets.map((target) => target.taskId)).toEqual(["visible-task"]);
  102. });
  103. it("falls back to runtime before persisted status when live activity is absent", () => {
  104. const tab = createTab({
  105. label: "Visible",
  106. workspacePath: "/workspace/visible",
  107. });
  108. const targets = buildWebRemoteControlTaskTargets({
  109. workspaceTabs: [tab],
  110. tasks: [
  111. createTask({
  112. taskId: "running-task",
  113. workspacePath: tab.workspacePath,
  114. status: "completed",
  115. }),
  116. createTask({
  117. taskId: "failed-runtime-task",
  118. workspacePath: tab.workspacePath,
  119. status: "running",
  120. }),
  121. createTask({
  122. taskId: "persisted-error-task",
  123. workspacePath: tab.workspacePath,
  124. status: "error",
  125. }),
  126. createTask({
  127. taskId: "stale-running-task",
  128. workspacePath: tab.workspacePath,
  129. status: "running",
  130. }),
  131. createTask({
  132. taskId: "creating-task",
  133. workspacePath: tab.workspacePath,
  134. status: "completed",
  135. }),
  136. ],
  137. taskRuntimeByWorkspaceKey: {
  138. [tab.workspacePath]: {
  139. "running-task": { status: "streaming" },
  140. "failed-runtime-task": { status: "failed" },
  141. "creating-task": { status: "creating" },
  142. },
  143. },
  144. });
  145. expect(
  146. Object.fromEntries(targets.map((target) => [target.taskId, target.displayStatus])),
  147. ).toEqual({
  148. "creating-task": "running",
  149. "failed-runtime-task": "error",
  150. "persisted-error-task": "error",
  151. "running-task": "running",
  152. "stale-running-task": "idle",
  153. });
  154. });
  155. it("carries the sidecar workflowActivity onto the target so the phone draws the same run line", () => {
  156. const tab = createTab({ label: "Visible", workspacePath: "/workspace/visible" });
  157. const workflowActivity = {
  158. runs: [
  159. {
  160. runId: "run-1",
  161. toolCallId: "tool-1",
  162. name: "Deep research",
  163. status: "running",
  164. phases: [
  165. { name: "Research", status: "running" },
  166. { name: "Write", status: "pending" },
  167. ],
  168. currentPhase: "Research",
  169. agentsWorking: 2,
  170. },
  171. ],
  172. };
  173. const targets = buildWebRemoteControlTaskTargets({
  174. workspaceTabs: [tab],
  175. tasks: [
  176. attachTaskListRowActivity(
  177. createTask({ taskId: "with-run", workspacePath: tab.workspacePath, status: "completed" }),
  178. {
  179. phase: "completedSuccess",
  180. lastActivityAt: 100,
  181. hasBackgroundWork: true,
  182. workflowActivity,
  183. },
  184. ),
  185. attachTaskListRowActivity(
  186. createTask({ taskId: "no-run", workspacePath: tab.workspacePath, status: "completed" }),
  187. { phase: "completedSuccess", lastActivityAt: 100, hasBackgroundWork: false },
  188. ),
  189. ],
  190. });
  191. expect(targets.find((target) => target.taskId === "with-run")?.workflowActivity).toEqual(
  192. workflowActivity,
  193. );
  194. expect(targets.find((target) => target.taskId === "no-run")).not.toHaveProperty(
  195. "workflowActivity",
  196. );
  197. });
  198. it("carries sessions-index hasBackgroundWork onto the target without changing displayStatus", () => {
  199. const tab = createTab({
  200. label: "Visible",
  201. workspacePath: "/workspace/visible",
  202. });
  203. const targets = buildWebRemoteControlTaskTargets({
  204. workspaceTabs: [tab],
  205. tasks: [
  206. attachTaskListRowActivity(
  207. createTask({ taskId: "with-run", workspacePath: tab.workspacePath, status: "completed" }),
  208. { phase: "completedSuccess", lastActivityAt: 100, hasBackgroundWork: true },
  209. ),
  210. attachTaskListRowActivity(
  211. createTask({
  212. taskId: "without-run",
  213. workspacePath: tab.workspacePath,
  214. status: "completed",
  215. }),
  216. { phase: "completedSuccess", lastActivityAt: 100, hasBackgroundWork: false },
  217. ),
  218. createTask({
  219. taskId: "no-activity",
  220. workspacePath: tab.workspacePath,
  221. status: "completed",
  222. }),
  223. ],
  224. });
  225. expect(
  226. Object.fromEntries(
  227. targets.map((target) => [target.taskId, [target.displayStatus, target.hasBackgroundWork]]),
  228. ),
  229. ).toEqual({
  230. "with-run": ["completed", true],
  231. "without-run": ["completed", undefined],
  232. "no-activity": ["completed", undefined],
  233. });
  234. });
  235. it("uses live sessions-index activity for every display status despite stale caches", () => {
  236. const tab = createTab({
  237. label: "Visible",
  238. workspacePath: "/workspace/visible",
  239. });
  240. const cases = [
  241. {
  242. taskId: "running-over-completed",
  243. phase: "running" as const,
  244. persistedStatus: "completed" as const,
  245. runtimeStatus: "completed" as const,
  246. expected: "running",
  247. },
  248. {
  249. taskId: "prewarming-over-error",
  250. phase: "prewarming" as const,
  251. persistedStatus: "error" as const,
  252. runtimeStatus: "failed" as const,
  253. expected: "running",
  254. },
  255. {
  256. taskId: "completed-over-streaming",
  257. phase: "completedSuccess" as const,
  258. persistedStatus: "running" as const,
  259. runtimeStatus: "streaming" as const,
  260. expected: "completed",
  261. },
  262. {
  263. taskId: "interrupted-over-failed",
  264. phase: "completedInterrupted" as const,
  265. persistedStatus: "error" as const,
  266. runtimeStatus: "failed" as const,
  267. expected: "completed",
  268. },
  269. {
  270. taskId: "error-over-streaming",
  271. phase: "error" as const,
  272. persistedStatus: "completed" as const,
  273. runtimeStatus: "streaming" as const,
  274. expected: "error",
  275. },
  276. {
  277. taskId: "draft-keeps-runtime-fallback",
  278. phase: "draft" as const,
  279. persistedStatus: "completed" as const,
  280. runtimeStatus: "creating" as const,
  281. expected: "running",
  282. },
  283. ];
  284. const targets = buildWebRemoteControlTaskTargets({
  285. workspaceTabs: [tab],
  286. tasks: cases.map(({ taskId, phase, persistedStatus }) =>
  287. attachTaskListRowActivity(
  288. createTask({
  289. taskId,
  290. workspacePath: tab.workspacePath,
  291. status: persistedStatus,
  292. }),
  293. {
  294. phase,
  295. lastActivityAt: 100,
  296. hasBackgroundWork: false,
  297. },
  298. ),
  299. ),
  300. taskRuntimeByWorkspaceKey: {
  301. [tab.workspacePath]: Object.fromEntries(
  302. cases.map(({ taskId, runtimeStatus }) => [
  303. taskId,
  304. { status: runtimeStatus },
  305. ]),
  306. ),
  307. },
  308. });
  309. expect(
  310. Object.fromEntries(targets.map((target) => [target.taskId, target.displayStatus])),
  311. ).toEqual(Object.fromEntries(cases.map(({ taskId, expected }) => [taskId, expected])));
  312. });
  313. it("marks pinned task targets for mobile remote lists", () => {
  314. const tab = createTab({
  315. label: "Visible",
  316. workspacePath: "/workspace/visible",
  317. });
  318. const targets = buildWebRemoteControlTaskTargets({
  319. workspaceTabs: [tab],
  320. tasks: [
  321. createTask({
  322. taskId: "pinned-task",
  323. workspacePath: tab.workspacePath,
  324. }),
  325. ],
  326. pinned: true,
  327. });
  328. expect(targets).toMatchObject([
  329. {
  330. taskId: "pinned-task",
  331. pinned: true,
  332. },
  333. ]);
  334. });
  335. it("marks archived task targets for remote archived lists", () => {
  336. const tab = createTab({
  337. label: "Visible",
  338. workspacePath: "/workspace/visible",
  339. });
  340. const targets = buildWebRemoteControlTaskTargets({
  341. workspaceTabs: [tab],
  342. tasks: [
  343. createTask({
  344. taskId: "archived-task",
  345. workspacePath: tab.workspacePath,
  346. }),
  347. ],
  348. archived: true,
  349. });
  350. expect(targets).toMatchObject([
  351. {
  352. taskId: "archived-task",
  353. archived: true,
  354. },
  355. ]);
  356. });
  357. it("groups mobile task targets by workspace order with tasks nested under each workspace", () => {
  358. const firstWorkspace = {
  359. workspacePath: "/workspace/a",
  360. label: "Workspace A",
  361. kind: "local" as const,
  362. };
  363. const secondWorkspace = {
  364. workspacePath: "/workspace/b",
  365. workspaceIdentity: "ssh://dev.example.com/workspace/b",
  366. remoteSessionId: "remote-session-1",
  367. label: "Workspace B",
  368. kind: "remote" as const,
  369. };
  370. const groups = groupWebRemoteControlTasksByWorkspace({
  371. workspaces: [firstWorkspace, secondWorkspace],
  372. tasks: [
  373. {
  374. taskId: "b-older",
  375. title: "B older",
  376. workspacePath: secondWorkspace.workspacePath,
  377. workspaceIdentity: secondWorkspace.workspaceIdentity,
  378. remoteSessionId: secondWorkspace.remoteSessionId,
  379. workspaceLabel: secondWorkspace.label,
  380. workspaceKind: "remote",
  381. createdAt: 1,
  382. updatedAt: 10,
  383. },
  384. {
  385. taskId: "a-task",
  386. title: "A task",
  387. workspacePath: firstWorkspace.workspacePath,
  388. workspaceLabel: firstWorkspace.label,
  389. workspaceKind: "local",
  390. createdAt: 1,
  391. updatedAt: 30,
  392. },
  393. {
  394. taskId: "b-newer",
  395. title: "B newer",
  396. workspacePath: secondWorkspace.workspacePath,
  397. workspaceIdentity: secondWorkspace.workspaceIdentity,
  398. remoteSessionId: secondWorkspace.remoteSessionId,
  399. workspaceLabel: secondWorkspace.label,
  400. workspaceKind: "remote",
  401. createdAt: 1,
  402. updatedAt: 20,
  403. },
  404. ],
  405. });
  406. expect(groups.map((group) => group.workspace.label)).toEqual(["Workspace A", "Workspace B"]);
  407. expect(groups[0]?.tasks.map((task) => task.taskId)).toEqual(["a-task"]);
  408. expect(groups[1]?.tasks.map((task) => task.taskId)).toEqual(["b-newer", "b-older"]);
  409. });
  410. it("sorts grouped remote tasks by the selected time field", () => {
  411. const workspace = {
  412. workspacePath: "/workspace/a",
  413. label: "Workspace A",
  414. kind: "local" as const,
  415. };
  416. const groups = groupWebRemoteControlTasksByWorkspace({
  417. workspaces: [workspace],
  418. sortBy: "created",
  419. tasks: [
  420. {
  421. taskId: "newer-updated",
  422. title: "Newer updated",
  423. workspacePath: workspace.workspacePath,
  424. workspaceLabel: workspace.label,
  425. workspaceKind: "local",
  426. createdAt: 10,
  427. updatedAt: 30,
  428. },
  429. {
  430. taskId: "newer-created",
  431. title: "Newer created",
  432. workspacePath: workspace.workspacePath,
  433. workspaceLabel: workspace.label,
  434. workspaceKind: "local",
  435. createdAt: 20,
  436. updatedAt: 1,
  437. },
  438. ],
  439. });
  440. expect(groups[0]?.tasks.map((task) => task.taskId)).toEqual([
  441. "newer-created",
  442. "newer-updated",
  443. ]);
  444. });
  445. it("远控把有后台工作的会话并入运行层,updatedAt 交替时不换位", () => {
  446. const workspace = {
  447. workspacePath: "/workspace/a",
  448. label: "Workspace A",
  449. kind: "local" as const,
  450. };
  451. const task = (taskId: string, createdAt: number, updatedAt: number) => ({
  452. taskId,
  453. title: taskId,
  454. workspacePath: workspace.workspacePath,
  455. workspaceLabel: workspace.label,
  456. workspaceKind: workspace.kind,
  457. createdAt,
  458. updatedAt,
  459. displayStatus: "completed" as const,
  460. hasBackgroundWork: true,
  461. });
  462. const idle = {
  463. ...task("idle", 30, 5_000),
  464. hasBackgroundWork: false,
  465. };
  466. const order = (tasks: ReturnType<typeof task>[]) =>
  467. groupWebRemoteControlTasksByWorkspace({
  468. workspaces: [workspace],
  469. sortBy: "updated",
  470. tasks,
  471. })[0]?.tasks.map((item) => item.taskId);
  472. expect(order([task("run-a", 10, 900), task("run-b", 20, 100), idle])).toEqual([
  473. "run-b",
  474. "run-a",
  475. "idle",
  476. ]);
  477. expect(order([task("run-a", 10, 100), task("run-b", 20, 900), idle])).toEqual([
  478. "run-b",
  479. "run-a",
  480. "idle",
  481. ]);
  482. });
  483. it("远控 running 置顶并按 createdAt 稳定排序", () => {
  484. const workspace = {
  485. workspacePath: "/workspace/a",
  486. label: "Workspace A",
  487. kind: "local" as const,
  488. };
  489. const task = (
  490. taskId: string,
  491. createdAt: number,
  492. updatedAt: number,
  493. displayStatus: "running" | "completed",
  494. ) => ({
  495. taskId,
  496. title: taskId,
  497. workspacePath: workspace.workspacePath,
  498. workspaceLabel: workspace.label,
  499. workspaceKind: workspace.kind,
  500. createdAt,
  501. updatedAt,
  502. displayStatus,
  503. });
  504. const groups = groupWebRemoteControlTasksByWorkspace({
  505. workspaces: [workspace],
  506. sortBy: "updated",
  507. tasks: [
  508. task("parent", 10, 1_000, "running"),
  509. task("fork", 20, 1, "running"),
  510. task("completed", 30, 2_000, "completed"),
  511. ],
  512. });
  513. expect(groups[0]?.tasks.map((item) => item.taskId)).toEqual([
  514. "fork",
  515. "parent",
  516. "completed",
  517. ]);
  518. });
  519. });
  520. describe("buildWebRemoteControlTaskIndexModel", () => {
  521. const localWorkspace = {
  522. workspacePath: "/workspace/local",
  523. label: "Local App",
  524. kind: "local" as const,
  525. };
  526. const remoteWorkspace = {
  527. workspacePath: "/workspace/remote",
  528. workspaceIdentity: "ssh://dev/workspace/remote",
  529. remoteSessionId: "remote-session",
  530. label: "Remote App",
  531. kind: "remote" as const,
  532. };
  533. const result = {
  534. workspaces: [localWorkspace, remoteWorkspace],
  535. tasks: [
  536. {
  537. taskId: "pinned-local",
  538. title: "Pinned local",
  539. workspacePath: localWorkspace.workspacePath,
  540. workspaceLabel: localWorkspace.label,
  541. workspaceKind: "local" as const,
  542. createdAt: 100,
  543. updatedAt: 100,
  544. pinned: true,
  545. },
  546. {
  547. taskId: "regular-local",
  548. title: "Regular local",
  549. workspacePath: localWorkspace.workspacePath,
  550. workspaceLabel: localWorkspace.label,
  551. workspaceKind: "local" as const,
  552. createdAt: 10,
  553. updatedAt: 50,
  554. },
  555. {
  556. taskId: "regular-remote",
  557. title: "Remote search hit",
  558. workspacePath: remoteWorkspace.workspacePath,
  559. workspaceIdentity: remoteWorkspace.workspaceIdentity,
  560. remoteSessionId: remoteWorkspace.remoteSessionId,
  561. workspaceLabel: remoteWorkspace.label,
  562. workspaceKind: "remote" as const,
  563. createdAt: 20,
  564. updatedAt: 40,
  565. },
  566. {
  567. taskId: "archived-remote",
  568. title: "Archived remote",
  569. workspacePath: remoteWorkspace.workspacePath,
  570. workspaceIdentity: remoteWorkspace.workspaceIdentity,
  571. remoteSessionId: remoteWorkspace.remoteSessionId,
  572. workspaceLabel: remoteWorkspace.label,
  573. workspaceKind: "remote" as const,
  574. createdAt: 30,
  575. updatedAt: 30,
  576. archived: true,
  577. },
  578. ],
  579. };
  580. it("builds workspace groups from regular tasks without duplicating pinned or archived tasks", () => {
  581. const model = buildWebRemoteControlTaskIndexModel({
  582. result,
  583. viewMode: "workspace",
  584. sortBy: "updated",
  585. });
  586. expect(model.pinnedTasks.map((task) => task.taskId)).toEqual(["pinned-local"]);
  587. expect(model.groups.map((group) => group.workspace.label)).toEqual([
  588. "Local App",
  589. "Remote App",
  590. ]);
  591. expect(model.groups.flatMap((group) => group.tasks.map((task) => task.taskId))).toEqual([
  592. "regular-local",
  593. "regular-remote",
  594. ]);
  595. expect(model.totalTaskCount).toBe(2);
  596. });
  597. it("builds a cross-workspace timeline using the selected sort field", () => {
  598. const model = buildWebRemoteControlTaskIndexModel({
  599. result,
  600. viewMode: "timeline",
  601. sortBy: "created",
  602. });
  603. expect(model.timelineTasks.map((task) => task.taskId)).toEqual([
  604. "regular-remote",
  605. "regular-local",
  606. ]);
  607. });
  608. it("builds a cross-workspace archived list from archived task targets", () => {
  609. const model = buildWebRemoteControlTaskIndexModel({
  610. result,
  611. viewMode: "archived",
  612. sortBy: "updated",
  613. });
  614. expect(model.archivedTasks.map((task) => task.taskId)).toEqual(["archived-remote"]);
  615. expect(model.totalTaskCount).toBe(1);
  616. });
  617. it("filters remote tasks by title and workspace label", () => {
  618. const titleModel = buildWebRemoteControlTaskIndexModel({
  619. result,
  620. viewMode: "workspace",
  621. sortBy: "updated",
  622. searchQuery: "search hit",
  623. });
  624. const workspaceModel = buildWebRemoteControlTaskIndexModel({
  625. result,
  626. viewMode: "workspace",
  627. sortBy: "updated",
  628. searchQuery: "local app",
  629. });
  630. expect(titleModel.groups.flatMap((group) => group.tasks.map((task) => task.taskId))).toEqual([
  631. "regular-remote",
  632. ]);
  633. expect(workspaceModel.groups.flatMap((group) => group.tasks.map((task) => task.taskId))).toEqual([
  634. "regular-local",
  635. ]);
  636. });
  637. it("keeps collapsed workspace groups visible while hiding their tasks", () => {
  638. const model = buildWebRemoteControlTaskIndexModel({
  639. result: {
  640. ...result,
  641. tasks: result.tasks.map((task) =>
  642. task.taskId === "regular-local" ? { ...task, unreadAt: 123 } : task,
  643. ),
  644. },
  645. viewMode: "workspace",
  646. sortBy: "updated",
  647. collapsedWorkspaceKeys: new Set([localWorkspace.workspacePath]),
  648. });
  649. expect(model.groups.map((group) => [group.workspace.label, group.collapsed])).toEqual([
  650. ["Local App", true],
  651. ["Remote App", false],
  652. ]);
  653. expect(model.groups.flatMap((group) => group.tasks.map((task) => task.taskId))).toEqual([
  654. "regular-remote",
  655. ]);
  656. expect(model.groups.find((group) => group.workspace.label === "Local App")?.hasUnread).toBe(
  657. true,
  658. );
  659. });
  660. it("does not aggregate pinned or archived unread tasks into workspace groups", () => {
  661. const model = buildWebRemoteControlTaskIndexModel({
  662. result: {
  663. ...result,
  664. tasks: result.tasks.map((task) =>
  665. task.pinned || task.archived ? { ...task, unreadAt: 123 } : task,
  666. ),
  667. },
  668. viewMode: "workspace",
  669. });
  670. expect(model.groups.every((group) => group.hasUnread === false)).toBe(true);
  671. });
  672. });
  673. describe("applyWebRemoteControlTaskMembershipUpdate", () => {
  674. const workspace = {
  675. workspacePath: "/workspace/local",
  676. label: "Local App",
  677. kind: "local" as const,
  678. };
  679. const baseResult = {
  680. workspaces: [workspace],
  681. tasks: [
  682. {
  683. taskId: "regular-local",
  684. title: "Regular local",
  685. workspacePath: workspace.workspacePath,
  686. workspaceLabel: workspace.label,
  687. workspaceKind: "local" as const,
  688. createdAt: 10,
  689. updatedAt: 50,
  690. },
  691. ],
  692. };
  693. it("moves a regular task into the pinned section without archiving it", () => {
  694. const next = applyWebRemoteControlTaskMembershipUpdate({
  695. result: baseResult,
  696. target: baseResult.tasks[0],
  697. membership: { pinned: true, archived: false },
  698. });
  699. expect(next.tasks?.[0]).toMatchObject({
  700. taskId: "regular-local",
  701. pinned: true,
  702. });
  703. expect(next.tasks?.[0]?.archived).toBeUndefined();
  704. });
  705. it("moves a task into the archived section and clears pinned membership", () => {
  706. const next = applyWebRemoteControlTaskMembershipUpdate({
  707. result: {
  708. ...baseResult,
  709. tasks: [{ ...baseResult.tasks[0], pinned: true }],
  710. },
  711. target: { ...baseResult.tasks[0], pinned: true },
  712. membership: { pinned: false, archived: true },
  713. });
  714. expect(next.tasks?.[0]).toMatchObject({
  715. taskId: "regular-local",
  716. archived: true,
  717. });
  718. expect(next.tasks?.[0]?.pinned).toBeUndefined();
  719. });
  720. });
  721. describe("getWebRemoteControlTaskMutationWorkspaceTargets", () => {
  722. it("does not treat local workspaces as mutable through a remote bridge host", () => {
  723. const localWorkspace = {
  724. workspacePath: "/Users/me/local-app",
  725. label: "local-app",
  726. kind: "local" as const,
  727. };
  728. const remoteWorkspace = {
  729. workspacePath: "/srv/app",
  730. workspaceIdentity: "ssh://box/srv/app",
  731. remoteSessionId: "remote-session-1",
  732. label: "remote-app",
  733. kind: "remote" as const,
  734. };
  735. const mutationTargets = getWebRemoteControlTaskMutationWorkspaceTargets({
  736. activeWorkspaceIdentity: "ssh://box/srv/app",
  737. workspaces: [localWorkspace, remoteWorkspace],
  738. });
  739. expect(mutationTargets).toEqual([remoteWorkspace]);
  740. });
  741. it("keeps local workspaces mutable when the current bridge host is local", () => {
  742. const localWorkspace = {
  743. workspacePath: "/Users/me/local-app",
  744. label: "local-app",
  745. kind: "local" as const,
  746. };
  747. expect(
  748. getWebRemoteControlTaskMutationWorkspaceTargets({
  749. workspaces: [localWorkspace],
  750. }),
  751. ).toEqual([localWorkspace]);
  752. });
  753. });
  754. describe("shouldRetryWebRemoteControlTaskIndexLoad", () => {
  755. const shouldRetryWebRemoteControlTaskIndexLoad = (
  756. webRemoteControlTaskIndexModule as {
  757. shouldRetryWebRemoteControlTaskIndexLoad?: (options: {
  758. activeTaskId: string | null;
  759. attempt: number;
  760. maxAttempts: number;
  761. taskCount: number;
  762. }) => boolean;
  763. }
  764. ).shouldRetryWebRemoteControlTaskIndexLoad;
  765. it("retries a transient empty index while a remote task is active", () => {
  766. expect(shouldRetryWebRemoteControlTaskIndexLoad).toBeTypeOf("function");
  767. expect(
  768. shouldRetryWebRemoteControlTaskIndexLoad?.({
  769. activeTaskId: "remote-task",
  770. attempt: 0,
  771. maxAttempts: 3,
  772. taskCount: 0,
  773. }),
  774. ).toBe(true);
  775. });
  776. it("does not retry when there is no active task, tasks are present, or attempts are exhausted", () => {
  777. expect(shouldRetryWebRemoteControlTaskIndexLoad).toBeTypeOf("function");
  778. expect(
  779. shouldRetryWebRemoteControlTaskIndexLoad?.({
  780. activeTaskId: null,
  781. attempt: 0,
  782. maxAttempts: 3,
  783. taskCount: 0,
  784. }),
  785. ).toBe(false);
  786. expect(
  787. shouldRetryWebRemoteControlTaskIndexLoad?.({
  788. activeTaskId: "remote-task",
  789. attempt: 0,
  790. maxAttempts: 3,
  791. taskCount: 1,
  792. }),
  793. ).toBe(false);
  794. expect(
  795. shouldRetryWebRemoteControlTaskIndexLoad?.({
  796. activeTaskId: "remote-task",
  797. attempt: 3,
  798. maxAttempts: 3,
  799. taskCount: 0,
  800. }),
  801. ).toBe(false);
  802. });
  803. });