Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { EnvironmentId, ProjectId, SourceControlProviderKind, ThreadId } from "@t3tools/contracts";
3import { planProjectRemoval } from "@t3tools/client-runtime/state/projects";
4
5import { buildSidebarProjectSnapshots } from "../../sidebarProjectGrouping";
6import type { Project } from "../../types";
7import {
8 projectGroupTitleNeedsUpdate,
9 scopeProjectSettingsGroup,
10} from "./ProjectSettingsPanel.logic";
11
12const localEnvironmentId = EnvironmentId.make("local");
13const remoteEnvironmentId = EnvironmentId.make("remote");
14const customIcon = { kind: "lucide", name: "cloud", color: "red" } as const;
15
16function makeProject(environmentId: EnvironmentId, overrides: Partial<Project> = {}): Project {
17 return {
18 environmentId,
19 id: ProjectId.make(`${environmentId}-project`),
20 title: "repo",
21 workspaceRoot: `/${environmentId}/repo`,
22 repositoryIdentity: {
23 canonicalKey: "github.com/owner/repo",
24 locator: {
25 source: "git-remote",
26 remoteName: "origin",
27 remoteUrl: "https://github.com/owner/repo.git",
28 },
29 provider: SourceControlProviderKind.make("github"),
30 owner: "owner",
31 name: "repo",
32 displayName: "owner/repo",
33 },
34 defaultModelSelection: null,
35 faviconPath: null,
36 projectIcon: null,
37 scripts: [],
38 createdAt: "2026-07-01T00:00:00.000Z",
39 updatedAt: "2026-07-01T00:00:00.000Z",
40 ...overrides,
41 };
42}
43
44function projectGroup(projects: Project[]) {
45 return buildSidebarProjectSnapshots({
46 projects,
47 settings: { sidebarProjectGroupingMode: "repository", sidebarProjectGroupingOverrides: {} },
48 primaryEnvironmentId: localEnvironmentId,
49 resolveEnvironmentLabel: (id) => id,
50 })[0]!;
51}
52
53describe("scopeProjectSettingsGroup", () => {
54 it("keeps the sidebar preview identity and edit targets when the remote checkout was registered first", () => {
55 const local = makeProject(localEnvironmentId, {
56 title: "Local repo",
57 faviconPath: "local.svg",
58 projectIcon: customIcon,
59 });
60 const remote = makeProject(remoteEnvironmentId, {
61 title: "Remote repo",
62 faviconPath: "remote.svg",
63 });
64 const group = projectGroup([remote, local]);
65 const scoped = scopeProjectSettingsGroup(group, group.memberProjects);
66
67 expect(group).toMatchObject(local);
68 expect(scoped).toMatchObject(local);
69 expect(scoped.memberProjects).toBe(group.memberProjects);
70 expect(scoped.memberProjects).toMatchObject([remote, local]);
71 });
72
73 it.each([
74 [remoteEnvironmentId, "local-project"],
75 [localEnvironmentId, "other-checkout"],
76 ])("uses the complete selected checkout identity for %s/%s", (environmentId, id) => {
77 const local = makeProject(localEnvironmentId, { projectIcon: customIcon });
78 const selected = makeProject(environmentId, {
79 id: ProjectId.make(id),
80 title: "Selected checkout",
81 workspaceRoot: "/selected/repo",
82 faviconPath: "selected.svg",
83 projectIcon: null,
84 });
85 const group = projectGroup([local, selected]);
86 const members = group.memberProjects.filter(
87 (member) => member.environmentId === selected.environmentId && member.id === selected.id,
88 );
89 const scoped = scopeProjectSettingsGroup(group, members);
90
91 expect(scoped).toMatchObject(selected);
92 expect(scoped.memberProjects).toBe(members);
93 expect(scoped.memberProjects).toMatchObject([selected]);
94 expect(group).toMatchObject(local);
95 });
96});
97
98describe("project removal confirmation", () => {
99 const local = {
100 id: ProjectId.make("shared-id"),
101 environmentId: EnvironmentId.make("local"),
102 title: "Repo",
103 workspaceRoot: "/work/repo",
104 environmentLabel: "Local",
105 };
106 const remote = {
107 ...local,
108 environmentId: EnvironmentId.make("remote"),
109 environmentLabel: "Remote",
110 };
111
112 const parentId = ThreadId.make("parent");
113 const listed = { rootThreadId: parentId, parentThreadId: null, relationshipToParent: null };
114 const child = { rootThreadId: parentId, parentThreadId: parentId };
115
116 it("limits a checkout's thread count to its listed threads in that environment and project", () => {
117 const plan = planProjectRemoval({
118 members: [local],
119 groupMemberCount: 2,
120 hasOtherMembers: false,
121 displayName: "Repo",
122 threads: [
123 { environmentId: local.environmentId, projectId: local.id, lineage: listed },
124 {
125 environmentId: local.environmentId,
126 projectId: local.id,
127 lineage: { ...child, relationshipToParent: "subagent" },
128 },
129 {
130 environmentId: local.environmentId,
131 projectId: local.id,
132 lineage: { ...child, relationshipToParent: "fork", sideChat: true },
133 },
134 { environmentId: remote.environmentId, projectId: remote.id, lineage: listed },
135 ],
136 });
137 expect(plan.projectThreads).toHaveLength(3);
138 expect(plan.confirmationMessage).toBe(
139 'Remove checkout "Repo" and delete its 1 thread?\nPath: /work/repo\nEnvironment: Local\nThose threads and any archived threads are deleted from T3 Code. Provider sessions are kept.\nOther entries in this grouped project are unaffected.\nThis action cannot be undone.',
140 );
141 });
142
143 it("keeps an environment-filtered group scoped to its selected checkout", () => {
144 const plan = planProjectRemoval({
145 members: [remote],
146 groupMemberCount: 1,
147 hasOtherMembers: true,
148 displayName: "Repo",
149 threads: [],
150 });
151 expect(plan.targetKind).toBe("checkout");
152 expect(plan.confirmationMessage).toContain("Environment: Remote");
153 expect(plan.confirmationMessage).toContain(
154 "Other entries in this grouped project are unaffected.",
155 );
156 expect(plan.confirmationMessage).toContain(
157 "Any archived threads are deleted from T3 Code. Provider sessions are kept.",
158 );
159 });
160
161 // regression: #21409 project-wide removal must count every checkout's threads and keep files
162 it("states the full group count and preserves files when removing a project everywhere", () => {
163 const plan = planProjectRemoval({
164 members: [local, remote],
165 groupMemberCount: 2,
166 hasOtherMembers: false,
167 displayName: "Repo",
168 threads: [],
169 });
170 expect(plan.targetKind).toBe("project");
171 expect(plan.confirmationMessage).toContain("This removes 2 grouped project entries.");
172 expect(plan.confirmationMessage).toContain(
173 "This removes only the project entries, not the files on disk.",
174 );
175 });
176});
177
178describe("projectGroupTitleNeedsUpdate", () => {
179 const folderTitle = { title: "repo" };
180 const renamed = { title: "repo", hasCustomTitle: true };
181
182 // regression: #15049 renaming to the default folder title was never saved
183 it("saves a title that matches the folder default so the rename is recorded", () => {
184 expect(projectGroupTitleNeedsUpdate([folderTitle, folderTitle], "repo", true)).toBe(true);
185 });
186
187 it("skips untouched fields and renames every member already carries", () => {
188 expect(projectGroupTitleNeedsUpdate([folderTitle, { title: "other" }], "repo", false)).toBe(
189 false,
190 );
191 expect(projectGroupTitleNeedsUpdate([renamed, renamed], "repo", true)).toBe(false);
192 });
193});