|
1 | 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
2 | | -import type { RuntimeMode } from "@t3tools/contracts"; |
| 2 | +import type { EnvironmentId, ProjectId, RuntimeMode } from "@t3tools/contracts"; |
3 | 3 |
|
4 | 4 | const testState = vi.hoisted(() => { |
5 | 5 | let completeProjectFileRead: (value: null) => void = () => undefined; |
@@ -145,7 +145,8 @@ vi.mock("@effect/atom-react", () => ({ |
145 | 145 | : testServerConfigs(), |
146 | 146 | })); |
147 | 147 | vi.mock("@t3tools/client-runtime/environment", () => ({ |
148 | | - scopedProjectKey: () => "remote-project", |
| 148 | + scopedProjectKey: (ref: { environmentId: string; projectId: string }) => |
| 149 | + `${ref.environmentId}:${ref.projectId}`, |
149 | 150 | scopeProjectRef: (environmentId: string, projectId: string) => ({ environmentId, projectId }), |
150 | 151 | scopeThreadRef: (environmentId: string, threadId: string) => ({ environmentId, threadId }), |
151 | 152 | })); |
@@ -183,7 +184,9 @@ vi.mock("react", () => ({ |
183 | 184 | useCallback: <T>(callback: T) => callback, |
184 | 185 | useMemo: <T>(factory: () => T) => factory(), |
185 | 186 | })); |
186 | | -vi.mock("../components/Sidebar.logic", () => ({ orderItemsByPreferredIds: () => [] })); |
| 187 | +vi.mock("../components/Sidebar.logic", () => ({ |
| 188 | + orderItemsByPreferredIds: ({ items }: { items: unknown[] }) => items, |
| 189 | +})); |
187 | 190 | vi.mock("../composerDraftStore", () => { |
188 | 191 | const useComposerDraftStore = Object.assign(() => null, { |
189 | 192 | getState: () => testState.draftStore, |
@@ -247,7 +250,7 @@ vi.mock("../uiStateStore", () => ({ |
247 | 250 | })); |
248 | 251 | vi.mock("./useSettings", () => ({ getClientSettings: () => ({}) })); |
249 | 252 |
|
250 | | -import { useNewThreadHandler } from "./useHandleNewThread"; |
| 253 | +import { resolveDefaultProjectRef, useNewThreadHandler } from "./useHandleNewThread"; |
251 | 254 |
|
252 | 255 | describe.each([ |
253 | 256 | ["new", null], |
@@ -365,3 +368,36 @@ describe("useNewThreadHandler", () => { |
365 | 368 | expect(testState.router.navigate).toHaveBeenCalledOnce(); |
366 | 369 | }); |
367 | 370 | }); |
| 371 | + |
| 372 | +describe("default project selection", () => { |
| 373 | + it("prefers the last used project in its environment and falls back when unavailable", () => { |
| 374 | + const projects = [ |
| 375 | + { id: "project", environmentId: "first", workspaceRoot: "/first" }, |
| 376 | + { id: "project", environmentId: "last-used", workspaceRoot: "/last-used" }, |
| 377 | + ].map((project) => ({ |
| 378 | + ...project, |
| 379 | + id: project.id as ProjectId, |
| 380 | + environmentId: project.environmentId as EnvironmentId, |
| 381 | + title: project.environmentId, |
| 382 | + defaultModelSelection: null, |
| 383 | + scripts: [], |
| 384 | + createdAt: "2026-11-09T00:00:00.000Z", |
| 385 | + updatedAt: "2026-11-09T00:00:00.000Z", |
| 386 | + })); |
| 387 | + expect(resolveDefaultProjectRef(projects, [], "last-used:project")).toEqual({ |
| 388 | + environmentId: "last-used", |
| 389 | + projectId: "project", |
| 390 | + }); |
| 391 | + expect(resolveDefaultProjectRef(projects.slice(0, 1), [], "last-used:project")).toEqual({ |
| 392 | + environmentId: "first", |
| 393 | + projectId: "project", |
| 394 | + }); |
| 395 | + expect( |
| 396 | + resolveDefaultProjectRef( |
| 397 | + [projects[0]!, { ...projects[1]!, archivedAt: "2026-11-09T00:00:00.000Z" }], |
| 398 | + [], |
| 399 | + "last-used:project", |
| 400 | + ), |
| 401 | + ).toEqual({ environmentId: "first", projectId: "project" }); |
| 402 | + }); |
| 403 | +}); |
0 commit comments