1
- import { expect , Page } from "@playwright/test"
1
+ import { expect , Page , TestInfo } from "@playwright/test"
2
2
import { spawn } from "child_process"
3
3
import { randomUUID } from "crypto"
4
4
import path from "path"
@@ -71,7 +71,55 @@ export const startAgent = async (page: Page, token: string): Promise<void> => {
71
71
"coder" ,
72
72
"main.go" ,
73
73
)
74
- const cp = spawn ( "go" , [ "run" , coderMain , "agent" , "--no-reap" ] , {
74
+ return startAgentWithCommand ( page , token , "go" , "run" , coderMain )
75
+ }
76
+
77
+ export const downloadCoderVersion = async ( testInfo : TestInfo , version : string ) : Promise < string > => {
78
+ if ( version . startsWith ( "v" ) ) {
79
+ version = version . slice ( 1 )
80
+ }
81
+
82
+ const binaryName = "coder-e2e-" + version
83
+ const tempDir = "/tmp"
84
+ // The install script adds `./bin` automatically to the path :shrug:
85
+ const binaryPath = path . join ( tempDir , "bin" , binaryName )
86
+
87
+ const exists = await new Promise < boolean > ( ( resolve ) => {
88
+ const cp = spawn ( binaryPath , [ "version" ] )
89
+ cp . on ( "close" , ( code ) => {
90
+ resolve ( code === 0 )
91
+ } )
92
+ cp . on ( "error" , ( ) => resolve ( false ) )
93
+ } )
94
+ if ( exists ) {
95
+ return binaryPath
96
+ }
97
+
98
+ await new Promise < void > ( ( resolve , reject ) => {
99
+ const cp = spawn ( "sh" , [ "-c" , [
100
+ "curl" , "-L" , "https://coder.com/install.sh" ,
101
+ "|" ,
102
+ "sh" , "-s" , "--" ,
103
+ "--version" , version ,
104
+ "--method" , "standalone" ,
105
+ "--prefix" , tempDir ,
106
+ "--binary-name" , binaryName ,
107
+ ] . join ( " " ) ] )
108
+ cp . stderr . on ( "data" , ( data ) => testInfo . stderr . push ( data ) )
109
+ cp . stdout . on ( "data" , ( data ) => testInfo . stdout . push ( data ) )
110
+ cp . on ( "close" , ( code ) => {
111
+ if ( code === 0 ) {
112
+ resolve ( )
113
+ } else {
114
+ reject ( new Error ( "curl failed with code " + code ) )
115
+ }
116
+ } )
117
+ } )
118
+ return binaryPath
119
+ }
120
+
121
+ export const startAgentWithCommand = async ( page : Page , token : string , command : string , ...args : string [ ] ) : Promise < void > => {
122
+ const cp = spawn ( command , [ ...args , "agent" , "--no-reap" ] , {
75
123
env : {
76
124
...process . env ,
77
125
CODER_AGENT_URL : "http://localhost:" + port ,
@@ -93,10 +141,10 @@ export const startAgent = async (page: Page, token: string): Promise<void> => {
93
141
// Allows users to more easily define properties they want for agents and resources!
94
142
type RecursivePartial < T > = {
95
143
[ P in keyof T ] ?: T [ P ] extends ( infer U ) [ ]
96
- ? RecursivePartial < U > [ ]
97
- : T [ P ] extends object | undefined
98
- ? RecursivePartial < T [ P ] >
99
- : T [ P ]
144
+ ? RecursivePartial < U > [ ]
145
+ : T [ P ] extends object | undefined
146
+ ? RecursivePartial < T [ P ] >
147
+ : T [ P ]
100
148
}
101
149
102
150
interface EchoProvisionerResponses {
@@ -259,3 +307,12 @@ export const createServer = async (
259
307
await new Promise < void > ( ( r ) => e . listen ( port , r ) )
260
308
return e
261
309
}
310
+
311
+ export const findSessionToken = async ( page : Page ) : Promise < string > => {
312
+ const cookies = await page . context ( ) . cookies ( )
313
+ const sessionCookie = cookies . find ( ( c ) => c . name === "coder_session_token" )
314
+ if ( ! sessionCookie ) {
315
+ throw new Error ( "session token not found" )
316
+ }
317
+ return sessionCookie . value
318
+ }
0 commit comments