1
1
import path from "node:path" ;
2
2
import { expect , test } from "@playwright/test" ;
3
3
import {
4
+ currentUser ,
4
5
importTemplate ,
5
6
login ,
6
7
randomName ,
@@ -17,7 +18,7 @@ test.beforeEach(async ({ page }) => {
17
18
test ( "create template with desired prebuilds" , async ( { page, baseURL} ) => {
18
19
requiresLicense ( ) ;
19
20
20
- const expectedPrebuilds = 3 ;
21
+ const expectedPrebuilds = 2 ;
21
22
22
23
// Create new template.
23
24
const templateName = randomName ( ) ;
@@ -28,15 +29,84 @@ test("create template with desired prebuilds", async ({ page, baseURL }) => {
28
29
29
30
await page . goto (
30
31
`/workspaces?filter=owner:prebuilds%20template:${ templateName } &page=1` ,
32
+ { waitUntil :"domcontentloaded" } ,
31
33
) ;
32
34
33
35
// Wait for prebuilds to show up.
34
36
const prebuilds = page . getByTestId ( / ^ w o r k s p a c e - .+ $ / ) ;
35
37
await prebuilds . first ( ) . waitFor ( { state :"visible" , timeout :120_000 } ) ;
36
38
expect ( ( await prebuilds . all ( ) ) . length ) . toEqual ( expectedPrebuilds ) ;
37
39
38
- // Wait for prebuilds to become ready.
39
- const readyPrebuilds = page . getByTestId ( "build-status" ) ;
40
- await readyPrebuilds . first ( ) . waitFor ( { state :"visible" , timeout :120_000 } ) ;
41
- expect ( ( await readyPrebuilds . all ( ) ) . length ) . toEqual ( expectedPrebuilds ) ;
40
+ // Wait for prebuilds to start.
41
+ const runningPrebuilds = page . getByTestId ( "build-status" ) . getByText ( "Running" ) ;
42
+ await runningPrebuilds . first ( ) . waitFor ( { state :"visible" , timeout :120_000 } ) ;
43
+ expect ( ( await runningPrebuilds . all ( ) ) . length ) . toEqual ( expectedPrebuilds ) ;
44
+ } ) ;
45
+
46
+ // NOTE: requires the `workspace-prebuilds` experiment enabled!
47
+ test ( "claim prebuild matching selected preset" , async ( { page, baseURL} ) => {
48
+ test . setTimeout ( 300_000 ) ;
49
+
50
+ requiresLicense ( ) ;
51
+
52
+ // Create new template.
53
+ const templateName = randomName ( ) ;
54
+ await importTemplate ( page , templateName , [
55
+ path . join ( __dirname , "basic-presets-with-prebuild/main.tf" ) ,
56
+ path . join ( __dirname , "basic-presets-with-prebuild/.terraform.lock.hcl" ) ,
57
+ ] ) ;
58
+
59
+ await page . goto (
60
+ `/workspaces?filter=owner:prebuilds%20template:${ templateName } &page=1` ,
61
+ { waitUntil :"domcontentloaded" } ,
62
+ ) ;
63
+
64
+ // Wait for prebuilds to show up.
65
+ const prebuilds = page . getByTestId ( / ^ w o r k s p a c e - .+ $ / ) ;
66
+ await prebuilds . first ( ) . waitFor ( { state :"visible" , timeout :120_000 } ) ;
67
+
68
+ // Wait for prebuilds to start.
69
+ const runningPrebuilds = page . getByTestId ( "build-status" ) . getByText ( "Running" ) ;
70
+ await runningPrebuilds . first ( ) . waitFor ( { state :"visible" , timeout :120_000 } ) ;
71
+
72
+ // Open the first prebuild.
73
+ await runningPrebuilds . first ( ) . click ( ) ;
74
+ await page . waitForURL ( / \/ @ p r e b u i l d s \/ p r e b u i l d - .+ / ) ;
75
+
76
+ // Wait for the prebuild to become ready so it's eligible to be claimed.
77
+ await page . getByTestId ( "agent-status-ready" ) . waitFor ( { timeout :60_000 } ) ;
78
+
79
+ // Create a new workspace using the same preset as one of the prebuilds.
80
+ await page . goto ( `/templates/coder/${ templateName } /workspace` , {
81
+ waitUntil :"domcontentloaded" ,
82
+ } ) ;
83
+
84
+ // Visit workspace creation page for new template.
85
+ await page . goto ( `/templates/default/${ templateName } /workspace` , {
86
+ waitUntil :"domcontentloaded" ,
87
+ } ) ;
88
+
89
+ // Choose a preset.
90
+ await page . locator ( 'button[aria-label="Preset"]' ) . click ( ) ;
91
+ // Choose the GoLand preset.
92
+ const preset = page . getByText ( "I Like GoLand" ) ;
93
+ await expect ( preset ) . toBeVisible ( ) ;
94
+ await preset . click ( ) ;
95
+
96
+ // Create a workspace.
97
+ const workspaceName = randomName ( ) ;
98
+ await page . locator ( "input[name=name]" ) . fill ( workspaceName ) ;
99
+ await page . getByRole ( "button" , { name :"Create workspace" } ) . click ( ) ;
100
+
101
+ // Wait for the workspace build display to be navigated to.
102
+ const user = currentUser ( page ) ;
103
+ await page . waitForURL ( `/@${ user . username } /${ workspaceName } ` , {
104
+ timeout :120_000 , // Account for workspace build time.
105
+ } ) ;
106
+
107
+ // Validate the workspace metadata that it was indeed a claimed prebuild.
108
+ const indicator = page . getByText ( "Was Prebuild" ) ;
109
+ await indicator . waitFor ( { timeout :60_000 } ) ;
110
+ const text = indicator . locator ( "xpath=.." ) . getByText ( "Yes" ) ;
111
+ await text . waitFor ( { timeout :30_000 } ) ;
42
112
} ) ;