projectWorkspaceNavigation.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { mkdtemp, rm } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { expect, it, vi } from "vitest";
  5. import { buildRemoteHostWorkspaceIdentity } from "@zcode/shared";
  6. import { TaskIndexRepo } from "#src/session/taskIndexRepo.js";
  7. import { bindRemoteProjectIdentities, createProjectService } from "#src/project/index.js";
  8. import { createProjectWorkspaceNavigation } from "#src/project-workspace-navigation/index.js";
  9. import type { ProjectNavigationPort } from "#src/project-workspace-navigation/contract.js";
  10. it("MRO01:真实远端目录解析补齐身份后可打开已有任务,不创建替代目录", async () => {
  11. const directory = await mkdtemp(join(tmpdir(), "zcode-project-navigation-"));
  12. const hostId = "remote:server:source";
  13. const workspacePath = directory;
  14. const workspaceIdentity = buildRemoteHostWorkspaceIdentity(hostId, workspacePath);
  15. const task = { taskId: "existing", workspacePath, workspaceIdentity };
  16. const index = new TaskIndexRepo(undefined, hostId, join(directory, "tasks-index.sqlite"));
  17. try {
  18. const repository = await index.getProjectRepository();
  19. const project = repository.saveProject({ roots: [workspacePath] });
  20. repository.reserveCreatedTask({ task, projectId: project.id });
  21. await index.applyCliFacts({
  22. meta: {
  23. ...task,
  24. traceId: "trace-existing",
  25. title: "Existing task",
  26. mode: "default",
  27. provider: "glm",
  28. createdAt: 1,
  29. updatedAt: 2,
  30. },
  31. titleSource: "generated",
  32. lastOutcome: "completed",
  33. });
  34. const source = createProjectService({
  35. repository: async () => repository,
  36. workspaceDirectory: directory,
  37. ownerHostId: hostId,
  38. });
  39. const resolveIdentity = vi.fn(async (path: string) =>
  40. buildRemoteHostWorkspaceIdentity(hostId, path),
  41. );
  42. const service = bindRemoteProjectIdentities(source, resolveIdentity);
  43. const prepareDraft = vi.spyOn(source, "prepareSession");
  44. const before = repository.readTree();
  45. const owner = {};
  46. const openRemoteWorkspace = vi.fn<ProjectNavigationPort["openRemoteWorkspace"]>(
  47. async ({ directories, assertCurrent }) => {
  48. await assertCurrent();
  49. return {
  50. sourceServerId: "source",
  51. remoteSessionId: "scope",
  52. generation: 1,
  53. target: { kind: "server", serverId: "source", url: "https://example.test" },
  54. workspacePath: directories.workspacePath,
  55. workspaceIdentity: directories.workspaceIdentity,
  56. };
  57. },
  58. );
  59. const navigation = createProjectWorkspaceNavigation({
  60. projectService: service,
  61. readConnectionOwner: () => owner,
  62. openRemoteWorkspace,
  63. remoteProjectService: {
  64. readDirectory: async () => ({
  65. hosts: [
  66. {
  67. id: hostId,
  68. membershipId: "member",
  69. kind: "server",
  70. target: { kind: "server", serverId: "source", url: "https://example.test" },
  71. position: 0,
  72. connectionStatus: "online",
  73. },
  74. ],
  75. entries: [
  76. {
  77. id: "entry",
  78. hostId,
  79. projectId: project.id,
  80. name: project.name,
  81. roots: project.roots,
  82. legacyWorkspace: null,
  83. lastSyncedAt: 1,
  84. },
  85. ],
  86. }),
  87. },
  88. });
  89. await expect(
  90. navigation.prepare({
  91. hostId,
  92. task: { taskId: task.taskId, workspaceKey: workspaceIdentity },
  93. }),
  94. ).resolves.toMatchObject({
  95. hostId,
  96. projectId: project.id,
  97. workspacePath,
  98. workspaceIdentity,
  99. additionalDirectories: [],
  100. remote: { remoteSessionId: "scope" },
  101. });
  102. expect(prepareDraft).not.toHaveBeenCalled();
  103. expect(repository.readTree()).toEqual(before);
  104. expect(openRemoteWorkspace).toHaveBeenCalledTimes(1);
  105. // resolver 失败不能复制请求身份继续;跨设备输入仍由原导航校验拒绝。
  106. resolveIdentity.mockRejectedValueOnce(new Error("identity unavailable"));
  107. await expect(service.resolveSessionDirectories(task, hostId)).rejects.toThrow(
  108. "identity unavailable",
  109. );
  110. await expect(
  111. navigation.prepare({
  112. hostId,
  113. task: {
  114. taskId: task.taskId,
  115. workspaceKey: buildRemoteHostWorkspaceIdentity("remote:server:another", workspacePath),
  116. },
  117. }),
  118. ).rejects.toThrow("project.task-not-found");
  119. expect(openRemoteWorkspace).toHaveBeenCalledTimes(1);
  120. } finally {
  121. index.close();
  122. await rm(directory, { recursive: true, force: true });
  123. }
  124. });