webRemoteControlTaskIndexLayout.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import { readFileSync } from "node:fs";
  2. import { resolve } from "node:path";
  3. import { createElement, type ComponentType, type ReactNode } from "react";
  4. import { renderToStaticMarkup } from "react-dom/server";
  5. import { describe, expect, it, vi } from "vitest";
  6. import { ServiceProvider } from "@/hooks/useServices.js";
  7. vi.mock("@/i18n/IntlProvider.js", () => ({
  8. useZCodeIntl: () => ({
  9. intl: {
  10. formatMessage: ({ id }: { id: string }) => id,
  11. },
  12. }),
  13. }));
  14. vi.mock("@/logger.js", () => ({
  15. logger: {
  16. debug: vi.fn(),
  17. error: vi.fn(),
  18. warn: vi.fn(),
  19. },
  20. }));
  21. vi.mock("@/ControlHintTooltip.js", () => ({
  22. ControlHintTooltip: ({ children }: { children?: ReactNode }) =>
  23. createElement("span", null, children),
  24. }));
  25. function createTestServices() {
  26. return {
  27. zcodeTaskService: {
  28. archiveTask: vi.fn(),
  29. setTaskPinned: vi.fn(),
  30. },
  31. };
  32. }
  33. async function renderTaskIndex() {
  34. const { WebRemoteControlTaskIndex } = await import("@/WebRemoteControlTaskIndex.js");
  35. return renderToStaticMarkup(
  36. createElement(
  37. ServiceProvider,
  38. { services: createTestServices() as never },
  39. createElement(WebRemoteControlTaskIndex, {
  40. activeTaskId: null,
  41. activeWorkspacePath: "/tmp/project",
  42. onSelectTask: vi.fn(),
  43. switcher: {
  44. listWorkspaces: vi.fn(async () => ({ tasks: [], workspaces: [] })),
  45. switchWorkspace: vi.fn(async () => {}),
  46. },
  47. }),
  48. ),
  49. );
  50. }
  51. async function renderTaskIndexWithInitialTasks() {
  52. const { WebRemoteControlTaskIndex } = await import("@/WebRemoteControlTaskIndex.js");
  53. const TaskIndex = WebRemoteControlTaskIndex as ComponentType<Record<string, unknown>>;
  54. return renderToStaticMarkup(
  55. createElement(
  56. ServiceProvider,
  57. { services: createTestServices() as never },
  58. createElement(TaskIndex, {
  59. activeTaskId: null,
  60. activeWorkspacePath: "/tmp/project",
  61. initialResult: {
  62. workspaces: [
  63. {
  64. workspacePath: "/tmp/project",
  65. label: "Project",
  66. kind: "local",
  67. },
  68. ],
  69. tasks: [
  70. {
  71. taskId: "task-1",
  72. title: "Task one",
  73. workspacePath: "/tmp/project",
  74. workspaceLabel: "Project",
  75. workspaceKind: "local",
  76. createdAt: 1,
  77. updatedAt: 2,
  78. },
  79. ],
  80. },
  81. onSelectTask: vi.fn(),
  82. switcher: {
  83. listWorkspaces: vi.fn(async () => ({ tasks: [], workspaces: [] })),
  84. switchWorkspace: vi.fn(async () => {}),
  85. },
  86. }),
  87. ),
  88. );
  89. }
  90. async function renderCollapsedTaskIndexWithUnread() {
  91. const { WebRemoteControlTaskIndex } = await import("@/WebRemoteControlTaskIndex.js");
  92. const TaskIndex = WebRemoteControlTaskIndex as ComponentType<Record<string, unknown>>;
  93. return renderToStaticMarkup(
  94. createElement(
  95. ServiceProvider,
  96. { services: createTestServices() as never },
  97. createElement(TaskIndex, {
  98. activeTaskId: null,
  99. activeWorkspacePath: "/tmp/project",
  100. collapsedWorkspaceKeys: new Set(["/tmp/project"]),
  101. initialResult: {
  102. workspaces: [{ workspacePath: "/tmp/project", label: "Project", kind: "local" }],
  103. tasks: [
  104. {
  105. taskId: "task-unread",
  106. title: "Unread task",
  107. workspacePath: "/tmp/project",
  108. workspaceLabel: "Project",
  109. workspaceKind: "local",
  110. createdAt: 1,
  111. updatedAt: 2,
  112. unreadAt: 123,
  113. },
  114. ],
  115. },
  116. onSelectTask: vi.fn(),
  117. switcher: {
  118. listWorkspaces: vi.fn(async () => ({ tasks: [], workspaces: [] })),
  119. switchWorkspace: vi.fn(async () => {}),
  120. },
  121. taskViewMode: "workspace",
  122. }),
  123. ),
  124. );
  125. }
  126. async function renderTaskIndexWithInitialPinnedTasks() {
  127. const { WebRemoteControlTaskIndex } = await import("@/WebRemoteControlTaskIndex.js");
  128. const TaskIndex = WebRemoteControlTaskIndex as ComponentType<Record<string, unknown>>;
  129. return renderToStaticMarkup(
  130. createElement(
  131. ServiceProvider,
  132. { services: createTestServices() as never },
  133. createElement(TaskIndex, {
  134. activeTaskId: null,
  135. activeWorkspacePath: "/tmp/project",
  136. initialResult: {
  137. workspaces: [
  138. {
  139. workspacePath: "/tmp/project",
  140. label: "Project",
  141. kind: "local",
  142. },
  143. ],
  144. tasks: [
  145. {
  146. taskId: "task-1",
  147. title: "Pinned task",
  148. workspacePath: "/tmp/project",
  149. workspaceLabel: "Project",
  150. workspaceKind: "local",
  151. createdAt: 1,
  152. updatedAt: 2,
  153. pinned: true,
  154. },
  155. ],
  156. },
  157. onSelectTask: vi.fn(),
  158. switcher: {
  159. listWorkspaces: vi.fn(async () => ({ tasks: [], workspaces: [] })),
  160. switchWorkspace: vi.fn(async () => {}),
  161. },
  162. }),
  163. ),
  164. );
  165. }
  166. async function renderTaskIndexWithInitialArchivedTasks(hasTasks = true) {
  167. const { WebRemoteControlTaskIndex } = await import("@/WebRemoteControlTaskIndex.js");
  168. const TaskIndex = WebRemoteControlTaskIndex as ComponentType<Record<string, unknown>>;
  169. return renderToStaticMarkup(
  170. createElement(
  171. ServiceProvider,
  172. { services: createTestServices() as never },
  173. createElement(TaskIndex, {
  174. activeTaskId: null,
  175. activeWorkspacePath: "/tmp/project",
  176. initialResult: {
  177. workspaces: [
  178. {
  179. workspacePath: "/tmp/project",
  180. label: "Project",
  181. kind: "local",
  182. },
  183. ],
  184. tasks: hasTasks
  185. ? [
  186. {
  187. taskId: "task-1",
  188. title: "Archived task",
  189. workspacePath: "/tmp/project",
  190. workspaceLabel: "Project",
  191. workspaceKind: "local",
  192. createdAt: 1,
  193. updatedAt: 2,
  194. archived: true,
  195. pinned: true,
  196. },
  197. ]
  198. : [],
  199. },
  200. onSelectTask: vi.fn(),
  201. switcher: {
  202. listWorkspaces: vi.fn(async () => ({ tasks: [], workspaces: [] })),
  203. switchWorkspace: vi.fn(async () => {}),
  204. },
  205. taskViewMode: "archived",
  206. }),
  207. ),
  208. );
  209. }
  210. describe("WebRemoteControlTaskIndex layout", () => {
  211. it("does not own the mobile navigation collapse or scroll viewport", async () => {
  212. const html = await renderTaskIndex();
  213. expect(html).toContain("webRemoteControl.noTasks");
  214. expect(html).not.toContain("aria-expanded");
  215. expect(html).not.toContain("webRemoteControl.collapseTasks");
  216. expect(html).not.toContain("overflow-y-auto");
  217. });
  218. it("binds desktop task index loading to active remote workspace transitions", () => {
  219. const source = readFileSync(
  220. resolve(process.cwd(), "packages/ui/src/WebRemoteControlTaskIndex.tsx"),
  221. "utf8",
  222. );
  223. expect(source).toContain("shouldRetryWebRemoteControlTaskIndexLoad");
  224. expect(source).toContain(
  225. "}, [activeTaskId, activeWorkspaceIdentity, activeWorkspacePath, switcher]);",
  226. );
  227. });
  228. it("lets the wide remote sidebar render its workspace toolbar after pinned tasks", () => {
  229. const taskIndexSource = readFileSync(
  230. resolve(process.cwd(), "packages/ui/src/WebRemoteControlTaskIndex.tsx"),
  231. "utf8",
  232. );
  233. const sidebarSource = readFileSync(
  234. resolve(process.cwd(), "packages/ui/src/WorkspaceSidebar.tsx"),
  235. "utf8",
  236. );
  237. expect(taskIndexSource).toContain("renderBeforePinnedTasks");
  238. expect(taskIndexSource).toContain("model.pinnedTasks.map((task) => renderTaskRow(task))");
  239. expect(
  240. taskIndexSource.indexOf("{renderBeforePinnedTasks ? renderBeforePinnedTasks() : null}"),
  241. ).toBeLessThan(taskIndexSource.indexOf("{model.pinnedTasks.length > 0 ? ("));
  242. expect(sidebarSource).toContain("const workspaceTaskToolbar");
  243. expect(sidebarSource).toContain("renderBeforePinnedTasks={workspaceTaskToolbar}");
  244. expect(sidebarSource).toContain("{!isWebRemoteControlSidebar ? workspaceTaskToolbar() : null}");
  245. });
  246. it("keeps mutable wide remote task actions out of the initial static markup", async () => {
  247. const html = await renderTaskIndexWithInitialTasks();
  248. expect(html).toContain('data-web-remote-task-row="/tmp/project:task-1"');
  249. expect(html).not.toContain('aria-label="taskList.pin"');
  250. expect(html).not.toContain('aria-label="taskList.archive"');
  251. });
  252. it("renders the aggregate unread dot on a collapsed wide workspace row", async () => {
  253. const html = await renderCollapsedTaskIndexWithUnread();
  254. expect(html).toContain('data-workspace-unread-indicator="true"');
  255. expect(html).not.toContain("Unread task");
  256. });
  257. it("keeps archived wide remote rows aligned with desktop archived actions", async () => {
  258. const html = await renderTaskIndexWithInitialArchivedTasks();
  259. expect(html).toContain("Archived task");
  260. expect(html).not.toContain('aria-label="taskList.pin"');
  261. expect(html).not.toContain('aria-label="taskList.unpin"');
  262. expect(html).not.toContain('aria-label="taskList.archive"');
  263. expect(html).not.toContain("lucide-pin");
  264. });
  265. it("renders wide remote task rows with button semantics for opening tasks", async () => {
  266. const html = await renderTaskIndexWithInitialTasks();
  267. expect(html).toMatch(
  268. /<button(?=[^>]*aria-label="webRemoteControl\.openTask")[^>]*data-testid="task-item-task-1"/,
  269. );
  270. });
  271. it("keeps the pinned row unpin action out of the initial static markup", async () => {
  272. const html = await renderTaskIndexWithInitialPinnedTasks();
  273. expect(html).toContain("Pinned task");
  274. expect(html).toContain("lucide-pin");
  275. expect(html).not.toContain('aria-label="taskList.unpin"');
  276. });
  277. });
  278. describe("Web 归档批量删除入口", () => {
  279. it("归档态提供共享入口,空态禁用,普通视图不显示", async () => {
  280. const populated = await renderTaskIndexWithInitialArchivedTasks();
  281. expect(populated).toContain('data-testid="archived-tasks-actions"');
  282. expect(populated).toContain('aria-label="taskList.archivedActions"');
  283. expect(populated).not.toContain('data-testid="delete-all-archived-tasks"');
  284. const empty = await renderTaskIndexWithInitialArchivedTasks(false);
  285. expect(empty).toMatch(/<button(?=[^>]*disabled="")(?=[^>]*data-testid="archived-tasks-actions")/);
  286. expect(await renderTaskIndex()).not.toContain('data-testid="archived-tasks-actions"');
  287. });
  288. });