@@ -2,11 +2,39 @@ import { describe, expect, it, vi } from "vite-plus/test";
22import { page , userEvent } from "vite-plus/test/browser" ;
33import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings" ;
44import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings" ;
5+ import type { SourceControlDiscoveryResult } from "@t3tools/contracts" ;
6+ import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients" ;
7+ import * as Option from "effect/Option" ;
58import { act , render } from "../../test/render" ;
69import { CommandPalette } from "./CommandPalette" ;
710import { openCommandPalette } from "../commandPaletteBus" ;
811import { primaryServerKeybindingsAtom } from "../state/server" ;
912
13+ const commands = vi . hoisted ( ( ) => ( {
14+ run : vi . fn ( async ( _input : unknown ) => ( { _tag : "Success" , value : undefined } ) ) ,
15+ createNew : vi . fn ( async ( _input : unknown ) => true ) ,
16+ query : vi . fn < ( _input : unknown ) => Promise < { _tag : "Success" ; value : unknown } > > ( async ( ) => ( {
17+ _tag : "Success" ,
18+ value : undefined ,
19+ } ) ) ,
20+ } ) ) ;
21+ const state = vi . hoisted ( ( ) => ( {
22+ discoveryKey : Symbol ( "source-control discovery" ) ,
23+ discovery : undefined as SourceControlDiscoveryResult | undefined ,
24+ canOperate : true ,
25+ multipleEnvironments : false ,
26+ } ) ) ;
27+
28+ function setup ( ) {
29+ commands . run . mockClear ( ) ;
30+ commands . createNew . mockClear ( ) ;
31+ commands . query . mockReset ( ) ;
32+ commands . query . mockResolvedValue ( { _tag : "Success" , value : undefined } ) ;
33+ state . discovery = undefined ;
34+ state . canOperate = true ;
35+ state . multipleEnvironments = false ;
36+ }
37+
1038vi . mock ( "@effect/atom-react" , async ( original ) => ( {
1139 ...( await original < typeof import ( "@effect/atom-react" ) > ( ) ) ,
1240 useAtomValue : ( atom : unknown ) =>
@@ -26,7 +54,7 @@ vi.mock("../hooks/useSettings", async (original) => ({
2654vi . mock ( "../hooks/useScratchProject" , ( ) => ( {
2755 useScratchProject : ( ) => ( { scratchEnvironmentId : ( ) => null } ) ,
2856} ) ) ;
29- vi . mock ( "../hooks/useNewProject" , ( ) => ( { useNewProject : ( ) => async ( ) => undefined } ) ) ;
57+ vi . mock ( "../hooks/useNewProject" , ( ) => ( { useNewProject : ( ) => commands . createNew } ) ) ;
3058vi . mock ( "../hooks/useHandleNewThread" , ( ) => ( {
3159 useHandleNewThread : ( ) => ( {
3260 activeDraftThread : null ,
@@ -39,26 +67,42 @@ vi.mock("../hooks/useHandleNewThread", () => ({
3967vi . mock ( "../hooks/useOpenPanelPullRequestUrl" , ( ) => ( { useOpenPanelPullRequestUrl : ( ) => null } ) ) ;
4068vi . mock ( "../connection/useDesktopLocalBootstraps" , ( ) => ( { useDesktopLocalBootstraps : ( ) => [ ] } ) ) ;
4169vi . mock ( "../state/use-atom-command" , ( ) => ( {
42- useAtomCommand : ( ) => async ( ) => ( { _tag : "Success" , value : undefined } ) ,
70+ useAtomCommand : ( ) => commands . run ,
4371} ) ) ;
4472vi . mock ( "../state/use-atom-query-runner" , ( ) => ( {
45- useAtomQueryRunner : ( ) => async ( ) => ( { _tag : "Success" , value : undefined } ) ,
73+ useAtomQueryRunner : ( ) => commands . query ,
4674} ) ) ;
75+ vi . mock ( "../state/sourceControl" , async ( original ) => {
76+ const actual = await original < typeof import ( "../state/sourceControl" ) > ( ) ;
77+ return {
78+ ...actual ,
79+ sourceControlEnvironment : {
80+ ...actual . sourceControlEnvironment ,
81+ discovery : ( ) => state . discoveryKey ,
82+ } ,
83+ } ;
84+ } ) ;
4785vi . mock ( "../state/environments" , async ( original ) => ( {
4886 ...( await original < typeof import ( "../state/environments" ) > ( ) ) ,
4987 useEnvironments : ( ) => ( {
50- environments : [
51- {
52- environmentId : "local" ,
53- label : " Local",
88+ environments : ( state . multipleEnvironments ? [ "local" , "remote" ] : [ "local" ] ) . map (
89+ ( environmentId ) => ( {
90+ environmentId,
91+ label : environmentId === "local" ? " Local" : "Remote ",
5492 connection : { phase : "connected" } ,
55- entry : { target : { _tag : "PrimaryConnectionTarget" } } ,
93+ entry : {
94+ target :
95+ environmentId === "local"
96+ ? { _tag : "PrimaryConnectionTarget" }
97+ : { _tag : "BearerConnectionTarget" , connectionId : "test-remote" } ,
98+ } ,
5699 serverConfig : {
100+ newProjectsRoot : "/workspace/" ,
57101 environment : { platform : { os : "linux" } , capabilities : { projectCloneTracking : true } } ,
58102 settings : { addProjectBaseDirectory : "/workspace/" } ,
59103 } ,
60- } ,
61- ] ,
104+ } ) ,
105+ ) ,
62106 } ) ,
63107 usePrimaryEnvironmentId : ( ) => "local" ,
64108 useEnvironmentProviders : ( ) => new Map ( ) ,
@@ -84,16 +128,19 @@ vi.mock("../state/queries", async (original) => ({
84128} ) ) ;
85129vi . mock ( "../state/session" , async ( original ) => ( {
86130 ...( await original < typeof import ( "../state/session" ) > ( ) ) ,
87- useEnvironmentScope : ( ) => true ,
88- readEnvironmentScope : ( ) => true ,
131+ useEnvironmentScope : ( ) => state . canOperate ,
132+ readEnvironmentScope : ( ) => state . canOperate ,
89133} ) ) ;
90134vi . mock ( "../state/filesystem" , async ( original ) => ( {
91135 ...( await original < typeof import ( "../state/filesystem" ) > ( ) ) ,
92136 useFilesystemReadAccess : ( ) => ( { canReadFiles : true , isPending : false , error : null } ) ,
93137} ) ) ;
94138vi . mock ( "../state/query" , async ( original ) => ( {
95139 ...( await original < typeof import ( "../state/query" ) > ( ) ) ,
96- useEnvironmentQuery : ( ) => ( { data : undefined , isLoading : false } ) ,
140+ useEnvironmentQuery : ( query : unknown ) => ( {
141+ data : query === state . discoveryKey ? state . discovery : undefined ,
142+ isLoading : false ,
143+ } ) ,
97144} ) ) ;
98145vi . mock ( "./settings/useAvailableSettingsSearchItems" , ( ) => ( {
99146 useAvailableSettingsSearchItems : ( ) => [ ] ,
@@ -105,6 +152,7 @@ describe.each(["repository", "folder", "destination"] as const)("add-project %s"
105152 { isComposing : true , keyCode : 13 } ,
106153 { isComposing : false , keyCode : 229 } ,
107154 ] ) ( "keeps Enter in the input during composition: %j" , async ( composition ) => {
155+ setup ( ) ;
108156 await using _screen = await render ( < CommandPalette > { null } </ CommandPalette > ) ;
109157 await act ( ( ) => openCommandPalette ( { open : "add-project" } ) ) ;
110158 await page . getByText ( step === "folder" ? "Local folder" : "Git URL" , { exact : true } ) . click ( ) ;
@@ -135,6 +183,7 @@ describe.each(["repository", "folder", "destination"] as const)("add-project %s"
135183} ) ;
136184
137185it ( "keeps the caret in place when an edit briefly makes the folder path not a path" , async ( ) => {
186+ setup ( ) ;
138187 await using _screen = await render ( < CommandPalette > { null } </ CommandPalette > ) ;
139188 await act ( ( ) => openCommandPalette ( { open : "add-project" } ) ) ;
140189 await page . getByText ( "Local folder" , { exact : true } ) . click ( ) ;
@@ -147,3 +196,200 @@ it("keeps the caret in place when an edit briefly makes the folder path not a pa
147196 await userEvent . keyboard ( "w" ) ;
148197 await expect . element ( input ( ) ) . toHaveValue ( "/wworkspace/project" ) ;
149198} ) ;
199+
200+ it . each ( [ "" , "Design system" ] ) (
201+ "confirms a selected folder with the project name %j" ,
202+ async ( name ) => {
203+ setup ( ) ;
204+ await using _screen = await render ( < CommandPalette > { null } </ CommandPalette > ) ;
205+ await act ( ( ) => openCommandPalette ( { open : "add-project" } ) ) ;
206+ await page . getByRole ( "textbox" , { name : "Project name" } ) . fill ( name ) ;
207+ await page . getByRole ( "button" , { name : "Local folder" , exact : true } ) . click ( ) ;
208+ await page . getByRole ( "combobox" ) . fill ( "/workspace/project" ) ;
209+ await userEvent . keyboard ( "{Enter}" ) ;
210+ await expect
211+ . element ( page . getByRole ( "textbox" , { name : "Project name" } ) )
212+ . toHaveValue ( name || "project" ) ;
213+ expect ( commands . run ) . not . toHaveBeenCalled ( ) ;
214+ await userEvent . keyboard ( "{Enter}" ) ;
215+ await vi . waitFor ( ( ) =>
216+ expect ( commands . run ) . toHaveBeenCalledWith (
217+ expect . objectContaining ( {
218+ input : expect . objectContaining ( {
219+ title : name || "project" ,
220+ workspaceRoot : "/workspace/project" ,
221+ } ) ,
222+ } ) ,
223+ ) ,
224+ ) ;
225+ } ,
226+ ) ;
227+
228+ it . each ( [ true , false ] ) ( "creates a named project only with permission: %j" , async ( canOperate ) => {
229+ setup ( ) ;
230+ state . canOperate = canOperate ;
231+ await using _screen = await render ( < CommandPalette > { null } </ CommandPalette > ) ;
232+ await act ( ( ) => openCommandPalette ( { open : "add-project" } ) ) ;
233+ await page . getByRole ( "textbox" , { name : "Project name" } ) . fill ( " Design system " ) ;
234+ await userEvent . keyboard ( "{Enter}" ) ;
235+ if ( canOperate ) {
236+ await vi . waitFor ( ( ) =>
237+ expect ( commands . createNew ) . toHaveBeenCalledWith ( {
238+ environmentId : "local" ,
239+ name : "Design system" ,
240+ publishTo : null ,
241+ } ) ,
242+ ) ;
243+ } else {
244+ expect ( commands . createNew ) . not . toHaveBeenCalled ( ) ;
245+ await expect
246+ . element ( page . getByRole ( "button" , { name : "Create project" , exact : true } ) )
247+ . toBeDisabled ( ) ;
248+ }
249+ } ) ;
250+
251+ it . each ( sourceControlClients . definitions ) (
252+ "clones from $label with the entered project name" ,
253+ async ( definition ) => {
254+ setup ( ) ;
255+ state . discovery = {
256+ versionControlSystems : [ ] ,
257+ sourceControlProviders : [
258+ {
259+ kind : definition . kind ,
260+ label : definition . label ,
261+ status : "available" ,
262+ version : Option . none ( ) ,
263+ installHint : "Install the provider" ,
264+ detail : Option . none ( ) ,
265+ auth : {
266+ status : "authenticated" ,
267+ account : Option . none ( ) ,
268+ host : Option . none ( ) ,
269+ detail : Option . none ( ) ,
270+ } ,
271+ } ,
272+ ] ,
273+ } ;
274+ const remoteUrl = "https://scm.example.test/team/repository" ;
275+ commands . query . mockImplementation ( async ( input ) => {
276+ const isLookup =
277+ typeof input === "object" &&
278+ input !== null &&
279+ "input" in input &&
280+ typeof input . input === "object" &&
281+ input . input !== null &&
282+ "repository" in input . input ;
283+ return {
284+ _tag : "Success" ,
285+ value : isLookup
286+ ? {
287+ provider : definition . kind ,
288+ nameWithOwner : "team/repository" ,
289+ url : remoteUrl ,
290+ sshUrl :
"[email protected] :team/repository.git" , 291+ }
292+ : undefined ,
293+ } ;
294+ } ) ;
295+ await using _screen = await render ( < CommandPalette > { null } </ CommandPalette > ) ;
296+ await act ( ( ) => openCommandPalette ( { open : "add-project" } ) ) ;
297+ await page . getByRole ( "textbox" , { name : "Project name" } ) . fill ( "Design system" ) ;
298+ await page
299+ . getByRole ( "button" , { name : `${ definition . pickerLabel } repository` , exact : true } )
300+ . click ( ) ;
301+ await page . getByRole ( "combobox" ) . fill ( "team/repository" ) ;
302+ await userEvent . keyboard ( "{Enter}" ) ;
303+ await expect . element ( page . getByRole ( "combobox" ) ) . toHaveValue ( "/workspace/repository" ) ;
304+ expect ( commands . query ) . toHaveBeenCalledWith ( {
305+ environmentId : "local" ,
306+ input : { provider : definition . kind , repository : "team/repository" } ,
307+ } ) ;
308+ await userEvent . keyboard ( "{Enter}" ) ;
309+ await vi . waitFor ( ( ) =>
310+ expect ( commands . run ) . toHaveBeenCalledWith (
311+ expect . objectContaining ( {
312+ environmentId : "local" ,
313+ input : expect . objectContaining ( {
314+ title : "Design system" ,
315+ destinationPath : "/workspace/repository" ,
316+ remoteUrl :
317+ definition . defaultCloneTransport === "https"
318+ ? remoteUrl
319+ :
"[email protected] :team/repository.git" , 320+ } ) ,
321+ } ) ,
322+ ) ,
323+ ) ;
324+ } ,
325+ ) ;
326+
327+ it . each ( [ "" , "Design system" ] ) ( "clones a Git URL with project name %j" , async ( name ) => {
328+ setup ( ) ;
329+ await using _screen = await render ( < CommandPalette > { null } </ CommandPalette > ) ;
330+ await act ( ( ) => openCommandPalette ( { open : "add-project" } ) ) ;
331+ await page . getByRole ( "textbox" , { name : "Project name" } ) . fill ( name ) ;
332+ await page . getByRole ( "button" , { name : "Git URL" , exact : true } ) . click ( ) ;
333+ await page . getByRole ( "combobox" ) . fill ( "https://scm.example.test/team/repository.git" ) ;
334+ await userEvent . keyboard ( "{Enter}" ) ;
335+ await expect . element ( page . getByRole ( "combobox" ) ) . toHaveValue ( "/workspace/repository" ) ;
336+ await userEvent . keyboard ( "{Enter}" ) ;
337+ await vi . waitFor ( ( ) =>
338+ expect ( commands . run ) . toHaveBeenCalledWith (
339+ expect . objectContaining ( {
340+ input : expect . objectContaining ( {
341+ title : name || "repository" ,
342+ destinationPath : "/workspace/repository" ,
343+ remoteUrl : "https://scm.example.test/team/repository.git" ,
344+ } ) ,
345+ } ) ,
346+ ) ,
347+ ) ;
348+ } ) ;
349+
350+ it ( "keeps unconfigured integrations disabled and their setup actions reachable" , async ( ) => {
351+ setup ( ) ;
352+ await using screen = await render ( < CommandPalette > { null } </ CommandPalette > ) ;
353+ await act ( ( ) => openCommandPalette ( { open : "add-project" } ) ) ;
354+ for ( const definition of sourceControlClients . definitions ) {
355+ await expect
356+ . element (
357+ page . getByRole ( "button" , {
358+ name : `${ definition . pickerLabel } repository` ,
359+ exact : true ,
360+ } ) ,
361+ )
362+ . toBeDisabled ( ) ;
363+ }
364+ await page . getByRole ( "button" , { name : "Setup Required" , exact : true } ) . first ( ) . click ( ) ;
365+ await vi . waitFor ( ( ) =>
366+ expect ( screen . router ?. state . location . pathname ) . toBe ( "/settings/source-control" ) ,
367+ ) ;
368+ expect ( commands . run ) . not . toHaveBeenCalled ( ) ;
369+ } ) ;
370+
371+ it ( "returns to machine selection without carrying a folder into another environment" , async ( ) => {
372+ setup ( ) ;
373+ state . multipleEnvironments = true ;
374+ await using _screen = await render ( < CommandPalette > { null } </ CommandPalette > ) ;
375+ await act ( ( ) => openCommandPalette ( { open : "add-project" } ) ) ;
376+ await page . getByRole ( "option" , { name : / ^ T h i s d e v i c e / } ) . click ( ) ;
377+ await page . getByRole ( "textbox" , { name : "Project name" } ) . fill ( "Keep me" ) ;
378+ await page . getByRole ( "button" , { name : "Local folder" , exact : true } ) . click ( ) ;
379+ await page . getByRole ( "combobox" ) . fill ( "/workspace/project" ) ;
380+ await userEvent . keyboard ( "{Enter}" ) ;
381+ await userEvent . keyboard ( "{Shift>}{Tab}{Tab}{/Shift}" ) ;
382+ await expect . element ( page . getByRole ( "button" , { name : "Back" , exact : true } ) ) . toHaveFocus ( ) ;
383+ await userEvent . keyboard ( "{Enter}" ) ;
384+ await page . getByRole ( "option" , { name : / ^ R e m o t e / } ) . click ( ) ;
385+ await expect . element ( page . getByRole ( "textbox" , { name : "Project name" } ) ) . toHaveValue ( "Keep me" ) ;
386+ await userEvent . keyboard ( "{Enter}" ) ;
387+ await vi . waitFor ( ( ) =>
388+ expect ( commands . createNew ) . toHaveBeenCalledWith ( {
389+ environmentId : "remote" ,
390+ name : "Keep me" ,
391+ publishTo : null ,
392+ } ) ,
393+ ) ;
394+ expect ( commands . run ) . not . toHaveBeenCalled ( ) ;
395+ } ) ;
0 commit comments