Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: read

jobs:
build-typecheck-test:
name: Build, typecheck & test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4

- uses: actions/setup-node@v4
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
with:
node-version: 22
cache: npm
Expand Down
94 changes: 55 additions & 39 deletions tests/benchmarks/native_worker_ddl_batch.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,68 @@
import '../unit/vscode_mock_setup';
import { createNativeDatabaseConnection } from '../../src/nativeWorker';
import type { ModificationEntry } from '../../src/core/types';
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs/promises';

async function runBenchmark() {
const bundle = await createNativeDatabaseConnection(vscode.Uri.file(process.cwd()));
const loadResult = await (bundle as any).loadDatabase({ buffer: new Uint8Array() });
const db = loadResult.databaseOps;

// create table
await db.createTable('test_table', [
{ name: 'id', type: 'INTEGER', primaryKey: true },
...Array.from({ length: 50 }, (_, i) => ({ name: `col_${i}`, type: 'TEXT' }))
]);

// insert row
await db.insertRow('test_table', {
id: 1,
...Object.fromEntries(Array.from({ length: 50 }, (_, i) => [`col_${i}`, `val_${i}`]))
});

const mod = {
modificationType: 'column_drop' as const,
targetTable: 'test_table',
deletedColumns: Array.from({ length: 50 }, (_, i) => ({
name: `col_${i}`,
type: 'TEXT',
data: [{ rowId: 1, value: `val_${i}` }]
}))
};

console.log('Warming up...');

// warmup
await db.undoModification(mod);
await db.redoModification(mod);

console.log('Running benchmark...');

const start = performance.now();
for (let i = 0; i < 5; i++) {
// The native API opens a file URI, so the benchmark uses a unique local database and removes it after disposal.
const databasePath = path.join(process.cwd(), `native_worker_ddl_batch_${process.pid}_${Date.now()}.sqlite`);

try {
const { databaseOps: db } = await bundle.establishConnection(
vscode.Uri.file(databasePath),
path.basename(databasePath)
);

// create table
await db.createTable('test_table', [
{ name: 'id', type: 'INTEGER', primaryKey: true, notNull: false },
...Array.from({ length: 50 }, (_, i) => ({
name: `col_${i}`,
type: 'TEXT',
primaryKey: false,
notNull: false
}))
]);

// insert row
await db.insertRow('test_table', {
id: 1,
...Object.fromEntries(Array.from({ length: 50 }, (_, i) => [`col_${i}`, `val_${i}`]))
});

const mod = {
description: 'Drop benchmark columns',
modificationType: 'column_drop' as const,
targetTable: 'test_table',
deletedColumns: Array.from({ length: 50 }, (_, i) => ({
name: `col_${i}`,
type: 'TEXT',
data: [{ rowId: 1, value: `val_${i}` }]
}))
} satisfies ModificationEntry;

console.log('Warming up...');

// warmup
await db.undoModification(mod);
await db.redoModification(mod);
}
const end = performance.now();
console.log(`Time taken: ${(end - start).toFixed(2)}ms`);

bundle.workerMethods[Symbol.dispose]();
console.log('Running benchmark...');

const start = performance.now();
for (let i = 0; i < 5; i++) {
await db.undoModification(mod);
await db.redoModification(mod);
}
const end = performance.now();
console.log(`Time taken: ${(end - start).toFixed(2)}ms`);
} finally {
bundle.workerMethods[Symbol.dispose]();
await fs.rm(databasePath, { force: true });
}
Comment on lines +62 to +65
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There is a potential race condition when deleting the database file immediately after calling bundle.workerMethods[Symbol.dispose]().

The Symbol.dispose call triggers worker.stop(), which asynchronously kills the child process (this.process.kill()). Since process termination and file descriptor release by the OS are asynchronous, fs.rm might execute while the file is still locked by the terminating process (especially on Windows). This can cause the benchmark to fail with EBUSY or EPERM errors.

To make this robust, we can implement a simple retry loop with a small delay to ensure the file is successfully deleted once the lock is released.

    } finally {
        bundle.workerMethods[Symbol.dispose]();
        // Allow the worker process some time to release the file lock (especially on Windows)
        for (let i = 0; i < 5; i++) {
            try {
                await fs.rm(databasePath, { force: true });
                break;
            } catch (err) {
                if (i === 4) throw err;
                await new Promise(resolve => setTimeout(resolve, 100));
            }
        }
    }

}

runBenchmark().catch(console.error);
18 changes: 13 additions & 5 deletions tests/unit/workerFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,19 @@ describe('workerFactory error path tests', () => {
workerTerminated = false;

// Reset VSCode mock behaviors
mockVscode.workspace.fs = {
readFile: async () => new Uint8Array(),
stat: async () => ({ size: 0 })
} as any;
(mockVscode.Uri as any).joinPath = () => ({ scheme: 'file', fsPath: '/test/path/assets/sqlite3.wasm' });
Object.defineProperty(mockVscode.workspace, 'fs', {
value: {
readFile: async () => new Uint8Array(),
stat: async () => ({ size: 0 })
},
writable: true,
configurable: true
});
Object.defineProperty(mockVscode.Uri, 'joinPath', {
value: () => ({ scheme: 'file', fsPath: '/test/path/assets/sqlite3.wasm' }),
writable: true,
configurable: true
});
Comment on lines +87 to +99
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Modifying shared mock objects like mockVscode.workspace and mockVscode.Uri in beforeEach without restoring them in afterEach can lead to test pollution, causing other tests in the suite to fail or behave unpredictably.

Since mockVscode is imported from a shared mock module (./mocks/vscode), it is a singleton cached by Node's module system. Any mutations to it will persist across test files.

To prevent this, we should capture the original properties before modifying them and restore them in afterEach. Since afterEach is not in the diff, you can implement this by storing the original values and restoring them. For example:

  let originalFs: any;
  let originalJoinPath: any;

  beforeEach(() => {
    connectionFailed = false;
    workerTerminated = false;

    originalFs = mockVscode.workspace.fs;
    originalJoinPath = mockVscode.Uri.joinPath;

    // Reset VSCode mock behaviors
    Object.defineProperty(mockVscode.workspace, 'fs', {
      value: {
        readFile: async () => new Uint8Array(),
        stat: async () => ({ size: 0 })
      },
      writable: true,
      configurable: true
    });
    Object.defineProperty(mockVscode.Uri, 'joinPath', {
      value: () => ({ scheme: 'file', fsPath: '/test/path/assets/sqlite3.wasm' }),
      writable: true,
      configurable: true
    });
  });

  afterEach(() => {
    // Restore original mock behaviors
    if (originalFs) {
      Object.defineProperty(mockVscode.workspace, 'fs', {
        value: originalFs,
        writable: true,
        configurable: true
      });
    }
    if (originalJoinPath) {
      Object.defineProperty(mockVscode.Uri, 'joinPath', {
        value: originalJoinPath,
        writable: true,
        configurable: true
      });
    }
    // Restore the original require implementation to avoid leaking to other tests
    Module.prototype.require = originalRequire;
  });

});

afterEach(() => {
Expand Down
84 changes: 41 additions & 43 deletions website/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
--color-accent: var(--ui-accent);
--color-accent-foreground: var(--ui-accent-fg);

--font-sans: var(--font-sans), system-ui, sans-serif;
--font-mono: var(--font-mono), monospace;
--font-sans: var(--font-sans-stack);
--font-mono: var(--font-mono-stack);

--animate-fade-in: fadeIn 0.5s ease-out;
--animate-slide-up: slideUp 0.5s ease-out;
Expand All @@ -44,6 +44,36 @@
}
}

:root {
/* Light theme (default) */
--ui-bg: #ffffff;
--ui-fg: #171717;
--ui-subtle: #f5f5f5;
--ui-subtle-fg: #737373;
--ui-edge: #e5e5e5;
--ui-accent: #0070f3;
--ui-accent-fg: #ffffff;

/* Typography */
--font-sans-stack:
var(--font-inter), -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
'Helvetica Neue', Arial, sans-serif;
--font-mono-stack: 'SF Mono', 'Fira Code', 'Fira Mono', 'Roboto Mono', monospace;
--font-sans: var(--font-sans-stack);
--font-mono: var(--font-mono-stack);
}

.dark {
/* Dark theme */
--ui-bg: #0a0a0a;
--ui-fg: #ededed;
--ui-subtle: #1a1a1a;
--ui-subtle-fg: #a3a3a3;
--ui-edge: #262626;
--ui-accent: #0070f3;
--ui-accent-fg: #ffffff;
}

/*
The default border color has changed to `currentcolor` in Tailwind CSS v4,
so we've added these compatibility styles to make sure everything still
Expand All @@ -60,46 +90,6 @@
::file-selector-button {
border-color: var(--color-gray-200, currentcolor);
}
}

@utility text-balance {
text-wrap: balance;
}
@utility text-gradient {
background: linear-gradient(to right, var(--ui-fg), var(--ui-subtle-fg));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}

@layer utilities {
:root {
/* Light theme (default) */
--ui-bg: #ffffff;
--ui-fg: #171717;
--ui-subtle: #f5f5f5;
--ui-subtle-fg: #737373;
--ui-edge: #e5e5e5;
--ui-accent: #0070f3;
--ui-accent-fg: #ffffff;

/* Typography */
--font-sans:
var(--font-inter), -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
'Helvetica Neue', Arial, sans-serif;
--font-mono: 'SF Mono', 'Fira Code', 'Fira Mono', 'Roboto Mono', monospace;
}

.dark {
/* Dark theme */
--ui-bg: #0a0a0a;
--ui-fg: #ededed;
--ui-subtle: #1a1a1a;
--ui-subtle-fg: #a3a3a3;
--ui-edge: #262626;
--ui-accent: #0070f3;
--ui-accent-fg: #ffffff;
}

/* Base styles */
* {
Expand All @@ -121,8 +111,16 @@
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
}

/* Utility classes */
@utility text-balance {
text-wrap: balance;
}
@utility text-gradient {
background: linear-gradient(to right, var(--ui-fg), var(--ui-subtle-fg));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}

/* Selection styling */
Expand Down
Loading