Skip to content

AI-native design editor. Open-source Figma alternative.

2.8 KB
Raw Blame
import { describe, expect, test } from 'bun:test'

import * as v from 'valibot'

import {
  buildCORSConfigurationJSON,
  buildCORSConfigurationXML,
  collectCloudCORSOrigins,
  isLikelyCORSOrNetworkError
} from '@/app/integrations/storage/s3/cors'
import { WEB_APP_ORIGIN } from '@/constants'

describe('cloud S3 CORS helpers', () => {
  test('builds XML with required methods and wildcard headers', () => {
    const xml = buildCORSConfigurationXML(['https://app.openpencil.dev', 'http://localhost:1420'])
    expect(xml).toContain('<AllowedOrigin>https://app.openpencil.dev</AllowedOrigin>')
    expect(xml).toContain('<AllowedOrigin>http://localhost:1420</AllowedOrigin>')
    expect(xml).toContain('<AllowedMethod>GET</AllowedMethod>')
    expect(xml).toContain('<AllowedMethod>PUT</AllowedMethod>')
    expect(xml).toContain('<AllowedMethod>HEAD</AllowedMethod>')
    expect(xml).toContain('<AllowedMethod>DELETE</AllowedMethod>')
    expect(xml).toContain('<AllowedHeader>*</AllowedHeader>')
    expect(xml).toContain('<ExposeHeader>ETag</ExposeHeader>')
    expect(xml).toContain('<ExposeHeader>Content-Range</ExposeHeader>')
  })

  test('escapes XML special characters in origins', () => {
    const xml = buildCORSConfigurationXML(['https://example.com/a&b'])
    expect(xml).toContain('https://example.com/a&amp;b')
  })

  test('builds AWS console JSON', () => {
    const json = v.parse(
      v.pipe(
        v.string(),
        v.parseJson(),
        v.array(
          v.object({
            AllowedOrigins: v.array(v.string()),
            AllowedMethods: v.array(v.string()),
            AllowedHeaders: v.array(v.string()),
            ExposeHeaders: v.array(v.string())
          })
        )
      ),
      buildCORSConfigurationJSON(['https://app.openpencil.dev'])
    )
    expect(json).toHaveLength(1)
    expect(json[0]?.AllowedOrigins).toContain('https://app.openpencil.dev')
    expect(json[0]?.ExposeHeaders).toEqual(expect.arrayContaining(['ETag', 'Content-Range']))
    expect(json[0]?.AllowedMethods).toEqual(
      expect.arrayContaining(['GET', 'PUT', 'POST', 'DELETE', 'HEAD'])
    )
    expect(json[0]?.AllowedHeaders).toEqual(['*'])
  })

  test('collects static web and localhost origins', () => {
    const origins = collectCloudCORSOrigins()
    expect(origins).toContain(WEB_APP_ORIGIN)
    expect(origins).toContain('https://*.openpencil.dev')
    expect(origins).toContain('https://*.openpencil-app.pages.dev')
    expect(origins).toContain('http://localhost:*')
    expect(origins).toContain('http://127.0.0.1:*')
  })

  test('detects typical browser CORS/network failures', () => {
    expect(isLikelyCORSOrNetworkError(new TypeError('Failed to fetch'))).toBe(true)
    expect(isLikelyCORSOrNetworkError(new Error('blocked by CORS policy'))).toBe(true)
    expect(isLikelyCORSOrNetworkError(new Error('Access key invalid'))).toBe(false)
  })
})