Skip to content

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

fix(tauri): use native dirname for save paths

Danila Poyarkove67c15ce4250
diff --git a/src/app/automation/bridge/file-handlers.ts b/src/app/automation/bridge/file-handlers.ts
index f92b60701..a198ae898 100644
--- a/src/app/automation/bridge/file-handlers.ts
+++ b/src/app/automation/bridge/file-handlers.ts
@@ -16,8 +16,12 @@ export async function handleSaveFile(store: EditorStore, args: unknown): Promise
export async function ensureTauriParentDirectory(path: string): Promise<void> {
if (!isTauri()) return
- const { mkdir } = await import('@tauri-apps/plugin-fs')
- const dir = path.replace(/[\\/][^\\/]+$/, '')
+ const [{ dirname }, { mkdir }] = await Promise.all([
+ import('@tauri-apps/api/path'),
+ import('@tauri-apps/plugin-fs')
+ ])
+ const dir = await dirname(path)
+ if (dir === path) return
await mkdir(dir, { recursive: true })
}
diff --git a/src/app/automation/mcp/spawn.ts b/src/app/automation/mcp/spawn.ts
index fa606d776..763c79df8 100644
--- a/src/app/automation/mcp/spawn.ts
+++ b/src/app/automation/mcp/spawn.ts
@@ -19,8 +19,12 @@ export interface AutomationServerHandle {
authToken: string | null
}
-const DEV_AUTOMATION_AUTH_TOKEN = import.meta.env.DEV ? __OPENPENCIL_LOCAL_AUTOMATION_TOKEN__ : null
-const APP_VERSION = __OPENPENCIL_APP_VERSION__
+const DEV_AUTOMATION_AUTH_TOKEN =
+ import.meta.env.DEV && typeof __OPENPENCIL_LOCAL_AUTOMATION_TOKEN__ === 'string'
+ ? __OPENPENCIL_LOCAL_AUTOMATION_TOKEN__
+ : null
+const APP_VERSION =
+ typeof __OPENPENCIL_APP_VERSION__ === 'string' ? __OPENPENCIL_APP_VERSION__ : '0.0.0-test'
const noop = () => undefined
let runtimeAutomationAuthToken: string | null = DEV_AUTOMATION_AUTH_TOKEN
diff --git a/tests/engine/mcp/server.test.ts b/tests/engine/mcp/server.test.ts
index 216f89a58..62d79fc1a 100644
--- a/tests/engine/mcp/server.test.ts
+++ b/tests/engine/mcp/server.test.ts
@@ -1,5 +1,7 @@
import { describe, expect, test, beforeEach, afterEach } from 'bun:test'
import type { AddressInfo } from 'node:net'
+import { tmpdir } from 'node:os'
+import path from 'node:path'
import { serve } from '@hono/node-server'
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
@@ -28,6 +30,8 @@ interface MockBrowser {
close: () => void
}
+const TEST_MCP_ROOT = path.join(tmpdir(), 'open-pencil-mcp-root')
+
function connectMockBrowser(port: number, graph: SceneGraph): Promise<MockBrowser> {
return new Promise((resolve, reject) => {
const ws = new WebSocket(`ws://127.0.0.1:${port}`)
@@ -59,7 +63,11 @@ function connectMockBrowser(port: number, graph: SceneGraph): Promise<MockBrowse
api.currentPage = api.wrapNode(graph.getPages()[0].id)
result = await def.execute(api, args.args ?? {})
if (def.mutates) computeAllLayouts(graph)
- } else if (command === 'save_file' || command === 'new_document' || command === 'open_file') {
+ } else if (
+ command === 'save_file' ||
+ command === 'new_document' ||
+ command === 'open_file'
+ ) {
result = {}
} else {
result = executeRpcCommand(graph, command, args ?? {})
@@ -248,7 +256,7 @@ describe('MCP server with mcpRoot', () => {
app,
wss,
close: closeServer
- } = startServer({ httpPort: 0, wsPort: 0, mcpRoot: '/tmp' })
+ } = startServer({ httpPort: 0, wsPort: 0, mcpRoot: TEST_MCP_ROOT })
const httpServer = serve({ fetch: app.fetch, port: 0, hostname: '127.0.0.1' })
const actualWsPort = await waitForWsListening(wss)
@@ -277,7 +285,7 @@ describe('MCP server with mcpRoot', () => {
app,
wss,
close: closeServer
- } = startServer({ httpPort: 0, wsPort: 0, mcpRoot: '/tmp' })
+ } = startServer({ httpPort: 0, wsPort: 0, mcpRoot: TEST_MCP_ROOT })
const httpServer = serve({ fetch: app.fetch, port: 0, hostname: '127.0.0.1' })
const actualWsPort = await waitForWsListening(wss)
@@ -290,14 +298,15 @@ describe('MCP server with mcpRoot', () => {
)
await client.connect(transport)
+ const savePath = path.join(TEST_MCP_ROOT, 'unicode', 'пример.fig')
const result = await client.callTool({
name: 'save_file',
- arguments: { path: '/tmp/unicode/пример.fig' }
+ arguments: { path: savePath }
})
expect(result.isError).not.toBe(true)
const request = browser.requests.find((item) => item.command === 'save_file')
- expect(request?.args).toEqual({ path: '/tmp/unicode/пример.fig' })
+ expect(request?.args).toEqual({ path: savePath })
await client.close()
browser.close()
@@ -310,7 +319,7 @@ describe('MCP server with mcpRoot', () => {
app,
wss,
close: closeServer
- } = startServer({ httpPort: 0, wsPort: 0, mcpRoot: '/tmp' })
+ } = startServer({ httpPort: 0, wsPort: 0, mcpRoot: TEST_MCP_ROOT })
const httpServer = serve({ fetch: app.fetch, port: 0, hostname: '127.0.0.1' })
const actualWsPort = await waitForWsListening(wss)
@@ -325,7 +334,7 @@ describe('MCP server with mcpRoot', () => {
const result = await client.callTool({
name: 'save_file',
- arguments: { path: '/var/tmp/out.fig' }
+ arguments: { path: path.join(path.dirname(TEST_MCP_ROOT), 'outside.fig') }
})
expect(result.isError).toBe(true)
diff --git a/tests/engine/tauri/acp-transport.test.ts b/tests/engine/tauri/acp-transport.test.ts
new file mode 100644
index 000000000..b14921e27
--- /dev/null
+++ b/tests/engine/tauri/acp-transport.test.ts
@@ -0,0 +1,28 @@
+import { afterEach, describe, expect, test, vi } from 'bun:test'
+
+import type { ACPChatTransport } from '@/app/ai/acp/transport'
+import { createACPTransport } from '@/app/ai/chat/transports'
+
+import { clearTauriMocks, mockTauriIPC } from '#tests/helpers/tauri/mocks'
+
+afterEach(async () => {
+ await clearTauriMocks()
+ vi.restoreAllMocks()
+ Reflect.deleteProperty(globalThis, 'window')
+})
+
+describe('Tauri ACP transport', () => {
+ test('uses Tauri home directory for transport cwd', async () => {
+ await mockTauriIPC((cmd, args) => {
+ expect(cmd).toBe('plugin:path|resolve_directory')
+ expect(args).toEqual({ directory: 21 })
+ return '/Users/tester'
+ })
+
+ const transport = (await createACPTransport('acp:claude-code')) as ACPChatTransport & {
+ cwd: string
+ }
+
+ expect(transport.cwd).toBe('/Users/tester')
+ })
+})
diff --git a/tests/engine/tauri/automation-files.test.ts b/tests/engine/tauri/automation-files.test.ts
new file mode 100644
index 000000000..96d9aa767
--- /dev/null
+++ b/tests/engine/tauri/automation-files.test.ts
@@ -0,0 +1,56 @@
+import { afterEach, describe, expect, test } from 'bun:test'
+
+import { ensureTauriParentDirectory } from '@/app/automation/bridge/file-handlers'
+
+import { clearTauriMocks, mockTauriIPC } from '#tests/helpers/tauri/mocks'
+
+afterEach(async () => {
+ await clearTauriMocks()
+ Reflect.deleteProperty(globalThis, 'window')
+})
+
+describe('Tauri automation file handling', () => {
+ test('creates parent directory for automation new-document paths', async () => {
+ const calls: Array<{ cmd: string; args: unknown }> = []
+ await mockTauriIPC((cmd, args) => {
+ calls.push({ cmd, args })
+ if (cmd === 'plugin:path|dirname') return '/tmp/open-pencil/nested'
+ return null
+ })
+
+ await ensureTauriParentDirectory('/tmp/open-pencil/nested/file.fig')
+
+ expect(calls).toEqual([
+ {
+ cmd: 'plugin:path|dirname',
+ args: { path: '/tmp/open-pencil/nested/file.fig' }
+ },
+ {
+ cmd: 'plugin:fs|mkdir',
+ args: { path: '/tmp/open-pencil/nested', options: { recursive: true } }
+ }
+ ])
+ })
+
+ test('uses native dirname for Unicode Windows save paths', async () => {
+ const calls: Array<{ cmd: string; args: unknown }> = []
+ await mockTauriIPC((cmd, args) => {
+ calls.push({ cmd, args })
+ if (cmd === 'plugin:path|dirname') return 'C:\\Users\\Тест\\Документы'
+ return null
+ })
+
+ await ensureTauriParentDirectory('C:\\Users\\Тест\\Документы\\пример.fig')
+
+ expect(calls).toEqual([
+ {
+ cmd: 'plugin:path|dirname',
+ args: { path: 'C:\\Users\\Тест\\Документы\\пример.fig' }
+ },
+ {
+ cmd: 'plugin:fs|mkdir',
+ args: { path: 'C:\\Users\\Тест\\Документы', options: { recursive: true } }
+ }
+ ])
+ })
+})
diff --git a/tests/engine/tauri/remaining-integrations.test.ts b/tests/engine/tauri/mcp-spawn.test.ts
similarity index 63%
rename from tests/engine/tauri/remaining-integrations.test.ts
rename to tests/engine/tauri/mcp-spawn.test.ts
index 51af2b996..8a84e0509 100644
--- a/tests/engine/tauri/remaining-integrations.test.ts
+++ b/tests/engine/tauri/mcp-spawn.test.ts
@@ -1,8 +1,5 @@
import { afterEach, describe, expect, test, vi } from 'bun:test'
-import type { ACPChatTransport } from '@/app/ai/acp/transport'
-import { createACPTransport } from '@/app/ai/chat/transports'
-import { ensureTauriParentDirectory } from '@/app/automation/bridge/file-handlers'
import { spawnMCPIfNeeded } from '@/app/automation/mcp/spawn'
import { clearTauriMocks, installTauriMockWindow, mockTauriIPC } from '#tests/helpers/tauri/mocks'
@@ -15,31 +12,7 @@ afterEach(async () => {
Reflect.deleteProperty(globalThis, 'location')
})
-describe('remaining Tauri integrations', () => {
- test('uses Tauri home directory for ACP transport cwd', async () => {
- await mockTauriIPC((cmd, args) => {
- expect(cmd).toBe('plugin:path|resolve_directory')
- expect(args).toEqual({ directory: 21 })
- return '/Users/tester'
- })
-
- const transport = (await createACPTransport('acp:claude-code')) as ACPChatTransport & {
- cwd: string
- }
-
- expect(transport.cwd).toBe('/Users/tester')
- })
-
- test('creates parent directory for automation new-document paths in Tauri', async () => {
- await mockTauriIPC((cmd, args) => {
- expect(cmd).toBe('plugin:fs|mkdir')
- expect(args).toEqual({ path: '/tmp/open-pencil/nested', options: { recursive: true } })
- return null
- })
-
- await ensureTauriParentDirectory('/tmp/open-pencil/nested/file.fig')
- })
-
+describe('Tauri MCP spawning', () => {
test('spawns MCP server with shell plugin when health check is missing', async () => {
installTauriMockWindow()
Object.assign(globalThis.window, { location: { origin: 'tauri://localhost' } })
@@ -52,7 +25,12 @@ describe('remaining Tauri integrations', () => {
vi.spyOn(globalThis, 'fetch').mockImplementation(async () => {
healthChecks += 1
if (healthChecks === 1) return new Response('', { status: 404 })
- return new Response(JSON.stringify({ status: 'ok', token: 'server-token' }), { status: 200 })
+ return new Response(
+ JSON.stringify({ status: 'ok', version: '0.0.0-test', token: 'server-token' }),
+ {
+ status: 200
+ }
+ )
})
let onEvent: ((event: unknown) => void) | null = null