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
@@ -39,7 +29,7 @@ describe("vscode", () => {
3929 expect ( resp . status ) . toBe ( 200 )
4030 const html = await resp . text ( )
4131 const url = new URL ( resp . url ) // Check there were no redirections.
42- expect ( url . pathname + decodeURIComponent ( url . search ) ) . toBe ( route )
32+ expect ( url . pathname + url . search ) . toBe ( route )
4333 switch ( route ) {
4434 case "/" :
4535 case "/vscode/" :
@@ -52,59 +42,33 @@ 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 using human-readable query" , 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 folder using human-readable query" , 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 ( ) => {
10467 codeServer = await integration . setup ( [ "--auth=none" ] , "" )
10568
10669 const folder = await tmpdir ( testName )
10770 const workspace = path . join ( await tmpdir ( testName ) , "test.code-workspace" )
71+ await fs . writeFile ( workspace , "" )
10872 let resp = await codeServer . fetch ( "/" , undefined , {
10973 folder,
11074 workspace,
@@ -118,21 +82,32 @@ describe("vscode", () => {
11882 resp = await codeServer . fetch ( route )
11983 const url = new URL ( resp . url )
12084 expect ( url . pathname ) . toBe ( route )
121- expect ( decodeURIComponent ( url . search ) ) . toBe ( `?folder=${ folder } &workspace=${ workspace } ` )
85+ expect ( url . search ) . toBe ( `?folder=${ folder } &workspace=${ workspace } ` )
12286 await resp . text ( )
12387 }
12488
12589 // Closing the folder should stop the redirecting.
12690 resp = await codeServer . fetch ( "/" , undefined , { ew : "true" } )
12791 let url = new URL ( resp . url )
12892 expect ( url . pathname ) . toBe ( "/" )
129- expect ( decodeURIComponent ( url . search ) ) . toBe ( "?ew=true" )
93+ expect ( url . search ) . toBe ( "?ew=true" )
13094 await resp . text ( )
13195
13296 resp = await codeServer . fetch ( "/" )
13397 url = new URL ( resp . url )
13498 expect ( url . pathname ) . toBe ( "/" )
135- expect ( decodeURIComponent ( url . search ) ) . toBe ( "" )
99+ expect ( url . search ) . toBe ( "" )
100+ await resp . text ( )
101+ } )
102+
103+ it ( "should do nothing when nothing is passed in" , async ( ) => {
104+ codeServer = await integration . setup ( [ "--auth=none" ] , "" )
105+
106+ let resp = await codeServer . fetch ( "/" , undefined )
107+
108+ expect ( resp . status ) . toBe ( 200 )
109+ const url = new URL ( resp . url )
110+ expect ( url . search ) . toBe ( "" )
136111 await resp . text ( )
137112 } )
138113
@@ -141,6 +116,8 @@ describe("vscode", () => {
141116
142117 const folder = await tmpdir ( testName )
143118 const workspace = path . join ( await tmpdir ( testName ) , "test.code-workspace" )
119+ await fs . writeFile ( workspace , "" )
120+
144121 let resp = await codeServer . fetch ( "/" , undefined , {
145122 folder,
146123 workspace,
@@ -152,7 +129,7 @@ describe("vscode", () => {
152129 resp = await codeServer . fetch ( "/" )
153130 const url = new URL ( resp . url )
154131 expect ( url . pathname ) . toBe ( "/" )
155- expect ( decodeURIComponent ( url . search ) ) . toBe ( "" )
132+ expect ( url . search ) . toBe ( "" )
156133 await resp . text ( )
157134 } )
158135} )
0 commit comments