Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [main]
pull_request:
branches: [main]
branches: [main, fix/color-conversion-safety]

jobs:
lint:
Expand Down Expand Up @@ -42,3 +42,16 @@ jobs:
cache: 'npm'
- run: npm ci
- run: npm run test

e2e:
name: Browser conversion regressions
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- run: npm ci
- run: npx playwright-core install --with-deps chromium
- run: npm run test:e2e
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,9 @@ Made with ❤️ using [Nuxt](https://nuxt.com) and [culori](https://culorijs.or
## Batch conversion boundaries

Batch conversion parses CSS and brace-based SCSS declarations, including nested rules and SCSS variable values. Selectors, comments, strings, URLs, and interpolation spans are preserved. Indented `.sass` is not supported; export it as `.scss` or CSS first. Literal colors alongside interpolation and inside var()/env() fallbacks are converted. Invalid stylesheets show an error instead of producing a partially converted file. Parsing and conversion run in a cancellable Web Worker, and stale results are discarded when files or settings change.

## Browser regression suite

Install Chromium with `npx playwright-core install --with-deps chromium`, then run `npm run test:e2e`. Browser tests run in Vitest's Node environment against the built app, using real input/conversion/worker/upload/download flows. A controlled worker stub is used only to deterministically deliver a late stale result after reset; conversion/download assertions use the real worker. CI runs the browser suite for main-target and stacked fixes-target PRs.
Comment thread
FADL285 marked this conversation as resolved.
Resolved

This E2E branch is stacked on `fix/color-conversion-safety`; review and merge the functional fixes first. Local browser execution may require network access for the Playwright browser download.
1 change: 1 addition & 0 deletions app/components/color/FormatCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async function copyToClipboard() {
<template>
<div
class="group flex items-center justify-between p-3 rounded-lg border border-default bg-default hover:border-primary transition-colors"
:data-testid="`color-format-${format}`"
:class="{ 'opacity-50': !isValid }"
>
<div class="flex-1 min-w-0">
Expand Down
1 change: 1 addition & 0 deletions app/components/file/ConversionSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const alphaOptions = [
<div class="space-y-2">
<label class="block text-sm font-medium text-default">Target Format</label>
<USelect
aria-label="Target format"
:model-value="targetFormat"
:items="formatOptions"
value-key="value"
Expand Down
1 change: 1 addition & 0 deletions app/components/file/FileUploader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ function clearFile() {
<input
ref="fileInput"
type="file"
data-testid="file-input"
:accept="acceptedTypes.join(',')"
class="hidden"
@change="onFileSelect"
Expand Down
291 changes: 291 additions & 0 deletions test/e2e/conversion-flow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
import { describe, it, expect } from 'vitest'
import { setup, createPage } from '@nuxt/test-utils/e2e'
import { readFile } from 'node:fs/promises'
import { parse, converter } from 'culori'

describe('Real browser conversion journeys', async () => {
await setup({ browser: true, browserOptions: { type: 'chromium' } })

it('emits valid transparent RGB/HSL preserving channels and recovers from invalid input', async () => {
const page = await createPage('/')
try {
const input = page.locator('input[type="text"]').first()
await input.fill('rgba(255, 87, 51, 0.5)')
for (const format of ['rgb', 'hsl']) {
const output = page.getByTestId(`color-format-${format}`).locator('p[title]')
await expect.poll(() => output.getAttribute('title')).toMatch(new RegExp(`^${format}\\(`))
const value = (await output.getAttribute('title'))!
expect(await page.evaluate((color) => CSS.supports('color', color), value)).toBe(true)
const actual = converter('rgb')(parse(value)!)!
expect(actual.alpha).toBeCloseTo(0.5)
expect(actual.r).toBeCloseTo(1, 2)
expect(actual.g).toBeCloseTo(87 / 255, 2)
expect(actual.b).toBeCloseTo(51 / 255, 2)
}
await input.fill('not-a-color')
await input.blur()
await expect
.poll(() => page.getByText('Invalid color format', { exact: true }).isVisible())
.toBe(true)
await input.fill('#00ff00')
await expect
.poll(() => page.getByTestId('color-format-rgb').locator('p').textContent())
.toBe('rgb(0, 255, 0)')
} finally {
await page.close()
}
})

for (const extension of ['css', 'scss', 'postcss']) {
it(`uploads ${extension}, converts safe literals and downloads exact preserved output`, async () => {
const page = await createPage('/file-converter')
try {
const source = `/* #fff */\n#fff:hover { content: "🎨 #fff"; background-image: url("icons.svg#fff"); color: var(--brand, #fff); box-shadow: 0 0 #000 #{ mix(#000, #fff) }; }`
const expected = `/* #fff */\n#fff:hover { content: "🎨 #fff"; background-image: url("icons.svg#fff"); color: var(--brand, rgb(255, 255, 255)); box-shadow: 0 0 rgb(0, 0, 0) #{ mix(#000, #fff) }; }`
await page.getByRole('combobox', { name: 'Target format' }).click()
await page.getByRole('option', { name: 'RGB', exact: true }).click()
await page.getByTestId('file-input').setInputFiles({
name: `fixture.${extension}`,
mimeType: 'text/css',
buffer: Buffer.from(source)
})
const downloadButton = page.getByRole('button', { name: 'Download Converted File' })
await expect.poll(() => downloadButton.isEnabled()).toBe(true)
const [download] = await Promise.all([
page.waitForEvent('download'),
downloadButton.click()
])
expect(download.suggestedFilename()).toBe(`fixture-converted.${extension}`)
expect(await readFile((await download.path())!, 'utf8')).toBe(expected)
await page.getByRole('button', { name: 'Remove file' }).click()
await expect
.poll(() => page.getByText('No file uploaded', { exact: true }).isVisible())
.toBe(true)
expect(await downloadButton.count()).toBe(0)
} finally {
await page.close()
}
})
}

it('rejects malformed, unsupported, and oversized files then accepts a valid replacement', async () => {
const page = await createPage('/file-converter')
try {
const input = page.getByTestId('file-input')
await input.setInputFiles({
name: 'bad.sass',
mimeType: 'text/plain',
buffer: Buffer.from('.a\n color: #fff')
})
await expect
.poll(() => page.getByText('Invalid file type', { exact: true }).isVisible())
.toBe(true)
await input.setInputFiles({
name: 'large.css',
mimeType: 'text/css',
buffer: Buffer.alloc(5 * 1024 * 1024 + 1, ' ')
})
await expect
.poll(() => page.getByText('File too large', { exact: true }).isVisible())
.toBe(true)
await input.setInputFiles({
name: 'bad.css',
mimeType: 'text/css',
buffer: Buffer.from('.a { color: #fff;')
})
await expect
.poll(() => page.getByText('Unable to process stylesheet', { exact: true }).isVisible())
.toBe(true)
expect(await page.getByRole('button', { name: 'Download Converted File' }).count()).toBe(0)
await page.getByRole('button', { name: 'Remove file' }).click()
await page.getByTestId('file-input').setInputFiles({
name: 'good.css',
mimeType: 'text/css',
buffer: Buffer.from('.a { color: #fff; }')
})
await expect
.poll(() => page.getByRole('button', { name: 'Download Converted File' }).isEnabled())
.toBe(true)
} finally {
await page.close()
}
})
it('reruns conversion on alpha settings and clears an empty file safely', async () => {
const page = await createPage('/file-converter')
try {
await page.getByRole('combobox', { name: 'Target format' }).click()
await page.getByRole('option', { name: 'RGB', exact: true }).click()
await page.getByTestId('file-input').setInputFiles({
name: 'alpha.css',
mimeType: 'text/css',
buffer: Buffer.from('.a { color: rgba(255, 87, 51, 0.5); }')
})
const button = page.getByRole('button', { name: 'Download Converted File' })
await expect.poll(() => button.isEnabled()).toBe(true)
await page.getByRole('radio', { name: 'Percentage (50%)', exact: true }).click()
await expect.poll(() => button.isEnabled()).toBe(true)
const [download] = await Promise.all([page.waitForEvent('download'), button.click()])
expect(await readFile((await download.path())!, 'utf8')).toBe(
'.a { color: rgb(255 87 51 / 50%); }'
)
await page.getByRole('button', { name: 'Remove file' }).click()
await page
.getByTestId('file-input')
.setInputFiles({ name: 'empty.css', mimeType: 'text/css', buffer: Buffer.from('') })
expect(await button.count()).toBe(0)
await page.getByRole('button', { name: 'Remove file' }).click()
await expect
.poll(() => page.getByText('No file uploaded', { exact: true }).isVisible())
.toBe(true)
} finally {
await page.close()
}
})

it('cancels processing on reset and ignores a late result from the old worker', async () => {
const page = await createPage('/file-converter')
try {
await page.addInitScript(() => {
const workers: Array<{
onmessage: ((event: { data: unknown }) => void) | null
terminated: boolean
}> = []
class ControlledWorker {
onmessage: ((event: { data: unknown }) => void) | null = null
onerror = null
terminated = false
constructor() {
workers.push(this)
}
postMessage() {}
terminate() {
this.terminated = true
}
}
Object.assign(window, { Worker: ControlledWorker, __testWorkers: workers })
})
await page.reload()
await page.getByTestId('file-input').setInputFiles({
name: 'job.css',
mimeType: 'text/css',
buffer: Buffer.from('.a { color: #fff; }')
})
await expect
.poll(() => page.getByText('Processing file...', { exact: true }).isVisible())
.toBe(true)
await page.getByRole('button', { name: 'Remove file' }).click()
const terminated = await page.evaluate(() => {
const workers = (
window as unknown as {
__testWorkers: Array<{
terminated: boolean
onmessage: (event: { data: unknown }) => void
}>
}
).__testWorkers
workers[0]!.onmessage({
data: {
result: {
original: 'old',
converted: 'stale',
changes: [],
stats: { total: 1, converted: 1, failed: 0 }
}
}
})
return workers[0]!.terminated
})
expect(terminated).toBe(true)
await expect
.poll(() => page.getByText('No file uploaded', { exact: true }).isVisible())
.toBe(true)
expect(await page.getByRole('button', { name: 'Download Converted File' }).count()).toBe(0)
expect(await page.getByText('stale', { exact: true }).count()).toBe(0)
} finally {
await page.close()
}
})
it('ignores an old completion while a replacement worker is pending', async () => {
const page = await createPage('/file-converter')
try {
await page.addInitScript(() => {
const workers: Array<{
onmessage: ((event: { data: unknown }) => void) | null
terminated: boolean
}> = []
class ControlledWorker {
onmessage: ((event: { data: unknown }) => void) | null = null
onerror = null
terminated = false
constructor() {
workers.push(this)
}
postMessage() {}
terminate() {
this.terminated = true
}
}
Object.assign(window, { Worker: ControlledWorker, __testWorkers: workers })
})
await page.reload()
for (const [index, name] of ['first.css', 'second.css'].entries()) {
if (index > 0) await page.getByRole('button', { name: 'Remove file' }).click()
await page.getByTestId('file-input').setInputFiles({
name,
mimeType: 'text/css',
buffer: Buffer.from('.a { color: #fff; }')
})
await expect
.poll(() => page.getByText('Processing file...', { exact: true }).isVisible())
.toBe(true)
}
await expect
.poll(
() =>
page.evaluate(() => {
const workers = (
window as unknown as { __testWorkers: Array<{ terminated: boolean }> }
).__testWorkers
return { count: workers.length, oldTerminated: workers[0]?.terminated }
}),
{ timeout: 3000 }
)
.toEqual({ count: 2, oldTerminated: true })
const deliver = async (index: number, converted: string) =>
page.evaluate(
({ index, converted }) => {
const workers = (
window as unknown as {
__testWorkers: Array<{
terminated: boolean
onmessage: (event: { data: unknown }) => void
}>
}
).__testWorkers
workers[index]!.onmessage({
data: {
result: {
original: 'original',
converted,
changes: [],
stats: { total: 1, converted: 1, failed: 0 }
}
}
})
return { count: workers.length, oldTerminated: workers[0]!.terminated }
},
{ index, converted }
)
expect(await deliver(0, 'stale')).toEqual({ count: 2, oldTerminated: true })
expect(await page.getByText('stale', { exact: true }).count()).toBe(0)
expect(await page.getByText('Processing file...', { exact: true }).isVisible()).toBe(true)
await deliver(1, 'fresh')
const button = page.getByRole('button', { name: 'Download Converted File' })
await expect.poll(() => button.isEnabled()).toBe(true)
const [download] = await Promise.all([page.waitForEvent('download'), button.click()])
expect(await readFile((await download.path())!, 'utf8')).toBe('fresh')
} finally {
await page.close()
}
})
})
26 changes: 21 additions & 5 deletions test/e2e/file-converter.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { describe, it, expect } from 'vitest'
import { setup, $fetch, createPage } from '@nuxt/test-utils/e2e'
import { setup, createPage, $fetch } from '@nuxt/test-utils/e2e'

async function renderedHtml(path: string) {
const page = await createPage(path)
try {
return await page.content()
} finally {
await page.close()
}
}

describe('File Converter Page', async () => {
await setup({
Expand All @@ -9,19 +18,26 @@ describe('File Converter Page', async () => {
}
})

describe('Server Rendering', () => {
it('serves page text in the raw server HTML response', async () => {
const html = await $fetch('/file-converter')
expect(html).toContain('Batch Convert CSS Files')
expect(html).toContain('Upload')
expect(html).toContain('Target Format')
})

describe('Rendered page', () => {
it('returns HTML with correct title', async () => {
const html = await $fetch('/file-converter')
const html = await renderedHtml('/file-converter')
expect(html).toContain('Batch Convert CSS Files')
})

it('renders upload section', async () => {
const html = await $fetch('/file-converter')
const html = await renderedHtml('/file-converter')
expect(html).toContain('Upload')
})

it('renders settings section', async () => {
const html = await $fetch('/file-converter')
const html = await renderedHtml('/file-converter')
expect(html).toContain('Target Format')
})
})
Expand Down
Loading
Loading