test(tauri): clean up font cache mocks

This commit is contained in:
Danila Poyarkov 2026-05-16 13:55:09 +03:00
parent bf636155cf
commit cb51fc73fc
2 changed files with 49 additions and 28 deletions

View file

@ -45,6 +45,14 @@ function hexDigest(data: ArrayBuffer) {
return [...new Uint8Array(data)].map((byte) => byte.toString(16).padStart(2, '0')).join('')
}
function isTauriRuntime() {
return 'window' in globalThis && '__TAURI_INTERNALS__' in globalThis.window
}
function isUnavailableTauriRuntimeError(error: unknown) {
return error instanceof ReferenceError && error.message.includes('window is not defined')
}
async function readManifest(): Promise<FontCacheManifest> {
const { BaseDirectory, readFile } = await import('@tauri-apps/plugin-fs')
try {
@ -89,6 +97,8 @@ export async function clearDownloadedFontCache(): Promise<void> {
export function createTauriDownloadedFontCache(): DownloadedFontCache {
return {
async read(family, style) {
if (!isTauriRuntime()) return null
try {
const { BaseDirectory, readFile } = await import('@tauri-apps/plugin-fs')
const manifest = await readManifest()
const entry = manifest.entries[await cacheKey(family, style)]
@ -101,9 +111,15 @@ export function createTauriDownloadedFontCache(): DownloadedFontCache {
if (buffer.byteLength !== entry.byteLength) return null
if ((await hashBytes(buffer)) !== entry.sha256) return null
return buffer
} catch (error) {
if (isUnavailableTauriRuntimeError(error)) return null
throw error
}
},
async write(family, style, data) {
if (!isTauriRuntime()) return
try {
const { BaseDirectory, mkdir, writeFile } = await import('@tauri-apps/plugin-fs')
await mkdir(CACHE_DIR, { baseDir: BaseDirectory.AppLocalData, recursive: true })
@ -124,6 +140,10 @@ export function createTauriDownloadedFontCache(): DownloadedFontCache {
updatedAt: Date.now()
}
await writeManifest(manifest)
} catch (error) {
if (isUnavailableTauriRuntimeError(error)) return
throw error
}
}
}
}

View file

@ -24,4 +24,5 @@ export async function clearTauriMocks() {
if (!('window' in globalThis)) return
const { clearMocks } = await import('@tauri-apps/api/mocks')
clearMocks()
delete (globalThis as typeof globalThis & { window?: unknown }).window
}