Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectSettingsPanel.logic.test.ts

173 lines6.3 KB

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