remote-project-directory.fixture.mjs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // MRO02:真实 ProjectService / RemoteProjectService / SQLite,网络和模型由用例控制。
  2. import { mkdir, mkdtemp } from "node:fs/promises";
  3. import { join } from "node:path";
  4. import { ServiceChannels } from "@zcode/shared";
  5. import { Emitter } from "../../../../../../../rpc/src/index.ts";
  6. import { TaskIndexRepo } from "../../../../../../../services/src/session/taskIndexRepo.ts";
  7. import {
  8. createProjectService,
  9. bindRemoteProjectIdentities,
  10. } from "../../../../../../../services/src/project/index.ts";
  11. import { createRemoteProjectService } from "../../../../../../../services/src/remote-projects/index.ts";
  12. import { createWindowProjectChangesService } from "../../../../../../../services/src/window-project-directory/index.ts";
  13. export async function createProjectDirectoryFixture(artifacts) {
  14. const root = await mkdtemp(join(artifacts, "data-"));
  15. const hostId = "remote:server:e2e-directory-source";
  16. const localPath = join(root, "local");
  17. const remotePath = join(root, "remote");
  18. const hiddenPath = join(root, "not-added");
  19. await Promise.all([localPath, remotePath, hiddenPath].map((path) => mkdir(path)));
  20. const identity = `${hostId}:${remotePath}`;
  21. const windowIndex = new TaskIndexRepo(undefined, "local", join(root, "window.sqlite"));
  22. const sourceIndex = new TaskIndexRepo(undefined, hostId, join(root, "source.sqlite"));
  23. const windowRepository = await windowIndex.getProjectRepository();
  24. const sourceRepository = await sourceIndex.getProjectRepository();
  25. const windowBroadcast = new Emitter();
  26. const sourceBroadcast = new Emitter();
  27. const windowChanges = createWindowProjectChangesService({ onMessage: windowBroadcast.event });
  28. const traffic = { calls: [], opens: [], views: [], errors: [] };
  29. const changed = () => {
  30. windowBroadcast.fire({ channel: "state:projects", payload: {} });
  31. };
  32. const sourceProject = createProjectService({
  33. repository: async () => sourceRepository,
  34. workspaceDirectory: remotePath,
  35. ownerHostId: hostId,
  36. afterChange: async () => sourceBroadcast.fire({ channel: "state:projects", payload: {} }),
  37. });
  38. const routedSourceProject = bindRemoteProjectIdentities(
  39. sourceProject,
  40. async (path) => `${hostId}:${path}`,
  41. );
  42. const windowProject = createProjectService({
  43. repository: async () => windowRepository,
  44. workspaceDirectory: localPath,
  45. resolveHost: async (id) => {
  46. if (id !== hostId) throw new Error("MRO02 unexpected host");
  47. return routedSourceProject;
  48. },
  49. afterChange: async () => changed(),
  50. });
  51. const windowRemote = createRemoteProjectService({
  52. database: () => windowIndex.getOrganizationDatabase(),
  53. readConnectionStates: async () => new Map([[hostId, { connectionStatus: "online" }]]),
  54. afterChange: changed,
  55. });
  56. const sourceRemote = createRemoteProjectService({
  57. database: () => sourceIndex.getOrganizationDatabase(),
  58. controller: false,
  59. });
  60. const local = await windowProject.saveProject({ name: "E2E_MRO02_LOCAL", roots: [localPath] });
  61. const remote = await sourceProject.saveProject({ name: "E2E_MRO02_REMOTE", roots: [remotePath] });
  62. const hidden = await sourceProject.saveProject({
  63. name: "E2E_MRO02_NOT_ADDED",
  64. roots: [hiddenPath],
  65. });
  66. await windowRemote.saveHost({
  67. id: hostId,
  68. target: { kind: "server", serverId: "e2e-directory-source", url: "https://example.test" },
  69. });
  70. await windowRemote.addEntry({
  71. hostId,
  72. project: { projectId: remote.id, name: remote.name, roots: remote.roots },
  73. });
  74. const tasks = [];
  75. for (const [index, repository, project, path, taskId, pinned] of [
  76. [windowIndex, windowRepository, local, localPath, "LOCAL_TASK", false],
  77. [windowIndex, windowRepository, local, localPath, "PINNED_TASK", true],
  78. [sourceIndex, sourceRepository, remote, remotePath, "REMOTE_TASK", false],
  79. [sourceIndex, sourceRepository, hidden, hiddenPath, "NOT_ADDED_TASK", false],
  80. ]) {
  81. const task = {
  82. taskId,
  83. workspacePath: path,
  84. ...(project.hostId === hostId ? { workspaceIdentity: `${hostId}:${path}` } : {}),
  85. };
  86. repository.reserveCreatedTask({ task, projectId: project.id });
  87. await index.applyCliFacts({
  88. meta: {
  89. ...task,
  90. traceId: `trace-${taskId}`,
  91. title: `E2E_MRO02_${taskId}`,
  92. mode: "default",
  93. createdAt: 1,
  94. updatedAt: 2,
  95. },
  96. titleSource: "generated",
  97. lastOutcome: "completed",
  98. });
  99. if (pinned) await index.updateTaskState({ ...task, patch: { pinned } });
  100. tasks.push(repository.readTree().tasks.find((item) => item.taskId === taskId));
  101. }
  102. const bind = (owner, channel, service, names) =>
  103. Object.fromEntries(
  104. names.map((method) => [
  105. `${channel}:${method}`,
  106. async (...args) => {
  107. traffic.calls.push({ owner, channel, method, args });
  108. return service[method](...args);
  109. },
  110. ]),
  111. );
  112. const projectMethods = [
  113. "readTree",
  114. "readProjects",
  115. "saveProject",
  116. "resolveSessionDirectories",
  117. "prepareSession",
  118. ];
  119. const remoteMethods = ["readDirectory", "refreshEntry", "removeEntry"];
  120. return {
  121. root,
  122. hostId,
  123. localPath,
  124. remotePath,
  125. identity,
  126. local,
  127. remote,
  128. hidden,
  129. tasks,
  130. traffic,
  131. windowProject,
  132. sourceProject,
  133. windowRepository,
  134. workspaces: [
  135. { workspacePath: localPath, kind: "local", label: "E2E local" },
  136. {
  137. workspacePath: remotePath,
  138. workspaceIdentity: identity,
  139. remoteSessionId: "mro02-remote-scope",
  140. kind: "remote",
  141. label: "E2E remote",
  142. connectionState: "connected",
  143. },
  144. ],
  145. channels(remoteBridge) {
  146. return {
  147. responses: {
  148. ...bind(
  149. remoteBridge ? "source" : "window",
  150. ServiceChannels.Project,
  151. remoteBridge ? sourceProject : windowProject,
  152. projectMethods,
  153. ),
  154. ...bind(
  155. remoteBridge ? "source" : "window",
  156. ServiceChannels.RemoteProjects,
  157. remoteBridge ? sourceRemote : windowRemote,
  158. remoteMethods,
  159. ),
  160. ...bind("window", "window-project", windowProject, projectMethods),
  161. ...bind("window", "window-remote-projects", windowRemote, remoteMethods),
  162. ...bind("window", "window-project-changes", windowChanges, ["getCapabilities"]),
  163. [`${ServiceChannels.ZCodeTask}:getTaskMeta`]: (input) =>
  164. tasks.find((task) => task.taskId === input.taskId) ?? null,
  165. },
  166. events: {
  167. [`${ServiceChannels.Broadcast}:onMessage`]: remoteBridge
  168. ? sourceBroadcast.event
  169. : windowBroadcast.event,
  170. "window-project-changes:onDidChange": windowChanges.onDidChange,
  171. },
  172. };
  173. },
  174. dispose() {
  175. windowIndex.close();
  176. sourceIndex.close();
  177. windowBroadcast.dispose();
  178. sourceBroadcast.dispose();
  179. },
  180. };
  181. }