taskIndexRepoWorkspaceIdentity.test.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { mkdtempSync, rmSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, beforeEach, describe, expect, it } from "vitest";
  5. import type { ZCodeTaskMeta } from "@zcode/shared";
  6. import { setDataBaseDir } from "#src/paths.js";
  7. import { TaskIndexRepo } from "#src/session/taskIndexRepo.js";
  8. let repo: TaskIndexRepo;
  9. let tempDir: string;
  10. beforeEach(() => {
  11. tempDir = mkdtempSync(join(tmpdir(), "zcode-task-index-workspace-identity-"));
  12. setDataBaseDir(tempDir);
  13. repo = new TaskIndexRepo();
  14. });
  15. afterEach(async () => {
  16. repo.close();
  17. setDataBaseDir(null);
  18. // Windows 需要等待 SQLite 释放文件句柄,避免临时目录清理出现 EPERM。
  19. await new Promise((resolve) => setTimeout(resolve, 50));
  20. rmSync(tempDir, { recursive: true, force: true });
  21. });
  22. function task(params: {
  23. taskId: string;
  24. workspaceIdentity: string;
  25. title?: string;
  26. }): ZCodeTaskMeta {
  27. return {
  28. taskId: params.taskId,
  29. traceId: `trace-${params.taskId}`,
  30. title: params.title ?? params.taskId,
  31. workspacePath: "/workspace/project",
  32. workspaceIdentity: params.workspaceIdentity,
  33. createdAt: 10,
  34. updatedAt: 20,
  35. mode: "default",
  36. status: "running",
  37. };
  38. }
  39. function getRepoDatabase(target: TaskIndexRepo): {
  40. prepare(sql: string): {
  41. run(...params: unknown[]): unknown;
  42. };
  43. } {
  44. return (
  45. target as unknown as {
  46. getDatabase(): {
  47. prepare(sql: string): {
  48. run(...params: unknown[]): unknown;
  49. };
  50. };
  51. }
  52. ).getDatabase();
  53. }
  54. function metaJsonWithoutWorkspaceIdentity(meta: ZCodeTaskMeta): string {
  55. const legacyMeta: Partial<ZCodeTaskMeta> = { ...meta };
  56. delete legacyMeta.workspaceIdentity;
  57. return JSON.stringify(legacyMeta);
  58. }
  59. describe("TaskIndexRepo workspace identity normalization", () => {
  60. it("归档条件删除只影响相同路径下指定 identity,冷启动仍保留 tombstone", async () => {
  61. const first = task({
  62. taskId: "same-id",
  63. workspaceIdentity: "remote:ssh:first:22:coder:/workspace/project",
  64. });
  65. const second = task({
  66. taskId: "same-id",
  67. workspaceIdentity: "remote:ssh:second:22:coder:/workspace/project",
  68. });
  69. for (const meta of [first, second]) {
  70. await repo.syncTaskMeta({ meta });
  71. await repo.updateTaskState({ ...meta, patch: { archived: true } });
  72. }
  73. expect(await repo.deleteArchivedTask(first)).toEqual(
  74. expect.objectContaining({ taskId: first.taskId }),
  75. );
  76. repo.close();
  77. repo = new TaskIndexRepo();
  78. expect(await repo.getTaskMeta(first)).toBeNull();
  79. expect(await repo.getTaskMeta(second)).toEqual(
  80. expect.objectContaining({ workspaceIdentity: second.workspaceIdentity }),
  81. );
  82. expect(await repo.deleteArchivedTask(first)).toBeNull();
  83. });
  84. it("以 SQLite remote workspace_key 覆盖 SSH 旧列和 meta_json 中过期的 identity", async () => {
  85. const workspaceIdentity = "remote:ssh:dev.example.test:22:coder:/workspace/project";
  86. const staleWorkspaceIdentity = "remote:ssh:stale.example.test:22:coder:/workspace/project";
  87. const meta = task({ taskId: "ssh-running", workspaceIdentity });
  88. await repo.syncTaskMeta({ meta });
  89. getRepoDatabase(repo)
  90. .prepare(
  91. `UPDATE tasks
  92. SET workspace_identity = ?, meta_json = ?
  93. WHERE workspace_key = ? AND task_id = ?`,
  94. )
  95. .run(
  96. staleWorkspaceIdentity,
  97. JSON.stringify({
  98. ...meta,
  99. workspaceIdentity: staleWorkspaceIdentity,
  100. }),
  101. workspaceIdentity,
  102. meta.taskId,
  103. );
  104. await expect(
  105. repo.listTaskMetas({
  106. workspacePath: meta.workspacePath,
  107. workspaceIdentity,
  108. }),
  109. ).resolves.toEqual([
  110. expect.objectContaining({
  111. taskId: meta.taskId,
  112. workspacePath: meta.workspacePath,
  113. workspaceIdentity,
  114. }),
  115. ]);
  116. });
  117. it("WSL 旧行 identity 列为空时从合法 remote workspace_key 恢复", async () => {
  118. const workspaceIdentity = "remote:wsl:ubuntu:/workspace/project";
  119. const meta = task({ taskId: "wsl-running", workspaceIdentity });
  120. await repo.syncTaskMeta({ meta });
  121. getRepoDatabase(repo)
  122. .prepare(
  123. `UPDATE tasks
  124. SET workspace_identity = NULL, meta_json = ?
  125. WHERE workspace_key = ? AND task_id = ?`,
  126. )
  127. .run(metaJsonWithoutWorkspaceIdentity(meta), workspaceIdentity, meta.taskId);
  128. await expect(
  129. repo.listTaskMetas({
  130. workspacePath: meta.workspacePath,
  131. workspaceIdentity,
  132. }),
  133. ).resolves.toEqual([
  134. expect.objectContaining({
  135. taskId: meta.taskId,
  136. workspaceIdentity,
  137. }),
  138. ]);
  139. });
  140. it("相同 path 和 taskId 的不同 SSH identity 仍保持隔离", async () => {
  141. const identityA = "remote:ssh:a.example.test:22:coder:/workspace/project";
  142. const identityB = "remote:ssh:b.example.test:22:coder:/workspace/project";
  143. const taskA = task({
  144. taskId: "shared-task",
  145. workspaceIdentity: identityA,
  146. title: "host-a",
  147. });
  148. const taskB = task({
  149. taskId: "shared-task",
  150. workspaceIdentity: identityB,
  151. title: "host-b",
  152. });
  153. await repo.syncTaskMeta({ meta: taskA });
  154. await repo.syncTaskMeta({ meta: taskB });
  155. const database = getRepoDatabase(repo);
  156. database
  157. .prepare(
  158. `UPDATE tasks
  159. SET meta_json = ?
  160. WHERE workspace_key = ? AND task_id = ?`,
  161. )
  162. .run(metaJsonWithoutWorkspaceIdentity(taskA), identityA, taskA.taskId);
  163. database
  164. .prepare(
  165. `UPDATE tasks
  166. SET meta_json = ?
  167. WHERE workspace_key = ? AND task_id = ?`,
  168. )
  169. .run(metaJsonWithoutWorkspaceIdentity(taskB), identityB, taskB.taskId);
  170. const [listedA, listedB] = await Promise.all([
  171. repo.listTaskMetas({
  172. workspacePath: taskA.workspacePath,
  173. workspaceIdentity: identityA,
  174. }),
  175. repo.listTaskMetas({
  176. workspacePath: taskB.workspacePath,
  177. workspaceIdentity: identityB,
  178. }),
  179. ]);
  180. expect(listedA).toEqual([
  181. expect.objectContaining({
  182. title: "host-a",
  183. workspaceIdentity: identityA,
  184. }),
  185. ]);
  186. expect(listedB).toEqual([
  187. expect.objectContaining({
  188. title: "host-b",
  189. workspaceIdentity: identityB,
  190. }),
  191. ]);
  192. });
  193. });