11import { promises as fs } from "fs"
2- import { Response } from "node-fetch"
32import * as path from "path"
43import { clean , tmpdir } from "../../../utils/helpers"
54import * as httpserver from "../../../utils/httpserver"
65import * as integration from "../../../utils/integration"
76
8- interface WorkbenchConfig {
9- folderUri ?: {
10- path : string
11- }
12- workspaceUri ?: {
13- path : string
14- }
15- }
16-
177describe ( "vscode" , ( ) => {
188 let codeServer : httpserver . HttpServer | undefined
199
@@ -52,52 +42,25 @@ describe("vscode", () => {
5242 }
5343 } )
5444
55- /**
56- * Get the workbench config from the provided response.
57- */
58- const getConfig = async ( resp : Response ) : Promise < WorkbenchConfig > => {
59- expect ( resp . status ) . toBe ( 200 )
60- const html = await resp . text ( )
61- const match = html . match ( / < m e t a i d = " v s c o d e - w o r k b e n c h - w e b - c o n f i g u r a t i o n " d a t a - s e t t i n g s = " ( .+ ) " > / )
62- if ( ! match || ! match [ 1 ] ) {
63- throw new Error ( "Unable to find workbench configuration" )
64- }
65- const config = match [ 1 ] . replace ( / & q u o t ; / g, '"' )
66- try {
67- return JSON . parse ( config )
68- } catch ( error ) {
69- console . error ( "Failed to parse workbench configuration" , config )
70- throw error
71- }
72- }
73-
74- it ( "should have no default folder or workspace" , async ( ) => {
75- codeServer = await integration . setup ( [ "--auth=none" ] , "" )
76-
77- const config = await getConfig ( await codeServer . fetch ( "/" ) )
78- expect ( config . folderUri ) . toBeUndefined ( )
79- expect ( config . workspaceUri ) . toBeUndefined ( )
80- } )
81-
82- it ( "should have a default folder" , async ( ) => {
83- const defaultDir = await tmpdir ( testName )
84- codeServer = await integration . setup ( [ "--auth=none" , defaultDir ] , "" )
45+ it ( "should redirect to the passed in workspace" , async ( ) => {
46+ const workspace = path . join ( await tmpdir ( testName ) , "test.code-workspace" )
47+ await fs . writeFile ( workspace , "" )
48+ codeServer = await integration . setup ( [ "--auth=none" , workspace ] , "" )
8549
86- // At first it will load the directory provided on the command line.
87- const config = await getConfig ( await codeServer . fetch ( "/" ) )
88- expect ( config . folderUri ?. path ) . toBe ( defaultDir )
89- expect ( config . workspaceUri ) . toBeUndefined ( )
50+ const resp = await codeServer . fetch ( "/" )
51+ const url = new URL ( resp . url )
52+ expect ( url . pathname ) . toBe ( "/" )
53+ expect ( url . search ) . toBe ( `?workspace= ${ workspace } ` )
9054 } )
9155
92- it ( "should have a default workspace" , async ( ) => {
93- const defaultWorkspace = path . join ( await tmpdir ( testName ) , "test.code-workspace" )
94- await fs . writeFile ( defaultWorkspace , "" )
95- codeServer = await integration . setup ( [ "--auth=none" , defaultWorkspace ] , "" )
56+ it ( "should redirect to the passed in directory" , async ( ) => {
57+ const folder = await tmpdir ( testName )
58+ codeServer = await integration . setup ( [ "--auth=none" , folder ] , "" )
9659
97- // At first it will load the workspace provided on the command line.
98- const config = await getConfig ( await codeServer . fetch ( "/" ) )
99- expect ( config . folderUri ) . toBeUndefined ( )
100- expect ( config . workspaceUri ?. path ) . toBe ( defaultWorkspace )
60+ const resp = await codeServer . fetch ( "/" )
61+ const url = new URL ( resp . url )
62+ expect ( url . pathname ) . toBe ( "/" )
63+ expect ( url . search ) . toBe ( `?folder= ${ folder } ` )
10164 } )
10265
10366 it ( "should redirect to last query folder/workspace" , async ( ) => {
@@ -136,7 +99,31 @@ describe("vscode", () => {
13699 await resp . text ( )
137100 } )
138101
139- it . only ( "should add the folder as a query param maintaining the slashes" , async ( ) => {
102+ it ( "should add the workspace as a query param maintaining the slashes" , async ( ) => {
103+ const workspace = path . join ( await tmpdir ( testName ) , "test.code-workspace" )
104+ await fs . writeFile ( workspace , "" )
105+ codeServer = await integration . setup ( [ "--auth=none" , workspace ] , "" )
106+
107+ let resp = await codeServer . fetch ( "/" , undefined )
108+
109+ expect ( resp . status ) . toBe ( 200 )
110+ const url = new URL ( resp . url )
111+ expect ( url . search ) . toBe ( `?workspace=${ workspace } ` )
112+ await resp . text ( )
113+ } )
114+
115+ it ( "should do nothing when nothing is passed in" , async ( ) => {
116+ codeServer = await integration . setup ( [ "--auth=none" ] , "" )
117+
118+ let resp = await codeServer . fetch ( "/" , undefined )
119+
120+ expect ( resp . status ) . toBe ( 200 )
121+ const url = new URL ( resp . url )
122+ expect ( url . search ) . toBe ( "" )
123+ await resp . text ( )
124+ } )
125+
126+ it ( "should add the folder as a query param maintaining the slashes" , async ( ) => {
140127 const folder = await tmpdir ( testName )
141128 codeServer = await integration . setup ( [ "--auth=none" , folder ] , "" )
142129
@@ -148,8 +135,6 @@ describe("vscode", () => {
148135 await resp . text ( )
149136 } )
150137
151- // TODO@jsjoeio - what about workspace?
152-
153138 it ( "should not redirect when last opened is ignored" , async ( ) => {
154139 codeServer = await integration . setup ( [ "--auth=none" , "--ignore-last-opened" ] , "" )
155140
0 commit comments