feat: add async SQLite on Node 24 and improve messaging diagnostics (#520)

Move database work off the main thread on Node.js 24 and improve message handling and runtime diagnostics.

Preserve migration, restart, and concurrency coverage with the async SQLite driver.
This commit is contained in:
MliKiowa authored and GitHub committed 2026-10-13 10:12:47 +08:00
1 parent 20f48d11ec
commit c7428363cb
170 files changed
+7748 -3913

No files matched your search

@@ -0,0 +1,52 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { createApiClient } from '../src/lib/api/client';
const tokenStore = { load: () => 'test-token', save: () => undefined };
afterEach(() => vi.unstubAllGlobals());
describe('API response JSON', () => {
it.each(['<html>proxy error</html>', '', 'null', '[]', '42'])(
'rejects an invalid successful response instead of returning an empty list (%j)',
async (body) => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(body, { status: 200 })));
const client = createApiClient({ tokenStore });
await expect(client.processes.list()).rejects.toMatchObject({
status: 502,
code: 'INVALID_RESPONSE',
});
},
);
it('preserves the HTTP status when an error response is not JSON', async () => {
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue(
new Response('<html>unavailable</html>', {
status: 503,
statusText: 'Service Unavailable',
}),
),
);
const client = createApiClient({ tokenStore });
await expect(client.processes.list()).rejects.toMatchObject({
status: 503,
message: 'Service Unavailable',
});
});
it('does not turn a failed JSON body read into successful empty data', async () => {
const failure = new Error('response stream interrupted');
vi.stubGlobal(
'fetch',
vi
.fn()
.mockResolvedValue({ ok: true, status: 200, json: () => Promise.reject(failure) }),
);
const client = createApiClient({ tokenStore });
await expect(client.processes.list()).rejects.toBe(failure);
});
});
+56
View File
@@ -0,0 +1,56 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { copyText } from '../src/lib/utils';
afterEach(() => vi.unstubAllGlobals());
describe('copyText', () => {
it('uses the clipboard API without creating a temporary element', async () => {
const writeText = vi.fn().mockResolvedValue(undefined);
vi.stubGlobal('navigator', { clipboard: { writeText } });
vi.stubGlobal('document', undefined);
await expect(copyText('hello')).resolves.toBe(true);
expect(writeText).toHaveBeenCalledWith('hello');
});
it.each(['success', 'rejected', 'focus throws', 'copy throws'])(
'removes the temporary element after legacy copy: %s',
async (outcome) => {
const children = new Set<unknown>();
const host = {
appendChild: (child: unknown) => children.add(child),
removeChild: (child: unknown) => children.delete(child),
};
const textarea = {
value: '',
style: {},
setAttribute: vi.fn(),
setSelectionRange: vi.fn(),
focus: () => {
if (outcome === 'focus throws') {
throw new Error('focus unavailable');
}
},
remove: () => children.delete(textarea),
};
vi.stubGlobal('navigator', {
clipboard: { writeText: vi.fn().mockRejectedValue(new Error('permission denied')) },
});
vi.stubGlobal('document', {
body: host,
createElement: () => textarea,
querySelector: () => host,
execCommand: () => {
if (outcome === 'copy throws') {
throw new Error('copy unavailable');
}
return outcome === 'success';
},
});
await expect(copyText('hello')).resolves.toBe(outcome === 'success');
expect(children.size).toBe(0);
},
);
});
@@ -0,0 +1,75 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
LOW_INTEGRITY_PERMANENT_KEY,
LOW_INTEGRITY_SESSION_KEY,
readLowIntegrityDismissed,
saveLowIntegrityDismissed,
} from '../src/lib/low-integrity-warning';
function storage() {
const data = new Map<string, string>();
return {
getItem: (key: string) => data.get(key) ?? null,
setItem: (key: string, value: string) => {
data.set(key, value);
},
};
}
let local = storage();
let session = storage();
beforeEach(() => {
local = storage();
session = storage();
vi.stubGlobal('window', { localStorage: local, sessionStorage: session });
vi.spyOn(console, 'warn').mockImplementation(() => {});
});
afterEach(() => {
vi.unstubAllGlobals();
vi.restoreAllMocks();
});
describe('Low integrity warning', () => {
it('keeps temporary dismissal across reads/reloads but not a new tab session', () => {
expect(readLowIntegrityDismissed()).toBe(false);
saveLowIntegrityDismissed('session');
expect(session.getItem(LOW_INTEGRITY_SESSION_KEY)).toBe('1');
expect(local.getItem(LOW_INTEGRITY_PERMANENT_KEY)).toBeNull();
expect(readLowIntegrityDismissed()).toBe(true);
expect(readLowIntegrityDismissed()).toBe(true);
vi.stubGlobal('window', { localStorage: local, sessionStorage: storage() });
expect(readLowIntegrityDismissed()).toBe(false);
});
it('keeps permanent dismissal across new sessions', () => {
saveLowIntegrityDismissed('permanent');
vi.stubGlobal('window', { localStorage: local, sessionStorage: storage() });
expect(readLowIntegrityDismissed()).toBe(true);
expect(session.getItem(LOW_INTEGRITY_SESSION_KEY)).toBeNull();
});
it('honors session dismissal when local storage is blocked', () => {
saveLowIntegrityDismissed('session');
vi.stubGlobal('window', {
get localStorage() {
throw new Error('blocked');
},
sessionStorage: session,
});
expect(readLowIntegrityDismissed()).toBe(true);
expect(() => saveLowIntegrityDismissed('permanent')).toThrow('blocked');
});
it('does not claim to save permanent dismissal when storage writes fail', () => {
vi.spyOn(local, 'setItem').mockImplementation(() => {
throw new Error('quota');
});
expect(() => saveLowIntegrityDismissed('permanent')).toThrow('quota');
expect(readLowIntegrityDismissed()).toBe(false);
});
it('can read in server rendering without accessing browser storage', () => {
vi.stubGlobal('window', undefined);
expect(readLowIntegrityDismissed()).toBe(false);
});
});