Open History to see the last commit
1import { EnvironmentId, ProjectId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
5import type { HomeProjectScope } from "../home/homeThreadList";
6import {
7 getProjectScopeSelectionTarget,
8 newTaskDestination,
9 resolveDraftProjectSelection,
10 resolveEnvironmentProjectMatch,
11} from "./new-task-project-selection";
12
13function makeProject(
14 id: string,
15 environmentId = "environment",
16 options: {
17 readonly title?: string;
18 readonly workspaceRoot?: string;
19 readonly repositoryKey?: string;
20 } = {},
21): EnvironmentProject {
22 return {
23 environmentId: EnvironmentId.make(environmentId),
24 id: ProjectId.make(id),
25 title: options.title ?? id,
26 workspaceRoot: options.workspaceRoot ?? `/work/${id}`,
27 repositoryIdentity: options.repositoryKey
28 ? {
29 canonicalKey: options.repositoryKey,
30 locator: {
31 source: "git-remote",
32 remoteName: "origin",
33 remoteUrl: `https://${options.repositoryKey}.git`,
34 },
35 }
36 : null,
37 defaultModelSelection: null,
38 scripts: [],
39 createdAt: "2026-07-01T00:00:00.000Z",
40 updatedAt: "2026-07-01T00:00:00.000Z",
41 };
42}
43
44function makeScope(projects: ReadonlyArray<EnvironmentProject>): HomeProjectScope {
45 return {
46 key: "github.com/t3tools/t3code",
47 title: "T3 Code",
48 representative: projects[0]!,
49 projects,
50 projectRefs: projects.map((project) => ({
51 environmentId: project.environmentId,
52 projectId: project.id,
53 })),
54 };
55}
56
57describe("getProjectScopeSelectionTarget", () => {
58 it("prefers a connected copy of a project that spans environments", () => {
59 const mac = makeProject("t3code", "mac");
60 const server = makeProject("t3code", "server");
61 const target = (connected: ReadonlyArray<string>, preferred: string | null = null) =>
62 getProjectScopeSelectionTarget(
63 makeScope([mac, server]),
64 preferred === null ? null : EnvironmentId.make(preferred),
65 new Set(connected.map((id) => EnvironmentId.make(id))),
66 );
67 expect(target(["server"])).toBe(server);
68 expect(target(["server"], "mac")).toBe(server);
69 expect(target(["mac", "server"], "server")).toBe(server);
70 expect(target(["mac", "server"])).toBe(mac);
71 // With nothing connected, the usual order still applies.
72 expect(target([], "server")).toBe(server);
73 expect(target([])).toBe(mac);
74 });
75});
76
77describe("resolveEnvironmentProjectMatch", () => {
78 it("follows the same repository onto the target machine", () => {
79 const selected = makeProject("t3code", "mac", { repositoryKey: "github.com/t3tools/t3code" });
80 const target = [
81 makeProject("other", "server", { repositoryKey: "github.com/t3tools/other" }),
82 makeProject("t3code-clone", "server", { repositoryKey: "github.com/t3tools/t3code" }),
83 ];
84 expect(resolveEnvironmentProjectMatch(target, selected)).toBe(target[1]);
85 });
86
87 it("falls back to workspace basename, then title, for unindexed projects", () => {
88 const selected = makeProject("t3code", "mac", { workspaceRoot: "/Users/me/t3code" });
89 const byBasename = [
90 makeProject("other", "server"),
91 makeProject("srv", "server", { workspaceRoot: "/home/me/t3code" }),
92 ];
93 expect(resolveEnvironmentProjectMatch(byBasename, selected)).toBe(byBasename[1]);
94
95 const byTitle = [
96 makeProject("other", "server"),
97 makeProject("srv", "server", { title: "t3code" }),
98 ];
99 expect(resolveEnvironmentProjectMatch(byTitle, selected)).toBe(byTitle[1]);
100 });
101
102 it("does not treat a known different repository as a basename or title match", () => {
103 const selected = makeProject("t3code", "mac", {
104 repositoryKey: "github.com/t3tools/t3code",
105 workspaceRoot: "/Users/me/t3code",
106 });
107 const fork = makeProject("fork", "server", {
108 repositoryKey: "github.com/someone/t3code",
109 title: "t3code",
110 workspaceRoot: "/home/me/t3code",
111 });
112 const unindexed = makeProject("unindexed", "server", { workspaceRoot: "/srv/t3code" });
113 expect(resolveEnvironmentProjectMatch([fork, unindexed], selected)).toBe(unindexed);
114 expect(resolveEnvironmentProjectMatch([fork], selected)).toBe(fork);
115 });
116});
117
118describe("resolveDraftProjectSelection", () => {
119 const projects = [makeProject("t3code"), makeProject("t3code-2")];
120 const draft = { incomingShareId: undefined, pickerClosed: false };
121
122 it("opens the picker instead of auto-selecting the only project", () => {
123 expect(resolveDraftProjectSelection(null, projects, [makeScope(projects)], draft)).toEqual({
124 kind: "pick",
125 });
126 });
127
128 it("selects the only logical project for shared content", () => {
129 expect(
130 resolveDraftProjectSelection(null, projects, [makeScope(projects)], {
131 ...draft,
132 incomingShareId: "share",
133 }),
134 ).toEqual({ kind: "select", project: projects[0] });
135 });
136
137 it("dismisses the draft when the first picker closes without a choice", () => {
138 expect(
139 resolveDraftProjectSelection(null, projects, [makeScope(projects)], {
140 ...draft,
141 pickerClosed: true,
142 }),
143 ).toEqual({ kind: "dismiss" });
144 });
145
146 it("keeps the draft when the picker closes after a choice", () => {
147 expect(
148 resolveDraftProjectSelection("environment:t3code-2", projects, [makeScope(projects)], {
149 ...draft,
150 pickerClosed: true,
151 }),
152 ).toEqual({ kind: "preserve" });
153 });
154
155 it("keeps a share draft asking for a project when its picker closes without a choice", () => {
156 const scopes = [makeScope([projects[0]!]), makeScope([projects[1]!])];
157 expect(
158 resolveDraftProjectSelection(null, projects, scopes, {
159 incomingShareId: "share",
160 pickerClosed: true,
161 }),
162 ).toEqual({ kind: "pick" });
163 });
164});
165
166describe("newTaskDestination", () => {
167 const picker = { draft: { requestId: "request" } };
168
169 function destination(options: {
170 readonly useLastProject: boolean | undefined;
171 readonly projects: ReadonlyArray<EnvironmentProject>;
172 readonly current?: EnvironmentProject;
173 readonly last?: { readonly environmentId: string; readonly projectId: string };
174 }) {
175 return newTaskDestination({
176 projects: options.projects,
177 requestId: "request",
178 useLastProject: options.useLastProject,
179 currentProject: options.current && {
180 environmentId: options.current.environmentId,
181 projectId: options.current.id,
182 },
183 lastProject: options.last,
184 });
185 }
186
187 it("starts in the current thread's project ahead of the remembered one", () => {
188 const projects = [makeProject("remembered"), makeProject("current", "other-environment")];
189 expect(
190 destination({
191 useLastProject: true,
192 projects,
193 current: projects[1],
194 last: { environmentId: "environment", projectId: "remembered" },
195 }),
196 ).toEqual({
197 draft: { requestId: "request", environmentId: "other-environment", projectId: "current" },
198 });
199 });
200
201 it("treats an unset preference as on", () => {
202 const projects = [makeProject("first"), makeProject("current")];
203 expect(destination({ useLastProject: undefined, projects, current: projects[1] })).toEqual({
204 draft: { requestId: "request", environmentId: "environment", projectId: "current" },
205 });
206 });
207
208 it("starts in the remembered project of its own environment when there is no current one", () => {
209 const projects = [makeProject("chosen"), makeProject("chosen", "other-environment")];
210 expect(
211 destination({
212 useLastProject: true,
213 projects,
214 last: { environmentId: "other-environment", projectId: "chosen" },
215 }),
216 ).toEqual({
217 draft: { requestId: "request", environmentId: "other-environment", projectId: "chosen" },
218 });
219 });
220
221 it("ignores a remembered project id from another environment", () => {
222 expect(
223 destination({
224 useLastProject: true,
225 projects: [makeProject("chosen")],
226 last: { environmentId: "removed-environment", projectId: "chosen" },
227 }),
228 ).toEqual(picker);
229 });
230
231 it("opens the picker rather than the first project when nothing matches", () => {
232 expect(
233 destination({
234 useLastProject: true,
235 projects: [makeProject("first"), makeProject("second")],
236 current: makeProject("removed"),
237 }),
238 ).toEqual(picker);
239 });
240
241 it("always opens the picker when the preference is off", () => {
242 const only = makeProject("only");
243 expect(
244 destination({
245 useLastProject: false,
246 projects: [only],
247 current: only,
248 last: { environmentId: "environment", projectId: "only" },
249 }),
250 ).toEqual(picker);
251 });
252});