Files
upstream-tldraw/apps/docs/utils/asset-url.test.ts
upstream-mirror a774ac673a
Some checks failed
Deploy analytics worker / Deploy analytics worker to ${{ (github.ref == 'refs/heads/production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Failing after 1s
i18n upload strings / Upload latest strings (push) Failing after 1s
Deploy bemo / Deploy bemo to ${{ (github.ref == 'refs/heads/bemo-production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Failing after 1s
Deploy grafana resources / ${{ github.event_name == 'push' && 'Deploy grafana resources' || 'Validate grafana resources (dry run)' }} (push) Failing after 0s
End to end tests (dotcom) / Check for relevant changes (push) Failing after 0s
End to end tests (examples) / Check for relevant changes (push) Failing after 0s
End to end tests (mermaid readability) / Check for relevant changes (push) Failing after 1s
End to end tests (perf) / Check for relevant changes (push) Failing after 0s
Publish editor extensions / Publish editor extensions (push) Failing after 1s
Publish SDK packages / Remove the publish-packages label (push) Has been skipped
Checks / Tests & checks (push) Has been cancelled
Checks / Build all projects (push) Has been cancelled
Deploy templates / Deploy templates (push) Has been cancelled
End to end tests (dotcom) / End to end tests (dotcom) (push) Has been cancelled
End to end tests (mermaid readability) / End to end tests (mermaid readability) (push) Has been cancelled
End to end tests (perf) / End to end tests (perf) (push) Has been cancelled
Deploy .com / Deploy dotcom to ${{ (github.ref == 'refs/heads/production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Has been cancelled
End to end tests (examples) / End to end tests (examples) (push) Has been cancelled
Publish SDK packages / Publish SDK packages (push) Has been cancelled
Publish SDK packages / Tell user released version (push) Has been cancelled
Publish SDK packages / Publishes code templates to separate repositories (push) Has been cancelled
Publish SDK packages / Deploys the template demos with the new SDK version (push) Has been cancelled
snapshot of https://github.com/tldraw/tldraw.git (main, 2026-09-23; blobs > 131072B omitted)
2026-09-23 16:12:52 +05:00

166 lines
4.5 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { assetUrl } from './asset-url'
describe('assetUrl', () => {
it('returns the original path when no origin env is set', () => {
const prev = saveEnv()
try {
clearAssetEnv()
expect(assetUrl('/favicon.svg')).toBe('/favicon.svg')
} finally {
restoreEnv(prev)
}
})
it('returns the original path when ASSET_PREFIX is empty/whitespace', () => {
const prev = saveEnv()
try {
clearAssetEnv()
process.env.ASSET_PREFIX = ' '
expect(assetUrl('/favicon.svg')).toBe('/favicon.svg')
} finally {
restoreEnv(prev)
}
})
it('prefixes root-relative paths with ASSET_PREFIX', () => {
const prev = saveEnv()
try {
clearAssetEnv()
process.env.ASSET_PREFIX = 'https://docs.example.com'
expect(assetUrl('/images/foo.png')).toBe('https://docs.example.com/images/foo.png')
} finally {
restoreEnv(prev)
}
})
it('strips a trailing slash from the prefix', () => {
const prev = saveEnv()
try {
clearAssetEnv()
process.env.ASSET_PREFIX = 'https://docs.example.com/'
expect(assetUrl('/images/foo.png')).toBe('https://docs.example.com/images/foo.png')
} finally {
restoreEnv(prev)
}
})
it('uses VERCEL_URL when ASSET_PREFIX is unset', () => {
const prev = saveEnv()
try {
clearAssetEnv()
process.env.VERCEL_URL = 'tldraw-docs-abc123.vercel.app'
expect(assetUrl('/images/foo.png')).toBe(
'https://tldraw-docs-abc123.vercel.app/images/foo.png'
)
} finally {
restoreEnv(prev)
}
})
it('uses NEXT_PUBLIC_VERCEL_URL when other origins unset', () => {
const prev = saveEnv()
try {
clearAssetEnv()
process.env.NEXT_PUBLIC_VERCEL_URL = 'preview.vercel.app'
expect(assetUrl('/x')).toBe('https://preview.vercel.app/x')
} finally {
restoreEnv(prev)
}
})
it('prefers ASSET_PREFIX over NEXT_PUBLIC_DOCS_ASSET_PREFIX', () => {
const prev = saveEnv()
try {
clearAssetEnv()
process.env.ASSET_PREFIX = 'https://primary.example.com'
process.env.NEXT_PUBLIC_DOCS_ASSET_PREFIX = 'https://ignored.example.com'
expect(assetUrl('/favicon.svg')).toBe('https://primary.example.com/favicon.svg')
} finally {
restoreEnv(prev)
}
})
it('uses NEXT_PUBLIC_DOCS_ASSET_PREFIX when ASSET_PREFIX unset', () => {
const prev = saveEnv()
try {
clearAssetEnv()
process.env.NEXT_PUBLIC_DOCS_ASSET_PREFIX = 'https://docs-public.example.com'
expect(assetUrl('/images/a.png')).toBe('https://docs-public.example.com/images/a.png')
} finally {
restoreEnv(prev)
}
})
it('uses NEXT_PUBLIC_ASSET_PREFIX when earlier keys unset', () => {
const prev = saveEnv()
try {
clearAssetEnv()
process.env.NEXT_PUBLIC_ASSET_PREFIX = 'https://np.example.com'
expect(assetUrl('/b.png')).toBe('https://np.example.com/b.png')
} finally {
restoreEnv(prev)
}
})
it('prefers ASSET_PREFIX over VERCEL_URL', () => {
const prev = saveEnv()
try {
clearAssetEnv()
process.env.ASSET_PREFIX = 'https://explicit.example.com'
process.env.VERCEL_URL = 'tldraw-docs-abc123.vercel.app'
expect(assetUrl('/favicon.svg')).toBe('https://explicit.example.com/favicon.svg')
} finally {
restoreEnv(prev)
}
})
it('does not change https URLs or relative filenames', () => {
const prev = saveEnv()
try {
clearAssetEnv()
process.env.ASSET_PREFIX = 'https://docs.example.com'
expect(assetUrl('https://cdn.example.com/a.png')).toBe('https://cdn.example.com/a.png')
expect(assetUrl('relative.png')).toBe('relative.png')
} finally {
restoreEnv(prev)
}
})
it('does not prefix protocol-relative URLs', () => {
const prev = saveEnv()
try {
clearAssetEnv()
process.env.ASSET_PREFIX = 'https://docs.example.com'
expect(assetUrl('//cdn.example.com/x.png')).toBe('//cdn.example.com/x.png')
} finally {
restoreEnv(prev)
}
})
})
function saveEnv() {
return {
ASSET_PREFIX: process.env.ASSET_PREFIX,
NEXT_PUBLIC_ASSET_PREFIX: process.env.NEXT_PUBLIC_ASSET_PREFIX,
NEXT_PUBLIC_DOCS_ASSET_PREFIX: process.env.NEXT_PUBLIC_DOCS_ASSET_PREFIX,
VERCEL_URL: process.env.VERCEL_URL,
NEXT_PUBLIC_VERCEL_URL: process.env.NEXT_PUBLIC_VERCEL_URL,
}
}
function restoreEnv(prev: ReturnType<typeof saveEnv>) {
for (const [k, v] of Object.entries(prev)) {
if (v === undefined) delete process.env[k as keyof typeof prev]
else process.env[k as keyof typeof prev] = v
}
}
function clearAssetEnv() {
delete process.env.ASSET_PREFIX
delete process.env.NEXT_PUBLIC_ASSET_PREFIX
delete process.env.NEXT_PUBLIC_DOCS_ASSET_PREFIX
delete process.env.VERCEL_URL
delete process.env.NEXT_PUBLIC_VERCEL_URL
}