Skip to content

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

test(app): cover font settings UI

Danila Poyarkovcc216c79db32
diff --git a/src/components/FontSettings/use.ts b/src/components/FontSettings/use.ts
new file mode 100644
index 000000000..d352c031a
--- /dev/null
+++ b/src/components/FontSettings/use.ts
@@ -0,0 +1,123 @@
+import { computed, ref } from 'vue'
+
+import {
+ clearDownloadedFontCache,
+ downloadedFontCacheSummary,
+ localFontAccessState,
+ predownloadFallbackFonts,
+ requestLocalFontAccess
+} from '@/app/editor/fonts'
+
+import type { LocalFontAccessState } from '@open-pencil/core/text'
+
+export interface FontCacheSummary {
+ count: number
+ byteLength: number
+ updatedAt: number | null
+}
+
+export interface FontSettingsActions {
+ clearDownloadedFontCache: () => Promise<void>
+ downloadedFontCacheSummary: () => Promise<FontCacheSummary>
+ localFontAccessState: () => LocalFontAccessState
+ predownloadFallbackFonts: () => Promise<unknown>
+ requestLocalFontAccess: () => Promise<string[]>
+}
+
+export type FontSettingsBusyAction = 'access' | 'download' | 'clear' | 'refresh'
+
+const defaultActions: FontSettingsActions = {
+ clearDownloadedFontCache,
+ downloadedFontCacheSummary,
+ localFontAccessState,
+ predownloadFallbackFonts,
+ requestLocalFontAccess
+}
+
+export function useFontSettings(actions: FontSettingsActions = defaultActions) {
+ const cacheCount = ref(0)
+ const cacheByteLength = ref(0)
+ const cacheUpdatedAt = ref<number | null>(null)
+ const accessState = ref(actions.localFontAccessState())
+ const busyAction = ref<FontSettingsBusyAction | null>(null)
+ const status = ref('')
+
+ const cacheSize = computed(() => {
+ if (cacheByteLength.value === 0) return '0 MB'
+ return `${(cacheByteLength.value / 1024 / 1024).toFixed(1)} MB`
+ })
+
+ const cacheUpdatedLabel = computed(() => {
+ if (cacheUpdatedAt.value === null) return 'Never'
+ return new Date(cacheUpdatedAt.value).toLocaleDateString()
+ })
+
+ async function refreshSummary() {
+ busyAction.value = busyAction.value ?? 'refresh'
+ try {
+ const summary = await actions.downloadedFontCacheSummary()
+ cacheCount.value = summary.count
+ cacheByteLength.value = summary.byteLength
+ cacheUpdatedAt.value = summary.updatedAt
+ accessState.value = actions.localFontAccessState()
+ } finally {
+ if (busyAction.value === 'refresh') busyAction.value = null
+ }
+ }
+
+ async function requestAccess() {
+ busyAction.value = 'access'
+ status.value = ''
+ try {
+ await actions.requestLocalFontAccess()
+ accessState.value = actions.localFontAccessState()
+ status.value = 'Local font access enabled.'
+ } catch {
+ accessState.value = actions.localFontAccessState()
+ status.value = 'Local font access was not granted.'
+ } finally {
+ busyAction.value = null
+ }
+ }
+
+ async function downloadFallbacks() {
+ busyAction.value = 'download'
+ status.value = ''
+ try {
+ await actions.predownloadFallbackFonts()
+ await refreshSummary()
+ status.value = 'Fallback fonts downloaded.'
+ } catch {
+ status.value = 'Could not download fallback fonts.'
+ } finally {
+ busyAction.value = null
+ }
+ }
+
+ async function clearCache() {
+ busyAction.value = 'clear'
+ status.value = ''
+ try {
+ await actions.clearDownloadedFontCache()
+ await refreshSummary()
+ status.value = 'Downloaded font cache cleared.'
+ } catch {
+ status.value = 'Could not clear downloaded font cache.'
+ } finally {
+ busyAction.value = null
+ }
+ }
+
+ return {
+ accessState,
+ busyAction,
+ cacheCount,
+ cacheSize,
+ cacheUpdatedLabel,
+ status,
+ clearCache,
+ downloadFallbacks,
+ refreshSummary,
+ requestAccess
+ }
+}
diff --git a/src/components/FontSettingsPopover.vue b/src/components/FontSettingsPopover.vue
index 7b6c54ef4..a3ccc5975 100644
--- a/src/components/FontSettingsPopover.vue
+++ b/src/components/FontSettingsPopover.vue
@@ -1,89 +1,23 @@
<script setup lang="ts">
import { PopoverContent, PopoverPortal, PopoverRoot, PopoverTrigger } from 'reka-ui'
-import { computed, onMounted, ref } from 'vue'
+import { onMounted } from 'vue'
-import {
- clearDownloadedFontCache,
- downloadedFontCacheSummary,
- localFontAccessState,
- predownloadFallbackFonts,
- requestLocalFontAccess
-} from '@/app/editor/fonts'
+import { useFontSettings } from '@/components/FontSettings/use'
import { usePopoverUI } from '@/components/ui/popover'
const cls = usePopoverUI({ content: 'isolate z-[51] w-72 p-3' })
-const cacheCount = ref(0)
-const cacheByteLength = ref(0)
-const cacheUpdatedAt = ref<number | null>(null)
-const accessState = ref(localFontAccessState())
-const busyAction = ref<'access' | 'download' | 'clear' | 'refresh' | null>(null)
-const status = ref('')
-
-const cacheSize = computed(() => {
- if (cacheByteLength.value === 0) return '0 MB'
- return `${(cacheByteLength.value / 1024 / 1024).toFixed(1)} MB`
-})
-
-const cacheUpdatedLabel = computed(() => {
- if (cacheUpdatedAt.value === null) return 'Never'
- return new Date(cacheUpdatedAt.value).toLocaleDateString()
-})
-
-async function refreshSummary() {
- busyAction.value = busyAction.value ?? 'refresh'
- try {
- const summary = await downloadedFontCacheSummary()
- cacheCount.value = summary.count
- cacheByteLength.value = summary.byteLength
- cacheUpdatedAt.value = summary.updatedAt
- accessState.value = localFontAccessState()
- } finally {
- if (busyAction.value === 'refresh') busyAction.value = null
- }
-}
-
-async function requestAccess() {
- busyAction.value = 'access'
- status.value = ''
- try {
- await requestLocalFontAccess()
- accessState.value = localFontAccessState()
- status.value = 'Local font access enabled.'
- } catch {
- accessState.value = localFontAccessState()
- status.value = 'Local font access was not granted.'
- } finally {
- busyAction.value = null
- }
-}
-
-async function downloadFallbacks() {
- busyAction.value = 'download'
- status.value = ''
- try {
- await predownloadFallbackFonts()
- await refreshSummary()
- status.value = 'Fallback fonts downloaded.'
- } catch {
- status.value = 'Could not download fallback fonts.'
- } finally {
- busyAction.value = null
- }
-}
-
-async function clearCache() {
- busyAction.value = 'clear'
- status.value = ''
- try {
- await clearDownloadedFontCache()
- await refreshSummary()
- status.value = 'Downloaded font cache cleared.'
- } catch {
- status.value = 'Could not clear downloaded font cache.'
- } finally {
- busyAction.value = null
- }
-}
+const {
+ accessState,
+ busyAction,
+ cacheCount,
+ cacheSize,
+ cacheUpdatedLabel,
+ status,
+ clearCache,
+ downloadFallbacks,
+ refreshSummary,
+ requestAccess
+} = useFontSettings()
onMounted(() => {
void refreshSummary()
diff --git a/tests/e2e/font-settings.spec.ts b/tests/e2e/font-settings.spec.ts
new file mode 100644
index 000000000..63bc248df
--- /dev/null
+++ b/tests/e2e/font-settings.spec.ts
@@ -0,0 +1,25 @@
+import { expect, test } from '@playwright/test'
+
+import { CanvasHelper } from '../helpers/canvas'
+
+test('font settings popover is available from typography panel', async ({ page }) => {
+ await page.goto('/')
+ const canvas = new CanvasHelper(page)
+ await canvas.waitForInit()
+
+ await page.evaluate(() => {
+ const store = window.__OPEN_PENCIL_STORE__!
+ const id = store.createShape('TEXT', 120, 120, 240, 40)
+ store.updateNode(id, { characters: 'Font settings smoke' })
+ store.select([id])
+ })
+
+ await expect(page.locator('[data-test-id="typography-section"]')).toBeVisible()
+ await page.locator('[data-test-id="font-settings-trigger"]').click()
+
+ await expect(page.getByText('Manage local font access')).toBeVisible()
+ await expect(page.locator('[data-test-id="font-settings-request-access"]')).toBeVisible()
+ await expect(page.locator('[data-test-id="font-settings-download-fallbacks"]')).toBeVisible()
+ await expect(page.locator('[data-test-id="font-settings-refresh-cache"]')).toBeVisible()
+ await expect(page.locator('[data-test-id="font-settings-clear-cache"]')).toBeVisible()
+})
diff --git a/tests/engine/font-settings.test.ts b/tests/engine/font-settings.test.ts
new file mode 100644
index 000000000..1e20bab5b
--- /dev/null
+++ b/tests/engine/font-settings.test.ts
@@ -0,0 +1,104 @@
+import { describe, expect, test } from 'bun:test'
+
+import { useFontSettings, type FontSettingsActions } from '@/components/FontSettings/use'
+
+function actions(overrides: Partial<FontSettingsActions> = {}): FontSettingsActions {
+ let accessState: ReturnType<FontSettingsActions['localFontAccessState']> = 'prompt'
+ return {
+ async clearDownloadedFontCache() {},
+ async downloadedFontCacheSummary() {
+ return { count: 2, byteLength: 1_572_864, updatedAt: 1_700_000_000_000 }
+ },
+ localFontAccessState() {
+ return accessState
+ },
+ async predownloadFallbackFonts() {},
+ async requestLocalFontAccess() {
+ accessState = 'granted'
+ return ['Inter']
+ },
+ ...overrides
+ }
+}
+
+describe('useFontSettings', () => {
+ test('loads cache summary and formats labels', async () => {
+ const settings = useFontSettings(actions())
+
+ await settings.refreshSummary()
+
+ expect(settings.cacheCount.value).toBe(2)
+ expect(settings.cacheSize.value).toBe('1.5 MB')
+ expect(settings.cacheUpdatedLabel.value).not.toBe('Never')
+ expect(settings.busyAction.value).toBeNull()
+ })
+
+ test('requests local font access and updates status', async () => {
+ const settings = useFontSettings(actions())
+
+ await settings.requestAccess()
+
+ expect(settings.accessState.value).toBe('granted')
+ expect(settings.status.value).toBe('Local font access enabled.')
+ expect(settings.busyAction.value).toBeNull()
+ })
+
+ test('downloads fallbacks then refreshes cache summary', async () => {
+ const calls: string[] = []
+ const settings = useFontSettings(
+ actions({
+ async predownloadFallbackFonts() {
+ calls.push('download')
+ },
+ async downloadedFontCacheSummary() {
+ calls.push('summary')
+ return { count: 3, byteLength: 3_145_728, updatedAt: null }
+ }
+ })
+ )
+
+ await settings.downloadFallbacks()
+
+ expect(calls).toEqual(['download', 'summary'])
+ expect(settings.cacheCount.value).toBe(3)
+ expect(settings.cacheSize.value).toBe('3.0 MB')
+ expect(settings.status.value).toBe('Fallback fonts downloaded.')
+ })
+
+ test('clears cache then refreshes cache summary', async () => {
+ const calls: string[] = []
+ const settings = useFontSettings(
+ actions({
+ async clearDownloadedFontCache() {
+ calls.push('clear')
+ },
+ async downloadedFontCacheSummary() {
+ calls.push('summary')
+ return { count: 0, byteLength: 0, updatedAt: null }
+ }
+ })
+ )
+
+ await settings.clearCache()
+
+ expect(calls).toEqual(['clear', 'summary'])
+ expect(settings.cacheCount.value).toBe(0)
+ expect(settings.cacheSize.value).toBe('0 MB')
+ expect(settings.status.value).toBe('Downloaded font cache cleared.')
+ })
+
+ test('reports action failures without leaving busy state stuck', async () => {
+ const settings = useFontSettings(
+ actions({
+ async requestLocalFontAccess() {
+ throw new Error('denied')
+ }
+ })
+ )
+
+ await settings.requestAccess()
+
+ expect(settings.status.value).toBe('Local font access was not granted.')
+ expect(settings.busyAction.value).toBeNull()
+ })
+})