taskFileTreeTarget.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { describe, expect, it } from "vitest";
  2. import type { ZCodeTaskMeta } from "@zcode/shared";
  3. import type { WorkspaceTabState } from "../src/store/tabStore.js";
  4. import {
  5. resolveTaskFileTreeTarget,
  6. resolveTaskFileTreeTargetFromTabs,
  7. } from "../src/lib/taskFileTreeTarget.js";
  8. function task(overrides: Partial<ZCodeTaskMeta> = {}): ZCodeTaskMeta {
  9. return {
  10. taskId: "task-1",
  11. workspacePath: "/work/repo",
  12. createdAt: 1,
  13. updatedAt: 1,
  14. ...overrides,
  15. } as ZCodeTaskMeta;
  16. }
  17. function tab(overrides: Partial<WorkspaceTabState> = {}): WorkspaceTabState {
  18. return {
  19. id: "tab-1",
  20. workspacePath: "/work/repo",
  21. label: "Repository",
  22. ...overrides,
  23. } as WorkspaceTabState;
  24. }
  25. describe("resolveTaskFileTreeTarget", () => {
  26. it("opens a local task workspace using the task path", () => {
  27. expect(resolveTaskFileTreeTarget(task(), tab())).toEqual({
  28. workspacePath: "/work/repo",
  29. workspaceName: "Repository",
  30. });
  31. });
  32. it("keeps remote identity and session together", () => {
  33. expect(
  34. resolveTaskFileTreeTarget(
  35. task({ workspaceIdentity: "ssh://host/work/repo" }),
  36. tab({
  37. workspaceIdentity: "ssh://host/work/repo",
  38. remoteSessionId: "remote-1",
  39. }),
  40. ),
  41. ).toEqual({
  42. workspacePath: "/work/repo",
  43. workspaceName: "Repository",
  44. workspaceIdentity: "ssh://host/work/repo",
  45. workspaceRemoteSessionId: "remote-1",
  46. });
  47. });
  48. it("blocks a remote task until its remote session is ready", () => {
  49. expect(
  50. resolveTaskFileTreeTarget(
  51. task({ workspaceIdentity: "ssh://host/work/repo" }),
  52. tab({ workspaceIdentity: "ssh://host/work/repo" }),
  53. ),
  54. ).toBeNull();
  55. });
  56. it("rejects a same-path remote tab with a different workspace identity", () => {
  57. expect(
  58. resolveTaskFileTreeTarget(
  59. task({ workspaceIdentity: "ssh://host-a/work/repo" }),
  60. tab({
  61. workspaceIdentity: "ssh://host-b/work/repo",
  62. remoteSessionId: "remote-b",
  63. }),
  64. ),
  65. ).toBeNull();
  66. });
  67. it("rejects a same-path remote tab for a local task without identity", () => {
  68. expect(
  69. resolveTaskFileTreeTarget(
  70. task(),
  71. tab({
  72. remoteTarget: {
  73. kind: "ssh",
  74. host: "example.com",
  75. username: "developer",
  76. },
  77. remoteSessionId: "remote-1",
  78. }),
  79. ),
  80. ).toBeNull();
  81. });
  82. it.each([
  83. [tab(), tab({ remoteTarget: { kind: "ssh", host: "example.com" } })],
  84. [tab({ remoteTarget: { kind: "ssh", host: "example.com" } }), tab()],
  85. ])("selects the local tab from same-path candidates regardless of order", (...tabs) => {
  86. expect(resolveTaskFileTreeTargetFromTabs(task(), tabs)).toEqual({
  87. workspacePath: "/work/repo",
  88. workspaceName: "Repository",
  89. });
  90. });
  91. it.each([
  92. [[]],
  93. [[tab({ workspacePath: "/other/repo", label: "Other repository" })]],
  94. ])("falls back to the task path when no local tab matches", (tabs) => {
  95. expect(resolveTaskFileTreeTargetFromTabs(task(), tabs)).toEqual({
  96. workspacePath: "/work/repo",
  97. workspaceName: "repo",
  98. });
  99. });
  100. });