windowHostControllerAttachmentIdentity.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { expect, it, vi } from "vitest";
  2. import type { IZCodeTaskService } from "@zcode/services";
  3. import type { ZCodeTaskMeta } from "@zcode/shared";
  4. import { createWindowHostControllerRuntime } from "../src/host/windowHostControllerService.js";
  5. const workspacePath = "/repo";
  6. const workspaceIdentity = "remote:server:source:/repo";
  7. const task: ZCodeTaskMeta = {
  8. taskId: "task",
  9. traceId: "trace",
  10. title: "Task",
  11. workspacePath,
  12. workspaceIdentity,
  13. createdAt: 1,
  14. updatedAt: 2,
  15. mode: "default",
  16. unreadAt: 10,
  17. };
  18. const attachment = (remoteSessionId: string) => ({
  19. kind: "remote" as const,
  20. remoteSessionId,
  21. workspacePath,
  22. workspaceIdentity,
  23. });
  24. it("clears unread through the first attachment after a Source-only sidebar query", async () => {
  25. let remoteSessionId: string | undefined;
  26. const service = {
  27. listTasks: async () => [task],
  28. listPinnedTasks: async () => [],
  29. listArchivedTasks: async () => [],
  30. setTaskUnread: vi.fn(async () => ({ ...task, unreadAt: undefined })),
  31. } as unknown as IZCodeTaskService;
  32. const runtime = createWindowHostControllerRuntime({
  33. createId: () => crypto.randomUUID(),
  34. resolveSource: () => ({
  35. scope: {
  36. kind: "remote",
  37. sourceServerId: "source",
  38. remoteSessionId,
  39. workspacePath,
  40. workspaceIdentity,
  41. },
  42. taskService: service,
  43. sourceAvailability: "online",
  44. }),
  45. });
  46. try {
  47. // 真实侧栏在打开项目以前就查询 Source;投影行还没有 logical attachment ID。
  48. await runtime.service.listTaskList({
  49. kind: "timeline",
  50. workspaceScopes: [{ workspacePath, workspaceIdentity }],
  51. sortBy: "updated",
  52. });
  53. remoteSessionId = "first-attachment";
  54. const address = await runtime.resolveTaskAddress({
  55. taskId: task.taskId,
  56. workspacePath,
  57. workspaceIdentity,
  58. attachmentScope: attachment(remoteSessionId),
  59. });
  60. await runtime.service.mutateTask({
  61. address,
  62. mutation: { kind: "mark-read", expectedUnreadAt: 10 },
  63. });
  64. expect(service.setTaskUnread).toHaveBeenCalledWith({
  65. taskId: task.taskId,
  66. workspacePath,
  67. workspaceIdentity,
  68. unread: false,
  69. expectedUnreadAt: 10,
  70. });
  71. await expect(
  72. runtime.resolveTaskAddress({
  73. taskId: task.taskId,
  74. workspacePath,
  75. workspaceIdentity,
  76. attachmentScope: attachment("stale-attachment"),
  77. }),
  78. ).rejects.toThrow("remote attachment source 不匹配");
  79. } finally {
  80. runtime.dispose();
  81. }
  82. });
  83. it("does not admit an attachment that changed while its first task snapshot was loading", async () => {
  84. let remoteSessionId = "first-attachment";
  85. const loading = Promise.withResolvers<ZCodeTaskMeta[]>();
  86. const service = {
  87. listTasks: vi.fn(() => loading.promise),
  88. listPinnedTasks: async () => [],
  89. listArchivedTasks: async () => [],
  90. } as unknown as IZCodeTaskService;
  91. const runtime = createWindowHostControllerRuntime({
  92. createId: () => crypto.randomUUID(),
  93. resolveSource: () => ({
  94. scope: {
  95. kind: "remote",
  96. sourceServerId: "source",
  97. remoteSessionId,
  98. workspacePath,
  99. workspaceIdentity,
  100. },
  101. taskService: service,
  102. sourceAvailability: "online",
  103. }),
  104. });
  105. try {
  106. const resolving = runtime.resolveTaskAddress({
  107. taskId: task.taskId,
  108. workspacePath,
  109. workspaceIdentity,
  110. attachmentScope: attachment(remoteSessionId),
  111. });
  112. const rejected = expect(resolving).rejects.toThrow("remote attachment source 不匹配");
  113. await vi.waitFor(() => expect(service.listTasks).toHaveBeenCalled());
  114. remoteSessionId = "next-attachment";
  115. loading.resolve([task]);
  116. await rejected;
  117. } finally {
  118. loading.resolve([]);
  119. runtime.dispose();
  120. }
  121. });