From e5b0767a439cb9c40c59b4ca36cd74ce2f4f8aa2 Mon Sep 17 00:00:00 2001 From: Danila Poyarkov Date: Tue, 5 May 2026 21:58:12 +0300 Subject: [PATCH] test(app): cover Tauri font cache helpers --- tests/engine/tauri-font-cache.test.ts | 80 +++++++++++++++++++++++++++ tests/helpers/tauri-mocks.ts | 19 +++++++ 2 files changed, 99 insertions(+) create mode 100644 tests/engine/tauri-font-cache.test.ts create mode 100644 tests/helpers/tauri-mocks.ts diff --git a/tests/engine/tauri-font-cache.test.ts b/tests/engine/tauri-font-cache.test.ts new file mode 100644 index 000000000..9a27aeb52 --- /dev/null +++ b/tests/engine/tauri-font-cache.test.ts @@ -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 } + } + } + ]) + }) +}) diff --git a/tests/helpers/tauri-mocks.ts b/tests/helpers/tauri-mocks.ts new file mode 100644 index 000000000..c14005c7f --- /dev/null +++ b/tests/helpers/tauri-mocks.ts @@ -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() +}