Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add field level merge e2e test (#1404)
Leendert de Borst754bb18125ef
diff --git a/apps/browser-extension/playwright.config.ts b/apps/browser-extension/playwright.config.tsindex 534a2d6c0..659e95a31 100644--- a/apps/browser-extension/playwright.config.ts+++ b/apps/browser-extension/playwright.config.ts@@ -24,7 +24,7 @@ export default defineConfig({forbidOnly: !!process.env.CI,// Retry failed tests (more retries on CI)- retries: process.env.CI ? 2 : 1,+ retries: process.env.CI ? 2 : 0,// Use single worker to ensure sequential executionworkers: 1,diff --git a/apps/browser-extension/src/utils/SqliteClient.ts b/apps/browser-extension/src/utils/SqliteClient.tsindex 5d7f2c474..24dc42987 100644--- a/apps/browser-extension/src/utils/SqliteClient.ts+++ b/apps/browser-extension/src/utils/SqliteClient.ts@@ -2118,19 +2118,38 @@ export class SqliteClient {item.Id]);- // 2. Track history for fields that have EnableHistory=true before deleting+ // 2. Track history for fields that have EnableHistory=true before updatingawait this.trackFieldHistory(item.Id, item.Fields, currentDateTime);- // 3. Delete all existing FieldValues for this item- const deleteFieldsQuery = `- UPDATE FieldValues- SET IsDeleted = 1,- UpdatedAt = ?- WHERE ItemId = ?`;+ // 3. Get existing FieldValues for this item (to update in place, preserving IDs for merge)+ const existingFieldValuesQuery = `+ SELECT Id, FieldKey, FieldDefinitionId, Value+ FROM FieldValues+ WHERE ItemId = ? AND IsDeleted = 0`;++ const existingFieldValues = this.executeQuery<{+ Id: string;+ FieldKey: string | null;+ FieldDefinitionId: string | null;+ Value: string;+ }>(existingFieldValuesQuery, [item.Id]);++ // Build a map of existing FieldValues by their effective key (FieldKey for system fields, FieldDefinitionId for custom)+ // Key format: "fieldKey:index" to handle multi-value fields+ const existingByKey = new Map<string, { Id: string; Value: string }>();+ const fieldValueCounts = new Map<string, number>();++ for (const fv of existingFieldValues) {+ const key = fv.FieldKey || fv.FieldDefinitionId || '';+ const count = fieldValueCounts.get(key) || 0;+ existingByKey.set(`${key}:${count}`, { Id: fv.Id, Value: fv.Value });+ fieldValueCounts.set(key, count + 1);+ }- this.executeUpdate(deleteFieldsQuery, [currentDateTime, item.Id]);+ // Track which existing FieldValue IDs we've processed (to know which to soft-delete)+ const processedIds = new Set<string>();- // 4. Insert new FieldValues+ // 4. Update existing or insert new FieldValuesif (item.Fields && item.Fields.length > 0) {for (const field of item.Fields) {// Skip empty fields@@ -2195,6 +2214,7 @@ export class SqliteClient {// Handle multi-value fields by creating separate FieldValue recordsconst values = Array.isArray(field.Value) ? field.Value : [field.Value];+ const effectiveKey = isCustomField ? field.FieldKey : field.FieldKey;for (let i = 0; i < values.length; i++) {const value = values[i];@@ -2204,26 +2224,65 @@ export class SqliteClient {continue;}- const fieldValueId = crypto.randomUUID().toUpperCase();- const fieldQuery = `- INSERT INTO FieldValues (Id, ItemId, FieldDefinitionId, FieldKey, Value, Weight, CreatedAt, UpdatedAt, IsDeleted)- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`;-- this.executeUpdate(fieldQuery, [- fieldValueId,- item.Id,- fieldDefinitionId, // NULL for system fields, custom field ID for custom fields- isCustomField ? null : field.FieldKey, // FieldKey set for system fields only- value, // Store each value separately, not as JSON- field.DisplayOrder ?? 0,- currentDateTime,- currentDateTime,- 0- ]);+ const lookupKey = `${effectiveKey}:${i}`;+ const existing = existingByKey.get(lookupKey);++ if (existing) {+ // Update existing FieldValue in place (preserves ID for merge)+ processedIds.add(existing.Id);++ // Only update if value actually changed+ if (existing.Value !== value) {+ const updateQuery = `+ UPDATE FieldValues+ SET Value = ?,+ Weight = ?,+ UpdatedAt = ?+ WHERE Id = ?`;++ this.executeUpdate(updateQuery, [+ value,+ field.DisplayOrder ?? 0,+ currentDateTime,+ existing.Id+ ]);+ }+ } else {+ // Insert new FieldValue+ const fieldValueId = crypto.randomUUID().toUpperCase();+ const fieldQuery = `+ INSERT INTO FieldValues (Id, ItemId, FieldDefinitionId, FieldKey, Value, Weight, CreatedAt, UpdatedAt, IsDeleted)+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`;++ this.executeUpdate(fieldQuery, [+ fieldValueId,+ item.Id,+ fieldDefinitionId, // NULL for system fields, custom field ID for custom fields+ isCustomField ? null : field.FieldKey, // FieldKey set for system fields only+ value, // Store each value separately, not as JSON+ field.DisplayOrder ?? 0,+ currentDateTime,+ currentDateTime,+ 0+ ]);+ }}}}+ // 5. Soft-delete any FieldValues that were not processed (removed fields)+ for (const fv of existingFieldValues) {+ if (!processedIds.has(fv.Id)) {+ const deleteQuery = `+ UPDATE FieldValues+ SET IsDeleted = 1,+ UpdatedAt = ?+ WHERE Id = ?`;++ this.executeUpdate(deleteQuery, [currentDateTime, fv.Id]);+ }+ }+await this.commitTransaction();return 1;} catch (error) {diff --git a/apps/browser-extension/src/utils/VaultMergeService.ts b/apps/browser-extension/src/utils/VaultMergeService.tsindex 2e3ba9e21..fa6379e41 100644--- a/apps/browser-extension/src/utils/VaultMergeService.ts+++ b/apps/browser-extension/src/utils/VaultMergeService.ts@@ -45,14 +45,19 @@ interface ITableConfig {name: string;primaryKey: string;isItemTable?: boolean; // Special handling for Item.DeletedAt+ compositeKey?: string[]; // For tables that should merge by composite key instead of Id}/*** All tables that extend SyncableEntity and need LWW merge.+ *+ * FieldValues uses a composite key (ItemId + FieldKey/FieldDefinitionId) for merging+ * because different clients may create FieldValues with different random UUIDs for the+ * same logical field. By matching on the composite key, we ensure proper LWW merging.*/const SYNCABLE_TABLES: ITableConfig[] = [{ name: 'Items', primaryKey: 'Id', isItemTable: true },- { name: 'FieldValues', primaryKey: 'Id' },+ { name: 'FieldValues', primaryKey: 'Id', compositeKey: ['ItemId', 'FieldKey', 'FieldDefinitionId'] },{ name: 'Folders', primaryKey: 'Id' },{ name: 'Tags', primaryKey: 'Id' },{ name: 'ItemTags', primaryKey: 'Id' },@@ -188,13 +193,22 @@ export class VaultMergeService {tableConfig: ITableConfig,stats: IMergeStats): Promise<void> {- const { name: tableName, primaryKey: _primaryKey, isItemTable } = tableConfig;+ const { name: tableName, isItemTable, compositeKey } = tableConfig;// Get all records from both databasesconst localRecords = this.getTableRecords(localDb, tableName);const serverRecords = this.getTableRecords(serverDb, tableName);- // Create a map of server records by primary key for quick lookup+ // Use composite key matching for tables that define it (e.g., FieldValues)+ if (compositeKey) {+ await this.mergeTableByCompositeKey(localDb, serverDb, tableName, localRecords, serverRecords, compositeKey, stats);+ return;+ }++ /*+ * Standard merge by Id for other tables+ * Create a map of server records by primary key for quick lookup+ */const serverMap = new Map<string, ISyncableRecord>();for (const record of serverRecords) {serverMap.set(record.Id, record);@@ -244,6 +258,142 @@ export class VaultMergeService {}}+ /**+ * Generate a composite key string for a record.+ * For FieldValues, this combines ItemId + (FieldKey or FieldDefinitionId).+ * @param record - The record to generate key for+ * @param keyColumns - The columns that make up the composite key+ * @returns A string representing the composite key+ */+ // eslint-disable-next-line @typescript-eslint/no-explicit-any+ private getCompositeKey(record: any, _keyColumns: string[]): string {+ /*+ * For FieldValues: use ItemId + (FieldKey if set, otherwise FieldDefinitionId)+ * This handles both system fields (FieldKey) and custom fields (FieldDefinitionId)+ */+ const itemId = record.ItemId || '';+ const fieldIdentifier = record.FieldKey || record.FieldDefinitionId || '';+ return `${itemId}:${fieldIdentifier}`;+ }++ /**+ * Merge a table using composite key matching instead of Id.+ * This is used for FieldValues where the same logical field may have different UUIDs+ * on different clients.+ *+ * @param localDb - The local database+ * @param serverDb - The server database+ * @param tableName - The name of the table+ * @param localRecords - Records from local database+ * @param serverRecords - Records from server database+ * @param keyColumns - Columns that make up the composite key+ * @param stats - Statistics to update+ */+ private async mergeTableByCompositeKey(+ localDb: Database,+ serverDb: Database,+ tableName: string,+ localRecords: ISyncableRecord[],+ serverRecords: ISyncableRecord[],+ keyColumns: string[],+ stats: IMergeStats+ ): Promise<void> {+ // Create a map of server records by composite key+ // eslint-disable-next-line @typescript-eslint/no-explicit-any+ const serverMap = new Map<string, any>();+ for (const record of serverRecords) {+ const key = this.getCompositeKey(record, keyColumns);+ // If multiple records have the same composite key, keep the one with latest UpdatedAt+ const existing = serverMap.get(key);+ if (!existing || new Date(record.UpdatedAt).getTime() > new Date(existing.UpdatedAt).getTime()) {+ serverMap.set(key, record);+ }+ }++ // Create a map of local records by composite key (to track processed keys)+ const processedKeys = new Set<string>();++ // Process local records+ for (const localRecord of localRecords) {+ const compositeKey = this.getCompositeKey(localRecord, keyColumns);+ processedKeys.add(compositeKey);++ const serverRecord = serverMap.get(compositeKey);++ if (!serverRecord) {+ // Record only exists locally (created offline) - keep it+ stats.recordsCreatedLocally++;+ continue;+ }++ // Record exists in both (by composite key) - compare UpdatedAt for LWW+ const localUpdated = new Date(localRecord.UpdatedAt).getTime();+ const serverUpdated = new Date(serverRecord.UpdatedAt).getTime();++ if (serverUpdated > localUpdated) {+ /*+ * Server wins - update local record with server data+ * Keep local Id but update all other fields+ */+ stats.conflicts++;+ stats.recordsFromServer++;++ this.updateRecordById(localDb, tableName, localRecord.Id, serverRecord, serverDb);+ } else {+ // Local wins - keep local (already there)+ stats.recordsFromLocal++;+ }++ // Remove from server map to track what's been processed+ serverMap.delete(compositeKey);+ }++ /*+ * Insert server-only records into local (new records from other devices)+ * These are records with composite keys that don't exist locally+ */+ for (const serverRecord of serverMap.values()) {+ stats.recordsFromServer++;+ this.insertRecord(localDb, tableName, serverRecord, serverDb);+ }+ }++ /**+ * Update a record by its Id, replacing all fields except Id with values from source.+ * Used when merging by composite key - we keep the local Id but take server's field values.+ *+ * @param localDb - The local database+ * @param tableName - The name of the table+ * @param localId - The local record's Id to update+ * @param sourceRecord - The source record with values to copy+ * @param serverDb - The server database (for schema info)+ */+ private updateRecordById(+ localDb: Database,+ tableName: string,+ localId: string,+ sourceRecord: ISyncableRecord,+ serverDb: Database+ ): void {+ const columns = this.getTableColumns(serverDb, tableName);++ // Build UPDATE statement - update all columns except Id+ const setClause = columns+ .filter(col => col !== 'Id')+ .map(col => `${col} = ?`)+ .join(', ');++ const values = columns+ .filter(col => col !== 'Id')+ // eslint-disable-next-line @typescript-eslint/no-explicit-any+ .map(col => (sourceRecord as any)[col]);++ values.push(localId); // For WHERE clause - use local Id++ const sql = `UPDATE ${tableName} SET ${setClause} WHERE Id = ?`;+ localDb.run(sql, values);+ }+/*** Get all records from a table.* @param db - The database to querydiff --git a/apps/browser-extension/tests/e2e/06-field-level-merge.spec.ts b/apps/browser-extension/tests/e2e/06-field-level-merge.spec.tsnew file mode 100644index 000000000..50422f78a--- /dev/null+++ b/apps/browser-extension/tests/e2e/06-field-level-merge.spec.ts@@ -0,0 +1,263 @@+import type { BrowserContext, Page } from '@playwright/test';++import {+ test,+ expect,+ openPopup,+ fullLoginFlow,+ waitForLoggedIn,+ createFreshContext,+} from '../fixtures';++/**+ * Helper to navigate to add credential form.+ *+ * @param popup - The popup page+ */+async function navigateToAddCredentialForm(popup: Page): Promise<void> {+ const addButton = popup.locator('button[title="Add new item"]');+ await expect(addButton).toBeVisible();+ await addButton.click();+ await expect(popup.locator('input#itemName')).toBeVisible();+}++/**+ * Helper to fill and save a credential form.+ *+ * @param popup - The popup page+ * @param name - The credential name+ * @param username - The username for the credential+ * @param password - The password for the credential+ */+async function fillAndSaveCredential(+ popup: Page,+ name: string,+ username: string,+ password: string+): Promise<void> {+ await popup.fill('input#itemName', name);+ await popup.click('button:has-text("Next")');+ await expect(popup.locator('input#login\\.username')).toBeVisible({ timeout: 10000 });+ await popup.fill('input#login\\.username', username);+ await popup.fill('input#login\\.password', password);+ await popup.click('button:has-text("Save")');+ await expect(popup.locator(`text=${name}`)).toBeVisible({ timeout: 30000 });+}++/**+ * Category 6: Field-Level Merge (Requires API + Multi-Client Scenario)+ *+ * These tests verify that when the same credential is modified on two clients+ * simultaneously, the merge happens at the field level:+ * - Fields modified only on Client A are preserved+ * - Fields modified only on Client B are preserved+ * - Fields modified on both clients: the last writer wins+ *+ * Scenario:+ * 1. Client A and Client B both login to the same account+ * 2. Client A creates a credential and both clients sync+ * 3. Client A edits the credential: changes username and notes+ * 4. Client B (with stale data) edits the same credential: changes password and notes+ * 5. After merge, both username (from A) and password (from B) should be preserved+ * 6. Notes should have Client B's value (last writer wins)+ */+test.describe.serial('6. Field-Level Merge', () => {+ let clientA: { context: BrowserContext; extensionId: string; popup: Page };+ let clientB: { context: BrowserContext; extensionId: string; popup: Page };++ const credentialName = `Field Merge Test ${Date.now()}`;+ const originalUsername = '[email protected]';+ const originalPassword = 'OriginalPassword123!';++ // Values for field-level merge test+ const clientAUsername = '[email protected]';+ const clientANotes = 'Notes modified by Client A';+ const clientBPassword = 'ClientBModifiedPassword789!';+ const clientBNotes = 'Notes modified by Client B - this should win';++ test.afterAll(async () => {+ await clientA?.popup?.close();+ await clientA?.context?.close();+ await clientB?.popup?.close();+ await clientB?.context?.close();+ });++ test('6.1 Setup: Both clients login and Client A creates a credential', async ({ testUser, apiUrl }) => {+ // Create Client A+ const contextA = await createFreshContext();+ const popupA = await openPopup(contextA.context, contextA.extensionId);+ await fullLoginFlow(popupA, apiUrl, testUser.username, testUser.password);+ await waitForLoggedIn(popupA);+ clientA = { ...contextA, popup: popupA };++ // Create Client B+ const contextB = await createFreshContext();+ const popupB = await openPopup(contextB.context, contextB.extensionId);+ await fullLoginFlow(popupB, apiUrl, testUser.username, testUser.password);+ await waitForLoggedIn(popupB);+ clientB = { ...contextB, popup: popupB };++ // Client A creates the credential+ await clientA.popup.getByRole('button', { name: 'Vault' }).click();+ await navigateToAddCredentialForm(clientA.popup);+ await fillAndSaveCredential(clientA.popup, credentialName, originalUsername, originalPassword);++ await clientA.popup.screenshot({ path: 'tests/screenshots/6.1-client-a-credential-created.png' });+ });++ test('6.2 Both clients sync and verify credential exists', async () => {+ // Client A navigates to vault and waits for sync+ await clientA.popup.getByRole('button', { name: 'Vault' }).click();+ await clientA.popup.waitForTimeout(100);++ // Take screenshot to debug what's visible+ await clientA.popup.screenshot({ path: 'tests/screenshots/6.2-client-a-vault.png' });+ await expect(clientA.popup.locator(`text=${credentialName}`)).toBeVisible({ timeout: 10000 });++ // Let Client B navigate to root so it will sync and see the credential+ await clientB.popup.evaluate(() => {+ window.location.href = '/popup.html';+ });+ await clientB.popup.waitForTimeout(100);+ await expect(clientB.popup.locator(`text=${credentialName}`)).toBeVisible({ timeout: 10000 });+ });++ test('6.3 Client A edits credential (username and notes) and saves', async () => {+ const popup = clientA.popup;++ // Click on the credential to open details+ await popup.locator(`text=${credentialName}`).click();+ await popup.waitForTimeout(100);++ // Click the edit button+ const editButton = popup.locator('button[title="Edit Credential"]');+ await expect(editButton).toBeVisible({ timeout: 5000 });+ await editButton.click();++ // Wait for edit form to load+ await expect(popup.locator('input#login\\.username')).toBeVisible({ timeout: 10000 });++ // Modify username+ await popup.fill('input#login\\.username', clientAUsername);++ // Modify notes+ await popup.fill('textarea#login\\.notes', clientANotes);++ await popup.screenshot({ path: 'tests/screenshots/6.3-client-a-before-save.png' });++ // Click Save button+ await popup.click('button:has-text("Save")');++ // Wait for save to complete+ await popup.waitForTimeout(500);++ await popup.screenshot({ path: 'tests/screenshots/6.3-client-a-after-save.png' });+ });++ test('6.4 Client B edits same credential (password and notes) with stale data', async () => {+ const popup = clientB.popup;++ // Client B has stale data - click on credential to open details+ await popup.locator(`text=${credentialName}`).click();+ await popup.waitForTimeout(100);++ await popup.screenshot({ path: 'tests/screenshots/6.4-client-b-stale-details.png' });++ // Click the edit button+ const editButton = popup.locator('button[title="Edit Credential"]');+ await expect(editButton).toBeVisible({ timeout: 5000 });+ await editButton.click();++ // Wait for edit form to load+ await expect(popup.locator('input#login\\.password')).toBeVisible({ timeout: 10000 });++ await popup.screenshot({ path: 'tests/screenshots/6.4-client-b-stale-form.png' });++ // Modify password+ await popup.fill('input#login\\.password', clientBPassword);++ // Modify notes (different value than Client A)+ await popup.fill('textarea#login\\.notes', clientBNotes);++ await popup.screenshot({ path: 'tests/screenshots/6.4-client-b-before-save.png' });++ // Click Save button - this should trigger a merge+ await popup.click('button:has-text("Save")');++ // Wait for save/merge to complete+ await popup.waitForTimeout(100);++ await popup.screenshot({ path: 'tests/screenshots/6.4-client-b-after-save.png' });+ });++ test('6.5 Client B verifies field-level merge result', async () => {+ const popup = clientB.popup;++ // Navigate to vault+ await popup.getByRole('button', { name: 'Vault' }).click();+ await popup.waitForTimeout(100);++ // Click on credential to view merged details+ await popup.locator(`text=${credentialName}`).click();+ await popup.waitForTimeout(100);++ // Click edit to see the actual field values+ const editButton = popup.locator('button[title="Edit Credential"]');+ await expect(editButton).toBeVisible({ timeout: 5000 });+ await editButton.click();++ await expect(popup.locator('input#login\\.username')).toBeVisible({ timeout: 10000 });++ await popup.screenshot({ path: 'tests/screenshots/6.5-client-b-merged-form.png' });++ // Verify username has Client A's value (preserved from earlier edit)+ const usernameValue = await popup.locator('input#login\\.username').inputValue();+ expect(usernameValue).toBe(clientAUsername);++ // Verify password has Client B's value (preserved from this client's edit)+ const passwordValue = await popup.locator('input#login\\.password').inputValue();+ expect(passwordValue).toBe(clientBPassword);++ // Verify notes has Client B's value (last writer wins)+ const notesValue = await popup.locator('textarea#login\\.notes').inputValue();+ expect(notesValue).toBe(clientBNotes);++ // Navigate back+ await popup.goBack();+ await popup.waitForTimeout(100);+ });++ test('6.6 Client A syncs and verifies merged credential', async () => {+ const popup = clientA.popup;++ // Navigate to root so it will sync and see the credential+ await popup.evaluate(() => {+ window.location.href = '/popup.html';+ });++ // Click on credential to view synced details+ await popup.locator(`text=${credentialName}`).click();+ await popup.waitForTimeout(100);++ // Click edit to verify the actual field values+ const editButton = popup.locator('button[title="Edit Credential"]');+ await expect(editButton).toBeVisible({ timeout: 5000 });+ await editButton.click();++ await expect(popup.locator('input#login\\.username')).toBeVisible({ timeout: 10000 });++ await popup.screenshot({ path: 'tests/screenshots/6.6-client-a-synced-form.png' });++ // Verify username has Client A's value (our original edit)+ const usernameValue = await popup.locator('input#login\\.username').inputValue();+ expect(usernameValue).toBe(clientAUsername);++ // Verify password has Client B's value (merged from other client)+ const passwordValue = await popup.locator('input#login\\.password').inputValue();+ expect(passwordValue).toBe(clientBPassword);++ // Verify notes has Client B's value (last writer wins)+ const notesValue = await popup.locator('textarea#login\\.notes').inputValue();+ expect(notesValue).toBe(clientBNotes);+ });+});