2021-06-22 23:34:44 +02:00
|
|
|
import { Logger, logger } from "@coder/logger"
|
|
|
|
import * as cp from "child_process"
|
2021-06-22 23:34:11 +02:00
|
|
|
import { promises as fs } from "fs"
|
|
|
|
import * as path from "path"
|
2021-04-20 00:25:57 +02:00
|
|
|
import { Page } from "playwright"
|
2021-06-22 23:34:44 +02:00
|
|
|
import { onLine } from "../../../src/node/util"
|
|
|
|
import { PASSWORD, workspaceDir } from "../../utils/constants"
|
2021-06-22 23:34:11 +02:00
|
|
|
import { tmpdir } from "../../utils/helpers"
|
|
|
|
|
2021-06-22 23:34:44 +02:00
|
|
|
interface CodeServerProcess {
|
|
|
|
process: cp.ChildProcess
|
|
|
|
address: string
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for spawning and managing code-server.
|
|
|
|
*/
|
2021-04-20 00:25:57 +02:00
|
|
|
export class CodeServer {
|
2021-06-22 23:34:44 +02:00
|
|
|
private process: Promise<CodeServerProcess> | undefined
|
|
|
|
private readonly logger: Logger
|
|
|
|
private closed = false
|
|
|
|
|
|
|
|
constructor(name: string) {
|
|
|
|
this.logger = logger.named(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The address at which code-server can be accessed. Spawns code-server if it
|
|
|
|
* has not yet been spawned.
|
|
|
|
*/
|
|
|
|
async address(): Promise<string> {
|
|
|
|
if (!this.process) {
|
|
|
|
this.process = this.spawn()
|
|
|
|
}
|
|
|
|
const { address } = await this.process
|
|
|
|
return address
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a random workspace and seed it with settings.
|
|
|
|
*/
|
|
|
|
private async createWorkspace(): Promise<string> {
|
|
|
|
const dir = await tmpdir(workspaceDir)
|
|
|
|
await fs.mkdir(path.join(dir, ".vscode"))
|
|
|
|
await fs.writeFile(
|
|
|
|
path.join(dir, ".vscode/settings.json"),
|
|
|
|
JSON.stringify({
|
|
|
|
"workbench.startupEditor": "none",
|
|
|
|
}),
|
|
|
|
"utf8",
|
|
|
|
)
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Spawn a new code-server process with its own workspace and data
|
|
|
|
* directories.
|
|
|
|
*/
|
|
|
|
private async spawn(): Promise<CodeServerProcess> {
|
|
|
|
// This will be used both as the workspace and data directory to ensure
|
|
|
|
// instances don't bleed into each other.
|
|
|
|
const dir = await this.createWorkspace()
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.logger.debug("spawning")
|
|
|
|
const proc = cp.spawn(
|
|
|
|
"node",
|
|
|
|
[
|
|
|
|
process.env.CODE_SERVER_TEST_ENTRY || ".",
|
|
|
|
// Using port zero will spawn on a random port.
|
|
|
|
"--bind-addr",
|
|
|
|
"127.0.0.1:0",
|
|
|
|
// Setting the XDG variables would be easier and more thorough but the
|
|
|
|
// modules we import ignores those variables for non-Linux operating
|
|
|
|
// systems so use these flags instead.
|
|
|
|
"--config",
|
|
|
|
path.join(dir, "config.yaml"),
|
|
|
|
"--user-data-dir",
|
|
|
|
dir,
|
|
|
|
// The last argument is the workspace to open.
|
|
|
|
dir,
|
|
|
|
],
|
|
|
|
{
|
|
|
|
cwd: path.join(__dirname, "../../.."),
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
PASSWORD,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
proc.on("error", (error) => {
|
|
|
|
this.logger.error(error.message)
|
|
|
|
reject(error)
|
|
|
|
})
|
|
|
|
|
|
|
|
proc.on("close", () => {
|
|
|
|
const error = new Error("closed unexpectedly")
|
|
|
|
if (!this.closed) {
|
|
|
|
this.logger.error(error.message)
|
|
|
|
}
|
|
|
|
reject(error)
|
|
|
|
})
|
|
|
|
|
|
|
|
let resolved = false
|
|
|
|
proc.stdout.setEncoding("utf8")
|
|
|
|
onLine(proc, (line) => {
|
|
|
|
// Log the line without the timestamp.
|
|
|
|
this.logger.trace(line.replace(/\[.+\]/, ""))
|
|
|
|
if (resolved) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const match = line.trim().match(/HTTP server listening on (https?:\/\/[.:\d]+)$/)
|
|
|
|
if (match) {
|
|
|
|
// Cookies don't seem to work on IP address so swap to localhost.
|
|
|
|
// TODO: Investigate whether this is a bug with code-server.
|
|
|
|
const address = match[1].replace("127.0.0.1", "localhost")
|
|
|
|
this.logger.debug(`spawned on ${address}`)
|
|
|
|
resolved = true
|
|
|
|
resolve({ process: proc, address })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the code-server process.
|
|
|
|
*/
|
|
|
|
async close(): Promise<void> {
|
|
|
|
logger.debug("closing")
|
|
|
|
if (this.process) {
|
|
|
|
const proc = (await this.process).process
|
|
|
|
this.closed = true // To prevent the close handler from erroring.
|
|
|
|
proc.kill()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a "Page Object Model" (https://playwright.dev/docs/pom/) meant to
|
|
|
|
* wrap over a page and represent actions on that page in a more readable way.
|
|
|
|
* This targets a specific code-server instance which must be passed in.
|
|
|
|
* Navigation and setup performed by this model will cause the code-server
|
|
|
|
* process to spawn if it hasn't yet.
|
|
|
|
*/
|
|
|
|
export class CodeServerPage {
|
2021-06-22 23:34:11 +02:00
|
|
|
private readonly editorSelector = "div.monaco-workbench"
|
2021-04-20 00:25:57 +02:00
|
|
|
|
2021-06-22 23:34:44 +02:00
|
|
|
constructor(private readonly codeServer: CodeServer, public readonly page: Page) {}
|
|
|
|
|
|
|
|
address() {
|
|
|
|
return this.codeServer.address()
|
|
|
|
}
|
2021-04-20 21:41:54 +02:00
|
|
|
|
|
|
|
/**
|
2021-06-22 23:34:44 +02:00
|
|
|
* Navigate to code-server.
|
2021-04-20 21:41:54 +02:00
|
|
|
*/
|
2021-04-20 00:25:57 +02:00
|
|
|
async navigate() {
|
2021-06-22 23:34:44 +02:00
|
|
|
const address = await this.codeServer.address()
|
|
|
|
await this.page.goto(address, { waitUntil: "networkidle" })
|
2021-04-21 23:31:15 +02:00
|
|
|
}
|
2021-04-20 21:41:54 +02:00
|
|
|
|
2021-04-21 23:31:15 +02:00
|
|
|
/**
|
|
|
|
* Checks if the editor is visible
|
2021-04-30 22:26:25 +02:00
|
|
|
* and that we are connected to the host
|
|
|
|
*
|
|
|
|
* Reload until both checks pass
|
2021-04-21 23:31:15 +02:00
|
|
|
*/
|
2021-04-30 22:26:25 +02:00
|
|
|
async reloadUntilEditorIsReady() {
|
2021-04-21 22:22:11 +02:00
|
|
|
const editorIsVisible = await this.isEditorVisible()
|
2021-04-30 22:26:25 +02:00
|
|
|
const editorIsConnected = await this.isConnected()
|
2021-04-20 21:41:54 +02:00
|
|
|
let reloadCount = 0
|
|
|
|
|
|
|
|
// Occassionally code-server timeouts in Firefox
|
|
|
|
// we're not sure why
|
|
|
|
// but usually a reload or two fixes it
|
|
|
|
// TODO@jsjoeio @oxy look into Firefox reconnection/timeout issues
|
2021-04-30 22:26:25 +02:00
|
|
|
while (!editorIsVisible && !editorIsConnected) {
|
2021-04-27 20:30:22 +02:00
|
|
|
// When a reload happens, we want to wait for all resources to be
|
|
|
|
// loaded completely. Hence why we use that instead of DOMContentLoaded
|
|
|
|
// Read more: https://thisthat.dev/dom-content-loaded-vs-load/
|
|
|
|
await this.page.waitForLoadState("load")
|
|
|
|
// Give it an extra second just in case it's feeling extra slow
|
|
|
|
await this.page.waitForTimeout(1000)
|
2021-04-20 21:41:54 +02:00
|
|
|
reloadCount += 1
|
2021-06-10 15:09:38 +02:00
|
|
|
if ((await this.isEditorVisible()) && (await this.isConnected())) {
|
2021-06-22 23:34:44 +02:00
|
|
|
logger.debug(`editor became ready after ${reloadCount} reloads`)
|
2021-04-20 21:41:54 +02:00
|
|
|
break
|
|
|
|
}
|
2021-04-27 20:30:22 +02:00
|
|
|
await this.page.reload()
|
2021-04-20 21:41:54 +02:00
|
|
|
}
|
2021-04-20 00:25:57 +02:00
|
|
|
}
|
2021-04-20 21:41:54 +02:00
|
|
|
|
2021-04-20 00:25:57 +02:00
|
|
|
/**
|
2021-04-20 21:41:54 +02:00
|
|
|
* Checks if the editor is visible
|
2021-04-20 00:25:57 +02:00
|
|
|
*/
|
2021-04-20 21:41:54 +02:00
|
|
|
async isEditorVisible() {
|
|
|
|
// Make sure the editor actually loaded
|
2021-06-10 15:09:38 +02:00
|
|
|
await this.page.waitForSelector(this.editorSelector)
|
2021-04-27 20:30:22 +02:00
|
|
|
return await this.page.isVisible(this.editorSelector)
|
2021-04-20 00:25:57 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 21:33:20 +02:00
|
|
|
/**
|
|
|
|
* Checks if the editor is visible
|
|
|
|
*/
|
|
|
|
async isConnected() {
|
|
|
|
await this.page.waitForLoadState("networkidle")
|
|
|
|
|
2021-06-22 23:34:44 +02:00
|
|
|
const host = new URL(await this.codeServer.address()).host
|
2021-04-30 22:26:25 +02:00
|
|
|
const hostSelector = `[title="Editing on ${host}"]`
|
|
|
|
await this.page.waitForSelector(hostSelector)
|
2021-04-30 21:33:20 +02:00
|
|
|
|
2021-04-30 22:26:25 +02:00
|
|
|
return await this.page.isVisible(hostSelector)
|
2021-04-30 21:33:20 +02:00
|
|
|
}
|
|
|
|
|
2021-04-20 00:25:57 +02:00
|
|
|
/**
|
2021-04-20 21:41:54 +02:00
|
|
|
* Focuses Integrated Terminal
|
2021-04-27 20:30:22 +02:00
|
|
|
* by using "Terminal: Focus Terminal"
|
|
|
|
* from the Command Palette
|
|
|
|
*
|
|
|
|
* This should focus the terminal no matter
|
|
|
|
* if it already has focus and/or is or isn't
|
|
|
|
* visible already.
|
2021-04-20 00:25:57 +02:00
|
|
|
*/
|
2021-04-20 21:41:54 +02:00
|
|
|
async focusTerminal() {
|
|
|
|
// Click [aria-label="Application Menu"] div[role="none"]
|
|
|
|
await this.page.click('[aria-label="Application Menu"] div[role="none"]')
|
2021-04-20 00:25:57 +02:00
|
|
|
|
2021-04-20 21:41:54 +02:00
|
|
|
// Click text=View
|
|
|
|
await this.page.hover("text=View")
|
|
|
|
await this.page.click("text=View")
|
2021-04-20 00:25:57 +02:00
|
|
|
|
2021-04-27 20:30:22 +02:00
|
|
|
// Click text=Command Palette
|
|
|
|
await this.page.hover("text=Command Palette")
|
|
|
|
await this.page.click("text=Command Palette")
|
|
|
|
|
|
|
|
// Type Terminal: Focus Terminal
|
|
|
|
await this.page.keyboard.type("Terminal: Focus Terminal")
|
|
|
|
|
|
|
|
// Click Terminal: Focus Terminal
|
|
|
|
await this.page.hover("text=Terminal: Focus Terminal")
|
|
|
|
await this.page.click("text=Terminal: Focus Terminal")
|
2021-04-21 23:31:15 +02:00
|
|
|
|
2021-04-27 20:30:22 +02:00
|
|
|
// Wait for terminal textarea to show up
|
|
|
|
await this.page.waitForSelector("textarea.xterm-helper-textarea")
|
2021-04-21 23:31:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-22 23:34:44 +02:00
|
|
|
* Navigates to code-server then reloads until the editor is ready.
|
2021-04-21 23:31:15 +02:00
|
|
|
*
|
2021-06-22 23:34:44 +02:00
|
|
|
* It is recommended to run setup before using this model in any tests.
|
2021-04-21 23:31:15 +02:00
|
|
|
*/
|
|
|
|
async setup() {
|
|
|
|
await this.navigate()
|
2021-04-30 22:26:25 +02:00
|
|
|
await this.reloadUntilEditorIsReady()
|
2021-04-20 00:25:57 +02:00
|
|
|
}
|
|
|
|
}
|