|
| 1 | +import { field, logger } from "@coder/logger" |
1 | 2 | import { test as base } from "@playwright/test" |
2 | | -import { CodeServer } from "./models/CodeServer" |
| 3 | +import { CodeServer, CodeServerPage } from "./models/CodeServer" |
3 | 4 |
|
4 | | -export const test = base.extend<{ codeServerPage: CodeServer }>({ |
5 | | - codeServerPage: async ({ page }, use) => { |
6 | | - const codeServer = new CodeServer(page) |
7 | | - await codeServer.navigate() |
8 | | - await use(codeServer) |
| 5 | +/** |
| 6 | + * Wraps `test.describe` to create and manage an instance of code-server. If you |
| 7 | + * don't use this you will need to create your own code-server instance and pass |
| 8 | + * it to `test.use`. |
| 9 | + * |
| 10 | + * If `includeCredentials` is `true` page requests will be authenticated. |
| 11 | + */ |
| 12 | +export const describe = (name: string, includeCredentials: boolean, fn: (codeServer: CodeServer) => void) => { |
| 13 | + test.describe(name, () => { |
| 14 | + // This will spawn on demand so nothing is necessary on before. |
| 15 | + const codeServer = new CodeServer(name) |
| 16 | + |
| 17 | + // Kill code-server after the suite has ended. This may happen even without |
| 18 | + // doing it explicitly but it seems prudent to be sure. |
| 19 | + test.afterAll(async () => { |
| 20 | + await codeServer.close() |
| 21 | + }) |
| 22 | + |
| 23 | + const storageState = JSON.parse(process.env.STORAGE || "{}") |
| 24 | + |
| 25 | + // Sanity check to ensure the cookie is set. |
| 26 | + const cookies = storageState?.cookies |
| 27 | + if (includeCredentials && (!cookies || cookies.length !== 1 || !!cookies[0].key)) { |
| 28 | + logger.error("no cookies", field("storage", JSON.stringify(cookies))) |
| 29 | + throw new Error("no credentials to include") |
| 30 | + } |
| 31 | + |
| 32 | + test.use({ |
| 33 | + // Makes `codeServer` and `authenticated` available to the extend call |
| 34 | + // below. |
| 35 | + codeServer, |
| 36 | + authenticated: includeCredentials, |
| 37 | + // This provides a cookie that authenticates with code-server. |
| 38 | + storageState: includeCredentials ? storageState : {}, |
| 39 | + }) |
| 40 | + |
| 41 | + fn(codeServer) |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +interface TestFixtures { |
| 46 | + authenticated: boolean |
| 47 | + codeServer: CodeServer |
| 48 | + codeServerPage: CodeServerPage |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Create a test that spawns code-server if necessary and ensures the page is |
| 53 | + * ready. |
| 54 | + */ |
| 55 | +export const test = base.extend<TestFixtures>({ |
| 56 | + authenticated: false, |
| 57 | + codeServer: undefined, // No default; should be provided through `test.use`. |
| 58 | + codeServerPage: async ({ authenticated, codeServer, page }, use) => { |
| 59 | + // It's possible code-server might prevent navigation because of unsaved |
| 60 | + // changes (seems to happen based on timing even if no changes have been |
| 61 | + // made too). In these cases just accept. |
| 62 | + page.on("dialog", (d) => d.accept()) |
| 63 | + |
| 64 | + const codeServerPage = new CodeServerPage(codeServer, page) |
| 65 | + await codeServerPage.setup(authenticated) |
| 66 | + await use(codeServerPage) |
9 | 67 | }, |
10 | 68 | }) |
11 | 69 |
|
| 70 | +/** Shorthand for test.expect. */ |
12 | 71 | export const expect = test.expect |
0 commit comments