-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_toolbar_e2e_test.go
More file actions
435 lines (383 loc) · 12.7 KB
/
exec_toolbar_e2e_test.go
File metadata and controls
435 lines (383 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//go:build !ci
package tinkerdown_test
import (
"fmt"
"strings"
"testing"
"time"
"net/http/httptest"
"github.com/chromedp/cdproto/runtime"
"github.com/chromedp/chromedp"
"github.com/livetemplate/tinkerdown/internal/config"
"github.com/livetemplate/tinkerdown/internal/server"
)
// TestExecToolbarManualMode tests the exec toolbar functionality with manual mode.
// This test verifies:
// 1. Manual exec source shows toolbar with "Ready" state
// 2. Click Run button triggers execution
// 3. Status updates to "Success" after execution
// 4. Output panel shows command output
func TestExecToolbarManualMode(t *testing.T) {
// Enable exec sources for this test (disabled by default for security)
config.SetAllowExec(true)
defer config.SetAllowExec(false)
// Load config from test example
cfg, err := config.LoadFromDir("examples/exec-toolbar-test")
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}
// Verify source is configured with manual: true
if cfg.Sources == nil {
t.Fatal("No sources configured in tinkerdown.yaml")
}
source, ok := cfg.Sources["system-info"]
if !ok {
t.Fatal("system-info source not found in config")
}
if source.Type != "exec" {
t.Fatalf("Expected exec source type, got: %s", source.Type)
}
if !source.Manual {
t.Fatal("Expected manual: true for system-info source")
}
t.Logf("Source config: type=%s, cmd=%s, manual=%t", source.Type, source.Cmd, source.Manual)
// Create test server
srv := server.NewWithConfig("examples/exec-toolbar-test", cfg)
if err := srv.Discover(); err != nil {
t.Fatalf("Failed to discover pages: %v", err)
}
handler := server.WithCompression(srv)
ts := httptest.NewServer(handler)
defer ts.Close()
// Setup Docker Chrome for reliable CI execution
chromeCtx, cleanup := SetupDockerChrome(t, 60*time.Second)
defer cleanup()
ctx := chromeCtx.Context
// Store console logs for debugging
var consoleLogs []string
chromedp.ListenTarget(ctx, func(ev interface{}) {
if ev, ok := ev.(*runtime.EventConsoleAPICalled); ok {
for _, arg := range ev.Args {
consoleLogs = append(consoleLogs, fmt.Sprintf("[Console] %s", arg.Value))
}
}
})
// Convert URL for Docker Chrome access
url := ConvertURLForDockerChrome(ts.URL)
t.Logf("Test server URL: %s (Docker: %s)", ts.URL, url)
// Test 1: Navigate and wait for page to load
var hasInteractiveBlock bool
err = chromedp.Run(ctx,
chromedp.Navigate(url+"/"),
chromedp.Sleep(3*time.Second),
chromedp.Evaluate(`document.querySelector('.tinkerdown-interactive-block') !== null`, &hasInteractiveBlock),
)
if err != nil {
t.Fatalf("Failed to navigate: %v", err)
}
if !hasInteractiveBlock {
var htmlContent string
chromedp.Run(ctx, chromedp.OuterHTML("html", &htmlContent))
t.Logf("HTML: %s", htmlContent[:min(2000, len(htmlContent))])
t.Fatal("Page did not load correctly - no interactive block found")
}
t.Log("Page loaded with interactive block")
// Test 2: Verify exec toolbar exists for manual source
var hasExecToolbar bool
var hasExecSource bool
err = chromedp.Run(ctx,
chromedp.Evaluate(`document.querySelector('.exec-toolbar') !== null`, &hasExecToolbar),
chromedp.Evaluate(`document.querySelector('[data-exec-source="true"]') !== null`, &hasExecSource),
)
if err != nil {
t.Fatalf("Failed to check exec toolbar: %v", err)
}
if !hasExecSource {
var htmlContent string
chromedp.Run(ctx, chromedp.OuterHTML("html", &htmlContent))
t.Logf("HTML: %s", htmlContent[:min(3000, len(htmlContent))])
t.Fatal("No element with data-exec-source found")
}
t.Log("Found element with data-exec-source attribute")
if !hasExecToolbar {
var htmlContent string
chromedp.Run(ctx, chromedp.OuterHTML("html", &htmlContent))
t.Logf("HTML: %s", htmlContent[:min(3000, len(htmlContent))])
t.Logf("Console logs: %v", consoleLogs)
t.Fatal("Exec toolbar not found - toolbar injection failed")
}
t.Log("Exec toolbar found")
// Test 3: Verify Run button exists and is enabled
var runBtnExists bool
var runBtnDisabled bool
err = chromedp.Run(ctx,
chromedp.Evaluate(`document.querySelector('.exec-toolbar-run-btn') !== null`, &runBtnExists),
chromedp.Evaluate(`document.querySelector('.exec-toolbar-run-btn')?.disabled === true`, &runBtnDisabled),
)
if err != nil {
t.Fatalf("Failed to check run button: %v", err)
}
if !runBtnExists {
t.Fatal("Run button not found")
}
t.Log("Run button exists")
if runBtnDisabled {
t.Fatal("Run button should be enabled in idle state")
}
t.Log("Run button is enabled")
// Test 4: Verify initial status is "Ready" (idle mode for manual source)
var statusText string
err = chromedp.Run(ctx,
chromedp.Evaluate(`
(() => {
const statusEl = document.querySelector('.exec-toolbar-status');
return statusEl ? statusEl.textContent.trim() : '';
})()
`, &statusText),
)
if err != nil {
t.Fatalf("Failed to get status text: %v", err)
}
if statusText != "Ready" {
t.Fatalf("Expected status 'Ready' for manual mode, got: '%s'", statusText)
}
t.Log("Initial status is 'Ready'")
// Wait for WebSocket connection before clicking Run
// The livemdtools client logs "[Livemdtools] Connected" when ready
var isConnected bool
for i := 0; i < 20; i++ {
chromedp.Run(ctx, chromedp.Evaluate(`
(() => {
// Check if any console log contains 'Connected'
return window.tinkerdown && window.tinkerdown._client && window.tinkerdown._client.isConnected ? true : false;
})()
`, &isConnected))
if isConnected {
break
}
time.Sleep(200 * time.Millisecond)
}
if !isConnected {
// Fallback: check console logs for connection
for _, log := range consoleLogs {
if strings.Contains(log, "[Livemdtools] Connected") {
isConnected = true
break
}
}
}
if !isConnected {
// One more approach: just wait a bit more for connection
time.Sleep(2 * time.Second)
}
t.Log("WebSocket connection established")
// Test 5: Click Run button and verify execution
err = chromedp.Run(ctx,
chromedp.Click(".exec-toolbar-run-btn", chromedp.ByQuery),
chromedp.Sleep(3*time.Second), // Wait for execution
)
if err != nil {
t.Fatalf("Failed to click Run button: %v", err)
}
t.Log("Clicked Run button")
// Test 6: Verify status changes to Success
var statusAfterRun string
var hasDuration bool
err = chromedp.Run(ctx,
chromedp.Evaluate(`
(() => {
const statusEl = document.querySelector('.exec-toolbar-status');
return statusEl ? statusEl.textContent.trim() : '';
})()
`, &statusAfterRun),
chromedp.Evaluate(`
(() => {
const durationEl = document.querySelector('.exec-toolbar-duration');
return durationEl && durationEl.textContent.trim() !== '';
})()
`, &hasDuration),
)
if err != nil {
t.Fatalf("Failed to check status after run: %v", err)
}
if statusAfterRun != "✓ Success" {
t.Logf("Console logs: %v", consoleLogs)
t.Fatalf("Expected status '✓ Success' after execution, got: '%s'", statusAfterRun)
}
t.Log("Status changed to '✓ Success'")
if !hasDuration {
t.Log("Warning: Duration not displayed (may not be critical)")
} else {
t.Log("Duration displayed")
}
// Test 7: Verify output panel can be toggled
var outputToggleExists bool
err = chromedp.Run(ctx,
chromedp.Evaluate(`document.querySelector('.exec-output-toggle') !== null`, &outputToggleExists),
)
if err != nil {
t.Fatalf("Failed to check output toggle: %v", err)
}
if !outputToggleExists {
t.Fatal("Output toggle button not found")
}
t.Log("Output toggle button exists")
// Click output toggle to expand using JavaScript click for reliability
var clickResult string
err = chromedp.Run(ctx,
chromedp.Evaluate(`
(() => {
// Find the first output toggle (in lvt-0 block)
const toggleBtn = document.querySelector('[data-block-id="lvt-0"] .exec-output-toggle');
if (!toggleBtn) {
return "no toggle button found";
}
toggleBtn.click();
return "clicked";
})()
`, &clickResult),
chromedp.Sleep(500*time.Millisecond),
)
if err != nil {
t.Fatalf("Failed to click output toggle: %v", err)
}
t.Logf("Click result: %s", clickResult)
// Verify output panel expanded
var outputExpanded bool
var debugInfo string
err = chromedp.Run(ctx,
chromedp.Evaluate(`
(() => {
const content = document.querySelector('[data-block-id="lvt-0"] .exec-output-content');
if (!content) return { expanded: false, debug: "no content element" };
return {
expanded: content.classList.contains('expanded'),
debug: "classes: " + content.className
};
})()
`, &struct {
Expanded bool `json:"expanded"`
Debug string `json:"debug"`
}{Expanded: outputExpanded, Debug: debugInfo}),
)
if err != nil {
t.Fatalf("Failed to check output expansion: %v", err)
}
// Also check with simple query
err = chromedp.Run(ctx,
chromedp.Evaluate(`document.querySelector('[data-block-id="lvt-0"] .exec-output-content.expanded') !== null`, &outputExpanded),
)
if err != nil {
t.Fatalf("Failed to check output expansion: %v", err)
}
if !outputExpanded {
// Get more debug info
var classInfo string
chromedp.Run(ctx,
chromedp.Evaluate(`document.querySelector('[data-block-id="lvt-0"] .exec-output-content')?.className || "not found"`, &classInfo),
)
t.Logf("Output content classes: %s", classInfo)
t.Fatal("Output panel did not expand")
}
t.Log("Output panel expanded")
// Test 8: Verify template content was updated with data
var hasHostnameData bool
err = chromedp.Run(ctx,
chromedp.Evaluate(`document.body.innerHTML.includes('hostname')`, &hasHostnameData),
)
if err != nil {
t.Fatalf("Failed to check data rendering: %v", err)
}
if !hasHostnameData {
var htmlContent string
chromedp.Run(ctx, chromedp.OuterHTML("html", &htmlContent))
t.Logf("HTML: %s", htmlContent[:min(3000, len(htmlContent))])
t.Fatal("Data was not rendered in template")
}
t.Log("Data rendered in template")
t.Log("All exec toolbar tests passed!")
}
// TestExecToolbarAutoMode tests that auto-execute mode works correctly
func TestExecToolbarAutoMode(t *testing.T) {
// Enable exec sources for this test (disabled by default for security)
config.SetAllowExec(true)
defer config.SetAllowExec(false)
// Load config from test example
cfg, err := config.LoadFromDir("examples/exec-toolbar-test")
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}
// Verify auto-info source (no manual: true)
source, ok := cfg.Sources["auto-info"]
if !ok {
t.Fatal("auto-info source not found in config")
}
if source.Manual {
t.Fatal("auto-info source should not have manual: true")
}
t.Logf("Auto-info source config: type=%s, manual=%t", source.Type, source.Manual)
// Create test server
srv := server.NewWithConfig("examples/exec-toolbar-test", cfg)
if err := srv.Discover(); err != nil {
t.Fatalf("Failed to discover pages: %v", err)
}
handler := server.WithCompression(srv)
ts := httptest.NewServer(handler)
defer ts.Close()
// Setup Docker Chrome for reliable CI execution
chromeCtx, cleanup := SetupDockerChrome(t, 60*time.Second)
defer cleanup()
ctx := chromeCtx.Context
// Convert URL for Docker Chrome access
url := ConvertURLForDockerChrome(ts.URL)
t.Logf("Test server URL: %s (Docker: %s)", ts.URL, url)
// Navigate and wait for page to load
err = chromedp.Run(ctx,
chromedp.Navigate(url+"/"),
chromedp.Sleep(5*time.Second), // Wait for auto-execution
)
if err != nil {
t.Fatalf("Failed to navigate: %v", err)
}
// For auto-execute mode, status should be "Success" after initial load
var statusText string
err = chromedp.Run(ctx,
chromedp.Evaluate(`
(() => {
// Find the second exec toolbar (for auto-info)
const toolbars = document.querySelectorAll('.exec-toolbar');
if (toolbars.length >= 2) {
const statusEl = toolbars[1].querySelector('.exec-toolbar-status');
return statusEl ? statusEl.textContent.trim() : '';
}
return 'not found';
})()
`, &statusText),
)
if err != nil {
t.Fatalf("Failed to get auto-info status: %v", err)
}
t.Logf("Auto-info status: %s", statusText)
// Verify auto-execute block shows success (it runs on page load)
if statusText != "✓ Success" && statusText != "Ready" {
// Note: might still be Ready if no toolbar is rendered for auto blocks
// depending on implementation. The key is that it auto-executed.
t.Logf("Warning: Auto-info status is '%s', might need to adjust test", statusText)
}
// Verify the auto-executed block rendered its data
var hasReadyData bool
err = chromedp.Run(ctx,
chromedp.Evaluate(`document.body.innerHTML.includes('ready')`, &hasReadyData),
)
if err != nil {
t.Fatalf("Failed to check auto data: %v", err)
}
if !hasReadyData {
var htmlContent string
chromedp.Run(ctx, chromedp.OuterHTML("html", &htmlContent))
t.Logf("HTML: %s", htmlContent[:min(3000, len(htmlContent))])
t.Fatal("Auto-execute data was not rendered")
}
t.Log("Auto-execute block data rendered")
t.Log("All auto mode tests passed!")
}