forked from CoreBunch/Instatic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.e2e.ts
More file actions
402 lines (348 loc) · 15.4 KB
/
Copy pathdashboard.e2e.ts
File metadata and controls
402 lines (348 loc) · 15.4 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
import { expect, test, type Locator, type Page } from '@playwright/test'
import { Type, type Static } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'
const DashboardLayoutSaveItemSchema = Type.Object({
id: Type.String(),
col: Type.Optional(Type.Number()),
row: Type.Optional(Type.Number()),
size: Type.Optional(Type.Number()),
rows: Type.Optional(Type.Number()),
})
const DashboardLayoutSaveBodySchema = Type.Object({
value: Type.Object({
items: Type.Array(DashboardLayoutSaveItemSchema),
onboardingDismissed: Type.Boolean(),
}),
})
type DashboardLayoutSaveBody = Static<typeof DashboardLayoutSaveBodySchema>
type DashboardLayoutSaveItem = Static<typeof DashboardLayoutSaveItemSchema>
/**
* DASH-001 - dashboard overview metrics.
*
* The dashboard is intentionally progressive: each widget owns a small API
* fetch and renders once that domain resolves. This smoke proves the default
* first-party layout appears from a clean owner session, the dynamic widgets
* leave their loading state, the main range/customize controls respond, and
* the same surface stays contained on a 390px mobile viewport.
*/
test.describe('dashboard', () => {
test('renders first-party metric widgets and mobile layout (DASH-001)', async ({
page,
}) => {
await page.goto('/admin/dashboard')
await expect(page).toHaveURL(/\/admin\/dashboard$/)
await test.step('dashboard chrome and controls render', async () => {
await expect(page.getByText('Admin').first()).toBeVisible()
await expect(page.getByText('Dashboard', { exact: true }).first()).toBeVisible()
await expect(page.getByRole('heading', { name: 'Overview' })).toBeVisible()
await expect(page.getByText('09 blocks')).toBeVisible()
const rangeTabs = page.getByRole('tablist', { name: 'Time range' })
await expect(rangeTabs).toBeVisible()
await expect(rangeTabs.getByRole('tab', { name: 'Today' })).toHaveAttribute(
'aria-selected',
'true',
)
await rangeTabs.getByRole('tab', { name: '7d' }).click()
await expect(rangeTabs.getByRole('tab', { name: '7d' })).toHaveAttribute(
'aria-selected',
'true',
)
await expect(page.getByRole('button', { name: 'Customize' })).toBeVisible()
await expect(page.getByRole('button', { name: 'Add block' })).toBeVisible()
})
await test.step('default first-party widgets load their data', async () => {
const storage = await expectWidget(page, 'storage', 'Storage')
await expectLoaded(storage)
await expect(storage).toContainText(/used.*SQLite.*self-hosted/)
const pages = await expectWidget(page, 'pages', 'Pages')
await expectLoaded(pages)
await expect(pages).toContainText('Published')
await expect(pages).toContainText(/draft/)
await expect(pages).toContainText('scheduled')
const posts = await expectWidget(page, 'posts', 'Posts')
await expectLoaded(posts)
await expect(posts).toContainText(/Total/)
const media = await expectWidget(page, 'media', 'Media')
await expectLoaded(media)
await expect(media).toContainText(/files/)
const status = await expectWidget(page, 'status', 'Status')
await expect(status).toContainText('Site')
await expect(status).toContainText('Live')
const activity = await expectWidget(page, 'activity', 'Activity')
await expectLoaded(activity)
const publish = await expectWidget(page, 'publish', 'Publish lineup')
await expectLoaded(publish)
const plugins = await expectWidget(page, 'plugins', 'Plugins')
await expectLoaded(plugins)
const domain = await expectWidget(page, 'domain', 'Domain')
await expect(domain).toContainText('instatic.com')
await expect(domain).toContainText('HTTPS')
})
await test.step('mobile dashboard remains contained', async () => {
await page.setViewportSize({ width: 390, height: 844 })
await expect(page.getByRole('heading', { name: 'Overview' })).toBeVisible()
await expect(page.locator('[data-widget="storage"]')).toBeVisible()
await expectPageContained(page)
await expectControlContained(
page,
page.getByRole('tablist', { name: 'Time range' }),
'time range tabs',
)
await expectControlContained(
page,
page.getByRole('button', { name: 'Customize' }),
'customize button',
)
await expectControlContained(
page,
page.getByRole('button', { name: 'Add block' }),
'add block button',
)
})
})
test('customizes the widget grid and persists changes (DASH-002)', async ({
page,
}) => {
await page.goto('/admin/dashboard')
await expect(page).toHaveURL(/\/admin\/dashboard$/)
await expect(page.getByText('09 blocks')).toBeVisible()
await expect(gridWidget(page, 'ai-usage')).toHaveCount(0)
await page.getByRole('button', { name: 'Customize' }).click()
await expect(page.getByRole('button', { name: 'Done' }).first()).toBeVisible()
await page.getByRole('button', { name: 'Add block' }).first().click()
const library = page.getByRole('dialog', { name: 'Block library' })
await expect(library).toBeVisible()
await expect(library.getByRole('button', { name: 'Add AI usage to dashboard' })).toBeVisible()
const addBody = await waitForDashboardLayoutSave(page, async () => {
await library.getByRole('button', { name: 'Add AI usage to dashboard' }).click()
})
const addedItem = requireSavedDashboardItem(addBody, 'ai-usage')
expect(addedItem.size).toBe(3)
expect(addedItem.rows).toBe(3)
await expect(page.getByText('10 blocks')).toBeVisible()
await expect(gridWidget(page, 'ai-usage')).toBeVisible()
await expect(library).toContainText('Every block is on your dashboard.')
await page.getByRole('button', { name: 'Close block library' }).click()
await expect(library).toBeHidden()
await page.reload()
await expect(page.getByText('10 blocks')).toBeVisible()
await expect(gridWidget(page, 'ai-usage')).toBeVisible()
await page.getByRole('button', { name: 'Customize' }).click()
await expect(page.getByRole('button', { name: 'Done' }).first()).toBeVisible()
const aiCell = gridCell(page, 'ai-usage')
await aiCell.scrollIntoViewIfNeeded()
const moveBody = await waitForDashboardLayoutSave(page, async () => {
await dragLocatorBy(page, aiCell, 320, 0)
})
const movedItem = requireSavedDashboardItem(moveBody, 'ai-usage')
expect(movedItem.col).toBeGreaterThan(1)
await expect(aiCell).toHaveAttribute('data-col', String(movedItem.col))
const resizeBody = await waitForDashboardLayoutSave(page, async () => {
await dragLocatorBy(page, page.getByLabel('Resize AI usage from right'), 220, 0)
})
const resizedItem = requireSavedDashboardItem(resizeBody, 'ai-usage')
expect(resizedItem.size).toBeGreaterThan(3)
await expect(gridWidget(page, 'ai-usage')).toHaveAttribute(
'data-span',
String(resizedItem.size),
)
const removeBody = await waitForDashboardLayoutSave(page, async () => {
await dragWidgetToLibrary(page, aiCell)
})
expect(findSavedDashboardItem(removeBody, 'ai-usage')).toBeUndefined()
await expect(page.getByText('09 blocks')).toBeVisible()
await expect(gridWidget(page, 'ai-usage')).toHaveCount(0)
await page.reload()
await expect(page.getByText('09 blocks')).toBeVisible()
await expect(gridWidget(page, 'ai-usage')).toHaveCount(0)
await page.setViewportSize({ width: 390, height: 844 })
await page.reload()
await expect(page.getByText('09 blocks')).toBeVisible()
await page.getByRole('button', { name: 'Customize' }).click()
await expect(page.getByRole('button', { name: 'Done' }).first()).toBeVisible()
await expectPageContained(page)
await page.getByRole('button', { name: 'Add block' }).first().click()
const mobileLibrary = page.getByRole('dialog', { name: 'Block library' })
await expect(mobileLibrary).toBeVisible()
await expectControlContained(page, mobileLibrary, 'mobile block library')
await expect(mobileLibrary.getByLabel('Search blocks')).toBeVisible()
await expect(
mobileLibrary.getByRole('button', { name: 'Add AI usage to dashboard' }),
).toBeVisible()
})
test('shows onboarding tasks, routes actions, and persists dismiss (DASH-003)', async ({
page,
}) => {
await page.goto('/admin/dashboard')
await expect(page).toHaveURL(/\/admin\/dashboard$/)
const panel = onboardingPanel(page)
await expect(panel).toBeVisible({ timeout: 20_000 })
await expect(panel).toContainText('2 of 5 steps complete.')
await expectOnboardingStep(panel, 'Set site identity', 'Completed', 'Open settings')
await expectOnboardingStep(panel, 'Choose Core Framework import', 'In progress', 'Configure')
await expectOnboardingStep(panel, 'Create your first page', 'Completed', 'New page')
await expectOnboardingStep(panel, 'Install a plugin', 'Not started', 'Browse plugins')
await expectOnboardingStep(panel, 'Invite your team', 'Not started', 'Add members')
await test.step('step actions route to the expected workspaces', async () => {
await panel.getByRole('button', { name: 'Open settings' }).click()
await expect(page.getByRole('dialog', { name: 'Settings' })).toBeVisible()
await page.keyboard.press('Escape')
await expect(page.getByRole('dialog', { name: 'Settings' })).toBeHidden()
await panel.getByRole('button', { name: 'New page' }).click()
await expect(page).toHaveURL(/\/admin\/site$/)
await page.goto('/admin/dashboard')
await expect(panel).toBeVisible({ timeout: 20_000 })
await panel.getByRole('button', { name: 'Browse plugins' }).click()
await expect(page).toHaveURL(/\/admin\/plugins$/)
await page.goto('/admin/dashboard')
await expect(panel).toBeVisible({ timeout: 20_000 })
await panel.getByRole('button', { name: 'Add members' }).click()
await expect(page).toHaveURL(/\/admin\/users$/)
await page.goto('/admin/dashboard')
await expect(panel).toBeVisible({ timeout: 20_000 })
})
await test.step('mobile task layout stays contained', async () => {
await page.setViewportSize({ width: 390, height: 844 })
await page.reload()
await expect(panel).toBeVisible({ timeout: 20_000 })
await expectPageContained(page)
await expectControlContained(page, panel, 'mobile onboarding panel')
await expectControlContained(
page,
onboardingStep(panel, 'Invite your team').getByRole('button', { name: 'Add members' }),
'mobile add members action',
)
})
await test.step('dismiss persists through the dashboard layout preference', async () => {
const dismissBody = await waitForDashboardLayoutSave(page, async () => {
await panel.getByRole('button', { name: 'Dismiss' }).click()
})
expect(dismissBody.value.onboardingDismissed).toBe(true)
await expect(panel).toHaveCount(0)
await page.reload()
await expect(page.getByRole('heading', { name: 'Overview' })).toBeVisible()
await expect(
page.getByRole('heading', { name: 'Finish setting up your site' }),
).toHaveCount(0)
})
})
})
async function expectWidget(page: Page, id: string, title: string): Promise<Locator> {
const widget = page.locator(`[data-widget="${id}"]`)
await expect(widget).toBeVisible({ timeout: 20_000 })
await expect(widget).toContainText(title)
return widget
}
function gridWidget(page: Page, id: string): Locator {
return page.locator(`[data-col] [data-widget="${id}"]`)
}
function gridCell(page: Page, id: string): Locator {
return gridWidget(page, id).locator('xpath=ancestor::*[@data-col][1]')
}
function onboardingPanel(page: Page): Locator {
return page.locator('section').filter({
has: page.getByRole('heading', { name: 'Finish setting up your site' }),
})
}
function onboardingStep(panel: Locator, title: string): Locator {
return panel.locator('li').filter({ hasText: title })
}
async function expectOnboardingStep(
panel: Locator,
title: string,
state: string,
action: string,
): Promise<void> {
const step = onboardingStep(panel, title)
await expect(step).toBeVisible()
await expect(step).toContainText(state)
await expect(step.getByRole('button', { name: action })).toBeVisible()
}
async function waitForDashboardLayoutSave(
page: Page,
action: () => Promise<void>,
): Promise<DashboardLayoutSaveBody> {
const responsePromise = page.waitForResponse((response) =>
response.url().includes('/admin/api/cms/me/preferences/dashboard-layout') &&
response.request().method() === 'PUT' &&
response.status() === 200,
)
await action()
const response = await responsePromise
return Value.Parse(DashboardLayoutSaveBodySchema, await response.json())
}
function findSavedDashboardItem(
body: DashboardLayoutSaveBody,
id: string,
): DashboardLayoutSaveItem | undefined {
return body.value?.items?.find((item) => item.id === id)
}
function requireSavedDashboardItem(
body: DashboardLayoutSaveBody,
id: string,
): DashboardLayoutSaveItem {
const item = findSavedDashboardItem(body, id)
if (!item) throw new Error(`Expected saved dashboard layout to include ${id}`)
return item
}
async function dragLocatorBy(
page: Page,
locator: Locator,
deltaX: number,
deltaY: number,
): Promise<void> {
const box = await locator.boundingBox()
if (!box) throw new Error('Cannot drag locator without a bounding box')
const startX = box.x + box.width / 2
const startY = box.y + box.height / 2
await page.mouse.move(startX, startY)
await page.mouse.down()
await page.mouse.move(startX + deltaX, startY + deltaY, { steps: 12 })
await page.mouse.up()
}
async function dragWidgetToLibrary(page: Page, widgetCell: Locator): Promise<void> {
const box = await widgetCell.boundingBox()
if (!box) throw new Error('Cannot remove widget without a bounding box')
const startX = box.x + box.width / 2
const startY = box.y + box.height / 2
await page.mouse.move(startX, startY)
await page.mouse.down()
await page.mouse.move(startX, Math.min(startY + 40, 760), { steps: 4 })
const pill = page.getByRole('dialog', { name: /Block library/ })
await expect(pill).toBeVisible()
const pillBox = await pill.boundingBox()
if (!pillBox) throw new Error('Block library drop zone had no bounding box')
await page.mouse.move(pillBox.x + pillBox.width / 2, pillBox.y + pillBox.height / 2, {
steps: 12,
})
await page.mouse.up()
}
async function expectLoaded(widget: Locator): Promise<void> {
await expect(widget).not.toHaveAttribute('aria-busy', 'true', {
timeout: 20_000,
})
}
async function expectPageContained(page: Page): Promise<void> {
const metrics = await page.evaluate(() => {
const doc = document.documentElement
return {
viewportWidth: doc.clientWidth,
viewportHeight: window.innerHeight,
pageOverflow: doc.scrollWidth - doc.clientWidth,
}
})
expect(metrics.viewportWidth).toBe(390)
expect(metrics.viewportHeight).toBe(844)
expect(metrics.pageOverflow).toBeLessThanOrEqual(1)
}
async function expectControlContained(
page: Page,
locator: Locator,
description: string,
): Promise<void> {
const box = await locator.boundingBox()
if (!box) throw new Error(`${description} was visible but had no bounding box`)
const viewportWidth = await page.evaluate(() => document.documentElement.clientWidth)
expect(box.x).toBeGreaterThanOrEqual(-1)
expect(box.x + box.width).toBeLessThanOrEqual(viewportWidth + 1)
}