perf: optimize app startup and background lifecycles

This commit is contained in:
inman
2026-08-19 00:56:15 +08:00
parent 31332f6d76
commit 927e13349b
121 changed files with 4359 additions and 1400 deletions

View File

@@ -0,0 +1,32 @@
import { describe, expect, it } from 'vitest';
import { dispatchHostApiRequest } from '@electron/api/host-api-dispatcher';
import { shouldUseLoopbackHostApi } from '@electron/api/host-api-transport';
describe('Host API transport selection', () => {
it('keeps long-lived and binary responses on loopback HTTP', () => {
expect(shouldUseLoopbackHostApi('/api/events')).toBe(true);
expect(shouldUseLoopbackHostApi('/api/events?token=session')).toBe(true);
expect(shouldUseLoopbackHostApi('/api/opencode/events')).toBe(true);
expect(shouldUseLoopbackHostApi('/api/ai-proxy/v1/chat/completions', 'POST')).toBe(true);
expect(shouldUseLoopbackHostApi('/api/image-workspace/workspaces/w1/conversations/c1/events')).toBe(true);
expect(shouldUseLoopbackHostApi('/api/image-workspace/workspaces/w1/assets/a1/content?download=1')).toBe(true);
});
it('dispatches JSON and control requests in Main', () => {
expect(shouldUseLoopbackHostApi('/api/app/runtime-info')).toBe(false);
expect(shouldUseLoopbackHostApi('/api/works/projects/release-jobs', 'POST')).toBe(false);
expect(shouldUseLoopbackHostApi('/api/opencode/status')).toBe(false);
});
it('reuses the existing route contract without opening a socket', async () => {
const data = await dispatchHostApiRequest({
opencodeManager: { getStatus: () => ({ state: 'stopped', port: null }) },
opencodeProjectStore: {},
eventBus: {},
mainWindow: null,
} as never, { path: '/api/app/runtime-info' });
expect(data).toMatchObject({ status: 200, ok: true });
expect(data.json).toMatchObject({ runtime: 'opencode', status: { state: 'stopped' } });
});
});