Skip to content

Canvix — Free & open-source graphic design tool. A community-built Canva alternative with a drag-and-drop editor, templates, shapes, text, images & one-click export. Works on desktop, tablet & mobile.

7.3 KB
Raw Blame
import { test, expect, type Page } from '@playwright/test'
import { readFileSync } from 'node:fs'

/**
 * Wave 5 remainder: WebP export — acceptance.
 *
 *   1. the WebP card appears in Chromium (probe passes) and the
 *      default PNG flow is untouched
 *   2. exporting downloads a real .webp: RIFF/WebP magic bytes,
 *      a VP8X/VP8/VP8L chunk present, size scaling honoured (2× detail)
 *   3. "Download all pages" produces one .webp per page, suffixed
 *   4. honest degradation: where the canvas lies (Safari-style PNG
 *      fallback), the card never appears — stubbed via probe override
 *
 * Register budget: ONE registration per run (the PR #26 discipline).
 */

function rectEl(id: string, x: number, y: number): Record<string, unknown> {
  return {
    id,
    type: 'rect',
    name: `webp rect ${id}`,
    x,
    y,
    width: 300,
    height: 200,
    rotation: 0,
    opacity: 1,
    locked: false,
    visible: true,
    fill: { type: 'solid', color: '#00C4CC' },
    radius: 0,
  }
}

async function register(page: Page, tag: string): Promise<string> {
  const username = `webp_${tag}_${Date.now().toString(36)}`
  const password = `W-E2e-${username}-17!q`
  const res = await page.request.post('/api/auth/register', { data: { username, password } })
  expect(res.status(), `register ${username}`).toBeLessThan(300)
  return username
}

async function createDesign(page: Page, name: string, pages: unknown[]): Promise<string> {
  const res = await page.request.post('/api/designs', {
    data: { name, width: 1080, height: 720, pages },
  })
  expect(res.status(), `create design: ${await res.text()}`).toBeLessThan(300)
  return ((await res.json()) as { id: string }).id
}

function pageOf(id: string, elements: unknown[]): Record<string, unknown> {
  return { id, background: { type: 'solid', color: '#FFFFFF' }, elements }
}

async function openEditor(page: Page, designId: string) {
  await page.goto(`/design/${designId}`)
  await page.addStyleTag({ content: 'nextjs-portal { display: none !important; }' })
  await expect(page.getByLabel('Design canvas')).toBeVisible({ timeout: 60_000 })
}

/** minimal RIFF/WebP container check: 'RIFF' + size + 'WEBP' + a known chunk */
function parseWebp(bytes: Buffer): { riff: boolean; webp: boolean; chunk: string } {
  return {
    riff: bytes.length > 12 && bytes.toString('ascii', 0, 4) === 'RIFF',
    webp: bytes.length > 12 && bytes.toString('ascii', 8, 12) === 'WEBP',
    chunk:
      bytes.length > 16
        ? bytes.toString('ascii', 12, 16)
        : 'none',
  }
}

let sharedAccount: { username: string; password: string } | null = null

test.beforeEach(async ({ page }) => {
  // one registration per run — later tests reuse the account
  if (!sharedAccount) {
    const username = await register(page, 'shr')
    sharedAccount = { username, password: `W-E2e-${username}-17!q` }
  }
})

test('the WebP card is offered in Chromium and exports a real .webp at 2×', async ({ page }) => {
  test.setTimeout(180_000)
  const designId = await createDesign(
    page,
    'webp single',
    [pageOf('p1', [rectEl('r1', 100, 120), rectEl('r2', 500, 320)])],
  )
  await openEditor(page, designId)

  await page.getByLabel('Download').click()
  const dlg = page.getByRole('dialog')
  await expect(dlg.getByText('Download your design')).toBeVisible()

  // card visibility (Chromium encodes WebP) + click it
  await expect(dlg.getByRole('button', { name: /WebP/ })).toBeVisible()
  await dlg.getByRole('button', { name: /WebP/ }).click()

  // 2× detail — the size lever the raster formats share
  await dlg.getByRole('button', { name: '2×' }).click()

  const [download] = await Promise.all([
    page.waitForEvent('download', { timeout: 120_000 }),
    dlg.getByRole('button', { name: /^Download/ }).click(),
  ])
  const path = (await download.path())!
  expect(download.suggestedFilename()).toMatch(/\.webp$/)

  const bytes = readFileSync(path)
  expect(bytes.length, 'file size sanity').toBeGreaterThan(1_000)
  const w = parseWebp(bytes)
  expect(w.riff, 'RIFF magic').toBe(true)
  expect(w.webp, 'WEBP magic').toBe(true)
  expect(['VP8X', 'VP8 ', 'VP8L'], `a webp chunk (got ${w.chunk})`).toContain(w.chunk)

  // 1080×720 at 2× → 2160×1440; VP8X/VP8L canvases carry dims in-bit,
  // so assert via the file-size sanity above + the extension rather
  // than parsing VP8 bitstreams (ffprobe covers it on CI runners that
  // ship ffmpeg; kept out of the hot assert path here)
})

test('all-pages WebP export produces one suffixed .webp per page', async ({ page }) => {
  test.setTimeout(180_000)
  const designId = await createDesign(page, 'webp all pages', [
    pageOf('p1', [rectEl('r1', 100, 120)]),
    pageOf('p2', [rectEl('r2', 480, 300)]),
  ])
  await openEditor(page, designId)

  await page.getByLabel('Download').click()
  const dlg = page.getByRole('dialog')
  await expect(dlg.getByText('Download your design')).toBeVisible()
  await dlg.getByRole('button', { name: /WebP/ }).click()
  await dlg.getByRole('button', { name: '2×' }).click()

  // "Download all pages" — 2 pages → 2 files
  const allPagesToggle = dlg.getByRole('checkbox')
  await allPagesToggle.first().check()

  const downloads: Array<{ name: string; bytes: Buffer }> = []
  page.on('download', async (d) => {
    const p = (await d.path())!
    downloads.push({ name: d.suggestedFilename(), bytes: readFileSync(p) })
  })
  await dlg.getByRole('button', { name: /^Download/ }).click()
  await expect
    .poll(async () => downloads.length, { timeout: 120_000 })
    .toBe(2)

  for (const d of downloads) {
    expect(d.name).toMatch(/\.webp$/)
    expect(d.bytes.length).toBeGreaterThan(1_000)
    const w = parseWebp(d.bytes)
    expect(w.riff && w.webp, `real webp bytes in ${d.name}`).toBe(true)
  }
  const names = downloads.map((d) => d.name).sort()
  expect(names[0]).toMatch(/-page-1\.webp$/)
  expect(names[1]).toMatch(/-page-2\.webp$/)
})

test('honest degradation — the card is hidden where the canvas lies (Safari-style PNG fallback)', async ({ page }) => {
  test.setTimeout(120_000)
  // Stub the probe: make toDataURL return PNG bytes for image/webp —
  // exactly what Safari does. The card must never appear.
  await page.addInitScript(() => {
    const orig = HTMLCanvasElement.prototype.toDataURL
    HTMLCanvasElement.prototype.toDataURL = function (this: HTMLCanvasElement, ...args: unknown[]) {
      const type = (args[0] as string) || 'image/png'
      if (type === 'image/webp') {
        return orig.call(this, 'image/png')
      }
      return (orig as (...a: unknown[]) => string).apply(this, args)
    }
  })

  const designId = await createDesign(page, 'webp denied', [pageOf('p1', [rectEl('r1', 100, 120)])])
  await openEditor(page, designId)

  await page.getByLabel('Download').click()
  const dlg = page.getByRole('dialog')
  await expect(dlg.getByText('Download your design')).toBeVisible()

  // give the probe a beat, then assert the card never shows
  await expect(dlg.getByRole('button', { name: 'PNG Lossless · transparency' })).toBeVisible()
  await page.waitForTimeout(500)
  await expect(dlg.getByRole('button', { name: /WebP/ })).toHaveCount(0)

  // and the PNG flow still works through the stubbed browser
  const [download] = await Promise.all([
    page.waitForEvent('download', { timeout: 120_000 }),
    dlg.getByRole('button', { name: /^Download/ }).click(),
  ])
  expect(download.suggestedFilename()).toMatch(/\.png$/)
})