mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
114 lines
4.3 KiB
TypeScript
114 lines
4.3 KiB
TypeScript
import { AxeBuilder } from '@axe-core/playwright'
|
|
import { request as playwrightRequest, type APIRequestContext, type Page } from '@playwright/test'
|
|
import { MANAGER_COLOR_THEME_ID, OPENCODE_THEMES, type ColorThemeId } from '../shared/src/themes'
|
|
import { test, expect, type RepoFixture } from './fixtures'
|
|
import { BASE_URL, STORAGE_STATE } from './env'
|
|
|
|
type ThemeMode = 'dark' | 'light'
|
|
|
|
interface TabBarView {
|
|
name: string
|
|
path: (repo: RepoFixture) => string
|
|
label: string
|
|
activeTab: string
|
|
}
|
|
|
|
const MODES: ThemeMode[] = ['dark', 'light']
|
|
const COLOR_THEME_IDS: ColorThemeId[] = [MANAGER_COLOR_THEME_ID, ...OPENCODE_THEMES.map((theme) => theme.id)]
|
|
const DEFAULT_APPEARANCE = { theme: 'dark', colorTheme: MANAGER_COLOR_THEME_ID } as const
|
|
const PRIMARY_NAV = 'Primary navigation'
|
|
const SCHEDULE_NAV = 'Schedule navigation'
|
|
|
|
const VIEWS: TabBarView[] = [
|
|
{ name: 'schedules-mobile', path: () => '/schedules', label: PRIMARY_NAV, activeTab: 'Schedules' },
|
|
{ name: 'repo-schedules-mobile', path: (repo) => `/repos/${repo.id}/schedules`, label: SCHEDULE_NAV, activeTab: 'Jobs' },
|
|
]
|
|
|
|
let api: APIRequestContext
|
|
|
|
test.use({ viewport: { width: 390, height: 844 }, hasTouch: true, isMobile: true })
|
|
|
|
test.beforeAll(async () => {
|
|
api = await playwrightRequest.newContext({ baseURL: BASE_URL, storageState: STORAGE_STATE })
|
|
})
|
|
|
|
test.afterAll(async () => {
|
|
await api.patch('/api/settings', { data: { preferences: DEFAULT_APPEARANCE } })
|
|
await api.dispose()
|
|
})
|
|
|
|
async function applyAppearance(colorTheme: ColorThemeId, mode: ThemeMode): Promise<void> {
|
|
const response = await api.patch('/api/settings', { data: { preferences: { theme: mode, colorTheme } } })
|
|
if (!response.ok()) {
|
|
throw new Error(`appearance update failed: ${response.status()} ${await response.text()}`)
|
|
}
|
|
}
|
|
|
|
function expectedPrimaryOverride(colorTheme: ColorThemeId, mode: ThemeMode): string {
|
|
const theme = OPENCODE_THEMES.find((candidate) => candidate.id === colorTheme)
|
|
return theme ? theme[mode].primary.toLowerCase() : ''
|
|
}
|
|
|
|
async function waitForAppearance(page: Page, colorTheme: ColorThemeId, mode: ThemeMode): Promise<void> {
|
|
await expect
|
|
.poll(() =>
|
|
page.evaluate(() => ({
|
|
dark: document.documentElement.classList.contains('dark'),
|
|
primary: document.documentElement.style.getPropertyValue('--color-primary').trim().toLowerCase(),
|
|
})),
|
|
)
|
|
.toEqual({ dark: mode === 'dark', primary: expectedPrimaryOverride(colorTheme, mode) })
|
|
await page.evaluate(() =>
|
|
Promise.allSettled(
|
|
document
|
|
.getAnimations()
|
|
.filter((animation) => animation instanceof CSSTransition)
|
|
.map((animation) => animation.finished),
|
|
),
|
|
)
|
|
}
|
|
|
|
async function openTabBar(page: Page, path: string, label: string, colorTheme: ColorThemeId, mode: ThemeMode) {
|
|
await applyAppearance(colorTheme, mode)
|
|
await page.goto(path)
|
|
const nav = page.getByRole('navigation', { name: label })
|
|
await expect(nav).toBeVisible()
|
|
await waitForAppearance(page, colorTheme, mode)
|
|
return nav
|
|
}
|
|
|
|
test.describe('mobile tab bar accessibility', () => {
|
|
for (const view of VIEWS) {
|
|
for (const mode of MODES) {
|
|
test(`${view.name} tab bar passes color-contrast and target-size in Manager ${mode}`, async ({ page, repo }) => {
|
|
const nav = await openTabBar(page, view.path(repo), view.label, MANAGER_COLOR_THEME_ID, mode)
|
|
await expect(nav.getByRole('button', { name: view.activeTab })).toHaveAttribute('aria-current', 'true')
|
|
|
|
const results = await new AxeBuilder({ page })
|
|
.include(`nav[aria-label="${view.label}"]`)
|
|
.withRules(['color-contrast', 'target-size'])
|
|
.analyze()
|
|
|
|
expect(results.violations).toEqual([])
|
|
expect(results.passes.map((rule) => rule.id)).toEqual(expect.arrayContaining(['color-contrast', 'target-size']))
|
|
})
|
|
}
|
|
}
|
|
|
|
for (const colorTheme of COLOR_THEME_IDS) {
|
|
for (const mode of MODES) {
|
|
test(`active tab meets color-contrast in ${colorTheme} ${mode}`, async ({ page }) => {
|
|
await openTabBar(page, '/schedules', PRIMARY_NAV, colorTheme, mode)
|
|
|
|
const results = await new AxeBuilder({ page })
|
|
.include(`nav[aria-label="${PRIMARY_NAV}"] [aria-current="true"]`)
|
|
.withRules(['color-contrast'])
|
|
.analyze()
|
|
|
|
expect(results.violations).toEqual([])
|
|
expect(results.passes.map((rule) => rule.id)).toContain('color-contrast')
|
|
})
|
|
}
|
|
}
|
|
})
|