test(app): cover Tauri font cache helpers

This commit is contained in:
Danila Poyarkov 2026-05-05 21:58:12 +03:00
parent dac6f171e3
commit e5b0767a43
2 changed files with 99 additions and 0 deletions

View file

@ -0,0 +1,80 @@
import { afterEach, describe, expect, test } from 'bun:test'
import { clearDownloadedFontCache, downloadedFontCacheSummary } from '@/app/editor/fonts/cache'
import { clearTauriMocks, mockTauriIPC } from '../helpers/tauri-mocks'
const encoder = new TextEncoder()
afterEach(async () => {
await clearTauriMocks()
})
describe('Tauri downloaded font cache helpers', () => {
test('summarizes manifest entries through mocked plugin-fs IPC', async () => {
await mockTauriIPC((cmd, args) => {
expect(cmd).toBe('plugin:fs|read_file')
expect(args).toMatchObject({ path: 'font-cache/v1/manifest.json' })
return [...encoder.encode(JSON.stringify({
version: 1,
entries: {
one: {
family: 'Noto Sans SC',
style: 'Regular',
file: 'one.ttf',
byteLength: 10,
sha256: 'a',
updatedAt: 100
},
two: {
family: 'Noto Naskh Arabic',
style: 'Regular',
file: 'two.ttf',
byteLength: 25,
sha256: 'b',
updatedAt: 250
}
}
}))]
})
await expect(downloadedFontCacheSummary()).resolves.toEqual({
count: 2,
byteLength: 35,
updatedAt: 250
})
})
test('returns an empty summary when manifest is missing', async () => {
await mockTauriIPC((cmd) => {
expect(cmd).toBe('plugin:fs|read_file')
throw new Error('missing')
})
await expect(downloadedFontCacheSummary()).resolves.toEqual({
count: 0,
byteLength: 0,
updatedAt: null
})
})
test('clears the cache directory through mocked plugin-fs IPC', async () => {
const calls: Array<{ cmd: string; args: unknown }> = []
await mockTauriIPC((cmd, args) => {
calls.push({ cmd, args })
return null
})
await clearDownloadedFontCache()
const { BaseDirectory } = await import('@tauri-apps/plugin-fs')
expect(calls).toEqual([
{
cmd: 'plugin:fs|remove',
args: {
path: 'font-cache/v1',
options: { baseDir: BaseDirectory.AppLocalData, recursive: true }
}
}
])
})
})

View file

@ -0,0 +1,19 @@
export function installTauriMockWindow() {
const windowLike = globalThis as typeof globalThis & {
__TAURI_INTERNALS__?: unknown
__TAURI_EVENT_PLUGIN_INTERNALS__?: unknown
}
Object.assign(globalThis, { window: windowLike })
}
export async function mockTauriIPC(handler: (cmd: string, args: unknown) => unknown) {
installTauriMockWindow()
const { mockIPC } = await import('@tauri-apps/api/mocks')
mockIPC(handler)
}
export async function clearTauriMocks() {
if (!('window' in globalThis)) return
const { clearMocks } = await import('@tauri-apps/api/mocks')
clearMocks()
}